blob: 384207a39030913e771a0c6ee1e890add431c9c9 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 server-side functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakker40e46942009-01-03 21:51:57 +000032#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/debug.h"
35#include "polarssl/ssl.h"
Paul Bakker41c83d32013-03-20 14:39:14 +010036#if defined(POLARSSL_ECP_C)
37#include "polarssl/ecp.h"
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker7dc4c442014-02-01 22:50:26 +010040#if defined(POLARSSL_PLATFORM_C)
41#include "polarssl/platform.h"
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020042#else
43#define polarssl_malloc malloc
44#define polarssl_free free
45#endif
46
Paul Bakker5121ce52009-01-03 21:22:43 +000047#include <stdlib.h>
48#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020049
50#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000051#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020052#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Paul Bakkera503a632013-08-14 13:48:06 +020054#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker34617722014-06-13 17:20:13 +020055/* Implementation that should never be optimized out by the compiler */
56static void polarssl_zeroize( void *v, size_t n ) {
57 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
58}
59
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020060/*
61 * Serialize a session in the following format:
62 * 0 . n-1 session structure, n = sizeof(ssl_session)
63 * n . n+2 peer_cert length = m (0 if no certificate)
64 * n+3 . n+2+m peer cert ASN.1
65 *
66 * Assumes ticket is NULL (always true on server side).
67 */
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020068static int ssl_save_session( const ssl_session *session,
69 unsigned char *buf, size_t buf_len,
70 size_t *olen )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020071{
72 unsigned char *p = buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020073 size_t left = buf_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020075 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020077
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020078 if( left < sizeof( ssl_session ) )
79 return( -1 );
80
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020081 memcpy( p, session, sizeof( ssl_session ) );
82 p += sizeof( ssl_session );
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020083 left -= sizeof( ssl_session );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020084
Paul Bakker7c6b2c32013-09-16 13:49:26 +020085#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020086 if( session->peer_cert == NULL )
87 cert_len = 0;
88 else
89 cert_len = session->peer_cert->raw.len;
90
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020091 if( left < 3 + cert_len )
92 return( -1 );
93
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020094 *p++ = (unsigned char)( cert_len >> 16 & 0xFF );
95 *p++ = (unsigned char)( cert_len >> 8 & 0xFF );
96 *p++ = (unsigned char)( cert_len & 0xFF );
97
98 if( session->peer_cert != NULL )
99 memcpy( p, session->peer_cert->raw.p, cert_len );
100
101 p += cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200102#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200103
104 *olen = p - buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200105
106 return( 0 );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200107}
108
109/*
110 * Unserialise session, see ssl_save_session()
111 */
112static int ssl_load_session( ssl_session *session,
113 const unsigned char *buf, size_t len )
114{
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200115 const unsigned char *p = buf;
116 const unsigned char * const end = buf + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200117#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200118 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200119#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200120
121 if( p + sizeof( ssl_session ) > end )
122 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
123
124 memcpy( session, p, sizeof( ssl_session ) );
125 p += sizeof( ssl_session );
126
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200127#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200128 if( p + 3 > end )
129 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
130
131 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
132 p += 3;
133
134 if( cert_len == 0 )
135 {
136 session->peer_cert = NULL;
137 }
138 else
139 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200140 int ret;
141
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200142 if( p + cert_len > end )
143 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
144
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200145 session->peer_cert = polarssl_malloc( sizeof( x509_crt ) );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200146
147 if( session->peer_cert == NULL )
148 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
149
Paul Bakkerb6b09562013-09-18 14:17:41 +0200150 x509_crt_init( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200151
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200152 if( ( ret = x509_crt_parse_der( session->peer_cert,
153 p, cert_len ) ) != 0 )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200154 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200155 x509_crt_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200156 polarssl_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200157 session->peer_cert = NULL;
158 return( ret );
159 }
160
161 p += cert_len;
162 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200163#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200164
165 if( p != end )
166 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
167
168 return( 0 );
169}
170
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200171/*
172 * Create session ticket, secured as recommended in RFC 5077 section 4:
173 *
174 * struct {
175 * opaque key_name[16];
176 * opaque iv[16];
177 * opaque encrypted_state<0..2^16-1>;
178 * opaque mac[32];
179 * } ticket;
180 *
181 * (the internal state structure differs, however).
182 */
183static int ssl_write_ticket( ssl_context *ssl, size_t *tlen )
184{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200185 int ret;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200186 unsigned char * const start = ssl->out_msg + 10;
187 unsigned char *p = start;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200188 unsigned char *state;
189 unsigned char iv[16];
190 size_t clear_len, enc_len, pad_len, i;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200191
Manuel Pégourié-Gonnard0a201712013-08-23 16:25:16 +0200192 *tlen = 0;
193
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200194 if( ssl->ticket_keys == NULL )
195 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
196
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200197 /* Write key name */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200198 memcpy( p, ssl->ticket_keys->key_name, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200199 p += 16;
200
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200201 /* Generate and write IV (with a copy for aes_crypt) */
202 if( ( ret = ssl->f_rng( ssl->p_rng, p, 16 ) ) != 0 )
203 return( ret );
204 memcpy( iv, p, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200205 p += 16;
206
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200207 /*
208 * Dump session state
209 *
210 * After the session state itself, we still need room for 16 bytes of
211 * padding and 32 bytes of MAC, so there's only so much room left
212 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200213 state = p + 2;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200214 if( ssl_save_session( ssl->session_negotiate, state,
Paul Bakker66d5d072014-06-17 16:39:18 +0200215 SSL_MAX_CONTENT_LEN - ( state - ssl->out_ctr ) - 48,
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200216 &clear_len ) != 0 )
217 {
218 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
219 }
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200220 SSL_DEBUG_BUF( 3, "session ticket cleartext", state, clear_len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200221
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200222 /* Apply PKCS padding */
223 pad_len = 16 - clear_len % 16;
224 enc_len = clear_len + pad_len;
225 for( i = clear_len; i < enc_len; i++ )
226 state[i] = (unsigned char) pad_len;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200227
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200228 /* Encrypt */
229 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->enc, AES_ENCRYPT,
230 enc_len, iv, state, state ) ) != 0 )
231 {
232 return( ret );
233 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200234
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200235 /* Write length */
236 *p++ = (unsigned char)( ( enc_len >> 8 ) & 0xFF );
237 *p++ = (unsigned char)( ( enc_len ) & 0xFF );
238 p = state + enc_len;
239
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200240 /* Compute and write MAC( key_name + iv + enc_state_len + enc_state ) */
241 sha256_hmac( ssl->ticket_keys->mac_key, 16, start, p - start, p, 0 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200242 p += 32;
243
244 *tlen = p - start;
245
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200246 SSL_DEBUG_BUF( 3, "session ticket structure", start, *tlen );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200247
248 return( 0 );
249}
250
251/*
252 * Load session ticket (see ssl_write_ticket for structure)
253 */
254static int ssl_parse_ticket( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200255 unsigned char *buf,
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200256 size_t len )
257{
258 int ret;
259 ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200260 unsigned char *key_name = buf;
261 unsigned char *iv = buf + 16;
262 unsigned char *enc_len_p = iv + 16;
263 unsigned char *ticket = enc_len_p + 2;
264 unsigned char *mac;
Manuel Pégourié-Gonnard34ced2d2013-09-20 11:37:39 +0200265 unsigned char computed_mac[32];
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200266 size_t enc_len, clear_len, i;
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100267 unsigned char pad_len, diff;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200268
269 SSL_DEBUG_BUF( 3, "session ticket structure", buf, len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200270
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200271 if( len < 34 || ssl->ticket_keys == NULL )
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200272 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
273
274 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
275 mac = ticket + enc_len;
276
277 if( len != enc_len + 66 )
278 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
279
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100280 /* Check name, in constant time though it's not a big secret */
281 diff = 0;
282 for( i = 0; i < 16; i++ )
283 diff |= key_name[i] ^ ssl->ticket_keys->key_name[i];
284 /* don't return yet, check the MAC anyway */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200285
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100286 /* Check mac, with constant-time buffer comparison */
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200287 sha256_hmac( ssl->ticket_keys->mac_key, 16, buf, len - 32,
288 computed_mac, 0 );
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100289
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200290 for( i = 0; i < 32; i++ )
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100291 diff |= mac[i] ^ computed_mac[i];
292
293 /* Now return if ticket is not authentic, since we want to avoid
294 * decrypting arbitrary attacker-chosen data */
295 if( diff != 0 )
296 return( POLARSSL_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200297
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200298 /* Decrypt */
299 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->dec, AES_DECRYPT,
300 enc_len, iv, ticket, ticket ) ) != 0 )
301 {
302 return( ret );
303 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200304
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200305 /* Check PKCS padding */
306 pad_len = ticket[enc_len - 1];
307
308 ret = 0;
309 for( i = 2; i < pad_len; i++ )
310 if( ticket[enc_len - i] != pad_len )
311 ret = POLARSSL_ERR_SSL_BAD_INPUT_DATA;
312 if( ret != 0 )
313 return( ret );
314
315 clear_len = enc_len - pad_len;
316
317 SSL_DEBUG_BUF( 3, "session ticket cleartext", ticket, clear_len );
318
319 /* Actually load session */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200320 if( ( ret = ssl_load_session( &session, ticket, clear_len ) ) != 0 )
321 {
322 SSL_DEBUG_MSG( 1, ( "failed to parse ticket content" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100323 ssl_session_free( &session );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200324 return( ret );
325 }
326
Paul Bakker606b4ba2013-08-14 16:52:14 +0200327#if defined(POLARSSL_HAVE_TIME)
328 /* Check if still valid */
329 if( (int) ( time( NULL) - session.start ) > ssl->ticket_lifetime )
330 {
331 SSL_DEBUG_MSG( 1, ( "session ticket expired" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100332 ssl_session_free( &session );
Paul Bakker606b4ba2013-08-14 16:52:14 +0200333 return( POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED );
334 }
335#endif
336
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200337 /*
338 * Keep the session ID sent by the client, since we MUST send it back to
339 * inform him we're accepting the ticket (RFC 5077 section 3.4)
340 */
341 session.length = ssl->session_negotiate->length;
342 memcpy( &session.id, ssl->session_negotiate->id, session.length );
343
344 ssl_session_free( ssl->session_negotiate );
345 memcpy( ssl->session_negotiate, &session, sizeof( ssl_session ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200346
347 /* Zeroize instead of free as we copied the content */
Paul Bakker34617722014-06-13 17:20:13 +0200348 polarssl_zeroize( &session, sizeof( ssl_session ) );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200349
350 return( 0 );
351}
Paul Bakkera503a632013-08-14 13:48:06 +0200352#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200353
Paul Bakker0be444a2013-08-27 21:55:01 +0200354#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200355/*
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200356 * Wrapper around f_sni, allowing use of ssl_set_own_cert() but
357 * making it act on ssl->hanshake->sni_key_cert instead.
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200358 */
359static int ssl_sni_wrapper( ssl_context *ssl,
360 const unsigned char* name, size_t len )
361{
362 int ret;
363 ssl_key_cert *key_cert_ori = ssl->key_cert;
364
365 ssl->key_cert = NULL;
366 ret = ssl->f_sni( ssl->p_sni, ssl, name, len );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200367 ssl->handshake->sni_key_cert = ssl->key_cert;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200368
369 ssl->key_cert = key_cert_ori;
370
371 return( ret );
372}
373
Paul Bakker5701cdc2012-09-27 21:49:42 +0000374static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000375 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000376 size_t len )
377{
378 int ret;
379 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000380 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000381
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100382 SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
383
Paul Bakker5701cdc2012-09-27 21:49:42 +0000384 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
385 if( servername_list_size + 2 != len )
386 {
387 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
388 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
389 }
390
391 p = buf + 2;
392 while( servername_list_size > 0 )
393 {
394 hostname_len = ( ( p[1] << 8 ) | p[2] );
395 if( hostname_len + 3 > servername_list_size )
396 {
397 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
398 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
399 }
400
401 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
402 {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200403 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000404 if( ret != 0 )
405 {
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100406 SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000407 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
408 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
409 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
410 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000411 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000412 }
413
414 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000415 p += hostname_len + 3;
416 }
417
418 if( servername_list_size != 0 )
419 {
420 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
421 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000422 }
423
424 return( 0 );
425}
Paul Bakker0be444a2013-08-27 21:55:01 +0200426#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000427
Paul Bakker48916f92012-09-16 19:57:18 +0000428static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000429 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000430 size_t len )
431{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000432 int ret;
433
Paul Bakker48916f92012-09-16 19:57:18 +0000434 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
435 {
436 if( len != 1 || buf[0] != 0x0 )
437 {
438 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000439
440 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
441 return( ret );
442
Paul Bakker48916f92012-09-16 19:57:18 +0000443 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
444 }
445
446 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
447 }
448 else
449 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100450 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000451 if( len != 1 + ssl->verify_data_len ||
452 buf[0] != ssl->verify_data_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100453 safer_memcmp( buf + 1, ssl->peer_verify_data,
454 ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000455 {
456 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000457
458 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
459 return( ret );
460
Paul Bakker48916f92012-09-16 19:57:18 +0000461 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
462 }
463 }
464
465 return( 0 );
466}
467
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200468#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +0000469static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
470 const unsigned char *buf,
471 size_t len )
472{
473 size_t sig_alg_list_size;
474 const unsigned char *p;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200475 const unsigned char *end = buf + len;
476 const int *md_cur;
477
Paul Bakker23f36802012-09-28 14:15:14 +0000478
479 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
480 if( sig_alg_list_size + 2 != len ||
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200481 sig_alg_list_size % 2 != 0 )
Paul Bakker23f36802012-09-28 14:15:14 +0000482 {
483 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
484 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
485 }
486
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200487 /*
488 * For now, ignore the SignatureAlgorithm part and rely on offered
489 * ciphersuites only for that part. To be fixed later.
490 *
491 * So, just look at the HashAlgorithm part.
492 */
493 for( md_cur = md_list(); *md_cur != POLARSSL_MD_NONE; md_cur++ ) {
494 for( p = buf + 2; p < end; p += 2 ) {
495 if( *md_cur == (int) ssl_md_alg_from_hash( p[0] ) ) {
496 ssl->handshake->sig_alg = p[0];
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200497 goto have_sig_alg;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200498 }
Paul Bakker23f36802012-09-28 14:15:14 +0000499 }
Paul Bakker23f36802012-09-28 14:15:14 +0000500 }
501
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200502 /* Some key echanges do not need signatures at all */
503 SSL_DEBUG_MSG( 3, ( "no signature_algorithm in common" ) );
504 return( 0 );
505
506have_sig_alg:
Paul Bakker23f36802012-09-28 14:15:14 +0000507 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
508 ssl->handshake->sig_alg ) );
509
510 return( 0 );
511}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200512#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker23f36802012-09-28 14:15:14 +0000513
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200514#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200515static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
516 const unsigned char *buf,
517 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100518{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200519 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100520 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200521 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100522
523 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
524 if( list_size + 2 != len ||
525 list_size % 2 != 0 )
526 {
527 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
528 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
529 }
530
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200531 /* Should never happen unless client duplicates the extension */
532 if( ssl->handshake->curves != NULL )
533 {
534 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
535 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
536 }
537
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +0100538 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200539 * and leave room for a final 0 */
540 our_size = list_size / 2 + 1;
541 if( our_size > POLARSSL_ECP_DP_MAX )
542 our_size = POLARSSL_ECP_DP_MAX;
543
544 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
545 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
546
Paul Bakker9af723c2014-05-01 13:03:14 +0200547 /* explicit void pointer cast for buggy MS compiler */
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200548 memset( (void *) curves, 0, our_size * sizeof( *curves ) );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200549 ssl->handshake->curves = curves;
550
Paul Bakker41c83d32013-03-20 14:39:14 +0100551 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200552 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100553 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200554 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200555
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200556 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100557 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200558 *curves++ = curve_info;
559 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100560 }
561
562 list_size -= 2;
563 p += 2;
564 }
565
566 return( 0 );
567}
568
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200569static int ssl_parse_supported_point_formats( ssl_context *ssl,
570 const unsigned char *buf,
571 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100572{
573 size_t list_size;
574 const unsigned char *p;
575
576 list_size = buf[0];
577 if( list_size + 1 != len )
578 {
579 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
580 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
581 }
582
583 p = buf + 2;
584 while( list_size > 0 )
585 {
586 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
587 p[0] == POLARSSL_ECP_PF_COMPRESSED )
588 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200589 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200590 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100591 return( 0 );
592 }
593
594 list_size--;
595 p++;
596 }
597
598 return( 0 );
599}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200600#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100601
Paul Bakker05decb22013-08-15 13:33:48 +0200602#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200603static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
604 const unsigned char *buf,
605 size_t len )
606{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200607 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200608 {
609 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
610 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
611 }
612
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200613 ssl->session_negotiate->mfl_code = buf[0];
614
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200615 return( 0 );
616}
Paul Bakker05decb22013-08-15 13:33:48 +0200617#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200618
Paul Bakker1f2bc622013-08-15 13:45:55 +0200619#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200620static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
621 const unsigned char *buf,
622 size_t len )
623{
624 if( len != 0 )
625 {
626 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
627 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
628 }
629
630 ((void) buf);
631
632 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
633
634 return( 0 );
635}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200636#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200637
Paul Bakkera503a632013-08-14 13:48:06 +0200638#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200639static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200640 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200641 size_t len )
642{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200643 int ret;
644
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200645 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
646 return( 0 );
647
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200648 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200649 ssl->handshake->new_session_ticket = 1;
650
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200651 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
652
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200653 if( len == 0 )
654 return( 0 );
655
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200656 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
657 {
658 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
659 return( 0 );
660 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200661
662 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200663 * Failures are ok: just ignore the ticket and proceed.
664 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200665 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
666 {
667 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200668 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200669 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200670
671 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
672
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200673 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200674
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200675 /* Don't send a new ticket after all, this one is OK */
676 ssl->handshake->new_session_ticket = 0;
677
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200678 return( 0 );
679}
Paul Bakkera503a632013-08-14 13:48:06 +0200680#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200681
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200682#if defined(POLARSSL_SSL_ALPN)
683static int ssl_parse_alpn_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200684 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200685{
Paul Bakker14b16c62014-05-28 11:33:54 +0200686 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200687 const unsigned char *theirs, *start, *end;
688 const char **ours;
689
690 /* If ALPN not configured, just ignore the extension */
691 if( ssl->alpn_list == NULL )
692 return( 0 );
693
694 /*
695 * opaque ProtocolName<1..2^8-1>;
696 *
697 * struct {
698 * ProtocolName protocol_name_list<2..2^16-1>
699 * } ProtocolNameList;
700 */
701
702 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
703 if( len < 4 )
704 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
705
706 list_len = ( buf[0] << 8 ) | buf[1];
707 if( list_len != len - 2 )
708 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
709
710 /*
711 * Use our order of preference
712 */
713 start = buf + 2;
714 end = buf + len;
715 for( ours = ssl->alpn_list; *ours != NULL; ours++ )
716 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200717 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200718 for( theirs = start; theirs != end; theirs += cur_len )
719 {
720 /* If the list is well formed, we should get equality first */
721 if( theirs > end )
722 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
723
724 cur_len = *theirs++;
725
726 /* Empty strings MUST NOT be included */
727 if( cur_len == 0 )
728 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
729
Paul Bakker14b16c62014-05-28 11:33:54 +0200730 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200731 memcmp( theirs, *ours, cur_len ) == 0 )
732 {
733 ssl->alpn_chosen = *ours;
734 return( 0 );
735 }
736 }
737 }
738
739 /* If we get there, no match was found */
740 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
741 SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
742 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
743}
744#endif /* POLARSSL_SSL_ALPN */
745
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100746/*
747 * Auxiliary functions for ServerHello parsing and related actions
748 */
749
750#if defined(POLARSSL_X509_CRT_PARSE_C)
751/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100752 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100753 */
754#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100755static int ssl_check_key_curve( pk_context *pk,
756 const ecp_curve_info **curves )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100757{
758 const ecp_curve_info **crv = curves;
759 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
760
761 while( *crv != NULL )
762 {
763 if( (*crv)->grp_id == grp_id )
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100764 return( 0 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100765 crv++;
766 }
767
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100768 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100769}
770#endif /* POLARSSL_ECDSA_C */
771
772/*
773 * Try picking a certificate for this ciphersuite,
774 * return 0 on success and -1 on failure.
775 */
776static int ssl_pick_cert( ssl_context *ssl,
777 const ssl_ciphersuite_t * ciphersuite_info )
778{
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100779 ssl_key_cert *cur, *list, *fallback = NULL;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100780 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
781
782#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
783 if( ssl->handshake->sni_key_cert != NULL )
784 list = ssl->handshake->sni_key_cert;
785 else
786#endif
787 list = ssl->handshake->key_cert;
788
789 if( pk_alg == POLARSSL_PK_NONE )
790 return( 0 );
791
792 for( cur = list; cur != NULL; cur = cur->next )
793 {
794 if( ! pk_can_do( cur->key, pk_alg ) )
795 continue;
796
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200797 /*
798 * This avoids sending the client a cert it'll reject based on
799 * keyUsage or other extensions.
800 *
801 * It also allows the user to provision different certificates for
802 * different uses based on keyUsage, eg if they want to avoid signing
803 * and decrypting with the same RSA key.
804 */
805 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
806 SSL_IS_SERVER ) != 0 )
807 {
808 continue;
809 }
810
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100811#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100812 if( pk_alg == POLARSSL_PK_ECDSA &&
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100813 ssl_check_key_curve( cur->key, ssl->handshake->curves ) != 0 )
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100814 continue;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100815#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100816
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100817 /*
818 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
819 * present them a SHA-higher cert rather than failing if it's the only
820 * one we got that satisfies the other conditions.
821 */
822 if( ssl->minor_ver < SSL_MINOR_VERSION_3 &&
823 cur->cert->sig_md != POLARSSL_MD_SHA1 )
824 {
825 if( fallback == NULL )
826 fallback = cur;
827 continue;
828 }
829
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100830 /* If we get there, we got a winner */
831 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100832 }
833
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100834 if( cur != NULL )
835 {
836 ssl->handshake->key_cert = cur;
837 return( 0 );
838 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100839
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100840 if( fallback != NULL )
841 {
842 ssl->handshake->key_cert = fallback;
843 return( 0 );
844 }
845
846 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100847}
848#endif /* POLARSSL_X509_CRT_PARSE_C */
849
850/*
851 * Check if a given ciphersuite is suitable for use with our config/keys/etc
852 * Sets ciphersuite_info only if the suite matches.
853 */
854static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
855 const ssl_ciphersuite_t **ciphersuite_info )
856{
857 const ssl_ciphersuite_t *suite_info;
858
859 suite_info = ssl_ciphersuite_from_id( suite_id );
860 if( suite_info == NULL )
861 {
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100862 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
863 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100864 }
865
866 if( suite_info->min_minor_ver > ssl->minor_ver ||
867 suite_info->max_minor_ver < ssl->minor_ver )
868 return( 0 );
869
870#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
871 if( ssl_ciphersuite_uses_ec( suite_info ) &&
872 ( ssl->handshake->curves == NULL ||
873 ssl->handshake->curves[0] == NULL ) )
874 return( 0 );
875#endif
876
877#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
878 /* If the ciphersuite requires a pre-shared key and we don't
879 * have one, skip it now rather than failing later */
880 if( ssl_ciphersuite_uses_psk( suite_info ) &&
881 ssl->f_psk == NULL &&
882 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
883 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
884 return( 0 );
885#endif
886
887#if defined(POLARSSL_X509_CRT_PARSE_C)
888 /*
889 * Final check: if ciphersuite requires us to have a
890 * certificate/key of a particular type:
891 * - select the appropriate certificate if we have one, or
892 * - try the next ciphersuite if we don't
893 * This must be done last since we modify the key_cert list.
894 */
895 if( ssl_pick_cert( ssl, suite_info ) != 0 )
896 return( 0 );
897#endif
898
899 *ciphersuite_info = suite_info;
900 return( 0 );
901}
902
Paul Bakker78a8c712013-03-06 17:01:52 +0100903#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
904static int ssl_parse_client_hello_v2( ssl_context *ssl )
905{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +0100906 int ret, got_common_suite;
Paul Bakker78a8c712013-03-06 17:01:52 +0100907 unsigned int i, j;
908 size_t n;
909 unsigned int ciph_len, sess_len, chal_len;
910 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200911 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200912 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100913
914 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
915
916 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
917 {
918 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
919
920 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
921 return( ret );
922
923 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
924 }
925
926 buf = ssl->in_hdr;
927
928 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
929
930 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
931 buf[2] ) );
932 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
933 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
934 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
935 buf[3], buf[4] ) );
936
937 /*
938 * SSLv2 Client Hello
939 *
940 * Record layer:
941 * 0 . 1 message length
942 *
943 * SSL layer:
944 * 2 . 2 message type
945 * 3 . 4 protocol version
946 */
947 if( buf[2] != SSL_HS_CLIENT_HELLO ||
948 buf[3] != SSL_MAJOR_VERSION_3 )
949 {
950 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
951 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
952 }
953
954 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
955
956 if( n < 17 || n > 512 )
957 {
958 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
959 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
960 }
961
962 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200963 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
964 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100965
966 if( ssl->minor_ver < ssl->min_minor_ver )
967 {
968 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200969 " [%d:%d] < [%d:%d]",
970 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +0100971 ssl->min_major_ver, ssl->min_minor_ver ) );
972
973 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
974 SSL_ALERT_MSG_PROTOCOL_VERSION );
975 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
976 }
977
Paul Bakker2fbefde2013-06-29 16:01:15 +0200978 ssl->handshake->max_major_ver = buf[3];
979 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100980
981 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
982 {
983 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
984 return( ret );
985 }
986
987 ssl->handshake->update_checksum( ssl, buf + 2, n );
988
989 buf = ssl->in_msg;
990 n = ssl->in_left - 5;
991
992 /*
993 * 0 . 1 ciphersuitelist length
994 * 2 . 3 session id length
995 * 4 . 5 challenge length
996 * 6 . .. ciphersuitelist
997 * .. . .. session id
998 * .. . .. challenge
999 */
1000 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1001
1002 ciph_len = ( buf[0] << 8 ) | buf[1];
1003 sess_len = ( buf[2] << 8 ) | buf[3];
1004 chal_len = ( buf[4] << 8 ) | buf[5];
1005
1006 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
1007 ciph_len, sess_len, chal_len ) );
1008
1009 /*
1010 * Make sure each parameter length is valid
1011 */
1012 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1013 {
1014 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1015 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1016 }
1017
1018 if( sess_len > 32 )
1019 {
1020 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1021 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1022 }
1023
1024 if( chal_len < 8 || chal_len > 32 )
1025 {
1026 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1027 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1028 }
1029
1030 if( n != 6 + ciph_len + sess_len + chal_len )
1031 {
1032 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1033 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1034 }
1035
1036 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1037 buf + 6, ciph_len );
1038 SSL_DEBUG_BUF( 3, "client hello, session id",
1039 buf + 6 + ciph_len, sess_len );
1040 SSL_DEBUG_BUF( 3, "client hello, challenge",
1041 buf + 6 + ciph_len + sess_len, chal_len );
1042
1043 p = buf + 6 + ciph_len;
1044 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001045 memset( ssl->session_negotiate->id, 0,
1046 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001047 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1048
1049 p += sess_len;
1050 memset( ssl->handshake->randbytes, 0, 64 );
1051 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1052
1053 /*
1054 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1055 */
1056 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1057 {
1058 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1059 {
1060 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1061 if( ssl->renegotiation == SSL_RENEGOTIATION )
1062 {
1063 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1064
1065 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1066 return( ret );
1067
1068 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1069 }
1070 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1071 break;
1072 }
1073 }
1074
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001075 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001076 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001077 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001078#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1079 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1080 {
1081 for( i = 0; ciphersuites[i] != 0; i++ )
1082#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001083 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001084 {
1085 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001086#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001087 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001088 if( p[0] != 0 ||
1089 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1090 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1091 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001092
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001093 got_common_suite = 1;
1094
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001095 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1096 &ciphersuite_info ) ) != 0 )
1097 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001098
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001099 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001100 goto have_ciphersuite_v2;
1101 }
1102 }
1103
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001104 if( got_common_suite )
1105 {
1106 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1107 "but none of them usable" ) );
1108 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
1109 }
1110 else
1111 {
1112 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1113 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1114 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001115
1116have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001117 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001118 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001119 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001120
1121 /*
1122 * SSLv2 Client Hello relevant renegotiation security checks
1123 */
1124 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1125 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1126 {
1127 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1128
1129 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1130 return( ret );
1131
1132 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1133 }
1134
1135 ssl->in_left = 0;
1136 ssl->state++;
1137
1138 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1139
1140 return( 0 );
1141}
1142#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1143
Paul Bakker5121ce52009-01-03 21:22:43 +00001144static int ssl_parse_client_hello( ssl_context *ssl )
1145{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001146 int ret, got_common_suite;
Paul Bakker23986e52011-04-24 08:57:21 +00001147 unsigned int i, j;
1148 size_t n;
1149 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001150 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001151 unsigned int ext_len = 0;
1152 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001153 int renegotiation_info_seen = 0;
1154 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001155 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001156 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001157
1158 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1159
Paul Bakker48916f92012-09-16 19:57:18 +00001160 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1161 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001162 {
1163 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1164 return( ret );
1165 }
1166
1167 buf = ssl->in_hdr;
1168
Paul Bakker78a8c712013-03-06 17:01:52 +01001169#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1170 if( ( buf[0] & 0x80 ) != 0 )
1171 return ssl_parse_client_hello_v2( ssl );
1172#endif
1173
Paul Bakkerec636f32012-09-09 19:17:02 +00001174 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1175
1176 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1177 buf[0] ) );
1178 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1179 ( buf[3] << 8 ) | buf[4] ) );
1180 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
1181 buf[1], buf[2] ) );
1182
1183 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001184 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001185 *
1186 * Record layer:
1187 * 0 . 0 message type
1188 * 1 . 2 protocol version
1189 * 3 . 4 message length
1190 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001191
1192 /* According to RFC 5246 Appendix E.1, the version here is typically
1193 * "{03,00}, the lowest version number supported by the client, [or] the
1194 * value of ClientHello.client_version", so the only meaningful check here
1195 * is the major version shouldn't be less than 3 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001196 if( buf[0] != SSL_MSG_HANDSHAKE ||
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001197 buf[1] < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001198 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001199 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1200 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1201 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001202
Paul Bakkerec636f32012-09-09 19:17:02 +00001203 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +00001204
Paul Bakker4f42c112014-04-17 14:48:23 +02001205 if( n < 45 || n > SSL_MAX_CONTENT_LEN )
Paul Bakkerec636f32012-09-09 19:17:02 +00001206 {
1207 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1208 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1209 }
1210
Paul Bakker48916f92012-09-16 19:57:18 +00001211 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1212 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001213 {
1214 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1215 return( ret );
1216 }
1217
1218 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +00001219 if( !ssl->renegotiation )
1220 n = ssl->in_left - 5;
1221 else
1222 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +00001223
Paul Bakker48916f92012-09-16 19:57:18 +00001224 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +00001225
1226 /*
1227 * SSL layer:
1228 * 0 . 0 handshake type
1229 * 1 . 3 handshake length
1230 * 4 . 5 protocol version
1231 * 6 . 9 UNIX time()
1232 * 10 . 37 random bytes
1233 * 38 . 38 session id length
1234 * 39 . 38+x session id
1235 * 39+x . 40+x ciphersuitelist length
Paul Bakker0f651c72014-05-22 15:12:19 +02001236 * 41+x . 40+y ciphersuitelist
1237 * 41+y . 41+y compression alg length
1238 * 42+y . 41+z compression algs
Paul Bakkerec636f32012-09-09 19:17:02 +00001239 * .. . .. extensions
1240 */
1241 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1242
1243 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1244 buf[0] ) );
1245 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1246 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1247 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1248 buf[4], buf[5] ) );
1249
1250 /*
1251 * Check the handshake type and protocol version
1252 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001253 if( buf[0] != SSL_HS_CLIENT_HELLO )
Paul Bakkerec636f32012-09-09 19:17:02 +00001254 {
1255 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1256 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1257 }
1258
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001259 ssl->major_ver = buf[4];
1260 ssl->minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001261
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001262 ssl->handshake->max_major_ver = ssl->major_ver;
1263 ssl->handshake->max_minor_ver = ssl->minor_ver;
1264
1265 if( ssl->major_ver < ssl->min_major_ver ||
1266 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001267 {
1268 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001269 " [%d:%d] < [%d:%d]",
1270 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001271 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001272
1273 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1274 SSL_ALERT_MSG_PROTOCOL_VERSION );
1275
1276 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1277 }
1278
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001279 if( ssl->major_ver > ssl->max_major_ver )
1280 {
1281 ssl->major_ver = ssl->max_major_ver;
1282 ssl->minor_ver = ssl->max_minor_ver;
1283 }
1284 else if( ssl->minor_ver > ssl->max_minor_ver )
1285 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001286
Paul Bakker48916f92012-09-16 19:57:18 +00001287 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001288
1289 /*
1290 * Check the handshake message length
1291 */
1292 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1293 {
1294 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1295 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1296 }
1297
1298 /*
1299 * Check the session length
1300 */
1301 sess_len = buf[38];
1302
Paul Bakker0f651c72014-05-22 15:12:19 +02001303 if( sess_len > 32 || sess_len > n - 42 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001304 {
1305 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1306 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1307 }
1308
Paul Bakker48916f92012-09-16 19:57:18 +00001309 ssl->session_negotiate->length = sess_len;
1310 memset( ssl->session_negotiate->id, 0,
1311 sizeof( ssl->session_negotiate->id ) );
1312 memcpy( ssl->session_negotiate->id, buf + 39,
1313 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001314
1315 /*
1316 * Check the ciphersuitelist length
1317 */
1318 ciph_len = ( buf[39 + sess_len] << 8 )
1319 | ( buf[40 + sess_len] );
1320
Paul Bakker0f651c72014-05-22 15:12:19 +02001321 if( ciph_len < 2 || ( ciph_len % 2 ) != 0 || ciph_len > n - 42 - sess_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001322 {
1323 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1324 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1325 }
1326
1327 /*
1328 * Check the compression algorithms length
1329 */
1330 comp_len = buf[41 + sess_len + ciph_len];
1331
Paul Bakker0f651c72014-05-22 15:12:19 +02001332 if( comp_len < 1 || comp_len > 16 ||
1333 comp_len > n - 42 - sess_len - ciph_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001334 {
1335 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1336 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1337 }
1338
Paul Bakker48916f92012-09-16 19:57:18 +00001339 /*
1340 * Check the extension length
1341 */
1342 if( n > 42 + sess_len + ciph_len + comp_len )
1343 {
1344 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1345 | ( buf[43 + sess_len + ciph_len + comp_len] );
1346
1347 if( ( ext_len > 0 && ext_len < 4 ) ||
1348 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1349 {
1350 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1351 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1352 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1353 }
1354 }
1355
1356 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001357#if defined(POLARSSL_ZLIB_SUPPORT)
1358 for( i = 0; i < comp_len; ++i )
1359 {
Paul Bakker48916f92012-09-16 19:57:18 +00001360 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001361 {
Paul Bakker48916f92012-09-16 19:57:18 +00001362 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001363 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001364 }
1365 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001366#endif
1367
Paul Bakkerec636f32012-09-09 19:17:02 +00001368 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1369 buf + 6, 32 );
1370 SSL_DEBUG_BUF( 3, "client hello, session id",
1371 buf + 38, sess_len );
1372 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1373 buf + 41 + sess_len, ciph_len );
1374 SSL_DEBUG_BUF( 3, "client hello, compression",
1375 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001376
Paul Bakkerec636f32012-09-09 19:17:02 +00001377 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001378 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1379 */
1380 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1381 {
1382 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1383 {
1384 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1385 if( ssl->renegotiation == SSL_RENEGOTIATION )
1386 {
1387 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001388
1389 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1390 return( ret );
1391
Paul Bakker48916f92012-09-16 19:57:18 +00001392 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1393 }
1394 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1395 break;
1396 }
1397 }
1398
Paul Bakker48916f92012-09-16 19:57:18 +00001399 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001400
1401 while( ext_len )
1402 {
1403 unsigned int ext_id = ( ( ext[0] << 8 )
1404 | ( ext[1] ) );
1405 unsigned int ext_size = ( ( ext[2] << 8 )
1406 | ( ext[3] ) );
1407
1408 if( ext_size + 4 > ext_len )
1409 {
1410 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1411 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1412 }
1413 switch( ext_id )
1414 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001415#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001416 case TLS_EXT_SERVERNAME:
1417 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1418 if( ssl->f_sni == NULL )
1419 break;
1420
1421 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1422 if( ret != 0 )
1423 return( ret );
1424 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001425#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001426
Paul Bakker48916f92012-09-16 19:57:18 +00001427 case TLS_EXT_RENEGOTIATION_INFO:
1428 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1429 renegotiation_info_seen = 1;
1430
Paul Bakker23f36802012-09-28 14:15:14 +00001431 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1432 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001433 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001434 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001435
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001436#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001437 case TLS_EXT_SIG_ALG:
1438 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1439 if( ssl->renegotiation == SSL_RENEGOTIATION )
1440 break;
1441
1442 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1443 if( ret != 0 )
1444 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001445 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001446#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001447
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001448#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001449 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1450 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1451
1452 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1453 if( ret != 0 )
1454 return( ret );
1455 break;
1456
1457 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1458 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001459 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001460
1461 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1462 if( ret != 0 )
1463 return( ret );
1464 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001465#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001466
Paul Bakker05decb22013-08-15 13:33:48 +02001467#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001468 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1469 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1470
1471 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1472 if( ret != 0 )
1473 return( ret );
1474 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001475#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001476
Paul Bakker1f2bc622013-08-15 13:45:55 +02001477#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001478 case TLS_EXT_TRUNCATED_HMAC:
1479 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1480
1481 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1482 if( ret != 0 )
1483 return( ret );
1484 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001485#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001486
Paul Bakkera503a632013-08-14 13:48:06 +02001487#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001488 case TLS_EXT_SESSION_TICKET:
1489 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1490
1491 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1492 if( ret != 0 )
1493 return( ret );
1494 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001495#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001496
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001497#if defined(POLARSSL_SSL_ALPN)
1498 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001499 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001500
1501 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1502 if( ret != 0 )
1503 return( ret );
1504 break;
1505#endif /* POLARSSL_SSL_SESSION_TICKETS */
1506
Paul Bakker48916f92012-09-16 19:57:18 +00001507 default:
1508 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1509 ext_id ) );
1510 }
1511
1512 ext_len -= 4 + ext_size;
1513 ext += 4 + ext_size;
1514
1515 if( ext_len > 0 && ext_len < 4 )
1516 {
1517 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1518 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1519 }
1520 }
1521
1522 /*
1523 * Renegotiation security checks
1524 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001525 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1526 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1527 {
1528 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1529 handshake_failure = 1;
1530 }
1531 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1532 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1533 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001534 {
1535 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001536 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001537 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001538 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1539 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1540 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001541 {
1542 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001543 handshake_failure = 1;
1544 }
1545 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1546 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1547 renegotiation_info_seen == 1 )
1548 {
1549 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1550 handshake_failure = 1;
1551 }
1552
1553 if( handshake_failure == 1 )
1554 {
1555 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1556 return( ret );
1557
Paul Bakker48916f92012-09-16 19:57:18 +00001558 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1559 }
Paul Bakker380da532012-04-18 16:10:25 +00001560
Paul Bakker41c83d32013-03-20 14:39:14 +01001561 /*
1562 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001563 * (At the end because we need information from the EC-based extensions
1564 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001565 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001566 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001567 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001568 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001569#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1570 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
1571 {
1572 for( i = 0; ciphersuites[i] != 0; i++ )
1573#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001574 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001575 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001576 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001577#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001578 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001579 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1580 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1581 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001582
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001583 got_common_suite = 1;
1584
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001585 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1586 &ciphersuite_info ) ) != 0 )
1587 return( ret );
1588
1589 if( ciphersuite_info != NULL )
1590 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001591 }
1592 }
1593
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001594 if( got_common_suite )
1595 {
1596 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1597 "but none of them usable" ) );
1598 ssl_send_fatal_handshake_failure( ssl );
1599 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
1600 }
1601 else
1602 {
1603 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1604 ssl_send_fatal_handshake_failure( ssl );
1605 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1606 }
Paul Bakker41c83d32013-03-20 14:39:14 +01001607
1608have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001609 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001610 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1611 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1612
Paul Bakker5121ce52009-01-03 21:22:43 +00001613 ssl->in_left = 0;
1614 ssl->state++;
1615
1616 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1617
1618 return( 0 );
1619}
1620
Paul Bakker1f2bc622013-08-15 13:45:55 +02001621#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001622static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1623 unsigned char *buf,
1624 size_t *olen )
1625{
1626 unsigned char *p = buf;
1627
1628 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1629 {
1630 *olen = 0;
1631 return;
1632 }
1633
1634 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1635
1636 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1637 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1638
1639 *p++ = 0x00;
1640 *p++ = 0x00;
1641
1642 *olen = 4;
1643}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001644#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001645
Paul Bakkera503a632013-08-14 13:48:06 +02001646#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001647static void ssl_write_session_ticket_ext( ssl_context *ssl,
1648 unsigned char *buf,
1649 size_t *olen )
1650{
1651 unsigned char *p = buf;
1652
1653 if( ssl->handshake->new_session_ticket == 0 )
1654 {
1655 *olen = 0;
1656 return;
1657 }
1658
1659 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1660
1661 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1662 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1663
1664 *p++ = 0x00;
1665 *p++ = 0x00;
1666
1667 *olen = 4;
1668}
Paul Bakkera503a632013-08-14 13:48:06 +02001669#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001670
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001671static void ssl_write_renegotiation_ext( ssl_context *ssl,
1672 unsigned char *buf,
1673 size_t *olen )
1674{
1675 unsigned char *p = buf;
1676
1677 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1678 {
1679 *olen = 0;
1680 return;
1681 }
1682
1683 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1684
1685 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1686 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1687
1688 *p++ = 0x00;
1689 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1690 *p++ = ssl->verify_data_len * 2 & 0xFF;
1691
1692 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1693 p += ssl->verify_data_len;
1694 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1695 p += ssl->verify_data_len;
1696
1697 *olen = 5 + ssl->verify_data_len * 2;
1698}
1699
Paul Bakker05decb22013-08-15 13:33:48 +02001700#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001701static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1702 unsigned char *buf,
1703 size_t *olen )
1704{
1705 unsigned char *p = buf;
1706
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001707 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1708 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001709 *olen = 0;
1710 return;
1711 }
1712
1713 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1714
1715 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1716 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1717
1718 *p++ = 0x00;
1719 *p++ = 1;
1720
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001721 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001722
1723 *olen = 5;
1724}
Paul Bakker05decb22013-08-15 13:33:48 +02001725#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001726
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001727#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001728static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1729 unsigned char *buf,
1730 size_t *olen )
1731{
1732 unsigned char *p = buf;
1733 ((void) ssl);
1734
Paul Bakker677377f2013-10-28 12:54:26 +01001735 if( ( ssl->handshake->cli_exts &
1736 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
1737 {
1738 *olen = 0;
1739 return;
1740 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001741
1742 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1743
1744 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1745 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1746
1747 *p++ = 0x00;
1748 *p++ = 2;
1749
1750 *p++ = 1;
1751 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1752
1753 *olen = 6;
1754}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001755#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001756
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001757#if defined(POLARSSL_SSL_ALPN )
1758static void ssl_write_alpn_ext( ssl_context *ssl,
1759 unsigned char *buf, size_t *olen )
1760{
1761 if( ssl->alpn_chosen == NULL )
1762 {
1763 *olen = 0;
1764 return;
1765 }
1766
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001767 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001768
1769 /*
1770 * 0 . 1 ext identifier
1771 * 2 . 3 ext length
1772 * 4 . 5 protocol list length
1773 * 6 . 6 protocol name length
1774 * 7 . 7+n protocol name
1775 */
1776 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
1777 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
1778
1779 *olen = 7 + strlen( ssl->alpn_chosen );
1780
1781 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
1782 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
1783
1784 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
1785 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
1786
1787 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
1788
1789 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
1790}
1791#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
1792
Paul Bakker5121ce52009-01-03 21:22:43 +00001793static int ssl_write_server_hello( ssl_context *ssl )
1794{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001795#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001796 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001797#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001798 int ret;
1799 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001800 unsigned char *buf, *p;
1801
1802 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1803
Paul Bakkera9a028e2013-11-21 17:31:06 +01001804 if( ssl->f_rng == NULL )
1805 {
1806 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
1807 return( POLARSSL_ERR_SSL_NO_RNG );
1808 }
1809
Paul Bakker5121ce52009-01-03 21:22:43 +00001810 /*
1811 * 0 . 0 handshake type
1812 * 1 . 3 handshake length
1813 * 4 . 5 protocol version
1814 * 6 . 9 UNIX time()
1815 * 10 . 37 random bytes
1816 */
1817 buf = ssl->out_msg;
1818 p = buf + 4;
1819
1820 *p++ = (unsigned char) ssl->major_ver;
1821 *p++ = (unsigned char) ssl->minor_ver;
1822
1823 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1824 buf[4], buf[5] ) );
1825
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001826#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001827 t = time( NULL );
1828 *p++ = (unsigned char)( t >> 24 );
1829 *p++ = (unsigned char)( t >> 16 );
1830 *p++ = (unsigned char)( t >> 8 );
1831 *p++ = (unsigned char)( t );
1832
1833 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001834#else
1835 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1836 return( ret );
1837
1838 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02001839#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001840
Paul Bakkera3d195c2011-11-27 21:07:34 +00001841 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1842 return( ret );
1843
1844 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001845
Paul Bakker48916f92012-09-16 19:57:18 +00001846 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001847
1848 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1849
1850 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001851 * Resume is 0 by default, see ssl_handshake_init().
1852 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1853 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001854 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001855 if( ssl->handshake->resume == 0 &&
1856 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001857 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001858 ssl->f_get_cache != NULL &&
1859 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1860 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01001861 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001862 ssl->handshake->resume = 1;
1863 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001864
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001865 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001866 {
1867 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001868 * New session, create a new session id,
1869 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001870 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001871 ssl->state++;
1872
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001873#if defined(POLARSSL_HAVE_TIME)
1874 ssl->session_negotiate->start = time( NULL );
1875#endif
1876
Paul Bakkera503a632013-08-14 13:48:06 +02001877#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001878 if( ssl->handshake->new_session_ticket != 0 )
1879 {
1880 ssl->session_negotiate->length = n = 0;
1881 memset( ssl->session_negotiate->id, 0, 32 );
1882 }
1883 else
1884#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001885 {
1886 ssl->session_negotiate->length = n = 32;
1887 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001888 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001889 return( ret );
1890 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001891 }
1892 else
1893 {
1894 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001895 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001896 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001897 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001898 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001899
1900 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1901 {
1902 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1903 return( ret );
1904 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001905 }
1906
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001907 /*
1908 * 38 . 38 session id length
1909 * 39 . 38+n session id
1910 * 39+n . 40+n chosen ciphersuite
1911 * 41+n . 41+n chosen compression alg.
1912 * 42+n . 43+n extensions length
1913 * 44+n . 43+n+m extensions
1914 */
1915 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001916 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1917 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001918
1919 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1920 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1921 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001922 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001923
Paul Bakker48916f92012-09-16 19:57:18 +00001924 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1925 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1926 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001927
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001928 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
1929 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001930 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001931 ssl->session_negotiate->compression ) );
1932
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001933 /*
1934 * First write extensions, then the total length
1935 */
1936 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1937 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001938
Paul Bakker05decb22013-08-15 13:33:48 +02001939#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001940 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1941 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001942#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001943
Paul Bakker1f2bc622013-08-15 13:45:55 +02001944#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001945 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1946 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001947#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001948
Paul Bakkera503a632013-08-14 13:48:06 +02001949#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001950 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1951 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001952#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001953
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001954#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001955 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1956 ext_len += olen;
1957#endif
1958
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001959#if defined(POLARSSL_SSL_ALPN)
1960 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
1961 ext_len += olen;
1962#endif
1963
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001964 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001965
Paul Bakkera7036632014-04-30 10:15:38 +02001966 if( ext_len > 0 )
1967 {
1968 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1969 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1970 p += ext_len;
1971 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001972
1973 ssl->out_msglen = p - buf;
1974 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1975 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1976
1977 ret = ssl_write_record( ssl );
1978
1979 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1980
1981 return( ret );
1982}
1983
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001984#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1985 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001986 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1987 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001988static int ssl_write_certificate_request( ssl_context *ssl )
1989{
Paul Bakkered27a042013-04-18 22:46:23 +02001990 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001991
1992 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1993
1994 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01001995 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001996 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1997 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001998 {
1999 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2000 ssl->state++;
2001 return( 0 );
2002 }
2003
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002004 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2005 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002006}
2007#else
2008static int ssl_write_certificate_request( ssl_context *ssl )
2009{
2010 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2011 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002012 size_t dn_size, total_dn_size; /* excluding length bytes */
2013 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002014 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002015 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00002016
2017 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2018
2019 ssl->state++;
2020
Paul Bakkerfbb17802013-04-17 19:10:21 +02002021 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002022 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002023 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002024 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002025 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002026 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002027 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002028 return( 0 );
2029 }
2030
2031 /*
2032 * 0 . 0 handshake type
2033 * 1 . 3 handshake length
2034 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002035 * 5 .. m-1 cert types
2036 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002037 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002038 * n .. n+1 length of all DNs
2039 * n+2 .. n+3 length of DN 1
2040 * n+4 .. ... Distinguished Name #1
2041 * ... .. ... length of DN 2, etc.
2042 */
2043 buf = ssl->out_msg;
2044 p = buf + 4;
2045
2046 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002047 * Supported certificate types
2048 *
2049 * ClientCertificateType certificate_types<1..2^8-1>;
2050 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002051 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002052 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002053
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002054#if defined(POLARSSL_RSA_C)
2055 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2056#endif
2057#if defined(POLARSSL_ECDSA_C)
2058 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2059#endif
2060
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002061 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002062 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002063
Paul Bakker577e0062013-08-28 11:57:20 +02002064 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002065#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002066 /*
2067 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002068 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002069 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2070 *
2071 * struct {
2072 * HashAlgorithm hash;
2073 * SignatureAlgorithm signature;
2074 * } SignatureAndHashAlgorithm;
2075 *
2076 * enum { (255) } HashAlgorithm;
2077 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002078 */
Paul Bakker21dca692013-01-03 11:41:08 +01002079 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002080 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002081 /*
2082 * Only use current running hash algorithm that is already required
2083 * for requested ciphersuite.
2084 */
Paul Bakker926af752012-11-23 13:38:07 +01002085 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2086
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002087 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2088 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002089 {
2090 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2091 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002092
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002093 /*
2094 * Supported signature algorithms
2095 */
2096#if defined(POLARSSL_RSA_C)
2097 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2098 p[2 + sa_len++] = SSL_SIG_RSA;
2099#endif
2100#if defined(POLARSSL_ECDSA_C)
2101 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2102 p[2 + sa_len++] = SSL_SIG_ECDSA;
2103#endif
Paul Bakker926af752012-11-23 13:38:07 +01002104
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002105 p[0] = (unsigned char)( sa_len >> 8 );
2106 p[1] = (unsigned char)( sa_len );
2107 sa_len += 2;
2108 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002109 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002110#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002111
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002112 /*
2113 * DistinguishedName certificate_authorities<0..2^16-1>;
2114 * opaque DistinguishedName<1..2^16-1>;
2115 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002116 p += 2;
2117 crt = ssl->ca_chain;
2118
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002119 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002120 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002121 {
2122 if( p - buf > 4096 )
2123 break;
2124
Paul Bakker926af752012-11-23 13:38:07 +01002125 dn_size = crt->subject_raw.len;
2126 *p++ = (unsigned char)( dn_size >> 8 );
2127 *p++ = (unsigned char)( dn_size );
2128 memcpy( p, crt->subject_raw.p, dn_size );
2129 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002130
Paul Bakker926af752012-11-23 13:38:07 +01002131 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2132
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002133 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002134 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002135 }
2136
Paul Bakker926af752012-11-23 13:38:07 +01002137 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002138 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2139 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002140 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2141 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002142
2143 ret = ssl_write_record( ssl );
2144
2145 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2146
2147 return( ret );
2148}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002149#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2150 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002151 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2152 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002153
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002154#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2155 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2156static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2157{
2158 int ret;
2159
2160 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2161 {
2162 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2163 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2164 }
2165
2166 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2167 pk_ec( *ssl_own_key( ssl ) ),
2168 POLARSSL_ECDH_OURS ) ) != 0 )
2169 {
2170 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2171 return( ret );
2172 }
2173
2174 return( 0 );
2175}
2176#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2177 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2178
Paul Bakker41c83d32013-03-20 14:39:14 +01002179static int ssl_write_server_key_exchange( ssl_context *ssl )
2180{
Paul Bakker23986e52011-04-24 08:57:21 +00002181 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002182 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002183 const ssl_ciphersuite_t *ciphersuite_info =
2184 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002185
2186#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2187 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2188 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002189 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002190 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002191 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002192 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002193 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002194 ((void) dig_signed);
2195 ((void) dig_signed_len);
2196#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002197
Paul Bakker5121ce52009-01-03 21:22:43 +00002198 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2199
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002200#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2201 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2202 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002203 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2204 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2205 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002206 {
2207 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2208 ssl->state++;
2209 return( 0 );
2210 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002211#endif
2212
2213#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2214 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2215 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2216 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2217 {
2218 ssl_get_ecdh_params_from_cert( ssl );
2219
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002220 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002221 ssl->state++;
2222 return( 0 );
2223 }
2224#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002225
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002226#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2227 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2228 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2229 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002230 {
2231 /* TODO: Support identity hints */
2232 *(p++) = 0x00;
2233 *(p++) = 0x00;
2234
2235 n += 2;
2236 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002237#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2238 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002239
2240#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2241 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2242 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2243 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002244 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002245 /*
2246 * Ephemeral DH parameters:
2247 *
2248 * struct {
2249 * opaque dh_p<1..2^16-1>;
2250 * opaque dh_g<1..2^16-1>;
2251 * opaque dh_Ys<1..2^16-1>;
2252 * } ServerDHParams;
2253 */
2254 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2255 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2256 {
2257 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2258 return( ret );
2259 }
Paul Bakker48916f92012-09-16 19:57:18 +00002260
Paul Bakker41c83d32013-03-20 14:39:14 +01002261 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002262 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2263 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002264 {
2265 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2266 return( ret );
2267 }
2268
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002269 dig_signed = p;
2270 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002271
2272 p += len;
2273 n += len;
2274
Paul Bakker41c83d32013-03-20 14:39:14 +01002275 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2276 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2277 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2278 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2279 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002280#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2281 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002282
Gergely Budai987bfb52014-01-19 21:48:42 +01002283#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002284 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002285 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2286 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002287 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002288 /*
2289 * Ephemeral ECDH parameters:
2290 *
2291 * struct {
2292 * ECParameters curve_params;
2293 * ECPoint public;
2294 * } ServerECDHParams;
2295 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002296 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002297#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002298 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002299
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002300 /* Match our preference list against the offered curves */
2301 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2302 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2303 if( (*curve)->grp_id == *gid )
2304 goto curve_matching_done;
2305
2306curve_matching_done:
2307#else
2308 curve = ssl->handshake->curves;
2309#endif
2310
2311 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002312 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002313 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2314 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002315 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002316
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002317 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002318
Paul Bakker41c83d32013-03-20 14:39:14 +01002319 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002320 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002321 {
2322 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2323 return( ret );
2324 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002325
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002326 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2327 p, SSL_MAX_CONTENT_LEN - n,
2328 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002329 {
2330 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2331 return( ret );
2332 }
2333
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002334 dig_signed = p;
2335 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002336
2337 p += len;
2338 n += len;
2339
Paul Bakker41c83d32013-03-20 14:39:14 +01002340 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2341 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002342#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002343
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002344#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002345 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2346 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002347 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002348 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2349 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002350 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002351 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002352 unsigned int hashlen = 0;
2353 unsigned char hash[64];
2354 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002355
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002356 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002357 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2358 */
Paul Bakker577e0062013-08-28 11:57:20 +02002359#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002360 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2361 {
2362 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2363
2364 if( md_alg == POLARSSL_MD_NONE )
2365 {
2366 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002367 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002368 }
2369 }
Paul Bakker577e0062013-08-28 11:57:20 +02002370 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002371#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002372#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2373 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002374 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002375 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2376 {
2377 md_alg = POLARSSL_MD_SHA1;
2378 }
2379 else
Paul Bakker577e0062013-08-28 11:57:20 +02002380#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002381 {
2382 md_alg = POLARSSL_MD_NONE;
2383 }
2384
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002385 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002386 * Compute the hash to be signed
2387 */
Paul Bakker577e0062013-08-28 11:57:20 +02002388#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2389 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002390 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002391 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002392 md5_context md5;
2393 sha1_context sha1;
2394
Paul Bakker5b4af392014-06-26 12:09:34 +02002395 md5_init( &md5 );
2396 sha1_init( &sha1 );
2397
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002398 /*
2399 * digitally-signed struct {
2400 * opaque md5_hash[16];
2401 * opaque sha_hash[20];
2402 * };
2403 *
2404 * md5_hash
2405 * MD5(ClientHello.random + ServerHello.random
2406 * + ServerParams);
2407 * sha_hash
2408 * SHA(ClientHello.random + ServerHello.random
2409 * + ServerParams);
2410 */
2411 md5_starts( &md5 );
2412 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002413 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002414 md5_finish( &md5, hash );
2415
2416 sha1_starts( &sha1 );
2417 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002418 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002419 sha1_finish( &sha1, hash + 16 );
2420
2421 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02002422
2423 md5_free( &md5 );
2424 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002425 }
2426 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002427#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2428 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002429#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2430 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002431 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002432 {
2433 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02002434 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002435
Paul Bakker84bbeb52014-07-01 14:53:22 +02002436 md_init( &ctx );
2437
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002438 /* Info from md_alg will be used instead */
2439 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002440
2441 /*
2442 * digitally-signed struct {
2443 * opaque client_random[32];
2444 * opaque server_random[32];
2445 * ServerDHParams params;
2446 * };
2447 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002448 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002449 {
2450 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2451 return( ret );
2452 }
2453
2454 md_starts( &ctx );
2455 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002456 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002457 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002458 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00002459 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002460 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002461#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2462 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002463 {
2464 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002465 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002466 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002467
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002468 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2469 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002470
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002471 /*
2472 * Make the signature
2473 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002474 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002475 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002476 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2477 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002478 }
Paul Bakker23f36802012-09-28 14:15:14 +00002479
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002480#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002481 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2482 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002483 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002484 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002485
2486 n += 2;
2487 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002488#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002489
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002490 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002491 p + 2 , &signature_len,
2492 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002493 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002494 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002495 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002496 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002497
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002498 *(p++) = (unsigned char)( signature_len >> 8 );
2499 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002500 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002501
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002502 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002503
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002504 p += signature_len;
2505 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002506 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002507#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002508 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2509 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002510
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002511 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002512 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2513 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2514
2515 ssl->state++;
2516
2517 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2518 {
2519 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2520 return( ret );
2521 }
2522
2523 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2524
2525 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002526}
2527
2528static int ssl_write_server_hello_done( ssl_context *ssl )
2529{
2530 int ret;
2531
2532 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2533
2534 ssl->out_msglen = 4;
2535 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2536 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2537
2538 ssl->state++;
2539
2540 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2541 {
2542 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2543 return( ret );
2544 }
2545
2546 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2547
2548 return( 0 );
2549}
2550
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002551#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2552 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2553static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2554 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002555{
2556 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002557 size_t n;
2558
2559 /*
2560 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2561 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002562 if( *p + 2 > end )
2563 {
2564 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2565 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2566 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002567
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002568 n = ( (*p)[0] << 8 ) | (*p)[1];
2569 *p += 2;
2570
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002571 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002572 {
2573 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2574 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2575 }
2576
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002577 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002578 {
2579 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2580 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2581 }
2582
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002583 *p += n;
2584
Paul Bakker70df2fb2013-04-17 17:19:09 +02002585 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2586
Paul Bakker70df2fb2013-04-17 17:19:09 +02002587 return( ret );
2588}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002589#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2590 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002591
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002592#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2593 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002594static int ssl_parse_encrypted_pms( ssl_context *ssl,
2595 const unsigned char *p,
2596 const unsigned char *end,
2597 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002598{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002599 int ret;
2600 size_t len = pk_get_len( ssl_own_key( ssl ) );
2601 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002602
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002603 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002604 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002605 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002606 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2607 }
2608
2609 /*
2610 * Decrypt the premaster using own private RSA key
2611 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002612#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2613 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002614 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2615 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002616 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2617 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002618 {
2619 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2620 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2621 }
2622 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002623#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002624
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002625 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002626 {
2627 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2628 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2629 }
2630
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002631 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2632 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002633 sizeof( ssl->handshake->premaster ) - pms_offset,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002634 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002635
2636 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002637 pms[0] != ssl->handshake->max_major_ver ||
2638 pms[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002639 {
2640 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2641
2642 /*
2643 * Protection against Bleichenbacher's attack:
2644 * invalid PKCS#1 v1.5 padding must not cause
2645 * the connection to end immediately; instead,
2646 * send a bad_record_mac later in the handshake.
2647 */
2648 ssl->handshake->pmslen = 48;
2649
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002650 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002651 if( ret != 0 )
2652 return( ret );
2653 }
2654
2655 return( ret );
2656}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002657#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2658 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002659
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002660#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002661static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2662 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002663{
Paul Bakker6db455e2013-09-18 17:29:31 +02002664 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002665 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002666
Paul Bakker6db455e2013-09-18 17:29:31 +02002667 if( ssl->f_psk == NULL &&
2668 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2669 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002670 {
2671 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2672 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2673 }
2674
2675 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002676 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002677 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002678 if( *p + 2 > end )
2679 {
2680 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2681 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2682 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002683
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002684 n = ( (*p)[0] << 8 ) | (*p)[1];
2685 *p += 2;
2686
2687 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002688 {
2689 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2690 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2691 }
2692
Paul Bakker6db455e2013-09-18 17:29:31 +02002693 if( ssl->f_psk != NULL )
2694 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002695 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002696 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2697 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002698 else
Paul Bakker6db455e2013-09-18 17:29:31 +02002699 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002700 /* Identity is not a big secret since clients send it in the clear,
2701 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02002702 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002703 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002704 {
2705 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2706 }
2707 }
2708
2709 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002710 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002711 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002712 if( ( ret = ssl_send_alert_message( ssl,
2713 SSL_ALERT_LEVEL_FATAL,
2714 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2715 {
2716 return( ret );
2717 }
2718
2719 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002720 }
2721
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002722 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002723
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002724 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002725}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002726#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002727
Paul Bakker5121ce52009-01-03 21:22:43 +00002728static int ssl_parse_client_key_exchange( ssl_context *ssl )
2729{
Paul Bakker23986e52011-04-24 08:57:21 +00002730 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002731 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002732
Paul Bakker41c83d32013-03-20 14:39:14 +01002733 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002734
2735 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2736
2737 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2738 {
2739 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2740 return( ret );
2741 }
2742
2743 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2744 {
2745 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002746 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002747 }
2748
2749 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2750 {
2751 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002752 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002753 }
2754
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002755#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002756 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002757 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002758 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002759 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002760
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002761 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002762 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002763 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2764 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002765 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002766
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002767 if( p != end )
2768 {
2769 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2770 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2771 }
2772
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002773 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002774
2775 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2776 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002777 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002778 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002779 {
2780 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2781 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2782 }
2783
2784 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002785 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002786 else
2787#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002788#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002789 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2790 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2791 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002792 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002793 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2794 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2795 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002796 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002797 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002798 ssl->in_msg + 4, ssl->in_hslen - 4 ) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002799 {
2800 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2801 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2802 }
2803
2804 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2805
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002806 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2807 &ssl->handshake->pmslen,
2808 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002809 POLARSSL_MPI_MAX_SIZE,
2810 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002811 {
2812 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2813 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2814 }
2815
2816 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002817 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002818 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002819#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002820 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2821 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2822 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002823#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2824 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002825 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002826 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002827 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002828
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002829 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002830 {
2831 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2832 return( ret );
2833 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002834
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002835 if( p != end )
2836 {
2837 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2838 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2839 }
2840
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002841 if( ( ret = ssl_psk_derive_premaster( ssl,
2842 ciphersuite_info->key_exchange ) ) != 0 )
2843 {
2844 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2845 return( ret );
2846 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002847 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002848 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002849#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002850#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2851 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2852 {
2853 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002854 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002855
2856 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2857 {
2858 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2859 return( ret );
2860 }
2861
2862 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
2863 {
2864 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
2865 return( ret );
2866 }
2867
2868 if( ( ret = ssl_psk_derive_premaster( ssl,
2869 ciphersuite_info->key_exchange ) ) != 0 )
2870 {
2871 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2872 return( ret );
2873 }
2874 }
2875 else
2876#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002877#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2878 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2879 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002880 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002881 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002882
2883 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2884 {
2885 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2886 return( ret );
2887 }
2888 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2889 {
2890 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2891 return( ret );
2892 }
2893
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002894 if( p != end )
2895 {
2896 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2897 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2898 }
2899
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002900 if( ( ret = ssl_psk_derive_premaster( ssl,
2901 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002902 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002903 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2904 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002905 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002906 }
2907 else
2908#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002909#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2910 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2911 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002912 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002913 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002914
2915 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2916 {
2917 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2918 return( ret );
2919 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002920
2921 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2922 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002923 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002924 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2925 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002926 }
2927
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002928 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2929
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002930 if( ( ret = ssl_psk_derive_premaster( ssl,
2931 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002932 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002933 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002934 return( ret );
2935 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002936 }
2937 else
2938#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002939#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2940 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002941 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002942 if( ( ret = ssl_parse_encrypted_pms( ssl,
2943 ssl->in_msg + 4,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002944 ssl->in_msg + ssl->in_hslen,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002945 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002946 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002947 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002948 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002949 }
2950 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002951 else
2952#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2953 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002954 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002955 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002956 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002957
Paul Bakkerff60ee62010-03-16 21:09:09 +00002958 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2959 {
2960 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2961 return( ret );
2962 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002963
Paul Bakker5121ce52009-01-03 21:22:43 +00002964 ssl->state++;
2965
2966 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2967
2968 return( 0 );
2969}
2970
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002971#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2972 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002973 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2974 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002975static int ssl_parse_certificate_verify( ssl_context *ssl )
2976{
Paul Bakkerfbb17802013-04-17 19:10:21 +02002977 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002978
2979 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2980
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002981 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002982 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002983 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002984 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002985 {
2986 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2987 ssl->state++;
2988 return( 0 );
2989 }
2990
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002991 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2992 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002993}
2994#else
2995static int ssl_parse_certificate_verify( ssl_context *ssl )
2996{
2997 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002998 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002999 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003000 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003001 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003002#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003003 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003004#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003005 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003006 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3007
3008 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3009
3010 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003011 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003012 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003013 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3014 {
3015 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3016 ssl->state++;
3017 return( 0 );
3018 }
3019
Paul Bakkered27a042013-04-18 22:46:23 +02003020 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003021 {
3022 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3023 ssl->state++;
3024 return( 0 );
3025 }
3026
Paul Bakker48916f92012-09-16 19:57:18 +00003027 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003028
3029 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3030 {
3031 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3032 return( ret );
3033 }
3034
3035 ssl->state++;
3036
3037 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3038 {
3039 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003040 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003041 }
3042
3043 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
3044 {
3045 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003046 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003047 }
3048
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003049 /*
3050 * 0 . 0 handshake type
3051 * 1 . 3 handshake length
3052 * 4 . 5 sig alg (TLS 1.2 only)
3053 * 4+n . 5+n signature length (n = sa_len)
3054 * 6+n . 6+n+m signature (m = sig_len)
3055 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003056
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003057#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3058 defined(POLARSSL_SSL_PROTO_TLS1_1)
3059 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003060 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003061 sa_len = 0;
3062
Paul Bakkerc70b9822013-04-07 22:00:46 +02003063 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003064 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003065
3066 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3067 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3068 POLARSSL_PK_ECDSA ) )
3069 {
3070 hash_start += 16;
3071 hashlen -= 16;
3072 md_alg = POLARSSL_MD_SHA1;
3073 }
Paul Bakker926af752012-11-23 13:38:07 +01003074 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003075 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003076#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3077 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003078#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3079 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003080 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003081 sa_len = 2;
3082
Paul Bakker5121ce52009-01-03 21:22:43 +00003083 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003084 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003085 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003086 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003087 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003088 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3089 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003090 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3091 }
3092
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003093 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003094
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003095 /* Info from md_alg will be used instead */
3096 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003097
3098 /*
3099 * Signature
3100 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003101 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
3102 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003103 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003104 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3105 " for verify message" ) );
3106 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003107 }
3108
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003109 /*
3110 * Check the certificate's key type matches the signature alg
3111 */
3112 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3113 {
3114 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3115 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3116 }
Paul Bakker577e0062013-08-28 11:57:20 +02003117 }
3118 else
3119#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3120 {
3121 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003122 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003123 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003124
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003125 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01003126
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003127 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003128 {
3129 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003130 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003131 }
3132
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003133 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003134 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003135 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003136 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003137 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003138 return( ret );
3139 }
3140
3141 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3142
Paul Bakkered27a042013-04-18 22:46:23 +02003143 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003144}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003145#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3146 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3147 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003148
Paul Bakkera503a632013-08-14 13:48:06 +02003149#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003150static int ssl_write_new_session_ticket( ssl_context *ssl )
3151{
3152 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003153 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003154 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003155
3156 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3157
3158 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3159 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3160
3161 /*
3162 * struct {
3163 * uint32 ticket_lifetime_hint;
3164 * opaque ticket<0..2^16-1>;
3165 * } NewSessionTicket;
3166 *
3167 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3168 * 8 . 9 ticket_len (n)
3169 * 10 . 9+n ticket content
3170 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003171
3172 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3173 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3174 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3175 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003176
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003177 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3178 {
3179 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3180 tlen = 0;
3181 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003182
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003183 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3184 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003185
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003186 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003187
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003188 /*
3189 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3190 * ChangeCipherSpec share the same state.
3191 */
3192 ssl->handshake->new_session_ticket = 0;
3193
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003194 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3195 {
3196 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3197 return( ret );
3198 }
3199
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003200 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3201
3202 return( 0 );
3203}
Paul Bakkera503a632013-08-14 13:48:06 +02003204#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003205
Paul Bakker5121ce52009-01-03 21:22:43 +00003206/*
Paul Bakker1961b702013-01-25 14:49:24 +01003207 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003208 */
Paul Bakker1961b702013-01-25 14:49:24 +01003209int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003210{
3211 int ret = 0;
3212
Paul Bakker1961b702013-01-25 14:49:24 +01003213 if( ssl->state == SSL_HANDSHAKE_OVER )
3214 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003215
Paul Bakker1961b702013-01-25 14:49:24 +01003216 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3217
3218 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3219 return( ret );
3220
3221 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003222 {
Paul Bakker1961b702013-01-25 14:49:24 +01003223 case SSL_HELLO_REQUEST:
3224 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003225 break;
3226
Paul Bakker1961b702013-01-25 14:49:24 +01003227 /*
3228 * <== ClientHello
3229 */
3230 case SSL_CLIENT_HELLO:
3231 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003232 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003233
3234 /*
3235 * ==> ServerHello
3236 * Certificate
3237 * ( ServerKeyExchange )
3238 * ( CertificateRequest )
3239 * ServerHelloDone
3240 */
3241 case SSL_SERVER_HELLO:
3242 ret = ssl_write_server_hello( ssl );
3243 break;
3244
3245 case SSL_SERVER_CERTIFICATE:
3246 ret = ssl_write_certificate( ssl );
3247 break;
3248
3249 case SSL_SERVER_KEY_EXCHANGE:
3250 ret = ssl_write_server_key_exchange( ssl );
3251 break;
3252
3253 case SSL_CERTIFICATE_REQUEST:
3254 ret = ssl_write_certificate_request( ssl );
3255 break;
3256
3257 case SSL_SERVER_HELLO_DONE:
3258 ret = ssl_write_server_hello_done( ssl );
3259 break;
3260
3261 /*
3262 * <== ( Certificate/Alert )
3263 * ClientKeyExchange
3264 * ( CertificateVerify )
3265 * ChangeCipherSpec
3266 * Finished
3267 */
3268 case SSL_CLIENT_CERTIFICATE:
3269 ret = ssl_parse_certificate( ssl );
3270 break;
3271
3272 case SSL_CLIENT_KEY_EXCHANGE:
3273 ret = ssl_parse_client_key_exchange( ssl );
3274 break;
3275
3276 case SSL_CERTIFICATE_VERIFY:
3277 ret = ssl_parse_certificate_verify( ssl );
3278 break;
3279
3280 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3281 ret = ssl_parse_change_cipher_spec( ssl );
3282 break;
3283
3284 case SSL_CLIENT_FINISHED:
3285 ret = ssl_parse_finished( ssl );
3286 break;
3287
3288 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003289 * ==> ( NewSessionTicket )
3290 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003291 * Finished
3292 */
3293 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003294#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003295 if( ssl->handshake->new_session_ticket != 0 )
3296 ret = ssl_write_new_session_ticket( ssl );
3297 else
Paul Bakkera503a632013-08-14 13:48:06 +02003298#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003299 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003300 break;
3301
3302 case SSL_SERVER_FINISHED:
3303 ret = ssl_write_finished( ssl );
3304 break;
3305
3306 case SSL_FLUSH_BUFFERS:
3307 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3308 ssl->state = SSL_HANDSHAKE_WRAPUP;
3309 break;
3310
3311 case SSL_HANDSHAKE_WRAPUP:
3312 ssl_handshake_wrapup( ssl );
3313 break;
3314
3315 default:
3316 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3317 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003318 }
3319
Paul Bakker5121ce52009-01-03 21:22:43 +00003320 return( ret );
3321}
Paul Bakker9af723c2014-05-01 13:03:14 +02003322#endif /* POLARSSL_SSL_SRV_C */