blob: a8e4f41bc367ac47eb6353d4b16bf6532d3d8fb3 [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 Bakker34617722014-06-13 17:20:13 +0200346 polarssl_zeroize( &session, sizeof( ssl_session ) );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200347
348 return( 0 );
349}
Paul Bakkera503a632013-08-14 13:48:06 +0200350#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200351
Paul Bakker0be444a2013-08-27 21:55:01 +0200352#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200353/*
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200354 * Wrapper around f_sni, allowing use of ssl_set_own_cert() but
355 * making it act on ssl->hanshake->sni_key_cert instead.
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200356 */
357static int ssl_sni_wrapper( ssl_context *ssl,
358 const unsigned char* name, size_t len )
359{
360 int ret;
361 ssl_key_cert *key_cert_ori = ssl->key_cert;
362
363 ssl->key_cert = NULL;
364 ret = ssl->f_sni( ssl->p_sni, ssl, name, len );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200365 ssl->handshake->sni_key_cert = ssl->key_cert;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200366
367 ssl->key_cert = key_cert_ori;
368
369 return( ret );
370}
371
Paul Bakker5701cdc2012-09-27 21:49:42 +0000372static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000373 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000374 size_t len )
375{
376 int ret;
377 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000378 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000379
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100380 SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
381
Paul Bakker5701cdc2012-09-27 21:49:42 +0000382 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
383 if( servername_list_size + 2 != len )
384 {
385 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
386 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
387 }
388
389 p = buf + 2;
390 while( servername_list_size > 0 )
391 {
392 hostname_len = ( ( p[1] << 8 ) | p[2] );
393 if( hostname_len + 3 > servername_list_size )
394 {
395 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
396 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
397 }
398
399 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
400 {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200401 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000402 if( ret != 0 )
403 {
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100404 SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000405 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
406 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
407 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
408 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000409 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000410 }
411
412 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000413 p += hostname_len + 3;
414 }
415
416 if( servername_list_size != 0 )
417 {
418 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
419 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000420 }
421
422 return( 0 );
423}
Paul Bakker0be444a2013-08-27 21:55:01 +0200424#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000425
Paul Bakker48916f92012-09-16 19:57:18 +0000426static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000427 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000428 size_t len )
429{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000430 int ret;
431
Paul Bakker48916f92012-09-16 19:57:18 +0000432 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
433 {
434 if( len != 1 || buf[0] != 0x0 )
435 {
436 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000437
438 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
439 return( ret );
440
Paul Bakker48916f92012-09-16 19:57:18 +0000441 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
442 }
443
444 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
445 }
446 else
447 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100448 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000449 if( len != 1 + ssl->verify_data_len ||
450 buf[0] != ssl->verify_data_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100451 safer_memcmp( buf + 1, ssl->peer_verify_data,
452 ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000453 {
454 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000455
456 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
457 return( ret );
458
Paul Bakker48916f92012-09-16 19:57:18 +0000459 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
460 }
461 }
462
463 return( 0 );
464}
465
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200466#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +0000467static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
468 const unsigned char *buf,
469 size_t len )
470{
471 size_t sig_alg_list_size;
472 const unsigned char *p;
473
474 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
475 if( sig_alg_list_size + 2 != len ||
476 sig_alg_list_size %2 != 0 )
477 {
478 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
479 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
480 }
481
482 p = buf + 2;
483 while( sig_alg_list_size > 0 )
484 {
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200485 /*
486 * For now, just ignore signature algorithm and rely on offered
487 * ciphersuites only. To be fixed later.
488 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200489#if defined(POLARSSL_SHA512_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000490 if( p[0] == SSL_HASH_SHA512 )
491 {
492 ssl->handshake->sig_alg = SSL_HASH_SHA512;
493 break;
494 }
495 if( p[0] == SSL_HASH_SHA384 )
496 {
497 ssl->handshake->sig_alg = SSL_HASH_SHA384;
498 break;
499 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200500#endif /* POLARSSL_SHA512_C */
Paul Bakker9e36f042013-06-30 14:34:05 +0200501#if defined(POLARSSL_SHA256_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000502 if( p[0] == SSL_HASH_SHA256 )
503 {
504 ssl->handshake->sig_alg = SSL_HASH_SHA256;
505 break;
506 }
507 if( p[0] == SSL_HASH_SHA224 )
508 {
509 ssl->handshake->sig_alg = SSL_HASH_SHA224;
510 break;
511 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200512#endif /* POLARSSL_SHA256_C */
Paul Bakker23f36802012-09-28 14:15:14 +0000513 if( p[0] == SSL_HASH_SHA1 )
514 {
515 ssl->handshake->sig_alg = SSL_HASH_SHA1;
516 break;
517 }
518 if( p[0] == SSL_HASH_MD5 )
519 {
520 ssl->handshake->sig_alg = SSL_HASH_MD5;
521 break;
522 }
523
524 sig_alg_list_size -= 2;
525 p += 2;
526 }
527
528 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
529 ssl->handshake->sig_alg ) );
530
531 return( 0 );
532}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200533#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker23f36802012-09-28 14:15:14 +0000534
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200535#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200536static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
537 const unsigned char *buf,
538 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100539{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200540 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100541 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200542 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100543
544 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
545 if( list_size + 2 != len ||
546 list_size % 2 != 0 )
547 {
548 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
549 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
550 }
551
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +0100552 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200553 * and leave room for a final 0 */
554 our_size = list_size / 2 + 1;
555 if( our_size > POLARSSL_ECP_DP_MAX )
556 our_size = POLARSSL_ECP_DP_MAX;
557
558 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
559 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
560
Paul Bakker9af723c2014-05-01 13:03:14 +0200561 /* explicit void pointer cast for buggy MS compiler */
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200562 memset( (void *) curves, 0, our_size * sizeof( *curves ) );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200563 ssl->handshake->curves = curves;
564
Paul Bakker41c83d32013-03-20 14:39:14 +0100565 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200566 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100567 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200568 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200569
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200570 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100571 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200572 *curves++ = curve_info;
573 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100574 }
575
576 list_size -= 2;
577 p += 2;
578 }
579
580 return( 0 );
581}
582
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200583static int ssl_parse_supported_point_formats( ssl_context *ssl,
584 const unsigned char *buf,
585 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100586{
587 size_t list_size;
588 const unsigned char *p;
589
590 list_size = buf[0];
591 if( list_size + 1 != len )
592 {
593 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
594 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
595 }
596
597 p = buf + 2;
598 while( list_size > 0 )
599 {
600 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
601 p[0] == POLARSSL_ECP_PF_COMPRESSED )
602 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200603 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200604 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100605 return( 0 );
606 }
607
608 list_size--;
609 p++;
610 }
611
612 return( 0 );
613}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200614#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100615
Paul Bakker05decb22013-08-15 13:33:48 +0200616#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200617static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
618 const unsigned char *buf,
619 size_t len )
620{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200621 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200622 {
623 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
624 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
625 }
626
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200627 ssl->session_negotiate->mfl_code = buf[0];
628
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200629 return( 0 );
630}
Paul Bakker05decb22013-08-15 13:33:48 +0200631#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200632
Paul Bakker1f2bc622013-08-15 13:45:55 +0200633#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200634static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
635 const unsigned char *buf,
636 size_t len )
637{
638 if( len != 0 )
639 {
640 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
641 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
642 }
643
644 ((void) buf);
645
646 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
647
648 return( 0 );
649}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200650#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200651
Paul Bakkera503a632013-08-14 13:48:06 +0200652#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200653static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200654 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200655 size_t len )
656{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200657 int ret;
658
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200659 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
660 return( 0 );
661
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200662 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200663 ssl->handshake->new_session_ticket = 1;
664
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200665 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
666
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200667 if( len == 0 )
668 return( 0 );
669
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200670 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
671 {
672 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
673 return( 0 );
674 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200675
676 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200677 * Failures are ok: just ignore the ticket and proceed.
678 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200679 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
680 {
681 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200682 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200683 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200684
685 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
686
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200687 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200688
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200689 /* Don't send a new ticket after all, this one is OK */
690 ssl->handshake->new_session_ticket = 0;
691
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200692 return( 0 );
693}
Paul Bakkera503a632013-08-14 13:48:06 +0200694#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200695
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200696#if defined(POLARSSL_SSL_ALPN)
697static int ssl_parse_alpn_ext( ssl_context *ssl,
698 unsigned char *buf, size_t len )
699{
Paul Bakker14b16c62014-05-28 11:33:54 +0200700 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200701 const unsigned char *theirs, *start, *end;
702 const char **ours;
703
704 /* If ALPN not configured, just ignore the extension */
705 if( ssl->alpn_list == NULL )
706 return( 0 );
707
708 /*
709 * opaque ProtocolName<1..2^8-1>;
710 *
711 * struct {
712 * ProtocolName protocol_name_list<2..2^16-1>
713 * } ProtocolNameList;
714 */
715
716 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
717 if( len < 4 )
718 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
719
720 list_len = ( buf[0] << 8 ) | buf[1];
721 if( list_len != len - 2 )
722 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
723
724 /*
725 * Use our order of preference
726 */
727 start = buf + 2;
728 end = buf + len;
729 for( ours = ssl->alpn_list; *ours != NULL; ours++ )
730 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200731 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200732 for( theirs = start; theirs != end; theirs += cur_len )
733 {
734 /* If the list is well formed, we should get equality first */
735 if( theirs > end )
736 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
737
738 cur_len = *theirs++;
739
740 /* Empty strings MUST NOT be included */
741 if( cur_len == 0 )
742 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
743
Paul Bakker14b16c62014-05-28 11:33:54 +0200744 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200745 memcmp( theirs, *ours, cur_len ) == 0 )
746 {
747 ssl->alpn_chosen = *ours;
748 return( 0 );
749 }
750 }
751 }
752
753 /* If we get there, no match was found */
754 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
755 SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
756 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
757}
758#endif /* POLARSSL_SSL_ALPN */
759
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100760/*
761 * Auxiliary functions for ServerHello parsing and related actions
762 */
763
764#if defined(POLARSSL_X509_CRT_PARSE_C)
765/*
766 * Return 1 if the given EC key uses the given curve, 0 otherwise
767 */
768#if defined(POLARSSL_ECDSA_C)
769static int ssl_key_matches_curves( pk_context *pk,
770 const ecp_curve_info **curves )
771{
772 const ecp_curve_info **crv = curves;
773 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
774
775 while( *crv != NULL )
776 {
777 if( (*crv)->grp_id == grp_id )
778 return( 1 );
779 crv++;
780 }
781
782 return( 0 );
783}
784#endif /* POLARSSL_ECDSA_C */
785
786/*
787 * Try picking a certificate for this ciphersuite,
788 * return 0 on success and -1 on failure.
789 */
790static int ssl_pick_cert( ssl_context *ssl,
791 const ssl_ciphersuite_t * ciphersuite_info )
792{
793 ssl_key_cert *cur, *list;
794 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
795
796#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
797 if( ssl->handshake->sni_key_cert != NULL )
798 list = ssl->handshake->sni_key_cert;
799 else
800#endif
801 list = ssl->handshake->key_cert;
802
803 if( pk_alg == POLARSSL_PK_NONE )
804 return( 0 );
805
806 for( cur = list; cur != NULL; cur = cur->next )
807 {
808 if( ! pk_can_do( cur->key, pk_alg ) )
809 continue;
810
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200811 /*
812 * This avoids sending the client a cert it'll reject based on
813 * keyUsage or other extensions.
814 *
815 * It also allows the user to provision different certificates for
816 * different uses based on keyUsage, eg if they want to avoid signing
817 * and decrypting with the same RSA key.
818 */
819 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
820 SSL_IS_SERVER ) != 0 )
821 {
822 continue;
823 }
824
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100825#if defined(POLARSSL_ECDSA_C)
826 if( pk_alg == POLARSSL_PK_ECDSA )
827 {
828 if( ssl_key_matches_curves( cur->key, ssl->handshake->curves ) )
829 break;
830 }
831 else
832#endif
833 break;
834 }
835
836 if( cur == NULL )
837 return( -1 );
838
839 ssl->handshake->key_cert = cur;
840 return( 0 );
841}
842#endif /* POLARSSL_X509_CRT_PARSE_C */
843
844/*
845 * Check if a given ciphersuite is suitable for use with our config/keys/etc
846 * Sets ciphersuite_info only if the suite matches.
847 */
848static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
849 const ssl_ciphersuite_t **ciphersuite_info )
850{
851 const ssl_ciphersuite_t *suite_info;
852
853 suite_info = ssl_ciphersuite_from_id( suite_id );
854 if( suite_info == NULL )
855 {
856 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", suite_id ) );
857 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
858 }
859
860 if( suite_info->min_minor_ver > ssl->minor_ver ||
861 suite_info->max_minor_ver < ssl->minor_ver )
862 return( 0 );
863
864#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
865 if( ssl_ciphersuite_uses_ec( suite_info ) &&
866 ( ssl->handshake->curves == NULL ||
867 ssl->handshake->curves[0] == NULL ) )
868 return( 0 );
869#endif
870
871#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
872 /* If the ciphersuite requires a pre-shared key and we don't
873 * have one, skip it now rather than failing later */
874 if( ssl_ciphersuite_uses_psk( suite_info ) &&
875 ssl->f_psk == NULL &&
876 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
877 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
878 return( 0 );
879#endif
880
881#if defined(POLARSSL_X509_CRT_PARSE_C)
882 /*
883 * Final check: if ciphersuite requires us to have a
884 * certificate/key of a particular type:
885 * - select the appropriate certificate if we have one, or
886 * - try the next ciphersuite if we don't
887 * This must be done last since we modify the key_cert list.
888 */
889 if( ssl_pick_cert( ssl, suite_info ) != 0 )
890 return( 0 );
891#endif
892
893 *ciphersuite_info = suite_info;
894 return( 0 );
895}
896
Paul Bakker78a8c712013-03-06 17:01:52 +0100897#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
898static int ssl_parse_client_hello_v2( ssl_context *ssl )
899{
900 int ret;
901 unsigned int i, j;
902 size_t n;
903 unsigned int ciph_len, sess_len, chal_len;
904 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200905 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200906 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100907
908 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
909
910 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
911 {
912 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
913
914 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
915 return( ret );
916
917 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
918 }
919
920 buf = ssl->in_hdr;
921
922 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
923
924 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
925 buf[2] ) );
926 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
927 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
928 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
929 buf[3], buf[4] ) );
930
931 /*
932 * SSLv2 Client Hello
933 *
934 * Record layer:
935 * 0 . 1 message length
936 *
937 * SSL layer:
938 * 2 . 2 message type
939 * 3 . 4 protocol version
940 */
941 if( buf[2] != SSL_HS_CLIENT_HELLO ||
942 buf[3] != SSL_MAJOR_VERSION_3 )
943 {
944 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
945 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
946 }
947
948 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
949
950 if( n < 17 || n > 512 )
951 {
952 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
953 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
954 }
955
956 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200957 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
958 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100959
960 if( ssl->minor_ver < ssl->min_minor_ver )
961 {
962 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200963 " [%d:%d] < [%d:%d]",
964 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +0100965 ssl->min_major_ver, ssl->min_minor_ver ) );
966
967 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
968 SSL_ALERT_MSG_PROTOCOL_VERSION );
969 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
970 }
971
Paul Bakker2fbefde2013-06-29 16:01:15 +0200972 ssl->handshake->max_major_ver = buf[3];
973 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100974
975 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
976 {
977 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
978 return( ret );
979 }
980
981 ssl->handshake->update_checksum( ssl, buf + 2, n );
982
983 buf = ssl->in_msg;
984 n = ssl->in_left - 5;
985
986 /*
987 * 0 . 1 ciphersuitelist length
988 * 2 . 3 session id length
989 * 4 . 5 challenge length
990 * 6 . .. ciphersuitelist
991 * .. . .. session id
992 * .. . .. challenge
993 */
994 SSL_DEBUG_BUF( 4, "record contents", buf, n );
995
996 ciph_len = ( buf[0] << 8 ) | buf[1];
997 sess_len = ( buf[2] << 8 ) | buf[3];
998 chal_len = ( buf[4] << 8 ) | buf[5];
999
1000 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
1001 ciph_len, sess_len, chal_len ) );
1002
1003 /*
1004 * Make sure each parameter length is valid
1005 */
1006 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1007 {
1008 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1009 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1010 }
1011
1012 if( sess_len > 32 )
1013 {
1014 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1015 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1016 }
1017
1018 if( chal_len < 8 || chal_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( n != 6 + ciph_len + sess_len + chal_len )
1025 {
1026 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1027 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1028 }
1029
1030 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1031 buf + 6, ciph_len );
1032 SSL_DEBUG_BUF( 3, "client hello, session id",
1033 buf + 6 + ciph_len, sess_len );
1034 SSL_DEBUG_BUF( 3, "client hello, challenge",
1035 buf + 6 + ciph_len + sess_len, chal_len );
1036
1037 p = buf + 6 + ciph_len;
1038 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001039 memset( ssl->session_negotiate->id, 0,
1040 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001041 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1042
1043 p += sess_len;
1044 memset( ssl->handshake->randbytes, 0, 64 );
1045 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1046
1047 /*
1048 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1049 */
1050 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1051 {
1052 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1053 {
1054 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1055 if( ssl->renegotiation == SSL_RENEGOTIATION )
1056 {
1057 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1058
1059 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1060 return( ret );
1061
1062 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1063 }
1064 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1065 break;
1066 }
1067 }
1068
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001069 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001070 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001071#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1072 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1073 {
1074 for( i = 0; ciphersuites[i] != 0; i++ )
1075#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001076 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001077 {
1078 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001079#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001080 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001081 if( p[0] != 0 ||
1082 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1083 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1084 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001085
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001086 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1087 &ciphersuite_info ) ) != 0 )
1088 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001089
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001090 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001091 goto have_ciphersuite_v2;
1092 }
1093 }
1094
1095 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1096
1097 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1098
1099have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001100 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001101 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001102 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001103
1104 /*
1105 * SSLv2 Client Hello relevant renegotiation security checks
1106 */
1107 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1108 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1109 {
1110 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1111
1112 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1113 return( ret );
1114
1115 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1116 }
1117
1118 ssl->in_left = 0;
1119 ssl->state++;
1120
1121 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1122
1123 return( 0 );
1124}
1125#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1126
Paul Bakker5121ce52009-01-03 21:22:43 +00001127static int ssl_parse_client_hello( ssl_context *ssl )
1128{
Paul Bakker23986e52011-04-24 08:57:21 +00001129 int ret;
1130 unsigned int i, j;
1131 size_t n;
1132 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001133 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001134 unsigned int ext_len = 0;
1135 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001136 int renegotiation_info_seen = 0;
1137 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001138 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001139 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001140
1141 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1142
Paul Bakker48916f92012-09-16 19:57:18 +00001143 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1144 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001145 {
1146 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1147 return( ret );
1148 }
1149
1150 buf = ssl->in_hdr;
1151
Paul Bakker78a8c712013-03-06 17:01:52 +01001152#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1153 if( ( buf[0] & 0x80 ) != 0 )
1154 return ssl_parse_client_hello_v2( ssl );
1155#endif
1156
Paul Bakkerec636f32012-09-09 19:17:02 +00001157 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1158
1159 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1160 buf[0] ) );
1161 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1162 ( buf[3] << 8 ) | buf[4] ) );
1163 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
1164 buf[1], buf[2] ) );
1165
1166 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001167 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001168 *
1169 * Record layer:
1170 * 0 . 0 message type
1171 * 1 . 2 protocol version
1172 * 3 . 4 message length
1173 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001174
1175 /* According to RFC 5246 Appendix E.1, the version here is typically
1176 * "{03,00}, the lowest version number supported by the client, [or] the
1177 * value of ClientHello.client_version", so the only meaningful check here
1178 * is the major version shouldn't be less than 3 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001179 if( buf[0] != SSL_MSG_HANDSHAKE ||
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001180 buf[1] < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001181 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001182 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1183 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1184 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001185
Paul Bakkerec636f32012-09-09 19:17:02 +00001186 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +00001187
Paul Bakker4f42c112014-04-17 14:48:23 +02001188 if( n < 45 || n > SSL_MAX_CONTENT_LEN )
Paul Bakkerec636f32012-09-09 19:17:02 +00001189 {
1190 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1191 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1192 }
1193
Paul Bakker48916f92012-09-16 19:57:18 +00001194 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1195 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001196 {
1197 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1198 return( ret );
1199 }
1200
1201 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +00001202 if( !ssl->renegotiation )
1203 n = ssl->in_left - 5;
1204 else
1205 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +00001206
Paul Bakker48916f92012-09-16 19:57:18 +00001207 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +00001208
1209 /*
1210 * SSL layer:
1211 * 0 . 0 handshake type
1212 * 1 . 3 handshake length
1213 * 4 . 5 protocol version
1214 * 6 . 9 UNIX time()
1215 * 10 . 37 random bytes
1216 * 38 . 38 session id length
1217 * 39 . 38+x session id
1218 * 39+x . 40+x ciphersuitelist length
Paul Bakker0f651c72014-05-22 15:12:19 +02001219 * 41+x . 40+y ciphersuitelist
1220 * 41+y . 41+y compression alg length
1221 * 42+y . 41+z compression algs
Paul Bakkerec636f32012-09-09 19:17:02 +00001222 * .. . .. extensions
1223 */
1224 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1225
1226 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1227 buf[0] ) );
1228 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1229 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1230 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1231 buf[4], buf[5] ) );
1232
1233 /*
1234 * Check the handshake type and protocol version
1235 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001236 if( buf[0] != SSL_HS_CLIENT_HELLO )
Paul Bakkerec636f32012-09-09 19:17:02 +00001237 {
1238 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1239 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1240 }
1241
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001242 ssl->major_ver = buf[4];
1243 ssl->minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001244
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001245 ssl->handshake->max_major_ver = ssl->major_ver;
1246 ssl->handshake->max_minor_ver = ssl->minor_ver;
1247
1248 if( ssl->major_ver < ssl->min_major_ver ||
1249 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001250 {
1251 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001252 " [%d:%d] < [%d:%d]",
1253 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001254 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001255
1256 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1257 SSL_ALERT_MSG_PROTOCOL_VERSION );
1258
1259 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1260 }
1261
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001262 if( ssl->major_ver > ssl->max_major_ver )
1263 {
1264 ssl->major_ver = ssl->max_major_ver;
1265 ssl->minor_ver = ssl->max_minor_ver;
1266 }
1267 else if( ssl->minor_ver > ssl->max_minor_ver )
1268 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001269
Paul Bakker48916f92012-09-16 19:57:18 +00001270 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001271
1272 /*
1273 * Check the handshake message length
1274 */
1275 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1276 {
1277 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1278 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1279 }
1280
1281 /*
1282 * Check the session length
1283 */
1284 sess_len = buf[38];
1285
Paul Bakker0f651c72014-05-22 15:12:19 +02001286 if( sess_len > 32 || sess_len > n - 42 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001287 {
1288 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1289 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1290 }
1291
Paul Bakker48916f92012-09-16 19:57:18 +00001292 ssl->session_negotiate->length = sess_len;
1293 memset( ssl->session_negotiate->id, 0,
1294 sizeof( ssl->session_negotiate->id ) );
1295 memcpy( ssl->session_negotiate->id, buf + 39,
1296 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001297
1298 /*
1299 * Check the ciphersuitelist length
1300 */
1301 ciph_len = ( buf[39 + sess_len] << 8 )
1302 | ( buf[40 + sess_len] );
1303
Paul Bakker0f651c72014-05-22 15:12:19 +02001304 if( ciph_len < 2 || ( ciph_len % 2 ) != 0 || ciph_len > n - 42 - sess_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001305 {
1306 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1307 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1308 }
1309
1310 /*
1311 * Check the compression algorithms length
1312 */
1313 comp_len = buf[41 + sess_len + ciph_len];
1314
Paul Bakker0f651c72014-05-22 15:12:19 +02001315 if( comp_len < 1 || comp_len > 16 ||
1316 comp_len > n - 42 - sess_len - ciph_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001317 {
1318 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1319 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1320 }
1321
Paul Bakker48916f92012-09-16 19:57:18 +00001322 /*
1323 * Check the extension length
1324 */
1325 if( n > 42 + sess_len + ciph_len + comp_len )
1326 {
1327 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1328 | ( buf[43 + sess_len + ciph_len + comp_len] );
1329
1330 if( ( ext_len > 0 && ext_len < 4 ) ||
1331 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1332 {
1333 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1334 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1335 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1336 }
1337 }
1338
1339 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001340#if defined(POLARSSL_ZLIB_SUPPORT)
1341 for( i = 0; i < comp_len; ++i )
1342 {
Paul Bakker48916f92012-09-16 19:57:18 +00001343 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001344 {
Paul Bakker48916f92012-09-16 19:57:18 +00001345 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001346 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001347 }
1348 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001349#endif
1350
Paul Bakkerec636f32012-09-09 19:17:02 +00001351 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1352 buf + 6, 32 );
1353 SSL_DEBUG_BUF( 3, "client hello, session id",
1354 buf + 38, sess_len );
1355 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1356 buf + 41 + sess_len, ciph_len );
1357 SSL_DEBUG_BUF( 3, "client hello, compression",
1358 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001359
Paul Bakkerec636f32012-09-09 19:17:02 +00001360 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001361 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1362 */
1363 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1364 {
1365 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1366 {
1367 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1368 if( ssl->renegotiation == SSL_RENEGOTIATION )
1369 {
1370 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001371
1372 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1373 return( ret );
1374
Paul Bakker48916f92012-09-16 19:57:18 +00001375 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1376 }
1377 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1378 break;
1379 }
1380 }
1381
Paul Bakker48916f92012-09-16 19:57:18 +00001382 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001383
1384 while( ext_len )
1385 {
1386 unsigned int ext_id = ( ( ext[0] << 8 )
1387 | ( ext[1] ) );
1388 unsigned int ext_size = ( ( ext[2] << 8 )
1389 | ( ext[3] ) );
1390
1391 if( ext_size + 4 > ext_len )
1392 {
1393 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1394 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1395 }
1396 switch( ext_id )
1397 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001398#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001399 case TLS_EXT_SERVERNAME:
1400 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1401 if( ssl->f_sni == NULL )
1402 break;
1403
1404 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1405 if( ret != 0 )
1406 return( ret );
1407 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001408#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001409
Paul Bakker48916f92012-09-16 19:57:18 +00001410 case TLS_EXT_RENEGOTIATION_INFO:
1411 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1412 renegotiation_info_seen = 1;
1413
Paul Bakker23f36802012-09-28 14:15:14 +00001414 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1415 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001416 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001417 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001418
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001419#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001420 case TLS_EXT_SIG_ALG:
1421 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1422 if( ssl->renegotiation == SSL_RENEGOTIATION )
1423 break;
1424
1425 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1426 if( ret != 0 )
1427 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001428 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001429#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001430
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001431#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001432 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1433 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1434
1435 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1436 if( ret != 0 )
1437 return( ret );
1438 break;
1439
1440 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1441 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001442 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001443
1444 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1445 if( ret != 0 )
1446 return( ret );
1447 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001448#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001449
Paul Bakker05decb22013-08-15 13:33:48 +02001450#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001451 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1452 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1453
1454 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1455 if( ret != 0 )
1456 return( ret );
1457 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001458#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001459
Paul Bakker1f2bc622013-08-15 13:45:55 +02001460#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001461 case TLS_EXT_TRUNCATED_HMAC:
1462 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1463
1464 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1465 if( ret != 0 )
1466 return( ret );
1467 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001468#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001469
Paul Bakkera503a632013-08-14 13:48:06 +02001470#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001471 case TLS_EXT_SESSION_TICKET:
1472 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1473
1474 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1475 if( ret != 0 )
1476 return( ret );
1477 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001478#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001479
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001480#if defined(POLARSSL_SSL_ALPN)
1481 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001482 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001483
1484 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1485 if( ret != 0 )
1486 return( ret );
1487 break;
1488#endif /* POLARSSL_SSL_SESSION_TICKETS */
1489
Paul Bakker48916f92012-09-16 19:57:18 +00001490 default:
1491 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1492 ext_id ) );
1493 }
1494
1495 ext_len -= 4 + ext_size;
1496 ext += 4 + ext_size;
1497
1498 if( ext_len > 0 && ext_len < 4 )
1499 {
1500 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1501 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1502 }
1503 }
1504
1505 /*
1506 * Renegotiation security checks
1507 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001508 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1509 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1510 {
1511 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1512 handshake_failure = 1;
1513 }
1514 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1515 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1516 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001517 {
1518 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001519 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001520 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001521 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1522 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1523 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001524 {
1525 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001526 handshake_failure = 1;
1527 }
1528 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1529 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1530 renegotiation_info_seen == 1 )
1531 {
1532 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1533 handshake_failure = 1;
1534 }
1535
1536 if( handshake_failure == 1 )
1537 {
1538 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1539 return( ret );
1540
Paul Bakker48916f92012-09-16 19:57:18 +00001541 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1542 }
Paul Bakker380da532012-04-18 16:10:25 +00001543
Paul Bakker41c83d32013-03-20 14:39:14 +01001544 /*
1545 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001546 * (At the end because we need information from the EC-based extensions
1547 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001548 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001549 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001550 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001551#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1552 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
1553 {
1554 for( i = 0; ciphersuites[i] != 0; i++ )
1555#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001556 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001557 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001558 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001559#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001560 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001561 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1562 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1563 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001564
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001565 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1566 &ciphersuite_info ) ) != 0 )
1567 return( ret );
1568
1569 if( ciphersuite_info != NULL )
1570 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001571 }
1572 }
1573
1574 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1575
1576 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1577 return( ret );
1578
1579 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1580
1581have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001582 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001583 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1584 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1585
Paul Bakker5121ce52009-01-03 21:22:43 +00001586 ssl->in_left = 0;
1587 ssl->state++;
1588
1589 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1590
1591 return( 0 );
1592}
1593
Paul Bakker1f2bc622013-08-15 13:45:55 +02001594#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001595static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1596 unsigned char *buf,
1597 size_t *olen )
1598{
1599 unsigned char *p = buf;
1600
1601 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1602 {
1603 *olen = 0;
1604 return;
1605 }
1606
1607 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1608
1609 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1610 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1611
1612 *p++ = 0x00;
1613 *p++ = 0x00;
1614
1615 *olen = 4;
1616}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001617#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001618
Paul Bakkera503a632013-08-14 13:48:06 +02001619#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001620static void ssl_write_session_ticket_ext( ssl_context *ssl,
1621 unsigned char *buf,
1622 size_t *olen )
1623{
1624 unsigned char *p = buf;
1625
1626 if( ssl->handshake->new_session_ticket == 0 )
1627 {
1628 *olen = 0;
1629 return;
1630 }
1631
1632 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1633
1634 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1635 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1636
1637 *p++ = 0x00;
1638 *p++ = 0x00;
1639
1640 *olen = 4;
1641}
Paul Bakkera503a632013-08-14 13:48:06 +02001642#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001643
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001644static void ssl_write_renegotiation_ext( ssl_context *ssl,
1645 unsigned char *buf,
1646 size_t *olen )
1647{
1648 unsigned char *p = buf;
1649
1650 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1651 {
1652 *olen = 0;
1653 return;
1654 }
1655
1656 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1657
1658 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1659 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1660
1661 *p++ = 0x00;
1662 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1663 *p++ = ssl->verify_data_len * 2 & 0xFF;
1664
1665 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1666 p += ssl->verify_data_len;
1667 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1668 p += ssl->verify_data_len;
1669
1670 *olen = 5 + ssl->verify_data_len * 2;
1671}
1672
Paul Bakker05decb22013-08-15 13:33:48 +02001673#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001674static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1675 unsigned char *buf,
1676 size_t *olen )
1677{
1678 unsigned char *p = buf;
1679
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001680 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1681 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001682 *olen = 0;
1683 return;
1684 }
1685
1686 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1687
1688 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1689 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1690
1691 *p++ = 0x00;
1692 *p++ = 1;
1693
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001694 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001695
1696 *olen = 5;
1697}
Paul Bakker05decb22013-08-15 13:33:48 +02001698#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001699
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001700#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001701static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1702 unsigned char *buf,
1703 size_t *olen )
1704{
1705 unsigned char *p = buf;
1706 ((void) ssl);
1707
Paul Bakker677377f2013-10-28 12:54:26 +01001708 if( ( ssl->handshake->cli_exts &
1709 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
1710 {
1711 *olen = 0;
1712 return;
1713 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001714
1715 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1716
1717 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1718 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1719
1720 *p++ = 0x00;
1721 *p++ = 2;
1722
1723 *p++ = 1;
1724 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1725
1726 *olen = 6;
1727}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001728#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001729
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001730#if defined(POLARSSL_SSL_ALPN )
1731static void ssl_write_alpn_ext( ssl_context *ssl,
1732 unsigned char *buf, size_t *olen )
1733{
1734 if( ssl->alpn_chosen == NULL )
1735 {
1736 *olen = 0;
1737 return;
1738 }
1739
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001740 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001741
1742 /*
1743 * 0 . 1 ext identifier
1744 * 2 . 3 ext length
1745 * 4 . 5 protocol list length
1746 * 6 . 6 protocol name length
1747 * 7 . 7+n protocol name
1748 */
1749 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
1750 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
1751
1752 *olen = 7 + strlen( ssl->alpn_chosen );
1753
1754 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
1755 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
1756
1757 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
1758 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
1759
1760 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
1761
1762 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
1763}
1764#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
1765
Paul Bakker5121ce52009-01-03 21:22:43 +00001766static int ssl_write_server_hello( ssl_context *ssl )
1767{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001768#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001769 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001770#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001771 int ret;
1772 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001773 unsigned char *buf, *p;
1774
1775 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1776
Paul Bakkera9a028e2013-11-21 17:31:06 +01001777 if( ssl->f_rng == NULL )
1778 {
1779 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
1780 return( POLARSSL_ERR_SSL_NO_RNG );
1781 }
1782
Paul Bakker5121ce52009-01-03 21:22:43 +00001783 /*
1784 * 0 . 0 handshake type
1785 * 1 . 3 handshake length
1786 * 4 . 5 protocol version
1787 * 6 . 9 UNIX time()
1788 * 10 . 37 random bytes
1789 */
1790 buf = ssl->out_msg;
1791 p = buf + 4;
1792
1793 *p++ = (unsigned char) ssl->major_ver;
1794 *p++ = (unsigned char) ssl->minor_ver;
1795
1796 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1797 buf[4], buf[5] ) );
1798
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001799#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001800 t = time( NULL );
1801 *p++ = (unsigned char)( t >> 24 );
1802 *p++ = (unsigned char)( t >> 16 );
1803 *p++ = (unsigned char)( t >> 8 );
1804 *p++ = (unsigned char)( t );
1805
1806 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001807#else
1808 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1809 return( ret );
1810
1811 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02001812#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001813
Paul Bakkera3d195c2011-11-27 21:07:34 +00001814 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1815 return( ret );
1816
1817 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001818
Paul Bakker48916f92012-09-16 19:57:18 +00001819 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001820
1821 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1822
1823 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001824 * Resume is 0 by default, see ssl_handshake_init().
1825 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1826 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001827 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001828 if( ssl->handshake->resume == 0 &&
1829 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001830 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001831 ssl->f_get_cache != NULL &&
1832 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1833 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01001834 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001835 ssl->handshake->resume = 1;
1836 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001837
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001838 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001839 {
1840 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001841 * New session, create a new session id,
1842 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001843 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001844 ssl->state++;
1845
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001846#if defined(POLARSSL_HAVE_TIME)
1847 ssl->session_negotiate->start = time( NULL );
1848#endif
1849
Paul Bakkera503a632013-08-14 13:48:06 +02001850#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001851 if( ssl->handshake->new_session_ticket != 0 )
1852 {
1853 ssl->session_negotiate->length = n = 0;
1854 memset( ssl->session_negotiate->id, 0, 32 );
1855 }
1856 else
1857#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001858 {
1859 ssl->session_negotiate->length = n = 32;
1860 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001861 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001862 return( ret );
1863 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001864 }
1865 else
1866 {
1867 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001868 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001869 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001870 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001871 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001872
1873 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1874 {
1875 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1876 return( ret );
1877 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001878 }
1879
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001880 /*
1881 * 38 . 38 session id length
1882 * 39 . 38+n session id
1883 * 39+n . 40+n chosen ciphersuite
1884 * 41+n . 41+n chosen compression alg.
1885 * 42+n . 43+n extensions length
1886 * 44+n . 43+n+m extensions
1887 */
1888 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001889 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1890 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001891
1892 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1893 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1894 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001895 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001896
Paul Bakker48916f92012-09-16 19:57:18 +00001897 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1898 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1899 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001900
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001901 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
1902 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001903 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001904 ssl->session_negotiate->compression ) );
1905
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001906 /*
1907 * First write extensions, then the total length
1908 */
1909 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1910 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001911
Paul Bakker05decb22013-08-15 13:33:48 +02001912#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001913 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1914 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001915#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001916
Paul Bakker1f2bc622013-08-15 13:45:55 +02001917#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001918 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1919 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001920#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001921
Paul Bakkera503a632013-08-14 13:48:06 +02001922#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001923 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1924 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001925#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001926
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001927#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001928 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1929 ext_len += olen;
1930#endif
1931
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001932#if defined(POLARSSL_SSL_ALPN)
1933 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
1934 ext_len += olen;
1935#endif
1936
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001937 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001938
Paul Bakkera7036632014-04-30 10:15:38 +02001939 if( ext_len > 0 )
1940 {
1941 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1942 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1943 p += ext_len;
1944 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001945
1946 ssl->out_msglen = p - buf;
1947 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1948 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1949
1950 ret = ssl_write_record( ssl );
1951
1952 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1953
1954 return( ret );
1955}
1956
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001957#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1958 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001959 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1960 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001961static int ssl_write_certificate_request( ssl_context *ssl )
1962{
Paul Bakkered27a042013-04-18 22:46:23 +02001963 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001964
1965 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1966
1967 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01001968 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001969 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1970 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001971 {
1972 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1973 ssl->state++;
1974 return( 0 );
1975 }
1976
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001977 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1978 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001979}
1980#else
1981static int ssl_write_certificate_request( ssl_context *ssl )
1982{
1983 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1984 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001985 size_t dn_size, total_dn_size; /* excluding length bytes */
1986 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00001987 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001988 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001989
1990 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1991
1992 ssl->state++;
1993
Paul Bakkerfbb17802013-04-17 19:10:21 +02001994 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 ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001996 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001997 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001998 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001999 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002000 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002001 return( 0 );
2002 }
2003
2004 /*
2005 * 0 . 0 handshake type
2006 * 1 . 3 handshake length
2007 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002008 * 5 .. m-1 cert types
2009 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002010 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002011 * n .. n+1 length of all DNs
2012 * n+2 .. n+3 length of DN 1
2013 * n+4 .. ... Distinguished Name #1
2014 * ... .. ... length of DN 2, etc.
2015 */
2016 buf = ssl->out_msg;
2017 p = buf + 4;
2018
2019 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002020 * Supported certificate types
2021 *
2022 * ClientCertificateType certificate_types<1..2^8-1>;
2023 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002024 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002025 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002026
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002027#if defined(POLARSSL_RSA_C)
2028 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2029#endif
2030#if defined(POLARSSL_ECDSA_C)
2031 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2032#endif
2033
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002034 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002035 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002036
Paul Bakker577e0062013-08-28 11:57:20 +02002037 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002038#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002039 /*
2040 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002041 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002042 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2043 *
2044 * struct {
2045 * HashAlgorithm hash;
2046 * SignatureAlgorithm signature;
2047 * } SignatureAndHashAlgorithm;
2048 *
2049 * enum { (255) } HashAlgorithm;
2050 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002051 */
Paul Bakker21dca692013-01-03 11:41:08 +01002052 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002053 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002054 /*
2055 * Only use current running hash algorithm that is already required
2056 * for requested ciphersuite.
2057 */
Paul Bakker926af752012-11-23 13:38:07 +01002058 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2059
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002060 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2061 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002062 {
2063 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2064 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002065
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002066 /*
2067 * Supported signature algorithms
2068 */
2069#if defined(POLARSSL_RSA_C)
2070 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2071 p[2 + sa_len++] = SSL_SIG_RSA;
2072#endif
2073#if defined(POLARSSL_ECDSA_C)
2074 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2075 p[2 + sa_len++] = SSL_SIG_ECDSA;
2076#endif
Paul Bakker926af752012-11-23 13:38:07 +01002077
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002078 p[0] = (unsigned char)( sa_len >> 8 );
2079 p[1] = (unsigned char)( sa_len );
2080 sa_len += 2;
2081 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002082 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002083#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002084
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002085 /*
2086 * DistinguishedName certificate_authorities<0..2^16-1>;
2087 * opaque DistinguishedName<1..2^16-1>;
2088 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002089 p += 2;
2090 crt = ssl->ca_chain;
2091
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002092 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002093 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 {
2095 if( p - buf > 4096 )
2096 break;
2097
Paul Bakker926af752012-11-23 13:38:07 +01002098 dn_size = crt->subject_raw.len;
2099 *p++ = (unsigned char)( dn_size >> 8 );
2100 *p++ = (unsigned char)( dn_size );
2101 memcpy( p, crt->subject_raw.p, dn_size );
2102 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002103
Paul Bakker926af752012-11-23 13:38:07 +01002104 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2105
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002106 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002107 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 }
2109
Paul Bakker926af752012-11-23 13:38:07 +01002110 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002111 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2112 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002113 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2114 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002115
2116 ret = ssl_write_record( ssl );
2117
2118 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2119
2120 return( ret );
2121}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002122#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2123 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002124 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2125 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002126
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002127#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2128 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2129static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2130{
2131 int ret;
2132
2133 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2134 {
2135 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2136 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2137 }
2138
2139 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2140 pk_ec( *ssl_own_key( ssl ) ),
2141 POLARSSL_ECDH_OURS ) ) != 0 )
2142 {
2143 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2144 return( ret );
2145 }
2146
2147 return( 0 );
2148}
2149#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2150 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2151
Paul Bakker41c83d32013-03-20 14:39:14 +01002152static int ssl_write_server_key_exchange( ssl_context *ssl )
2153{
Paul Bakker23986e52011-04-24 08:57:21 +00002154 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002155 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002156 const ssl_ciphersuite_t *ciphersuite_info =
2157 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002158
2159#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2160 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2161 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002162 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002163 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002164 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002165 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002166 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002167 ((void) dig_signed);
2168 ((void) dig_signed_len);
2169#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002170
Paul Bakker5121ce52009-01-03 21:22:43 +00002171 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2172
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002173#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2174 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2175 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002176 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2177 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2178 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002179 {
2180 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2181 ssl->state++;
2182 return( 0 );
2183 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002184#endif
2185
2186#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2187 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2188 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2189 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2190 {
2191 ssl_get_ecdh_params_from_cert( ssl );
2192
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002193 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002194 ssl->state++;
2195 return( 0 );
2196 }
2197#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002198
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002199#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2200 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2201 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2202 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002203 {
2204 /* TODO: Support identity hints */
2205 *(p++) = 0x00;
2206 *(p++) = 0x00;
2207
2208 n += 2;
2209 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002210#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2211 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002212
2213#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2214 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2215 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2216 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002217 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002218 /*
2219 * Ephemeral DH parameters:
2220 *
2221 * struct {
2222 * opaque dh_p<1..2^16-1>;
2223 * opaque dh_g<1..2^16-1>;
2224 * opaque dh_Ys<1..2^16-1>;
2225 * } ServerDHParams;
2226 */
2227 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2228 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2229 {
2230 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2231 return( ret );
2232 }
Paul Bakker48916f92012-09-16 19:57:18 +00002233
Paul Bakker41c83d32013-03-20 14:39:14 +01002234 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002235 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2236 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002237 {
2238 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2239 return( ret );
2240 }
2241
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002242 dig_signed = p;
2243 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002244
2245 p += len;
2246 n += len;
2247
Paul Bakker41c83d32013-03-20 14:39:14 +01002248 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2249 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2250 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2251 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2252 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002253#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2254 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002255
Gergely Budai987bfb52014-01-19 21:48:42 +01002256#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002257 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002258 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2259 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002260 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002261 /*
2262 * Ephemeral ECDH parameters:
2263 *
2264 * struct {
2265 * ECParameters curve_params;
2266 * ECPoint public;
2267 * } ServerECDHParams;
2268 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002269 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002270#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002271 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002272
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002273 /* Match our preference list against the offered curves */
2274 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2275 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2276 if( (*curve)->grp_id == *gid )
2277 goto curve_matching_done;
2278
2279curve_matching_done:
2280#else
2281 curve = ssl->handshake->curves;
2282#endif
2283
2284 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002285 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002286 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2287 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002288 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002289
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002290 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002291
Paul Bakker41c83d32013-03-20 14:39:14 +01002292 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002293 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002294 {
2295 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2296 return( ret );
2297 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002298
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002299 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2300 p, SSL_MAX_CONTENT_LEN - n,
2301 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002302 {
2303 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2304 return( ret );
2305 }
2306
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002307 dig_signed = p;
2308 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002309
2310 p += len;
2311 n += len;
2312
Paul Bakker41c83d32013-03-20 14:39:14 +01002313 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2314 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002315#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002316
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002317#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002318 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2319 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002320 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002321 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2322 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002323 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002324 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002325 unsigned int hashlen = 0;
2326 unsigned char hash[64];
2327 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002328
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002329 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002330 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2331 */
Paul Bakker577e0062013-08-28 11:57:20 +02002332#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002333 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2334 {
2335 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2336
2337 if( md_alg == POLARSSL_MD_NONE )
2338 {
2339 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002340 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002341 }
2342 }
Paul Bakker577e0062013-08-28 11:57:20 +02002343 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002344#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002345#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2346 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002347 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002348 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2349 {
2350 md_alg = POLARSSL_MD_SHA1;
2351 }
2352 else
Paul Bakker577e0062013-08-28 11:57:20 +02002353#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002354 {
2355 md_alg = POLARSSL_MD_NONE;
2356 }
2357
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002358 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002359 * Compute the hash to be signed
2360 */
Paul Bakker577e0062013-08-28 11:57:20 +02002361#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2362 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002363 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002364 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002365 md5_context md5;
2366 sha1_context sha1;
2367
2368 /*
2369 * digitally-signed struct {
2370 * opaque md5_hash[16];
2371 * opaque sha_hash[20];
2372 * };
2373 *
2374 * md5_hash
2375 * MD5(ClientHello.random + ServerHello.random
2376 * + ServerParams);
2377 * sha_hash
2378 * SHA(ClientHello.random + ServerHello.random
2379 * + ServerParams);
2380 */
2381 md5_starts( &md5 );
2382 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002383 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002384 md5_finish( &md5, hash );
2385
2386 sha1_starts( &sha1 );
2387 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002388 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002389 sha1_finish( &sha1, hash + 16 );
2390
2391 hashlen = 36;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002392 }
2393 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002394#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2395 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002396#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2397 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002398 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002399 {
2400 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02002401 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002402
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002403 /* Info from md_alg will be used instead */
2404 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002405
2406 /*
2407 * digitally-signed struct {
2408 * opaque client_random[32];
2409 * opaque server_random[32];
2410 * ServerDHParams params;
2411 * };
2412 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002413 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002414 {
2415 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2416 return( ret );
2417 }
2418
2419 md_starts( &ctx );
2420 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002421 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002422 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02002423
2424 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
2425 {
2426 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
2427 return( ret );
2428 }
2429
Paul Bakker23f36802012-09-28 14:15:14 +00002430 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002431 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002432#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2433 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002434 {
2435 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002436 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002437 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002438
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002439 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2440 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002441
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002442 /*
2443 * Make the signature
2444 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002445 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002446 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002447 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2448 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002449 }
Paul Bakker23f36802012-09-28 14:15:14 +00002450
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002451#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002452 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2453 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002454 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002455 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002456
2457 n += 2;
2458 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002459#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002460
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002461 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002462 p + 2 , &signature_len,
2463 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002464 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002465 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002466 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002467 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002468
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002469 *(p++) = (unsigned char)( signature_len >> 8 );
2470 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002471 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002472
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002473 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002474
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002475 p += signature_len;
2476 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002477 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002478#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002479 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2480 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002481
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002482 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002483 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2484 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2485
2486 ssl->state++;
2487
2488 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2489 {
2490 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2491 return( ret );
2492 }
2493
2494 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2495
2496 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002497}
2498
2499static int ssl_write_server_hello_done( ssl_context *ssl )
2500{
2501 int ret;
2502
2503 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2504
2505 ssl->out_msglen = 4;
2506 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2507 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2508
2509 ssl->state++;
2510
2511 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2512 {
2513 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2514 return( ret );
2515 }
2516
2517 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2518
2519 return( 0 );
2520}
2521
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002522#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2523 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2524static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2525 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002526{
2527 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002528 size_t n;
2529
2530 /*
2531 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2532 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002533 if( *p + 2 > end )
2534 {
2535 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2536 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2537 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002538
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002539 n = ( (*p)[0] << 8 ) | (*p)[1];
2540 *p += 2;
2541
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002542 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002543 {
2544 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2545 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2546 }
2547
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002548 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002549 {
2550 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2551 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2552 }
2553
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002554 *p += n;
2555
Paul Bakker70df2fb2013-04-17 17:19:09 +02002556 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2557
Paul Bakker70df2fb2013-04-17 17:19:09 +02002558 return( ret );
2559}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002560#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2561 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002562
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002563#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2564 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002565static int ssl_parse_encrypted_pms( ssl_context *ssl,
2566 const unsigned char *p,
2567 const unsigned char *end,
2568 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002569{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002570 int ret;
2571 size_t len = pk_get_len( ssl_own_key( ssl ) );
2572 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002573
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002574 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002575 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002576 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002577 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2578 }
2579
2580 /*
2581 * Decrypt the premaster using own private RSA key
2582 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002583#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2584 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002585 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2586 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002587 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2588 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002589 {
2590 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2591 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2592 }
2593 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002594#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002595
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002596 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002597 {
2598 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2599 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2600 }
2601
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002602 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2603 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002604 sizeof( ssl->handshake->premaster ) - pms_offset,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002605 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002606
2607 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002608 pms[0] != ssl->handshake->max_major_ver ||
2609 pms[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002610 {
2611 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2612
2613 /*
2614 * Protection against Bleichenbacher's attack:
2615 * invalid PKCS#1 v1.5 padding must not cause
2616 * the connection to end immediately; instead,
2617 * send a bad_record_mac later in the handshake.
2618 */
2619 ssl->handshake->pmslen = 48;
2620
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002621 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002622 if( ret != 0 )
2623 return( ret );
2624 }
2625
2626 return( ret );
2627}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002628#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2629 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002630
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002631#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002632static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2633 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002634{
Paul Bakker6db455e2013-09-18 17:29:31 +02002635 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002636 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002637
Paul Bakker6db455e2013-09-18 17:29:31 +02002638 if( ssl->f_psk == NULL &&
2639 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2640 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002641 {
2642 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2643 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2644 }
2645
2646 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002647 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002648 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002649 if( *p + 2 > end )
2650 {
2651 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2652 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2653 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002654
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002655 n = ( (*p)[0] << 8 ) | (*p)[1];
2656 *p += 2;
2657
2658 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002659 {
2660 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2661 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2662 }
2663
Paul Bakker6db455e2013-09-18 17:29:31 +02002664 if( ssl->f_psk != NULL )
2665 {
2666 if( ( ret != ssl->f_psk( ssl->p_psk, ssl, *p, n ) ) != 0 )
2667 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2668 }
2669
2670 if( ret == 0 )
2671 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002672 /* Identity is not a big secret since clients send it in the clear,
2673 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02002674 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002675 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002676 {
2677 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2678 }
2679 }
2680
2681 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002682 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002683 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002684 if( ( ret = ssl_send_alert_message( ssl,
2685 SSL_ALERT_LEVEL_FATAL,
2686 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2687 {
2688 return( ret );
2689 }
2690
2691 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002692 }
2693
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002694 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002695 ret = 0;
2696
Paul Bakkerfbb17802013-04-17 19:10:21 +02002697 return( ret );
2698}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002699#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002700
Paul Bakker5121ce52009-01-03 21:22:43 +00002701static int ssl_parse_client_key_exchange( ssl_context *ssl )
2702{
Paul Bakker23986e52011-04-24 08:57:21 +00002703 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002704 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002705
Paul Bakker41c83d32013-03-20 14:39:14 +01002706 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002707
2708 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2709
2710 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2711 {
2712 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2713 return( ret );
2714 }
2715
2716 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2717 {
2718 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002719 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002720 }
2721
2722 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2723 {
2724 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002725 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002726 }
2727
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002728#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002729 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002730 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002731 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002732 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002733
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002734 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002735 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002736 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2737 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002738 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002739
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002740 if( p != end )
2741 {
2742 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2743 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2744 }
2745
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002746 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002747
2748 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2749 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002750 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002751 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002752 {
2753 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2754 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2755 }
2756
2757 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002758 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002759 else
2760#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002761#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002762 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2763 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2764 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002765 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002766 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2767 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2768 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002769 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002770 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002771 ssl->in_msg + 4, ssl->in_hslen - 4 ) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002772 {
2773 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2774 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2775 }
2776
2777 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2778
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002779 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2780 &ssl->handshake->pmslen,
2781 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002782 POLARSSL_MPI_MAX_SIZE,
2783 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002784 {
2785 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2786 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2787 }
2788
2789 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002790 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002791 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002792#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002793 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2794 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2795 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002796#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2797 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002798 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002799 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002800 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002801
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002802 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002803 {
2804 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2805 return( ret );
2806 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002807
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002808 if( p != end )
2809 {
2810 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2811 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2812 }
2813
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002814 if( ( ret = ssl_psk_derive_premaster( ssl,
2815 ciphersuite_info->key_exchange ) ) != 0 )
2816 {
2817 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2818 return( ret );
2819 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002820 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002821 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002822#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002823#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2824 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2825 {
2826 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é-Gonnard0fae60b2013-10-14 17:39:48 +02002828
2829 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2830 {
2831 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2832 return( ret );
2833 }
2834
2835 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
2836 {
2837 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
2838 return( ret );
2839 }
2840
2841 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 }
2847 }
2848 else
2849#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002850#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2851 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2852 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002853 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;
Paul Bakker48f7a5d2013-04-19 14:30:58 +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 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2862 {
2863 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2864 return( ret );
2865 }
2866
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002867 if( p != end )
2868 {
2869 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2870 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2871 }
2872
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002873 if( ( ret = ssl_psk_derive_premaster( ssl,
2874 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002875 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002876 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2877 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002878 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002879 }
2880 else
2881#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002882#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2883 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2884 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002885 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002886 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002887
2888 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2889 {
2890 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2891 return( ret );
2892 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002893
2894 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2895 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002896 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002897 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2898 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002899 }
2900
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002901 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2902
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002903 if( ( ret = ssl_psk_derive_premaster( ssl,
2904 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002905 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002906 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002907 return( ret );
2908 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002909 }
2910 else
2911#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002912#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2913 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002914 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002915 if( ( ret = ssl_parse_encrypted_pms( ssl,
2916 ssl->in_msg + 4,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002917 ssl->in_msg + ssl->in_hslen,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002918 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002919 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002920 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002921 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002922 }
2923 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002924 else
2925#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2926 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002927 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002928 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002929 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002930
Paul Bakkerff60ee62010-03-16 21:09:09 +00002931 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2932 {
2933 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2934 return( ret );
2935 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002936
Paul Bakker5121ce52009-01-03 21:22:43 +00002937 ssl->state++;
2938
2939 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2940
2941 return( 0 );
2942}
2943
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002944#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2945 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002946 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2947 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002948static int ssl_parse_certificate_verify( ssl_context *ssl )
2949{
Paul Bakkerfbb17802013-04-17 19:10:21 +02002950 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002951
2952 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2953
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002954 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002955 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002956 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002957 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002958 {
2959 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2960 ssl->state++;
2961 return( 0 );
2962 }
2963
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002964 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2965 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002966}
2967#else
2968static int ssl_parse_certificate_verify( ssl_context *ssl )
2969{
2970 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002971 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002972 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002973 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002974 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02002975#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002976 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02002977#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002978 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002979 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2980
2981 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2982
2983 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002984 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002985 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002986 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2987 {
2988 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2989 ssl->state++;
2990 return( 0 );
2991 }
2992
Paul Bakkered27a042013-04-18 22:46:23 +02002993 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002994 {
2995 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2996 ssl->state++;
2997 return( 0 );
2998 }
2999
Paul Bakker48916f92012-09-16 19:57:18 +00003000 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003001
3002 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3003 {
3004 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3005 return( ret );
3006 }
3007
3008 ssl->state++;
3009
3010 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3011 {
3012 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003013 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003014 }
3015
3016 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
3017 {
3018 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003019 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003020 }
3021
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003022 /*
3023 * 0 . 0 handshake type
3024 * 1 . 3 handshake length
3025 * 4 . 5 sig alg (TLS 1.2 only)
3026 * 4+n . 5+n signature length (n = sa_len)
3027 * 6+n . 6+n+m signature (m = sig_len)
3028 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003029
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003030#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3031 defined(POLARSSL_SSL_PROTO_TLS1_1)
3032 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003033 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003034 sa_len = 0;
3035
Paul Bakkerc70b9822013-04-07 22:00:46 +02003036 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003037 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003038
3039 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3040 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3041 POLARSSL_PK_ECDSA ) )
3042 {
3043 hash_start += 16;
3044 hashlen -= 16;
3045 md_alg = POLARSSL_MD_SHA1;
3046 }
Paul Bakker926af752012-11-23 13:38:07 +01003047 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003048 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003049#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3050 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003051#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3052 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003053 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003054 sa_len = 2;
3055
Paul Bakker5121ce52009-01-03 21:22:43 +00003056 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003057 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003058 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003059 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003060 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003061 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3062 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003063 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3064 }
3065
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003066 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003067
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003068 /* Info from md_alg will be used instead */
3069 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003070
3071 /*
3072 * Signature
3073 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003074 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
3075 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003076 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003077 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3078 " for verify message" ) );
3079 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003080 }
3081
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003082 /*
3083 * Check the certificate's key type matches the signature alg
3084 */
3085 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3086 {
3087 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3088 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3089 }
Paul Bakker577e0062013-08-28 11:57:20 +02003090 }
3091 else
3092#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3093 {
3094 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003095 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003096 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003097
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003098 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01003099
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003100 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003101 {
3102 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003103 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003104 }
3105
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003106 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003107 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003108 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003109 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003110 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003111 return( ret );
3112 }
3113
3114 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3115
Paul Bakkered27a042013-04-18 22:46:23 +02003116 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003117}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003118#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3119 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3120 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003121
Paul Bakkera503a632013-08-14 13:48:06 +02003122#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003123static int ssl_write_new_session_ticket( ssl_context *ssl )
3124{
3125 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003126 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003127 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003128
3129 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3130
3131 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3132 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3133
3134 /*
3135 * struct {
3136 * uint32 ticket_lifetime_hint;
3137 * opaque ticket<0..2^16-1>;
3138 * } NewSessionTicket;
3139 *
3140 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3141 * 8 . 9 ticket_len (n)
3142 * 10 . 9+n ticket content
3143 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003144
3145 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3146 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3147 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3148 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003149
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003150 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3151 {
3152 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3153 tlen = 0;
3154 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003155
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003156 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3157 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003158
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003159 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003160
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003161 /*
3162 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3163 * ChangeCipherSpec share the same state.
3164 */
3165 ssl->handshake->new_session_ticket = 0;
3166
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003167 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3168 {
3169 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3170 return( ret );
3171 }
3172
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003173 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3174
3175 return( 0 );
3176}
Paul Bakkera503a632013-08-14 13:48:06 +02003177#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003178
Paul Bakker5121ce52009-01-03 21:22:43 +00003179/*
Paul Bakker1961b702013-01-25 14:49:24 +01003180 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003181 */
Paul Bakker1961b702013-01-25 14:49:24 +01003182int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003183{
3184 int ret = 0;
3185
Paul Bakker1961b702013-01-25 14:49:24 +01003186 if( ssl->state == SSL_HANDSHAKE_OVER )
3187 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003188
Paul Bakker1961b702013-01-25 14:49:24 +01003189 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3190
3191 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3192 return( ret );
3193
3194 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003195 {
Paul Bakker1961b702013-01-25 14:49:24 +01003196 case SSL_HELLO_REQUEST:
3197 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003198 break;
3199
Paul Bakker1961b702013-01-25 14:49:24 +01003200 /*
3201 * <== ClientHello
3202 */
3203 case SSL_CLIENT_HELLO:
3204 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003205 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003206
3207 /*
3208 * ==> ServerHello
3209 * Certificate
3210 * ( ServerKeyExchange )
3211 * ( CertificateRequest )
3212 * ServerHelloDone
3213 */
3214 case SSL_SERVER_HELLO:
3215 ret = ssl_write_server_hello( ssl );
3216 break;
3217
3218 case SSL_SERVER_CERTIFICATE:
3219 ret = ssl_write_certificate( ssl );
3220 break;
3221
3222 case SSL_SERVER_KEY_EXCHANGE:
3223 ret = ssl_write_server_key_exchange( ssl );
3224 break;
3225
3226 case SSL_CERTIFICATE_REQUEST:
3227 ret = ssl_write_certificate_request( ssl );
3228 break;
3229
3230 case SSL_SERVER_HELLO_DONE:
3231 ret = ssl_write_server_hello_done( ssl );
3232 break;
3233
3234 /*
3235 * <== ( Certificate/Alert )
3236 * ClientKeyExchange
3237 * ( CertificateVerify )
3238 * ChangeCipherSpec
3239 * Finished
3240 */
3241 case SSL_CLIENT_CERTIFICATE:
3242 ret = ssl_parse_certificate( ssl );
3243 break;
3244
3245 case SSL_CLIENT_KEY_EXCHANGE:
3246 ret = ssl_parse_client_key_exchange( ssl );
3247 break;
3248
3249 case SSL_CERTIFICATE_VERIFY:
3250 ret = ssl_parse_certificate_verify( ssl );
3251 break;
3252
3253 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3254 ret = ssl_parse_change_cipher_spec( ssl );
3255 break;
3256
3257 case SSL_CLIENT_FINISHED:
3258 ret = ssl_parse_finished( ssl );
3259 break;
3260
3261 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003262 * ==> ( NewSessionTicket )
3263 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003264 * Finished
3265 */
3266 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003267#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003268 if( ssl->handshake->new_session_ticket != 0 )
3269 ret = ssl_write_new_session_ticket( ssl );
3270 else
Paul Bakkera503a632013-08-14 13:48:06 +02003271#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003272 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003273 break;
3274
3275 case SSL_SERVER_FINISHED:
3276 ret = ssl_write_finished( ssl );
3277 break;
3278
3279 case SSL_FLUSH_BUFFERS:
3280 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3281 ssl->state = SSL_HANDSHAKE_WRAPUP;
3282 break;
3283
3284 case SSL_HANDSHAKE_WRAPUP:
3285 ssl_handshake_wrapup( ssl );
3286 break;
3287
3288 default:
3289 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3290 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003291 }
3292
Paul Bakker5121ce52009-01-03 21:22:43 +00003293 return( ret );
3294}
Paul Bakker9af723c2014-05-01 13:03:14 +02003295#endif /* POLARSSL_SSL_SRV_C */