blob: 6cce2ef9f8c339bd5843004cfdb02c826a2ccc37 [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é-Gonnardc3f6b62c2014-02-06 10:13:09 +0100531 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200532 * and leave room for a final 0 */
533 our_size = list_size / 2 + 1;
534 if( our_size > POLARSSL_ECP_DP_MAX )
535 our_size = POLARSSL_ECP_DP_MAX;
536
537 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
538 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
539
Paul Bakker9af723c2014-05-01 13:03:14 +0200540 /* explicit void pointer cast for buggy MS compiler */
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200541 memset( (void *) curves, 0, our_size * sizeof( *curves ) );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200542 ssl->handshake->curves = curves;
543
Paul Bakker41c83d32013-03-20 14:39:14 +0100544 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200545 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100546 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200547 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200548
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200549 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100550 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200551 *curves++ = curve_info;
552 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100553 }
554
555 list_size -= 2;
556 p += 2;
557 }
558
559 return( 0 );
560}
561
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200562static int ssl_parse_supported_point_formats( ssl_context *ssl,
563 const unsigned char *buf,
564 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100565{
566 size_t list_size;
567 const unsigned char *p;
568
569 list_size = buf[0];
570 if( list_size + 1 != len )
571 {
572 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
573 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
574 }
575
576 p = buf + 2;
577 while( list_size > 0 )
578 {
579 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
580 p[0] == POLARSSL_ECP_PF_COMPRESSED )
581 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200582 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200583 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100584 return( 0 );
585 }
586
587 list_size--;
588 p++;
589 }
590
591 return( 0 );
592}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200593#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100594
Paul Bakker05decb22013-08-15 13:33:48 +0200595#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200596static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
597 const unsigned char *buf,
598 size_t len )
599{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200600 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200601 {
602 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
603 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
604 }
605
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200606 ssl->session_negotiate->mfl_code = buf[0];
607
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200608 return( 0 );
609}
Paul Bakker05decb22013-08-15 13:33:48 +0200610#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200611
Paul Bakker1f2bc622013-08-15 13:45:55 +0200612#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200613static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
614 const unsigned char *buf,
615 size_t len )
616{
617 if( len != 0 )
618 {
619 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
620 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
621 }
622
623 ((void) buf);
624
625 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
626
627 return( 0 );
628}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200629#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200630
Paul Bakkera503a632013-08-14 13:48:06 +0200631#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200632static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200633 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200634 size_t len )
635{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200636 int ret;
637
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200638 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
639 return( 0 );
640
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200641 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200642 ssl->handshake->new_session_ticket = 1;
643
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200644 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
645
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200646 if( len == 0 )
647 return( 0 );
648
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200649 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
650 {
651 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
652 return( 0 );
653 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200654
655 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200656 * Failures are ok: just ignore the ticket and proceed.
657 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200658 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
659 {
660 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200661 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200662 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200663
664 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
665
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200666 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200667
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200668 /* Don't send a new ticket after all, this one is OK */
669 ssl->handshake->new_session_ticket = 0;
670
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200671 return( 0 );
672}
Paul Bakkera503a632013-08-14 13:48:06 +0200673#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200674
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200675#if defined(POLARSSL_SSL_ALPN)
676static int ssl_parse_alpn_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200677 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200678{
Paul Bakker14b16c62014-05-28 11:33:54 +0200679 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200680 const unsigned char *theirs, *start, *end;
681 const char **ours;
682
683 /* If ALPN not configured, just ignore the extension */
684 if( ssl->alpn_list == NULL )
685 return( 0 );
686
687 /*
688 * opaque ProtocolName<1..2^8-1>;
689 *
690 * struct {
691 * ProtocolName protocol_name_list<2..2^16-1>
692 * } ProtocolNameList;
693 */
694
695 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
696 if( len < 4 )
697 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
698
699 list_len = ( buf[0] << 8 ) | buf[1];
700 if( list_len != len - 2 )
701 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
702
703 /*
704 * Use our order of preference
705 */
706 start = buf + 2;
707 end = buf + len;
708 for( ours = ssl->alpn_list; *ours != NULL; ours++ )
709 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200710 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200711 for( theirs = start; theirs != end; theirs += cur_len )
712 {
713 /* If the list is well formed, we should get equality first */
714 if( theirs > end )
715 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
716
717 cur_len = *theirs++;
718
719 /* Empty strings MUST NOT be included */
720 if( cur_len == 0 )
721 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
722
Paul Bakker14b16c62014-05-28 11:33:54 +0200723 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200724 memcmp( theirs, *ours, cur_len ) == 0 )
725 {
726 ssl->alpn_chosen = *ours;
727 return( 0 );
728 }
729 }
730 }
731
732 /* If we get there, no match was found */
733 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
734 SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
735 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
736}
737#endif /* POLARSSL_SSL_ALPN */
738
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100739/*
740 * Auxiliary functions for ServerHello parsing and related actions
741 */
742
743#if defined(POLARSSL_X509_CRT_PARSE_C)
744/*
745 * Return 1 if the given EC key uses the given curve, 0 otherwise
746 */
747#if defined(POLARSSL_ECDSA_C)
748static int ssl_key_matches_curves( pk_context *pk,
749 const ecp_curve_info **curves )
750{
751 const ecp_curve_info **crv = curves;
752 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
753
754 while( *crv != NULL )
755 {
756 if( (*crv)->grp_id == grp_id )
757 return( 1 );
758 crv++;
759 }
760
761 return( 0 );
762}
763#endif /* POLARSSL_ECDSA_C */
764
765/*
766 * Try picking a certificate for this ciphersuite,
767 * return 0 on success and -1 on failure.
768 */
769static int ssl_pick_cert( ssl_context *ssl,
770 const ssl_ciphersuite_t * ciphersuite_info )
771{
772 ssl_key_cert *cur, *list;
773 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
774
775#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
776 if( ssl->handshake->sni_key_cert != NULL )
777 list = ssl->handshake->sni_key_cert;
778 else
779#endif
780 list = ssl->handshake->key_cert;
781
782 if( pk_alg == POLARSSL_PK_NONE )
783 return( 0 );
784
785 for( cur = list; cur != NULL; cur = cur->next )
786 {
787 if( ! pk_can_do( cur->key, pk_alg ) )
788 continue;
789
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200790 /*
791 * This avoids sending the client a cert it'll reject based on
792 * keyUsage or other extensions.
793 *
794 * It also allows the user to provision different certificates for
795 * different uses based on keyUsage, eg if they want to avoid signing
796 * and decrypting with the same RSA key.
797 */
798 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
799 SSL_IS_SERVER ) != 0 )
800 {
801 continue;
802 }
803
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100804#if defined(POLARSSL_ECDSA_C)
805 if( pk_alg == POLARSSL_PK_ECDSA )
806 {
807 if( ssl_key_matches_curves( cur->key, ssl->handshake->curves ) )
808 break;
809 }
810 else
811#endif
812 break;
813 }
814
815 if( cur == NULL )
816 return( -1 );
817
818 ssl->handshake->key_cert = cur;
819 return( 0 );
820}
821#endif /* POLARSSL_X509_CRT_PARSE_C */
822
823/*
824 * Check if a given ciphersuite is suitable for use with our config/keys/etc
825 * Sets ciphersuite_info only if the suite matches.
826 */
827static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
828 const ssl_ciphersuite_t **ciphersuite_info )
829{
830 const ssl_ciphersuite_t *suite_info;
831
832 suite_info = ssl_ciphersuite_from_id( suite_id );
833 if( suite_info == NULL )
834 {
835 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", suite_id ) );
836 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
837 }
838
839 if( suite_info->min_minor_ver > ssl->minor_ver ||
840 suite_info->max_minor_ver < ssl->minor_ver )
841 return( 0 );
842
843#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
844 if( ssl_ciphersuite_uses_ec( suite_info ) &&
845 ( ssl->handshake->curves == NULL ||
846 ssl->handshake->curves[0] == NULL ) )
847 return( 0 );
848#endif
849
850#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
851 /* If the ciphersuite requires a pre-shared key and we don't
852 * have one, skip it now rather than failing later */
853 if( ssl_ciphersuite_uses_psk( suite_info ) &&
854 ssl->f_psk == NULL &&
855 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
856 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
857 return( 0 );
858#endif
859
860#if defined(POLARSSL_X509_CRT_PARSE_C)
861 /*
862 * Final check: if ciphersuite requires us to have a
863 * certificate/key of a particular type:
864 * - select the appropriate certificate if we have one, or
865 * - try the next ciphersuite if we don't
866 * This must be done last since we modify the key_cert list.
867 */
868 if( ssl_pick_cert( ssl, suite_info ) != 0 )
869 return( 0 );
870#endif
871
872 *ciphersuite_info = suite_info;
873 return( 0 );
874}
875
Paul Bakker78a8c712013-03-06 17:01:52 +0100876#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
877static int ssl_parse_client_hello_v2( ssl_context *ssl )
878{
879 int ret;
880 unsigned int i, j;
881 size_t n;
882 unsigned int ciph_len, sess_len, chal_len;
883 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200884 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200885 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100886
887 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
888
889 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
890 {
891 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
892
893 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
894 return( ret );
895
896 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
897 }
898
899 buf = ssl->in_hdr;
900
901 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
902
903 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
904 buf[2] ) );
905 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
906 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
907 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
908 buf[3], buf[4] ) );
909
910 /*
911 * SSLv2 Client Hello
912 *
913 * Record layer:
914 * 0 . 1 message length
915 *
916 * SSL layer:
917 * 2 . 2 message type
918 * 3 . 4 protocol version
919 */
920 if( buf[2] != SSL_HS_CLIENT_HELLO ||
921 buf[3] != SSL_MAJOR_VERSION_3 )
922 {
923 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
924 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
925 }
926
927 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
928
929 if( n < 17 || n > 512 )
930 {
931 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
932 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
933 }
934
935 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200936 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
937 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100938
939 if( ssl->minor_ver < ssl->min_minor_ver )
940 {
941 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200942 " [%d:%d] < [%d:%d]",
943 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +0100944 ssl->min_major_ver, ssl->min_minor_ver ) );
945
946 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
947 SSL_ALERT_MSG_PROTOCOL_VERSION );
948 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
949 }
950
Paul Bakker2fbefde2013-06-29 16:01:15 +0200951 ssl->handshake->max_major_ver = buf[3];
952 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100953
954 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
955 {
956 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
957 return( ret );
958 }
959
960 ssl->handshake->update_checksum( ssl, buf + 2, n );
961
962 buf = ssl->in_msg;
963 n = ssl->in_left - 5;
964
965 /*
966 * 0 . 1 ciphersuitelist length
967 * 2 . 3 session id length
968 * 4 . 5 challenge length
969 * 6 . .. ciphersuitelist
970 * .. . .. session id
971 * .. . .. challenge
972 */
973 SSL_DEBUG_BUF( 4, "record contents", buf, n );
974
975 ciph_len = ( buf[0] << 8 ) | buf[1];
976 sess_len = ( buf[2] << 8 ) | buf[3];
977 chal_len = ( buf[4] << 8 ) | buf[5];
978
979 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
980 ciph_len, sess_len, chal_len ) );
981
982 /*
983 * Make sure each parameter length is valid
984 */
985 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
986 {
987 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
988 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
989 }
990
991 if( sess_len > 32 )
992 {
993 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
994 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
995 }
996
997 if( chal_len < 8 || chal_len > 32 )
998 {
999 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1000 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1001 }
1002
1003 if( n != 6 + ciph_len + sess_len + chal_len )
1004 {
1005 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1006 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1007 }
1008
1009 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1010 buf + 6, ciph_len );
1011 SSL_DEBUG_BUF( 3, "client hello, session id",
1012 buf + 6 + ciph_len, sess_len );
1013 SSL_DEBUG_BUF( 3, "client hello, challenge",
1014 buf + 6 + ciph_len + sess_len, chal_len );
1015
1016 p = buf + 6 + ciph_len;
1017 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001018 memset( ssl->session_negotiate->id, 0,
1019 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001020 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1021
1022 p += sess_len;
1023 memset( ssl->handshake->randbytes, 0, 64 );
1024 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1025
1026 /*
1027 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1028 */
1029 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1030 {
1031 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1032 {
1033 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1034 if( ssl->renegotiation == SSL_RENEGOTIATION )
1035 {
1036 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1037
1038 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1039 return( ret );
1040
1041 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1042 }
1043 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1044 break;
1045 }
1046 }
1047
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001048 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001049 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001050#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1051 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1052 {
1053 for( i = 0; ciphersuites[i] != 0; i++ )
1054#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001055 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001056 {
1057 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001058#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001059 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001060 if( p[0] != 0 ||
1061 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1062 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1063 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001064
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001065 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1066 &ciphersuite_info ) ) != 0 )
1067 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001068
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001069 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001070 goto have_ciphersuite_v2;
1071 }
1072 }
1073
1074 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1075
1076 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1077
1078have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001079 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001080 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001081 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001082
1083 /*
1084 * SSLv2 Client Hello relevant renegotiation security checks
1085 */
1086 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1087 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1088 {
1089 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1090
1091 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1092 return( ret );
1093
1094 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1095 }
1096
1097 ssl->in_left = 0;
1098 ssl->state++;
1099
1100 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1101
1102 return( 0 );
1103}
1104#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1105
Paul Bakker5121ce52009-01-03 21:22:43 +00001106static int ssl_parse_client_hello( ssl_context *ssl )
1107{
Paul Bakker23986e52011-04-24 08:57:21 +00001108 int ret;
1109 unsigned int i, j;
1110 size_t n;
1111 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001112 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001113 unsigned int ext_len = 0;
1114 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001115 int renegotiation_info_seen = 0;
1116 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001117 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001118 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001119
1120 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1121
Paul Bakker48916f92012-09-16 19:57:18 +00001122 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1123 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001124 {
1125 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1126 return( ret );
1127 }
1128
1129 buf = ssl->in_hdr;
1130
Paul Bakker78a8c712013-03-06 17:01:52 +01001131#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1132 if( ( buf[0] & 0x80 ) != 0 )
1133 return ssl_parse_client_hello_v2( ssl );
1134#endif
1135
Paul Bakkerec636f32012-09-09 19:17:02 +00001136 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1137
1138 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1139 buf[0] ) );
1140 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1141 ( buf[3] << 8 ) | buf[4] ) );
1142 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
1143 buf[1], buf[2] ) );
1144
1145 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001146 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001147 *
1148 * Record layer:
1149 * 0 . 0 message type
1150 * 1 . 2 protocol version
1151 * 3 . 4 message length
1152 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001153
1154 /* According to RFC 5246 Appendix E.1, the version here is typically
1155 * "{03,00}, the lowest version number supported by the client, [or] the
1156 * value of ClientHello.client_version", so the only meaningful check here
1157 * is the major version shouldn't be less than 3 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001158 if( buf[0] != SSL_MSG_HANDSHAKE ||
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001159 buf[1] < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001160 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001161 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1162 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1163 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001164
Paul Bakkerec636f32012-09-09 19:17:02 +00001165 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +00001166
Paul Bakker4f42c112014-04-17 14:48:23 +02001167 if( n < 45 || n > SSL_MAX_CONTENT_LEN )
Paul Bakkerec636f32012-09-09 19:17:02 +00001168 {
1169 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1170 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1171 }
1172
Paul Bakker48916f92012-09-16 19:57:18 +00001173 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1174 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001175 {
1176 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1177 return( ret );
1178 }
1179
1180 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +00001181 if( !ssl->renegotiation )
1182 n = ssl->in_left - 5;
1183 else
1184 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +00001185
Paul Bakker48916f92012-09-16 19:57:18 +00001186 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +00001187
1188 /*
1189 * SSL layer:
1190 * 0 . 0 handshake type
1191 * 1 . 3 handshake length
1192 * 4 . 5 protocol version
1193 * 6 . 9 UNIX time()
1194 * 10 . 37 random bytes
1195 * 38 . 38 session id length
1196 * 39 . 38+x session id
1197 * 39+x . 40+x ciphersuitelist length
Paul Bakker0f651c72014-05-22 15:12:19 +02001198 * 41+x . 40+y ciphersuitelist
1199 * 41+y . 41+y compression alg length
1200 * 42+y . 41+z compression algs
Paul Bakkerec636f32012-09-09 19:17:02 +00001201 * .. . .. extensions
1202 */
1203 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1204
1205 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1206 buf[0] ) );
1207 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1208 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1209 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1210 buf[4], buf[5] ) );
1211
1212 /*
1213 * Check the handshake type and protocol version
1214 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001215 if( buf[0] != SSL_HS_CLIENT_HELLO )
Paul Bakkerec636f32012-09-09 19:17:02 +00001216 {
1217 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1218 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1219 }
1220
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001221 ssl->major_ver = buf[4];
1222 ssl->minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001223
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001224 ssl->handshake->max_major_ver = ssl->major_ver;
1225 ssl->handshake->max_minor_ver = ssl->minor_ver;
1226
1227 if( ssl->major_ver < ssl->min_major_ver ||
1228 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001229 {
1230 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001231 " [%d:%d] < [%d:%d]",
1232 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001233 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001234
1235 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1236 SSL_ALERT_MSG_PROTOCOL_VERSION );
1237
1238 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1239 }
1240
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001241 if( ssl->major_ver > ssl->max_major_ver )
1242 {
1243 ssl->major_ver = ssl->max_major_ver;
1244 ssl->minor_ver = ssl->max_minor_ver;
1245 }
1246 else if( ssl->minor_ver > ssl->max_minor_ver )
1247 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001248
Paul Bakker48916f92012-09-16 19:57:18 +00001249 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001250
1251 /*
1252 * Check the handshake message length
1253 */
1254 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1255 {
1256 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1257 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1258 }
1259
1260 /*
1261 * Check the session length
1262 */
1263 sess_len = buf[38];
1264
Paul Bakker0f651c72014-05-22 15:12:19 +02001265 if( sess_len > 32 || sess_len > n - 42 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001266 {
1267 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1268 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1269 }
1270
Paul Bakker48916f92012-09-16 19:57:18 +00001271 ssl->session_negotiate->length = sess_len;
1272 memset( ssl->session_negotiate->id, 0,
1273 sizeof( ssl->session_negotiate->id ) );
1274 memcpy( ssl->session_negotiate->id, buf + 39,
1275 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001276
1277 /*
1278 * Check the ciphersuitelist length
1279 */
1280 ciph_len = ( buf[39 + sess_len] << 8 )
1281 | ( buf[40 + sess_len] );
1282
Paul Bakker0f651c72014-05-22 15:12:19 +02001283 if( ciph_len < 2 || ( ciph_len % 2 ) != 0 || ciph_len > n - 42 - sess_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001284 {
1285 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1286 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1287 }
1288
1289 /*
1290 * Check the compression algorithms length
1291 */
1292 comp_len = buf[41 + sess_len + ciph_len];
1293
Paul Bakker0f651c72014-05-22 15:12:19 +02001294 if( comp_len < 1 || comp_len > 16 ||
1295 comp_len > n - 42 - sess_len - ciph_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001296 {
1297 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1298 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1299 }
1300
Paul Bakker48916f92012-09-16 19:57:18 +00001301 /*
1302 * Check the extension length
1303 */
1304 if( n > 42 + sess_len + ciph_len + comp_len )
1305 {
1306 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1307 | ( buf[43 + sess_len + ciph_len + comp_len] );
1308
1309 if( ( ext_len > 0 && ext_len < 4 ) ||
1310 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1311 {
1312 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1313 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1314 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1315 }
1316 }
1317
1318 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001319#if defined(POLARSSL_ZLIB_SUPPORT)
1320 for( i = 0; i < comp_len; ++i )
1321 {
Paul Bakker48916f92012-09-16 19:57:18 +00001322 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001323 {
Paul Bakker48916f92012-09-16 19:57:18 +00001324 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001325 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001326 }
1327 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001328#endif
1329
Paul Bakkerec636f32012-09-09 19:17:02 +00001330 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1331 buf + 6, 32 );
1332 SSL_DEBUG_BUF( 3, "client hello, session id",
1333 buf + 38, sess_len );
1334 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1335 buf + 41 + sess_len, ciph_len );
1336 SSL_DEBUG_BUF( 3, "client hello, compression",
1337 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001338
Paul Bakkerec636f32012-09-09 19:17:02 +00001339 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001340 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1341 */
1342 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1343 {
1344 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1345 {
1346 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1347 if( ssl->renegotiation == SSL_RENEGOTIATION )
1348 {
1349 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001350
1351 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1352 return( ret );
1353
Paul Bakker48916f92012-09-16 19:57:18 +00001354 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1355 }
1356 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1357 break;
1358 }
1359 }
1360
Paul Bakker48916f92012-09-16 19:57:18 +00001361 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001362
1363 while( ext_len )
1364 {
1365 unsigned int ext_id = ( ( ext[0] << 8 )
1366 | ( ext[1] ) );
1367 unsigned int ext_size = ( ( ext[2] << 8 )
1368 | ( ext[3] ) );
1369
1370 if( ext_size + 4 > ext_len )
1371 {
1372 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1373 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1374 }
1375 switch( ext_id )
1376 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001377#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001378 case TLS_EXT_SERVERNAME:
1379 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1380 if( ssl->f_sni == NULL )
1381 break;
1382
1383 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1384 if( ret != 0 )
1385 return( ret );
1386 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001387#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001388
Paul Bakker48916f92012-09-16 19:57:18 +00001389 case TLS_EXT_RENEGOTIATION_INFO:
1390 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1391 renegotiation_info_seen = 1;
1392
Paul Bakker23f36802012-09-28 14:15:14 +00001393 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1394 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001395 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001396 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001397
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001398#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001399 case TLS_EXT_SIG_ALG:
1400 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1401 if( ssl->renegotiation == SSL_RENEGOTIATION )
1402 break;
1403
1404 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1405 if( ret != 0 )
1406 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001407 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001408#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001409
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001410#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001411 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1412 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1413
1414 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1415 if( ret != 0 )
1416 return( ret );
1417 break;
1418
1419 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1420 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001421 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001422
1423 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1424 if( ret != 0 )
1425 return( ret );
1426 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001427#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001428
Paul Bakker05decb22013-08-15 13:33:48 +02001429#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001430 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1431 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1432
1433 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1434 if( ret != 0 )
1435 return( ret );
1436 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001437#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001438
Paul Bakker1f2bc622013-08-15 13:45:55 +02001439#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001440 case TLS_EXT_TRUNCATED_HMAC:
1441 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1442
1443 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1444 if( ret != 0 )
1445 return( ret );
1446 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001447#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001448
Paul Bakkera503a632013-08-14 13:48:06 +02001449#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001450 case TLS_EXT_SESSION_TICKET:
1451 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1452
1453 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1454 if( ret != 0 )
1455 return( ret );
1456 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001457#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001458
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001459#if defined(POLARSSL_SSL_ALPN)
1460 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001461 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001462
1463 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1464 if( ret != 0 )
1465 return( ret );
1466 break;
1467#endif /* POLARSSL_SSL_SESSION_TICKETS */
1468
Paul Bakker48916f92012-09-16 19:57:18 +00001469 default:
1470 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1471 ext_id ) );
1472 }
1473
1474 ext_len -= 4 + ext_size;
1475 ext += 4 + ext_size;
1476
1477 if( ext_len > 0 && ext_len < 4 )
1478 {
1479 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1480 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1481 }
1482 }
1483
1484 /*
1485 * Renegotiation security checks
1486 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001487 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1488 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1489 {
1490 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1491 handshake_failure = 1;
1492 }
1493 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1494 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1495 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001496 {
1497 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001498 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001499 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001500 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1501 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1502 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001503 {
1504 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001505 handshake_failure = 1;
1506 }
1507 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1508 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1509 renegotiation_info_seen == 1 )
1510 {
1511 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1512 handshake_failure = 1;
1513 }
1514
1515 if( handshake_failure == 1 )
1516 {
1517 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1518 return( ret );
1519
Paul Bakker48916f92012-09-16 19:57:18 +00001520 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1521 }
Paul Bakker380da532012-04-18 16:10:25 +00001522
Paul Bakker41c83d32013-03-20 14:39:14 +01001523 /*
1524 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001525 * (At the end because we need information from the EC-based extensions
1526 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001527 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001528 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001529 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001530#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1531 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
1532 {
1533 for( i = 0; ciphersuites[i] != 0; i++ )
1534#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001535 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001536 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001537 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001538#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001539 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001540 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1541 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1542 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001543
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001544 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1545 &ciphersuite_info ) ) != 0 )
1546 return( ret );
1547
1548 if( ciphersuite_info != NULL )
1549 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001550 }
1551 }
1552
1553 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1554
1555 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1556 return( ret );
1557
1558 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1559
1560have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001561 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001562 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1563 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1564
Paul Bakker5121ce52009-01-03 21:22:43 +00001565 ssl->in_left = 0;
1566 ssl->state++;
1567
1568 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1569
1570 return( 0 );
1571}
1572
Paul Bakker1f2bc622013-08-15 13:45:55 +02001573#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001574static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1575 unsigned char *buf,
1576 size_t *olen )
1577{
1578 unsigned char *p = buf;
1579
1580 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1581 {
1582 *olen = 0;
1583 return;
1584 }
1585
1586 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1587
1588 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1589 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1590
1591 *p++ = 0x00;
1592 *p++ = 0x00;
1593
1594 *olen = 4;
1595}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001596#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001597
Paul Bakkera503a632013-08-14 13:48:06 +02001598#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001599static void ssl_write_session_ticket_ext( ssl_context *ssl,
1600 unsigned char *buf,
1601 size_t *olen )
1602{
1603 unsigned char *p = buf;
1604
1605 if( ssl->handshake->new_session_ticket == 0 )
1606 {
1607 *olen = 0;
1608 return;
1609 }
1610
1611 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1612
1613 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1614 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1615
1616 *p++ = 0x00;
1617 *p++ = 0x00;
1618
1619 *olen = 4;
1620}
Paul Bakkera503a632013-08-14 13:48:06 +02001621#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001622
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001623static void ssl_write_renegotiation_ext( ssl_context *ssl,
1624 unsigned char *buf,
1625 size_t *olen )
1626{
1627 unsigned char *p = buf;
1628
1629 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1630 {
1631 *olen = 0;
1632 return;
1633 }
1634
1635 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1636
1637 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1638 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1639
1640 *p++ = 0x00;
1641 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1642 *p++ = ssl->verify_data_len * 2 & 0xFF;
1643
1644 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1645 p += ssl->verify_data_len;
1646 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1647 p += ssl->verify_data_len;
1648
1649 *olen = 5 + ssl->verify_data_len * 2;
1650}
1651
Paul Bakker05decb22013-08-15 13:33:48 +02001652#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001653static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1654 unsigned char *buf,
1655 size_t *olen )
1656{
1657 unsigned char *p = buf;
1658
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001659 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1660 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001661 *olen = 0;
1662 return;
1663 }
1664
1665 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1666
1667 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1668 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1669
1670 *p++ = 0x00;
1671 *p++ = 1;
1672
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001673 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001674
1675 *olen = 5;
1676}
Paul Bakker05decb22013-08-15 13:33:48 +02001677#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001678
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001679#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001680static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1681 unsigned char *buf,
1682 size_t *olen )
1683{
1684 unsigned char *p = buf;
1685 ((void) ssl);
1686
Paul Bakker677377f2013-10-28 12:54:26 +01001687 if( ( ssl->handshake->cli_exts &
1688 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
1689 {
1690 *olen = 0;
1691 return;
1692 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001693
1694 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1695
1696 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1697 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1698
1699 *p++ = 0x00;
1700 *p++ = 2;
1701
1702 *p++ = 1;
1703 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1704
1705 *olen = 6;
1706}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001707#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001708
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001709#if defined(POLARSSL_SSL_ALPN )
1710static void ssl_write_alpn_ext( ssl_context *ssl,
1711 unsigned char *buf, size_t *olen )
1712{
1713 if( ssl->alpn_chosen == NULL )
1714 {
1715 *olen = 0;
1716 return;
1717 }
1718
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001719 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001720
1721 /*
1722 * 0 . 1 ext identifier
1723 * 2 . 3 ext length
1724 * 4 . 5 protocol list length
1725 * 6 . 6 protocol name length
1726 * 7 . 7+n protocol name
1727 */
1728 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
1729 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
1730
1731 *olen = 7 + strlen( ssl->alpn_chosen );
1732
1733 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
1734 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
1735
1736 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
1737 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
1738
1739 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
1740
1741 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
1742}
1743#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
1744
Paul Bakker5121ce52009-01-03 21:22:43 +00001745static int ssl_write_server_hello( ssl_context *ssl )
1746{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001747#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001748 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001749#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001750 int ret;
1751 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001752 unsigned char *buf, *p;
1753
1754 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1755
Paul Bakkera9a028e2013-11-21 17:31:06 +01001756 if( ssl->f_rng == NULL )
1757 {
1758 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
1759 return( POLARSSL_ERR_SSL_NO_RNG );
1760 }
1761
Paul Bakker5121ce52009-01-03 21:22:43 +00001762 /*
1763 * 0 . 0 handshake type
1764 * 1 . 3 handshake length
1765 * 4 . 5 protocol version
1766 * 6 . 9 UNIX time()
1767 * 10 . 37 random bytes
1768 */
1769 buf = ssl->out_msg;
1770 p = buf + 4;
1771
1772 *p++ = (unsigned char) ssl->major_ver;
1773 *p++ = (unsigned char) ssl->minor_ver;
1774
1775 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1776 buf[4], buf[5] ) );
1777
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001778#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001779 t = time( NULL );
1780 *p++ = (unsigned char)( t >> 24 );
1781 *p++ = (unsigned char)( t >> 16 );
1782 *p++ = (unsigned char)( t >> 8 );
1783 *p++ = (unsigned char)( t );
1784
1785 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001786#else
1787 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1788 return( ret );
1789
1790 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02001791#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001792
Paul Bakkera3d195c2011-11-27 21:07:34 +00001793 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1794 return( ret );
1795
1796 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001797
Paul Bakker48916f92012-09-16 19:57:18 +00001798 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001799
1800 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1801
1802 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001803 * Resume is 0 by default, see ssl_handshake_init().
1804 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1805 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001806 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001807 if( ssl->handshake->resume == 0 &&
1808 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001809 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001810 ssl->f_get_cache != NULL &&
1811 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1812 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01001813 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001814 ssl->handshake->resume = 1;
1815 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001816
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001817 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001818 {
1819 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001820 * New session, create a new session id,
1821 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001822 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001823 ssl->state++;
1824
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001825#if defined(POLARSSL_HAVE_TIME)
1826 ssl->session_negotiate->start = time( NULL );
1827#endif
1828
Paul Bakkera503a632013-08-14 13:48:06 +02001829#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001830 if( ssl->handshake->new_session_ticket != 0 )
1831 {
1832 ssl->session_negotiate->length = n = 0;
1833 memset( ssl->session_negotiate->id, 0, 32 );
1834 }
1835 else
1836#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001837 {
1838 ssl->session_negotiate->length = n = 32;
1839 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001840 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001841 return( ret );
1842 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001843 }
1844 else
1845 {
1846 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001847 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001848 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001849 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001850 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001851
1852 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1853 {
1854 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1855 return( ret );
1856 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001857 }
1858
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001859 /*
1860 * 38 . 38 session id length
1861 * 39 . 38+n session id
1862 * 39+n . 40+n chosen ciphersuite
1863 * 41+n . 41+n chosen compression alg.
1864 * 42+n . 43+n extensions length
1865 * 44+n . 43+n+m extensions
1866 */
1867 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001868 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1869 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001870
1871 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1872 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1873 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001874 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001875
Paul Bakker48916f92012-09-16 19:57:18 +00001876 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1877 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1878 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001879
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001880 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
1881 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001882 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001883 ssl->session_negotiate->compression ) );
1884
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001885 /*
1886 * First write extensions, then the total length
1887 */
1888 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1889 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001890
Paul Bakker05decb22013-08-15 13:33:48 +02001891#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001892 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1893 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001894#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001895
Paul Bakker1f2bc622013-08-15 13:45:55 +02001896#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001897 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1898 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001899#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001900
Paul Bakkera503a632013-08-14 13:48:06 +02001901#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001902 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1903 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001904#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001905
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001906#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001907 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1908 ext_len += olen;
1909#endif
1910
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001911#if defined(POLARSSL_SSL_ALPN)
1912 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
1913 ext_len += olen;
1914#endif
1915
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001916 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001917
Paul Bakkera7036632014-04-30 10:15:38 +02001918 if( ext_len > 0 )
1919 {
1920 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1921 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1922 p += ext_len;
1923 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001924
1925 ssl->out_msglen = p - buf;
1926 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1927 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1928
1929 ret = ssl_write_record( ssl );
1930
1931 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1932
1933 return( ret );
1934}
1935
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001936#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1937 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001938 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1939 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001940static int ssl_write_certificate_request( ssl_context *ssl )
1941{
Paul Bakkered27a042013-04-18 22:46:23 +02001942 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001943
1944 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1945
1946 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01001947 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001948 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1949 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001950 {
1951 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1952 ssl->state++;
1953 return( 0 );
1954 }
1955
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001956 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1957 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001958}
1959#else
1960static int ssl_write_certificate_request( ssl_context *ssl )
1961{
1962 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1963 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001964 size_t dn_size, total_dn_size; /* excluding length bytes */
1965 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00001966 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001967 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001968
1969 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1970
1971 ssl->state++;
1972
Paul Bakkerfbb17802013-04-17 19:10:21 +02001973 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01001974 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001975 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001976 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001977 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001978 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001979 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001980 return( 0 );
1981 }
1982
1983 /*
1984 * 0 . 0 handshake type
1985 * 1 . 3 handshake length
1986 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01001987 * 5 .. m-1 cert types
1988 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02001989 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001990 * n .. n+1 length of all DNs
1991 * n+2 .. n+3 length of DN 1
1992 * n+4 .. ... Distinguished Name #1
1993 * ... .. ... length of DN 2, etc.
1994 */
1995 buf = ssl->out_msg;
1996 p = buf + 4;
1997
1998 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001999 * Supported certificate types
2000 *
2001 * ClientCertificateType certificate_types<1..2^8-1>;
2002 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002003 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002004 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002005
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002006#if defined(POLARSSL_RSA_C)
2007 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2008#endif
2009#if defined(POLARSSL_ECDSA_C)
2010 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2011#endif
2012
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002013 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002014 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002015
Paul Bakker577e0062013-08-28 11:57:20 +02002016 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002017#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002018 /*
2019 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002020 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002021 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2022 *
2023 * struct {
2024 * HashAlgorithm hash;
2025 * SignatureAlgorithm signature;
2026 * } SignatureAndHashAlgorithm;
2027 *
2028 * enum { (255) } HashAlgorithm;
2029 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002030 */
Paul Bakker21dca692013-01-03 11:41:08 +01002031 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002032 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002033 /*
2034 * Only use current running hash algorithm that is already required
2035 * for requested ciphersuite.
2036 */
Paul Bakker926af752012-11-23 13:38:07 +01002037 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2038
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002039 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2040 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002041 {
2042 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2043 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002044
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002045 /*
2046 * Supported signature algorithms
2047 */
2048#if defined(POLARSSL_RSA_C)
2049 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2050 p[2 + sa_len++] = SSL_SIG_RSA;
2051#endif
2052#if defined(POLARSSL_ECDSA_C)
2053 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2054 p[2 + sa_len++] = SSL_SIG_ECDSA;
2055#endif
Paul Bakker926af752012-11-23 13:38:07 +01002056
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002057 p[0] = (unsigned char)( sa_len >> 8 );
2058 p[1] = (unsigned char)( sa_len );
2059 sa_len += 2;
2060 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002061 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002062#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002063
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002064 /*
2065 * DistinguishedName certificate_authorities<0..2^16-1>;
2066 * opaque DistinguishedName<1..2^16-1>;
2067 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 p += 2;
2069 crt = ssl->ca_chain;
2070
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002071 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002072 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002073 {
2074 if( p - buf > 4096 )
2075 break;
2076
Paul Bakker926af752012-11-23 13:38:07 +01002077 dn_size = crt->subject_raw.len;
2078 *p++ = (unsigned char)( dn_size >> 8 );
2079 *p++ = (unsigned char)( dn_size );
2080 memcpy( p, crt->subject_raw.p, dn_size );
2081 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002082
Paul Bakker926af752012-11-23 13:38:07 +01002083 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2084
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002085 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002086 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002087 }
2088
Paul Bakker926af752012-11-23 13:38:07 +01002089 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002090 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2091 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002092 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2093 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002094
2095 ret = ssl_write_record( ssl );
2096
2097 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2098
2099 return( ret );
2100}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002101#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2102 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002103 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2104 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002105
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002106#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2107 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2108static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2109{
2110 int ret;
2111
2112 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2113 {
2114 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2115 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2116 }
2117
2118 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2119 pk_ec( *ssl_own_key( ssl ) ),
2120 POLARSSL_ECDH_OURS ) ) != 0 )
2121 {
2122 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2123 return( ret );
2124 }
2125
2126 return( 0 );
2127}
2128#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2129 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2130
Paul Bakker41c83d32013-03-20 14:39:14 +01002131static int ssl_write_server_key_exchange( ssl_context *ssl )
2132{
Paul Bakker23986e52011-04-24 08:57:21 +00002133 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002134 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002135 const ssl_ciphersuite_t *ciphersuite_info =
2136 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002137
2138#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2139 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2140 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002141 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002142 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002143 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002144 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002145 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002146 ((void) dig_signed);
2147 ((void) dig_signed_len);
2148#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002149
Paul Bakker5121ce52009-01-03 21:22:43 +00002150 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2151
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002152#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2153 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2154 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002155 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2156 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2157 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002158 {
2159 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2160 ssl->state++;
2161 return( 0 );
2162 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002163#endif
2164
2165#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2166 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2167 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2168 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2169 {
2170 ssl_get_ecdh_params_from_cert( ssl );
2171
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002172 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002173 ssl->state++;
2174 return( 0 );
2175 }
2176#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002177
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002178#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2179 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2180 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2181 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002182 {
2183 /* TODO: Support identity hints */
2184 *(p++) = 0x00;
2185 *(p++) = 0x00;
2186
2187 n += 2;
2188 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002189#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2190 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002191
2192#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2193 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2194 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2195 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002196 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002197 /*
2198 * Ephemeral DH parameters:
2199 *
2200 * struct {
2201 * opaque dh_p<1..2^16-1>;
2202 * opaque dh_g<1..2^16-1>;
2203 * opaque dh_Ys<1..2^16-1>;
2204 * } ServerDHParams;
2205 */
2206 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2207 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2208 {
2209 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2210 return( ret );
2211 }
Paul Bakker48916f92012-09-16 19:57:18 +00002212
Paul Bakker41c83d32013-03-20 14:39:14 +01002213 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002214 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2215 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002216 {
2217 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2218 return( ret );
2219 }
2220
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002221 dig_signed = p;
2222 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002223
2224 p += len;
2225 n += len;
2226
Paul Bakker41c83d32013-03-20 14:39:14 +01002227 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2228 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2229 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2230 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2231 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002232#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2233 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002234
Gergely Budai987bfb52014-01-19 21:48:42 +01002235#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002236 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002237 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2238 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002239 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002240 /*
2241 * Ephemeral ECDH parameters:
2242 *
2243 * struct {
2244 * ECParameters curve_params;
2245 * ECPoint public;
2246 * } ServerECDHParams;
2247 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002248 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002249#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002250 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002251
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002252 /* Match our preference list against the offered curves */
2253 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2254 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2255 if( (*curve)->grp_id == *gid )
2256 goto curve_matching_done;
2257
2258curve_matching_done:
2259#else
2260 curve = ssl->handshake->curves;
2261#endif
2262
2263 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002264 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002265 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2266 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002267 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002268
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002269 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002270
Paul Bakker41c83d32013-03-20 14:39:14 +01002271 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002272 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002273 {
2274 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2275 return( ret );
2276 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002277
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002278 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2279 p, SSL_MAX_CONTENT_LEN - n,
2280 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002281 {
2282 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2283 return( ret );
2284 }
2285
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002286 dig_signed = p;
2287 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002288
2289 p += len;
2290 n += len;
2291
Paul Bakker41c83d32013-03-20 14:39:14 +01002292 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2293 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002294#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002295
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002296#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002297 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2298 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002299 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002300 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2301 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002302 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002303 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002304 unsigned int hashlen = 0;
2305 unsigned char hash[64];
2306 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002307
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002308 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002309 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2310 */
Paul Bakker577e0062013-08-28 11:57:20 +02002311#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002312 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2313 {
2314 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2315
2316 if( md_alg == POLARSSL_MD_NONE )
2317 {
2318 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002319 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002320 }
2321 }
Paul Bakker577e0062013-08-28 11:57:20 +02002322 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002323#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002324#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2325 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002326 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002327 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2328 {
2329 md_alg = POLARSSL_MD_SHA1;
2330 }
2331 else
Paul Bakker577e0062013-08-28 11:57:20 +02002332#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002333 {
2334 md_alg = POLARSSL_MD_NONE;
2335 }
2336
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002337 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002338 * Compute the hash to be signed
2339 */
Paul Bakker577e0062013-08-28 11:57:20 +02002340#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2341 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002342 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002343 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002344 md5_context md5;
2345 sha1_context sha1;
2346
Paul Bakker5b4af392014-06-26 12:09:34 +02002347 md5_init( &md5 );
2348 sha1_init( &sha1 );
2349
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002350 /*
2351 * digitally-signed struct {
2352 * opaque md5_hash[16];
2353 * opaque sha_hash[20];
2354 * };
2355 *
2356 * md5_hash
2357 * MD5(ClientHello.random + ServerHello.random
2358 * + ServerParams);
2359 * sha_hash
2360 * SHA(ClientHello.random + ServerHello.random
2361 * + ServerParams);
2362 */
2363 md5_starts( &md5 );
2364 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002365 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002366 md5_finish( &md5, hash );
2367
2368 sha1_starts( &sha1 );
2369 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002370 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002371 sha1_finish( &sha1, hash + 16 );
2372
2373 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02002374
2375 md5_free( &md5 );
2376 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002377 }
2378 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002379#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2380 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002381#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2382 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002383 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002384 {
2385 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02002386 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002387
Paul Bakker84bbeb52014-07-01 14:53:22 +02002388 md_init( &ctx );
2389
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002390 /* Info from md_alg will be used instead */
2391 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002392
2393 /*
2394 * digitally-signed struct {
2395 * opaque client_random[32];
2396 * opaque server_random[32];
2397 * ServerDHParams params;
2398 * };
2399 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002400 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002401 {
2402 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2403 return( ret );
2404 }
2405
2406 md_starts( &ctx );
2407 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002408 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002409 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002410 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00002411 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002412 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002413#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2414 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002415 {
2416 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002417 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002418 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002419
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002420 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2421 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002422
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002423 /*
2424 * Make the signature
2425 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002426 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002427 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002428 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2429 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002430 }
Paul Bakker23f36802012-09-28 14:15:14 +00002431
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002432#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002433 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2434 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002435 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002436 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002437
2438 n += 2;
2439 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002440#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002441
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002442 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002443 p + 2 , &signature_len,
2444 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002445 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002446 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002447 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002448 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002449
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002450 *(p++) = (unsigned char)( signature_len >> 8 );
2451 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002452 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002453
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002454 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002455
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002456 p += signature_len;
2457 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002458 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002459#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002460 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2461 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002462
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002463 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002464 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2465 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2466
2467 ssl->state++;
2468
2469 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2470 {
2471 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2472 return( ret );
2473 }
2474
2475 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2476
2477 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002478}
2479
2480static int ssl_write_server_hello_done( ssl_context *ssl )
2481{
2482 int ret;
2483
2484 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2485
2486 ssl->out_msglen = 4;
2487 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2488 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2489
2490 ssl->state++;
2491
2492 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2493 {
2494 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2495 return( ret );
2496 }
2497
2498 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2499
2500 return( 0 );
2501}
2502
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002503#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2504 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2505static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2506 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002507{
2508 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002509 size_t n;
2510
2511 /*
2512 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2513 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002514 if( *p + 2 > end )
2515 {
2516 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2517 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2518 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002519
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002520 n = ( (*p)[0] << 8 ) | (*p)[1];
2521 *p += 2;
2522
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002523 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002524 {
2525 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2526 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2527 }
2528
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002529 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002530 {
2531 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2532 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2533 }
2534
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002535 *p += n;
2536
Paul Bakker70df2fb2013-04-17 17:19:09 +02002537 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2538
Paul Bakker70df2fb2013-04-17 17:19:09 +02002539 return( ret );
2540}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002541#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2542 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002543
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002544#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2545 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002546static int ssl_parse_encrypted_pms( ssl_context *ssl,
2547 const unsigned char *p,
2548 const unsigned char *end,
2549 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002550{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002551 int ret;
2552 size_t len = pk_get_len( ssl_own_key( ssl ) );
2553 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002554
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002555 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002556 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002557 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002558 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2559 }
2560
2561 /*
2562 * Decrypt the premaster using own private RSA key
2563 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002564#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2565 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002566 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2567 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002568 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2569 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002570 {
2571 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2572 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2573 }
2574 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002575#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002576
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002577 if( p + len != 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é-Gonnard0fae60b2013-10-14 17:39:48 +02002583 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2584 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002585 sizeof( ssl->handshake->premaster ) - pms_offset,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002586 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002587
2588 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002589 pms[0] != ssl->handshake->max_major_ver ||
2590 pms[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002591 {
2592 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2593
2594 /*
2595 * Protection against Bleichenbacher's attack:
2596 * invalid PKCS#1 v1.5 padding must not cause
2597 * the connection to end immediately; instead,
2598 * send a bad_record_mac later in the handshake.
2599 */
2600 ssl->handshake->pmslen = 48;
2601
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002602 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002603 if( ret != 0 )
2604 return( ret );
2605 }
2606
2607 return( ret );
2608}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002609#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2610 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002611
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002612#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002613static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2614 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002615{
Paul Bakker6db455e2013-09-18 17:29:31 +02002616 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002617 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002618
Paul Bakker6db455e2013-09-18 17:29:31 +02002619 if( ssl->f_psk == NULL &&
2620 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2621 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002622 {
2623 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2624 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2625 }
2626
2627 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002628 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002629 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002630 if( *p + 2 > end )
2631 {
2632 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2633 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2634 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002635
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002636 n = ( (*p)[0] << 8 ) | (*p)[1];
2637 *p += 2;
2638
2639 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002640 {
2641 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2642 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2643 }
2644
Paul Bakker6db455e2013-09-18 17:29:31 +02002645 if( ssl->f_psk != NULL )
2646 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002647 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002648 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2649 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002650 else
Paul Bakker6db455e2013-09-18 17:29:31 +02002651 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002652 /* Identity is not a big secret since clients send it in the clear,
2653 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02002654 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002655 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002656 {
2657 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2658 }
2659 }
2660
2661 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002662 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002663 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002664 if( ( ret = ssl_send_alert_message( ssl,
2665 SSL_ALERT_LEVEL_FATAL,
2666 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2667 {
2668 return( ret );
2669 }
2670
2671 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002672 }
2673
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002674 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002675
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002676 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002677}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002678#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002679
Paul Bakker5121ce52009-01-03 21:22:43 +00002680static int ssl_parse_client_key_exchange( ssl_context *ssl )
2681{
Paul Bakker23986e52011-04-24 08:57:21 +00002682 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002683 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002684
Paul Bakker41c83d32013-03-20 14:39:14 +01002685 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002686
2687 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2688
2689 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2690 {
2691 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2692 return( ret );
2693 }
2694
2695 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2696 {
2697 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002698 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002699 }
2700
2701 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2702 {
2703 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002704 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002705 }
2706
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002707#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002708 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002709 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002710 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002711 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002712
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002713 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002714 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002715 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2716 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002717 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002718
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002719 if( p != end )
2720 {
2721 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2722 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2723 }
2724
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002725 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002726
2727 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2728 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002729 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002730 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002731 {
2732 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2733 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2734 }
2735
2736 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002737 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002738 else
2739#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002740#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002741 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2742 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2743 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002744 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002745 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2746 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2747 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002748 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002749 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002750 ssl->in_msg + 4, ssl->in_hslen - 4 ) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002751 {
2752 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2753 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2754 }
2755
2756 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2757
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002758 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2759 &ssl->handshake->pmslen,
2760 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002761 POLARSSL_MPI_MAX_SIZE,
2762 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002763 {
2764 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2765 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2766 }
2767
2768 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002769 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002770 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002771#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002772 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2773 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2774 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002775#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2776 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002777 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002778 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002779 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002780
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002781 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002782 {
2783 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2784 return( ret );
2785 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002786
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002787 if( p != end )
2788 {
2789 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2790 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2791 }
2792
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002793 if( ( ret = ssl_psk_derive_premaster( ssl,
2794 ciphersuite_info->key_exchange ) ) != 0 )
2795 {
2796 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2797 return( ret );
2798 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002799 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002800 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002801#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002802#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2803 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2804 {
2805 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002806 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002807
2808 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2809 {
2810 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2811 return( ret );
2812 }
2813
2814 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
2815 {
2816 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
2817 return( ret );
2818 }
2819
2820 if( ( ret = ssl_psk_derive_premaster( ssl,
2821 ciphersuite_info->key_exchange ) ) != 0 )
2822 {
2823 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2824 return( ret );
2825 }
2826 }
2827 else
2828#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002829#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2830 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2831 {
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;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002834
2835 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2836 {
2837 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2838 return( ret );
2839 }
2840 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2841 {
2842 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2843 return( ret );
2844 }
2845
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002846 if( p != end )
2847 {
2848 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2849 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2850 }
2851
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002852 if( ( ret = ssl_psk_derive_premaster( ssl,
2853 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002854 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002855 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2856 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002857 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002858 }
2859 else
2860#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002861#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2862 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2863 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002864 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002865 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002866
2867 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2868 {
2869 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2870 return( ret );
2871 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002872
2873 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2874 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002875 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002876 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2877 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002878 }
2879
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002880 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2881
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002882 if( ( ret = ssl_psk_derive_premaster( ssl,
2883 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002884 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002885 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002886 return( ret );
2887 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002888 }
2889 else
2890#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002891#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2892 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002893 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002894 if( ( ret = ssl_parse_encrypted_pms( ssl,
2895 ssl->in_msg + 4,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002896 ssl->in_msg + ssl->in_hslen,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002897 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002898 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002899 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002900 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002901 }
2902 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002903 else
2904#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2905 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002906 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002907 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002908 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002909
Paul Bakkerff60ee62010-03-16 21:09:09 +00002910 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2911 {
2912 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2913 return( ret );
2914 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002915
Paul Bakker5121ce52009-01-03 21:22:43 +00002916 ssl->state++;
2917
2918 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2919
2920 return( 0 );
2921}
2922
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002923#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2924 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002925 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2926 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002927static int ssl_parse_certificate_verify( ssl_context *ssl )
2928{
Paul Bakkerfbb17802013-04-17 19:10:21 +02002929 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002930
2931 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2932
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002933 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002934 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002935 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002936 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002937 {
2938 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2939 ssl->state++;
2940 return( 0 );
2941 }
2942
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002943 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2944 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002945}
2946#else
2947static int ssl_parse_certificate_verify( ssl_context *ssl )
2948{
2949 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002950 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002951 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002952 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002953 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02002954#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002955 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02002956#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002957 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002958 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2959
2960 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2961
2962 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002963 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002964 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002965 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2966 {
2967 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2968 ssl->state++;
2969 return( 0 );
2970 }
2971
Paul Bakkered27a042013-04-18 22:46:23 +02002972 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002973 {
2974 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2975 ssl->state++;
2976 return( 0 );
2977 }
2978
Paul Bakker48916f92012-09-16 19:57:18 +00002979 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002980
2981 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2982 {
2983 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2984 return( ret );
2985 }
2986
2987 ssl->state++;
2988
2989 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2990 {
2991 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002992 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002993 }
2994
2995 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
2996 {
2997 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002998 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002999 }
3000
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003001 /*
3002 * 0 . 0 handshake type
3003 * 1 . 3 handshake length
3004 * 4 . 5 sig alg (TLS 1.2 only)
3005 * 4+n . 5+n signature length (n = sa_len)
3006 * 6+n . 6+n+m signature (m = sig_len)
3007 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003008
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003009#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3010 defined(POLARSSL_SSL_PROTO_TLS1_1)
3011 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003012 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003013 sa_len = 0;
3014
Paul Bakkerc70b9822013-04-07 22:00:46 +02003015 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003016 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003017
3018 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3019 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3020 POLARSSL_PK_ECDSA ) )
3021 {
3022 hash_start += 16;
3023 hashlen -= 16;
3024 md_alg = POLARSSL_MD_SHA1;
3025 }
Paul Bakker926af752012-11-23 13:38:07 +01003026 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003027 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003028#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3029 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003030#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3031 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003032 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003033 sa_len = 2;
3034
Paul Bakker5121ce52009-01-03 21:22:43 +00003035 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003036 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003037 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003038 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003039 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003040 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3041 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003042 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3043 }
3044
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003045 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003046
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003047 /* Info from md_alg will be used instead */
3048 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003049
3050 /*
3051 * Signature
3052 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003053 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
3054 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003055 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003056 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3057 " for verify message" ) );
3058 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003059 }
3060
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003061 /*
3062 * Check the certificate's key type matches the signature alg
3063 */
3064 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3065 {
3066 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3067 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3068 }
Paul Bakker577e0062013-08-28 11:57:20 +02003069 }
3070 else
3071#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3072 {
3073 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003074 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003075 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003076
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003077 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01003078
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003079 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003080 {
3081 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003082 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003083 }
3084
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003085 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003086 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003087 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003088 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003089 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003090 return( ret );
3091 }
3092
3093 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3094
Paul Bakkered27a042013-04-18 22:46:23 +02003095 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003096}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003097#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3098 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3099 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003100
Paul Bakkera503a632013-08-14 13:48:06 +02003101#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003102static int ssl_write_new_session_ticket( ssl_context *ssl )
3103{
3104 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003105 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003106 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003107
3108 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3109
3110 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3111 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3112
3113 /*
3114 * struct {
3115 * uint32 ticket_lifetime_hint;
3116 * opaque ticket<0..2^16-1>;
3117 * } NewSessionTicket;
3118 *
3119 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3120 * 8 . 9 ticket_len (n)
3121 * 10 . 9+n ticket content
3122 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003123
3124 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3125 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3126 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3127 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003128
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003129 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3130 {
3131 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3132 tlen = 0;
3133 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003134
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003135 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3136 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003137
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003138 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003139
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003140 /*
3141 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3142 * ChangeCipherSpec share the same state.
3143 */
3144 ssl->handshake->new_session_ticket = 0;
3145
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003146 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3147 {
3148 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3149 return( ret );
3150 }
3151
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003152 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3153
3154 return( 0 );
3155}
Paul Bakkera503a632013-08-14 13:48:06 +02003156#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003157
Paul Bakker5121ce52009-01-03 21:22:43 +00003158/*
Paul Bakker1961b702013-01-25 14:49:24 +01003159 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003160 */
Paul Bakker1961b702013-01-25 14:49:24 +01003161int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003162{
3163 int ret = 0;
3164
Paul Bakker1961b702013-01-25 14:49:24 +01003165 if( ssl->state == SSL_HANDSHAKE_OVER )
3166 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003167
Paul Bakker1961b702013-01-25 14:49:24 +01003168 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3169
3170 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3171 return( ret );
3172
3173 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003174 {
Paul Bakker1961b702013-01-25 14:49:24 +01003175 case SSL_HELLO_REQUEST:
3176 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003177 break;
3178
Paul Bakker1961b702013-01-25 14:49:24 +01003179 /*
3180 * <== ClientHello
3181 */
3182 case SSL_CLIENT_HELLO:
3183 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003184 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003185
3186 /*
3187 * ==> ServerHello
3188 * Certificate
3189 * ( ServerKeyExchange )
3190 * ( CertificateRequest )
3191 * ServerHelloDone
3192 */
3193 case SSL_SERVER_HELLO:
3194 ret = ssl_write_server_hello( ssl );
3195 break;
3196
3197 case SSL_SERVER_CERTIFICATE:
3198 ret = ssl_write_certificate( ssl );
3199 break;
3200
3201 case SSL_SERVER_KEY_EXCHANGE:
3202 ret = ssl_write_server_key_exchange( ssl );
3203 break;
3204
3205 case SSL_CERTIFICATE_REQUEST:
3206 ret = ssl_write_certificate_request( ssl );
3207 break;
3208
3209 case SSL_SERVER_HELLO_DONE:
3210 ret = ssl_write_server_hello_done( ssl );
3211 break;
3212
3213 /*
3214 * <== ( Certificate/Alert )
3215 * ClientKeyExchange
3216 * ( CertificateVerify )
3217 * ChangeCipherSpec
3218 * Finished
3219 */
3220 case SSL_CLIENT_CERTIFICATE:
3221 ret = ssl_parse_certificate( ssl );
3222 break;
3223
3224 case SSL_CLIENT_KEY_EXCHANGE:
3225 ret = ssl_parse_client_key_exchange( ssl );
3226 break;
3227
3228 case SSL_CERTIFICATE_VERIFY:
3229 ret = ssl_parse_certificate_verify( ssl );
3230 break;
3231
3232 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3233 ret = ssl_parse_change_cipher_spec( ssl );
3234 break;
3235
3236 case SSL_CLIENT_FINISHED:
3237 ret = ssl_parse_finished( ssl );
3238 break;
3239
3240 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003241 * ==> ( NewSessionTicket )
3242 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003243 * Finished
3244 */
3245 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003246#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003247 if( ssl->handshake->new_session_ticket != 0 )
3248 ret = ssl_write_new_session_ticket( ssl );
3249 else
Paul Bakkera503a632013-08-14 13:48:06 +02003250#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003251 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003252 break;
3253
3254 case SSL_SERVER_FINISHED:
3255 ret = ssl_write_finished( ssl );
3256 break;
3257
3258 case SSL_FLUSH_BUFFERS:
3259 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3260 ssl->state = SSL_HANDSHAKE_WRAPUP;
3261 break;
3262
3263 case SSL_HANDSHAKE_WRAPUP:
3264 ssl_handshake_wrapup( ssl );
3265 break;
3266
3267 default:
3268 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3269 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003270 }
3271
Paul Bakker5121ce52009-01-03 21:22:43 +00003272 return( ret );
3273}
Paul Bakker9af723c2014-05-01 13:03:14 +02003274#endif /* POLARSSL_SSL_SRV_C */