blob: 0fd67096d6e64eb04be0edc50ff9c144bb27d84a [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 server-side functions
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Paul Bakker40e46942009-01-03 21:51:57 +000029#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Paul Bakker40e46942009-01-03 21:51:57 +000031#include "polarssl/debug.h"
32#include "polarssl/ssl.h"
Rich Evans00ab4702015-02-06 13:43:58 +000033
34#include <string.h>
35
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
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <stdlib.h>
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020044#define polarssl_malloc malloc
45#define polarssl_free free
46#endif
47
Paul Bakkerfa9b1002013-07-03 15:31:03 +020048#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000049#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020050#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000051
Paul Bakkera503a632013-08-14 13:48:06 +020052#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker34617722014-06-13 17:20:13 +020053/* Implementation that should never be optimized out by the compiler */
54static void polarssl_zeroize( void *v, size_t n ) {
55 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
56}
57
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020058/*
59 * Serialize a session in the following format:
60 * 0 . n-1 session structure, n = sizeof(ssl_session)
61 * n . n+2 peer_cert length = m (0 if no certificate)
62 * n+3 . n+2+m peer cert ASN.1
63 *
64 * Assumes ticket is NULL (always true on server side).
65 */
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020066static int ssl_save_session( const ssl_session *session,
67 unsigned char *buf, size_t buf_len,
68 size_t *olen )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020069{
70 unsigned char *p = buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020071 size_t left = buf_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020072#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020073 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020075
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020076 if( left < sizeof( ssl_session ) )
77 return( -1 );
78
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020079 memcpy( p, session, sizeof( ssl_session ) );
80 p += sizeof( ssl_session );
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020081 left -= sizeof( ssl_session );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020082
Paul Bakker7c6b2c32013-09-16 13:49:26 +020083#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020084 if( session->peer_cert == NULL )
85 cert_len = 0;
86 else
87 cert_len = session->peer_cert->raw.len;
88
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020089 if( left < 3 + cert_len )
90 return( -1 );
91
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020092 *p++ = (unsigned char)( cert_len >> 16 & 0xFF );
93 *p++ = (unsigned char)( cert_len >> 8 & 0xFF );
94 *p++ = (unsigned char)( cert_len & 0xFF );
95
96 if( session->peer_cert != NULL )
97 memcpy( p, session->peer_cert->raw.p, cert_len );
98
99 p += cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200100#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200101
102 *olen = p - buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200103
104 return( 0 );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200105}
106
107/*
108 * Unserialise session, see ssl_save_session()
109 */
110static int ssl_load_session( ssl_session *session,
111 const unsigned char *buf, size_t len )
112{
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200113 const unsigned char *p = buf;
114 const unsigned char * const end = buf + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200115#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200116 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200117#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200118
119 if( p + sizeof( ssl_session ) > end )
120 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
121
122 memcpy( session, p, sizeof( ssl_session ) );
123 p += sizeof( ssl_session );
124
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200125#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200126 if( p + 3 > end )
127 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
128
129 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
130 p += 3;
131
132 if( cert_len == 0 )
133 {
134 session->peer_cert = NULL;
135 }
136 else
137 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200138 int ret;
139
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200140 if( p + cert_len > end )
141 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
142
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200143 session->peer_cert = polarssl_malloc( sizeof( x509_crt ) );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200144
145 if( session->peer_cert == NULL )
146 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
147
Paul Bakkerb6b09562013-09-18 14:17:41 +0200148 x509_crt_init( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200149
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200150 if( ( ret = x509_crt_parse_der( session->peer_cert,
151 p, cert_len ) ) != 0 )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200152 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200153 x509_crt_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200154 polarssl_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200155 session->peer_cert = NULL;
156 return( ret );
157 }
158
159 p += cert_len;
160 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200161#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200162
163 if( p != end )
164 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
165
166 return( 0 );
167}
168
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200169/*
170 * Create session ticket, secured as recommended in RFC 5077 section 4:
171 *
172 * struct {
173 * opaque key_name[16];
174 * opaque iv[16];
175 * opaque encrypted_state<0..2^16-1>;
176 * opaque mac[32];
177 * } ticket;
178 *
179 * (the internal state structure differs, however).
180 */
181static int ssl_write_ticket( ssl_context *ssl, size_t *tlen )
182{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200183 int ret;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200184 unsigned char * const start = ssl->out_msg + 10;
185 unsigned char *p = start;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200186 unsigned char *state;
187 unsigned char iv[16];
188 size_t clear_len, enc_len, pad_len, i;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200189
Manuel Pégourié-Gonnard0a201712013-08-23 16:25:16 +0200190 *tlen = 0;
191
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200192 if( ssl->ticket_keys == NULL )
193 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
194
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200195 /* Write key name */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200196 memcpy( p, ssl->ticket_keys->key_name, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200197 p += 16;
198
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200199 /* Generate and write IV (with a copy for aes_crypt) */
200 if( ( ret = ssl->f_rng( ssl->p_rng, p, 16 ) ) != 0 )
201 return( ret );
202 memcpy( iv, p, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200203 p += 16;
204
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200205 /*
206 * Dump session state
207 *
208 * After the session state itself, we still need room for 16 bytes of
209 * padding and 32 bytes of MAC, so there's only so much room left
210 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200211 state = p + 2;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200212 if( ssl_save_session( ssl->session_negotiate, state,
Paul Bakker66d5d072014-06-17 16:39:18 +0200213 SSL_MAX_CONTENT_LEN - ( state - ssl->out_ctr ) - 48,
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200214 &clear_len ) != 0 )
215 {
216 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
217 }
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200218 SSL_DEBUG_BUF( 3, "session ticket cleartext", state, clear_len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200219
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200220 /* Apply PKCS padding */
221 pad_len = 16 - clear_len % 16;
222 enc_len = clear_len + pad_len;
223 for( i = clear_len; i < enc_len; i++ )
224 state[i] = (unsigned char) pad_len;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200225
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200226 /* Encrypt */
227 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->enc, AES_ENCRYPT,
228 enc_len, iv, state, state ) ) != 0 )
229 {
230 return( ret );
231 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200232
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200233 /* Write length */
234 *p++ = (unsigned char)( ( enc_len >> 8 ) & 0xFF );
235 *p++ = (unsigned char)( ( enc_len ) & 0xFF );
236 p = state + enc_len;
237
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200238 /* Compute and write MAC( key_name + iv + enc_state_len + enc_state ) */
239 sha256_hmac( ssl->ticket_keys->mac_key, 16, start, p - start, p, 0 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200240 p += 32;
241
242 *tlen = p - start;
243
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200244 SSL_DEBUG_BUF( 3, "session ticket structure", start, *tlen );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200245
246 return( 0 );
247}
248
249/*
250 * Load session ticket (see ssl_write_ticket for structure)
251 */
252static int ssl_parse_ticket( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200253 unsigned char *buf,
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200254 size_t len )
255{
256 int ret;
257 ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200258 unsigned char *key_name = buf;
259 unsigned char *iv = buf + 16;
260 unsigned char *enc_len_p = iv + 16;
261 unsigned char *ticket = enc_len_p + 2;
262 unsigned char *mac;
Manuel Pégourié-Gonnard34ced2d2013-09-20 11:37:39 +0200263 unsigned char computed_mac[32];
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200264 size_t enc_len, clear_len, i;
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100265 unsigned char pad_len, diff;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200266
267 SSL_DEBUG_BUF( 3, "session ticket structure", buf, len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200268
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200269 if( len < 34 || ssl->ticket_keys == NULL )
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200270 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
271
272 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
273 mac = ticket + enc_len;
274
275 if( len != enc_len + 66 )
276 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
277
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100278 /* Check name, in constant time though it's not a big secret */
279 diff = 0;
280 for( i = 0; i < 16; i++ )
281 diff |= key_name[i] ^ ssl->ticket_keys->key_name[i];
282 /* don't return yet, check the MAC anyway */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200283
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100284 /* Check mac, with constant-time buffer comparison */
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200285 sha256_hmac( ssl->ticket_keys->mac_key, 16, buf, len - 32,
286 computed_mac, 0 );
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100287
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200288 for( i = 0; i < 32; i++ )
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100289 diff |= mac[i] ^ computed_mac[i];
290
291 /* Now return if ticket is not authentic, since we want to avoid
292 * decrypting arbitrary attacker-chosen data */
293 if( diff != 0 )
294 return( POLARSSL_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200295
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200296 /* Decrypt */
297 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->dec, AES_DECRYPT,
298 enc_len, iv, ticket, ticket ) ) != 0 )
299 {
300 return( ret );
301 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200302
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200303 /* Check PKCS padding */
304 pad_len = ticket[enc_len - 1];
305
306 ret = 0;
307 for( i = 2; i < pad_len; i++ )
308 if( ticket[enc_len - i] != pad_len )
309 ret = POLARSSL_ERR_SSL_BAD_INPUT_DATA;
310 if( ret != 0 )
311 return( ret );
312
313 clear_len = enc_len - pad_len;
314
315 SSL_DEBUG_BUF( 3, "session ticket cleartext", ticket, clear_len );
316
317 /* Actually load session */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200318 if( ( ret = ssl_load_session( &session, ticket, clear_len ) ) != 0 )
319 {
320 SSL_DEBUG_MSG( 1, ( "failed to parse ticket content" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100321 ssl_session_free( &session );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200322 return( ret );
323 }
324
Paul Bakker606b4ba2013-08-14 16:52:14 +0200325#if defined(POLARSSL_HAVE_TIME)
326 /* Check if still valid */
327 if( (int) ( time( NULL) - session.start ) > ssl->ticket_lifetime )
328 {
329 SSL_DEBUG_MSG( 1, ( "session ticket expired" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100330 ssl_session_free( &session );
Paul Bakker606b4ba2013-08-14 16:52:14 +0200331 return( POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED );
332 }
333#endif
334
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200335 /*
336 * Keep the session ID sent by the client, since we MUST send it back to
337 * inform him we're accepting the ticket (RFC 5077 section 3.4)
338 */
339 session.length = ssl->session_negotiate->length;
340 memcpy( &session.id, ssl->session_negotiate->id, session.length );
341
342 ssl_session_free( ssl->session_negotiate );
343 memcpy( ssl->session_negotiate, &session, sizeof( ssl_session ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200344
345 /* Zeroize instead of free as we copied the content */
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
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100432#if defined(POLARSSL_SSL_RENEGOTIATION)
433 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
434 {
435 /* Check verify-data in constant-time. The length OTOH is no secret */
436 if( len != 1 + ssl->verify_data_len ||
437 buf[0] != ssl->verify_data_len ||
438 safer_memcmp( buf + 1, ssl->peer_verify_data,
439 ssl->verify_data_len ) != 0 )
440 {
441 SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
442
443 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
444 return( ret );
445
446 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
447 }
448 }
449 else
450#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +0000451 {
452 if( len != 1 || buf[0] != 0x0 )
453 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100454 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
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 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
463 }
Paul Bakker48916f92012-09-16 19:57:18 +0000464
465 return( 0 );
466}
467
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100468#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
469 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakker23f36802012-09-28 14:15:14 +0000470static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
471 const unsigned char *buf,
472 size_t len )
473{
474 size_t sig_alg_list_size;
475 const unsigned char *p;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200476 const unsigned char *end = buf + len;
477 const int *md_cur;
478
Paul Bakker23f36802012-09-28 14:15:14 +0000479
480 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
481 if( sig_alg_list_size + 2 != len ||
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200482 sig_alg_list_size % 2 != 0 )
Paul Bakker23f36802012-09-28 14:15:14 +0000483 {
484 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
485 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
486 }
487
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200488 /*
489 * For now, ignore the SignatureAlgorithm part and rely on offered
490 * ciphersuites only for that part. To be fixed later.
491 *
492 * So, just look at the HashAlgorithm part.
493 */
494 for( md_cur = md_list(); *md_cur != POLARSSL_MD_NONE; md_cur++ ) {
Simon Butcher14400c82016-01-02 00:08:13 +0000495#if !defined(POLARSSL_SSL_ENABLE_MD5_SIGNATURES)
496 /* Skip MD5 */
497 if( *md_cur == POLARSSL_MD_MD5 )
498 continue;
499#endif
500
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200501 for( p = buf + 2; p < end; p += 2 ) {
502 if( *md_cur == (int) ssl_md_alg_from_hash( p[0] ) ) {
503 ssl->handshake->sig_alg = p[0];
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200504 goto have_sig_alg;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200505 }
Paul Bakker23f36802012-09-28 14:15:14 +0000506 }
Paul Bakker23f36802012-09-28 14:15:14 +0000507 }
508
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200509 /* Some key echanges do not need signatures at all */
510 SSL_DEBUG_MSG( 3, ( "no signature_algorithm in common" ) );
511 return( 0 );
512
513have_sig_alg:
Paul Bakker23f36802012-09-28 14:15:14 +0000514 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
515 ssl->handshake->sig_alg ) );
516
517 return( 0 );
518}
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100519#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
520 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker23f36802012-09-28 14:15:14 +0000521
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200522#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200523static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
524 const unsigned char *buf,
525 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100526{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200527 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100528 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200529 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100530
531 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
532 if( list_size + 2 != len ||
533 list_size % 2 != 0 )
534 {
535 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
536 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
537 }
538
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200539 /* Should never happen unless client duplicates the extension */
540 if( ssl->handshake->curves != NULL )
541 {
542 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
543 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
544 }
545
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +0100546 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200547 * and leave room for a final 0 */
548 our_size = list_size / 2 + 1;
549 if( our_size > POLARSSL_ECP_DP_MAX )
550 our_size = POLARSSL_ECP_DP_MAX;
551
552 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
553 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
554
Paul Bakker9af723c2014-05-01 13:03:14 +0200555 /* explicit void pointer cast for buggy MS compiler */
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200556 memset( (void *) curves, 0, our_size * sizeof( *curves ) );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200557 ssl->handshake->curves = curves;
558
Paul Bakker41c83d32013-03-20 14:39:14 +0100559 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200560 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100561 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200562 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200563
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200564 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100565 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200566 *curves++ = curve_info;
567 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100568 }
569
570 list_size -= 2;
571 p += 2;
572 }
573
574 return( 0 );
575}
576
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200577static int ssl_parse_supported_point_formats( ssl_context *ssl,
578 const unsigned char *buf,
579 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100580{
581 size_t list_size;
582 const unsigned char *p;
583
584 list_size = buf[0];
585 if( list_size + 1 != len )
586 {
587 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
588 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
589 }
590
Manuel Pégourié-Gonnarda701d2f2015-09-16 11:32:18 +0200591 p = buf + 1;
Paul Bakker41c83d32013-03-20 14:39:14 +0100592 while( list_size > 0 )
593 {
594 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
595 p[0] == POLARSSL_ECP_PF_COMPRESSED )
596 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200597 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200598 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100599 return( 0 );
600 }
601
602 list_size--;
603 p++;
604 }
605
606 return( 0 );
607}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200608#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100609
Paul Bakker05decb22013-08-15 13:33:48 +0200610#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200611static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
612 const unsigned char *buf,
613 size_t len )
614{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200615 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200616 {
617 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
618 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
619 }
620
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200621 ssl->session_negotiate->mfl_code = buf[0];
622
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200623 return( 0 );
624}
Paul Bakker05decb22013-08-15 13:33:48 +0200625#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200626
Paul Bakker1f2bc622013-08-15 13:45:55 +0200627#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200628static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
629 const unsigned char *buf,
630 size_t len )
631{
632 if( len != 0 )
633 {
634 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
635 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
636 }
637
638 ((void) buf);
639
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100640 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
641 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200642
643 return( 0 );
644}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200645#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200646
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100647#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
648static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
649 const unsigned char *buf,
650 size_t len )
651{
652 if( len != 0 )
653 {
654 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
655 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
656 }
657
658 ((void) buf);
659
660 if( ssl->encrypt_then_mac == SSL_ETM_ENABLED &&
661 ssl->minor_ver != SSL_MINOR_VERSION_0 )
662 {
663 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
664 }
665
666 return( 0 );
667}
668#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
669
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200670#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
671static int ssl_parse_extended_ms_ext( ssl_context *ssl,
672 const unsigned char *buf,
673 size_t len )
674{
675 if( len != 0 )
676 {
677 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
678 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
679 }
680
681 ((void) buf);
682
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200683 if( ssl->extended_ms == SSL_EXTENDED_MS_ENABLED &&
684 ssl->minor_ver != SSL_MINOR_VERSION_0 )
685 {
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200686 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200687 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200688
689 return( 0 );
690}
691#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
692
Paul Bakkera503a632013-08-14 13:48:06 +0200693#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200694static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200695 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200696 size_t len )
697{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200698 int ret;
699
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200700 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
701 return( 0 );
702
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200703 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200704 ssl->handshake->new_session_ticket = 1;
705
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200706 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
707
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200708 if( len == 0 )
709 return( 0 );
710
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100711#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200712 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
713 {
714 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
715 return( 0 );
716 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100717#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200718
719 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200720 * Failures are ok: just ignore the ticket and proceed.
721 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200722 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
723 {
724 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200725 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200726 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200727
728 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
729
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200730 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200731
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200732 /* Don't send a new ticket after all, this one is OK */
733 ssl->handshake->new_session_ticket = 0;
734
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200735 return( 0 );
736}
Paul Bakkera503a632013-08-14 13:48:06 +0200737#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200738
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200739#if defined(POLARSSL_SSL_ALPN)
740static int ssl_parse_alpn_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200741 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200742{
Paul Bakker14b16c62014-05-28 11:33:54 +0200743 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200744 const unsigned char *theirs, *start, *end;
745 const char **ours;
746
747 /* If ALPN not configured, just ignore the extension */
748 if( ssl->alpn_list == NULL )
749 return( 0 );
750
751 /*
752 * opaque ProtocolName<1..2^8-1>;
753 *
754 * struct {
755 * ProtocolName protocol_name_list<2..2^16-1>
756 * } ProtocolNameList;
757 */
758
759 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
760 if( len < 4 )
761 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
762
763 list_len = ( buf[0] << 8 ) | buf[1];
764 if( list_len != len - 2 )
765 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
766
767 /*
768 * Use our order of preference
769 */
770 start = buf + 2;
771 end = buf + len;
772 for( ours = ssl->alpn_list; *ours != NULL; ours++ )
773 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200774 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200775 for( theirs = start; theirs != end; theirs += cur_len )
776 {
777 /* If the list is well formed, we should get equality first */
778 if( theirs > end )
779 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
780
781 cur_len = *theirs++;
782
783 /* Empty strings MUST NOT be included */
784 if( cur_len == 0 )
785 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
786
Paul Bakker14b16c62014-05-28 11:33:54 +0200787 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200788 memcmp( theirs, *ours, cur_len ) == 0 )
789 {
790 ssl->alpn_chosen = *ours;
791 return( 0 );
792 }
793 }
794 }
795
796 /* If we get there, no match was found */
797 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
798 SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
799 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
800}
801#endif /* POLARSSL_SSL_ALPN */
802
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100803/*
804 * Auxiliary functions for ServerHello parsing and related actions
805 */
806
807#if defined(POLARSSL_X509_CRT_PARSE_C)
808/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100809 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100810 */
811#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100812static int ssl_check_key_curve( pk_context *pk,
813 const ecp_curve_info **curves )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100814{
815 const ecp_curve_info **crv = curves;
816 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
817
818 while( *crv != NULL )
819 {
820 if( (*crv)->grp_id == grp_id )
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100821 return( 0 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100822 crv++;
823 }
824
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100825 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100826}
827#endif /* POLARSSL_ECDSA_C */
828
829/*
830 * Try picking a certificate for this ciphersuite,
831 * return 0 on success and -1 on failure.
832 */
833static int ssl_pick_cert( ssl_context *ssl,
834 const ssl_ciphersuite_t * ciphersuite_info )
835{
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100836 ssl_key_cert *cur, *list, *fallback = NULL;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100837 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +0200838 int flags;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100839
840#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
841 if( ssl->handshake->sni_key_cert != NULL )
842 list = ssl->handshake->sni_key_cert;
843 else
844#endif
845 list = ssl->handshake->key_cert;
846
847 if( pk_alg == POLARSSL_PK_NONE )
848 return( 0 );
849
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000850 SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) );
851
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100852 for( cur = list; cur != NULL; cur = cur->next )
853 {
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000854 SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate",
855 cur->cert );
856
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100857 if( ! pk_can_do( cur->key, pk_alg ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000858 {
859 SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100860 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000861 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100862
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200863 /*
864 * This avoids sending the client a cert it'll reject based on
865 * keyUsage or other extensions.
866 *
867 * It also allows the user to provision different certificates for
868 * different uses based on keyUsage, eg if they want to avoid signing
869 * and decrypting with the same RSA key.
870 */
871 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +0200872 SSL_IS_SERVER, &flags ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200873 {
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000874 SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
875 "(extended) key usage extension" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200876 continue;
877 }
878
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100879#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100880 if( pk_alg == POLARSSL_PK_ECDSA &&
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100881 ssl_check_key_curve( cur->key, ssl->handshake->curves ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000882 {
883 SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) );
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100884 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000885 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100886#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100887
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100888 /*
889 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
890 * present them a SHA-higher cert rather than failing if it's the only
891 * one we got that satisfies the other conditions.
892 */
893 if( ssl->minor_ver < SSL_MINOR_VERSION_3 &&
894 cur->cert->sig_md != POLARSSL_MD_SHA1 )
895 {
896 if( fallback == NULL )
897 fallback = cur;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000898 {
899 SSL_DEBUG_MSG( 3, ( "certificate not preferred: "
900 "sha-2 with pre-TLS 1.2 client" ) );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100901 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000902 }
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100903 }
904
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100905 /* If we get there, we got a winner */
906 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100907 }
908
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000909 if( cur == NULL )
910 cur = fallback;
911
912
913 /* Do not update ssl->handshake->key_cert unless the is a match */
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100914 if( cur != NULL )
915 {
916 ssl->handshake->key_cert = cur;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000917 SSL_DEBUG_CRT( 3, "selected certificate chain, certificate",
918 ssl->handshake->key_cert->cert );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100919 return( 0 );
920 }
921
922 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100923}
924#endif /* POLARSSL_X509_CRT_PARSE_C */
925
926/*
927 * Check if a given ciphersuite is suitable for use with our config/keys/etc
928 * Sets ciphersuite_info only if the suite matches.
929 */
930static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
931 const ssl_ciphersuite_t **ciphersuite_info )
932{
933 const ssl_ciphersuite_t *suite_info;
934
935 suite_info = ssl_ciphersuite_from_id( suite_id );
936 if( suite_info == NULL )
937 {
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100938 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
939 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100940 }
941
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000942 SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %s", suite_info->name ) );
943
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100944 if( suite_info->min_minor_ver > ssl->minor_ver ||
945 suite_info->max_minor_ver < ssl->minor_ver )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000946 {
947 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100948 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000949 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100950
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100951 if( ssl->arc4_disabled == SSL_ARC4_DISABLED &&
952 suite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000953 {
954 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100955 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000956 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100957
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100958#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
959 if( ssl_ciphersuite_uses_ec( suite_info ) &&
960 ( ssl->handshake->curves == NULL ||
961 ssl->handshake->curves[0] == NULL ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000962 {
963 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
964 "no common elliptic curve" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100965 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000966 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100967#endif
968
969#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
970 /* If the ciphersuite requires a pre-shared key and we don't
971 * have one, skip it now rather than failing later */
972 if( ssl_ciphersuite_uses_psk( suite_info ) &&
973 ssl->f_psk == NULL &&
974 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
975 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000976 {
977 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100978 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000979 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100980#endif
981
982#if defined(POLARSSL_X509_CRT_PARSE_C)
983 /*
984 * Final check: if ciphersuite requires us to have a
985 * certificate/key of a particular type:
986 * - select the appropriate certificate if we have one, or
987 * - try the next ciphersuite if we don't
988 * This must be done last since we modify the key_cert list.
989 */
990 if( ssl_pick_cert( ssl, suite_info ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000991 {
992 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
993 "no suitable certificate" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100994 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000995 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100996#endif
997
998 *ciphersuite_info = suite_info;
999 return( 0 );
1000}
1001
Paul Bakker78a8c712013-03-06 17:01:52 +01001002#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1003static int ssl_parse_client_hello_v2( ssl_context *ssl )
1004{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001005 int ret, got_common_suite;
Paul Bakker78a8c712013-03-06 17:01:52 +01001006 unsigned int i, j;
1007 size_t n;
1008 unsigned int ciph_len, sess_len, chal_len;
1009 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001010 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +02001011 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001012
1013 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
1014
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001015#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker78a8c712013-03-06 17:01:52 +01001016 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
1017 {
1018 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
1019
1020 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1021 return( ret );
1022
1023 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1024 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001025#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001026
1027 buf = ssl->in_hdr;
1028
1029 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1030
1031 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
1032 buf[2] ) );
1033 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
1034 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
1035 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
1036 buf[3], buf[4] ) );
1037
1038 /*
1039 * SSLv2 Client Hello
1040 *
1041 * Record layer:
1042 * 0 . 1 message length
1043 *
1044 * SSL layer:
1045 * 2 . 2 message type
1046 * 3 . 4 protocol version
1047 */
1048 if( buf[2] != SSL_HS_CLIENT_HELLO ||
1049 buf[3] != SSL_MAJOR_VERSION_3 )
1050 {
1051 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1052 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1053 }
1054
1055 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
1056
1057 if( n < 17 || n > 512 )
1058 {
1059 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1060 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1061 }
1062
1063 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +02001064 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
1065 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +01001066
1067 if( ssl->minor_ver < ssl->min_minor_ver )
1068 {
1069 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001070 " [%d:%d] < [%d:%d]",
1071 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +01001072 ssl->min_major_ver, ssl->min_minor_ver ) );
1073
1074 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1075 SSL_ALERT_MSG_PROTOCOL_VERSION );
1076 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1077 }
1078
Paul Bakker2fbefde2013-06-29 16:01:15 +02001079 ssl->handshake->max_major_ver = buf[3];
1080 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +01001081
1082 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
1083 {
1084 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1085 return( ret );
1086 }
1087
1088 ssl->handshake->update_checksum( ssl, buf + 2, n );
1089
1090 buf = ssl->in_msg;
1091 n = ssl->in_left - 5;
1092
1093 /*
1094 * 0 . 1 ciphersuitelist length
1095 * 2 . 3 session id length
1096 * 4 . 5 challenge length
1097 * 6 . .. ciphersuitelist
1098 * .. . .. session id
1099 * .. . .. challenge
1100 */
1101 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1102
1103 ciph_len = ( buf[0] << 8 ) | buf[1];
1104 sess_len = ( buf[2] << 8 ) | buf[3];
1105 chal_len = ( buf[4] << 8 ) | buf[5];
1106
1107 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
1108 ciph_len, sess_len, chal_len ) );
1109
1110 /*
1111 * Make sure each parameter length is valid
1112 */
1113 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1114 {
1115 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1116 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1117 }
1118
1119 if( sess_len > 32 )
1120 {
1121 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1122 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1123 }
1124
1125 if( chal_len < 8 || chal_len > 32 )
1126 {
1127 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1128 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1129 }
1130
1131 if( n != 6 + ciph_len + sess_len + chal_len )
1132 {
1133 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1134 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1135 }
1136
1137 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1138 buf + 6, ciph_len );
1139 SSL_DEBUG_BUF( 3, "client hello, session id",
1140 buf + 6 + ciph_len, sess_len );
1141 SSL_DEBUG_BUF( 3, "client hello, challenge",
1142 buf + 6 + ciph_len + sess_len, chal_len );
1143
1144 p = buf + 6 + ciph_len;
1145 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001146 memset( ssl->session_negotiate->id, 0,
1147 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001148 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1149
1150 p += sess_len;
1151 memset( ssl->handshake->randbytes, 0, 64 );
1152 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1153
1154 /*
1155 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1156 */
1157 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1158 {
1159 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1160 {
1161 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001162#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker78a8c712013-03-06 17:01:52 +01001163 if( ssl->renegotiation == SSL_RENEGOTIATION )
1164 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001165 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
1166 "during renegotiation" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001167
1168 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1169 return( ret );
1170
1171 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1172 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001173#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001174 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1175 break;
1176 }
1177 }
1178
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001179#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1180 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1181 {
1182 if( p[0] == 0 &&
1183 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1184 p[2] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1185 {
1186 SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
1187
1188 if( ssl->minor_ver < ssl->max_minor_ver )
1189 {
1190 SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
1191
1192 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1193 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1194
1195 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1196 }
1197
1198 break;
1199 }
1200 }
1201#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1202
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001203 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001204 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001205 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001206#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1207 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1208 {
1209 for( i = 0; ciphersuites[i] != 0; i++ )
1210#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001211 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001212 {
1213 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001214#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001215 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001216 if( p[0] != 0 ||
1217 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1218 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1219 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001220
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001221 got_common_suite = 1;
1222
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001223 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1224 &ciphersuite_info ) ) != 0 )
1225 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001226
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001227 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001228 goto have_ciphersuite_v2;
1229 }
1230 }
1231
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001232 if( got_common_suite )
1233 {
1234 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1235 "but none of them usable" ) );
1236 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
1237 }
1238 else
1239 {
1240 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1241 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1242 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001243
1244have_ciphersuite_v2:
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001245 SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
1246
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001247 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001248 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001249 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001250
1251 /*
1252 * SSLv2 Client Hello relevant renegotiation security checks
1253 */
1254 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1255 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1256 {
1257 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1258
1259 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1260 return( ret );
1261
1262 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1263 }
1264
1265 ssl->in_left = 0;
1266 ssl->state++;
1267
1268 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1269
1270 return( 0 );
1271}
1272#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1273
Paul Bakker5121ce52009-01-03 21:22:43 +00001274static int ssl_parse_client_hello( ssl_context *ssl )
1275{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001276 int ret, got_common_suite;
Paul Bakker23986e52011-04-24 08:57:21 +00001277 unsigned int i, j;
1278 size_t n;
1279 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001280 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001281 unsigned int ext_len = 0;
1282 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001283#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001284 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001285#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001286 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001287 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001288 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001289
1290 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1291
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001292#if defined(POLARSSL_SSL_RENEGOTIATION)
1293 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1294#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001295 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001296 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
1297 {
1298 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1299 return( ret );
1300 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001301 }
1302
1303 buf = ssl->in_hdr;
1304
Paul Bakker78a8c712013-03-06 17:01:52 +01001305#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1306 if( ( buf[0] & 0x80 ) != 0 )
1307 return ssl_parse_client_hello_v2( ssl );
1308#endif
1309
Paul Bakkerec636f32012-09-09 19:17:02 +00001310 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1311
1312 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1313 buf[0] ) );
1314 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1315 ( buf[3] << 8 ) | buf[4] ) );
1316 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
1317 buf[1], buf[2] ) );
1318
1319 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001320 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001321 *
1322 * Record layer:
1323 * 0 . 0 message type
1324 * 1 . 2 protocol version
1325 * 3 . 4 message length
1326 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001327
1328 /* According to RFC 5246 Appendix E.1, the version here is typically
1329 * "{03,00}, the lowest version number supported by the client, [or] the
1330 * value of ClientHello.client_version", so the only meaningful check here
1331 * is the major version shouldn't be less than 3 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001332 if( buf[0] != SSL_MSG_HANDSHAKE ||
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001333 buf[1] < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001334 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001335 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1336 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1337 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001338
Paul Bakkerec636f32012-09-09 19:17:02 +00001339 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +00001340
Paul Bakker4f42c112014-04-17 14:48:23 +02001341 if( n < 45 || n > SSL_MAX_CONTENT_LEN )
Paul Bakkerec636f32012-09-09 19:17:02 +00001342 {
1343 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1344 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1345 }
1346
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001347#if defined(POLARSSL_SSL_RENEGOTIATION)
1348 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1349#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001350 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001351 if( ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
1352 {
1353 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1354 return( ret );
1355 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001356 }
1357
1358 buf = ssl->in_msg;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001359#if defined(POLARSSL_SSL_RENEGOTIATION)
1360 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001361 n = ssl->in_msglen;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001362 else
1363#endif
1364 n = ssl->in_left - 5;
Paul Bakkerec636f32012-09-09 19:17:02 +00001365
Paul Bakker48916f92012-09-16 19:57:18 +00001366 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +00001367
1368 /*
1369 * SSL layer:
1370 * 0 . 0 handshake type
1371 * 1 . 3 handshake length
1372 * 4 . 5 protocol version
1373 * 6 . 9 UNIX time()
1374 * 10 . 37 random bytes
1375 * 38 . 38 session id length
1376 * 39 . 38+x session id
1377 * 39+x . 40+x ciphersuitelist length
Paul Bakker0f651c72014-05-22 15:12:19 +02001378 * 41+x . 40+y ciphersuitelist
1379 * 41+y . 41+y compression alg length
1380 * 42+y . 41+z compression algs
Paul Bakkerec636f32012-09-09 19:17:02 +00001381 * .. . .. extensions
1382 */
1383 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1384
1385 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1386 buf[0] ) );
1387 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1388 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1389 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1390 buf[4], buf[5] ) );
1391
1392 /*
1393 * Check the handshake type and protocol version
1394 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001395 if( buf[0] != SSL_HS_CLIENT_HELLO )
Paul Bakkerec636f32012-09-09 19:17:02 +00001396 {
1397 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1398 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1399 }
1400
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001401 ssl->major_ver = buf[4];
1402 ssl->minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001403
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001404 ssl->handshake->max_major_ver = ssl->major_ver;
1405 ssl->handshake->max_minor_ver = ssl->minor_ver;
1406
1407 if( ssl->major_ver < ssl->min_major_ver ||
1408 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001409 {
1410 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001411 " [%d:%d] < [%d:%d]",
1412 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001413 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001414
1415 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1416 SSL_ALERT_MSG_PROTOCOL_VERSION );
1417
1418 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1419 }
1420
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001421 if( ssl->major_ver > ssl->max_major_ver )
1422 {
1423 ssl->major_ver = ssl->max_major_ver;
1424 ssl->minor_ver = ssl->max_minor_ver;
1425 }
1426 else if( ssl->minor_ver > ssl->max_minor_ver )
1427 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001428
Paul Bakker48916f92012-09-16 19:57:18 +00001429 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001430
1431 /*
1432 * Check the handshake message length
1433 */
1434 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1435 {
1436 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1437 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1438 }
1439
1440 /*
1441 * Check the session length
1442 */
1443 sess_len = buf[38];
1444
Paul Bakker0f651c72014-05-22 15:12:19 +02001445 if( sess_len > 32 || sess_len > n - 42 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001446 {
1447 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1448 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1449 }
1450
Paul Bakker48916f92012-09-16 19:57:18 +00001451 ssl->session_negotiate->length = sess_len;
1452 memset( ssl->session_negotiate->id, 0,
1453 sizeof( ssl->session_negotiate->id ) );
1454 memcpy( ssl->session_negotiate->id, buf + 39,
1455 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001456
1457 /*
1458 * Check the ciphersuitelist length
1459 */
1460 ciph_len = ( buf[39 + sess_len] << 8 )
1461 | ( buf[40 + sess_len] );
1462
Paul Bakker0f651c72014-05-22 15:12:19 +02001463 if( ciph_len < 2 || ( ciph_len % 2 ) != 0 || ciph_len > n - 42 - sess_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001464 {
1465 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1466 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1467 }
1468
1469 /*
1470 * Check the compression algorithms length
1471 */
1472 comp_len = buf[41 + sess_len + ciph_len];
1473
Paul Bakker0f651c72014-05-22 15:12:19 +02001474 if( comp_len < 1 || comp_len > 16 ||
1475 comp_len > n - 42 - sess_len - ciph_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001476 {
1477 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1478 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1479 }
1480
Paul Bakker48916f92012-09-16 19:57:18 +00001481 /*
1482 * Check the extension length
1483 */
1484 if( n > 42 + sess_len + ciph_len + comp_len )
1485 {
1486 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1487 | ( buf[43 + sess_len + ciph_len + comp_len] );
1488
1489 if( ( ext_len > 0 && ext_len < 4 ) ||
1490 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1491 {
1492 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1493 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1494 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1495 }
1496 }
1497
1498 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001499#if defined(POLARSSL_ZLIB_SUPPORT)
1500 for( i = 0; i < comp_len; ++i )
1501 {
Paul Bakker48916f92012-09-16 19:57:18 +00001502 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001503 {
Paul Bakker48916f92012-09-16 19:57:18 +00001504 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001505 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001506 }
1507 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001508#endif
1509
Paul Bakkerec636f32012-09-09 19:17:02 +00001510 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1511 buf + 6, 32 );
1512 SSL_DEBUG_BUF( 3, "client hello, session id",
1513 buf + 38, sess_len );
1514 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1515 buf + 41 + sess_len, ciph_len );
1516 SSL_DEBUG_BUF( 3, "client hello, compression",
1517 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001518
Paul Bakkerec636f32012-09-09 19:17:02 +00001519 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001520 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1521 */
1522 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1523 {
1524 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1525 {
1526 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001527#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001528 if( ssl->renegotiation == SSL_RENEGOTIATION )
1529 {
1530 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001531
1532 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1533 return( ret );
1534
Paul Bakker48916f92012-09-16 19:57:18 +00001535 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1536 }
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001537 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001538#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00001539 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1540 break;
1541 }
1542 }
1543
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001544#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1545 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1546 {
1547 if( p[0] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1548 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1549 {
1550 SSL_DEBUG_MSG( 0, ( "received FALLBACK_SCSV" ) );
1551
1552 if( ssl->minor_ver < ssl->max_minor_ver )
1553 {
1554 SSL_DEBUG_MSG( 0, ( "inapropriate fallback" ) );
1555
1556 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1557 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1558
1559 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1560 }
1561
1562 break;
1563 }
1564 }
1565#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1566
Janos Follath307e1812016-05-23 18:52:14 +01001567 /* Do not parse the extensions if the protocol is SSLv3 */
1568#if defined(POLARSSL_SSL_PROTO_SSL3)
1569 if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
Paul Bakker48916f92012-09-16 19:57:18 +00001570 {
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001571#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001572
Janos Follath307e1812016-05-23 18:52:14 +01001573 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001574
Janos Follath307e1812016-05-23 18:52:14 +01001575 while( ext_len )
Paul Bakker48916f92012-09-16 19:57:18 +00001576 {
Janos Follath307e1812016-05-23 18:52:14 +01001577 unsigned int ext_id = ( ( ext[0] << 8 )
1578 | ( ext[1] ) );
1579 unsigned int ext_size = ( ( ext[2] << 8 )
1580 | ( ext[3] ) );
1581
1582 if( ext_size + 4 > ext_len )
1583 {
1584 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1585 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1586 }
1587 switch( ext_id )
1588 {
1589 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
1590 case TLS_EXT_SERVERNAME:
1591 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1592 if( ssl->f_sni == NULL )
1593 break;
1594
1595 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1596 if( ret != 0 )
1597 return( ret );
1598 break;
1599 #endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
1600
1601 case TLS_EXT_RENEGOTIATION_INFO:
1602 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1603 #if defined(POLARSSL_SSL_RENEGOTIATION)
1604 renegotiation_info_seen = 1;
1605 #endif
1606
1607 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1608 if( ret != 0 )
1609 return( ret );
1610 break;
1611
1612 #if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1613 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
1614 case TLS_EXT_SIG_ALG:
1615 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1616 #if defined(POLARSSL_SSL_RENEGOTIATION)
1617 if( ssl->renegotiation == SSL_RENEGOTIATION )
1618 break;
1619 #endif
1620
1621 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1622 if( ret != 0 )
1623 return( ret );
1624 break;
1625 #endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
1626 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
1627
1628 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
1629 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1630 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1631
1632 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1633 if( ret != 0 )
1634 return( ret );
1635 break;
1636
1637 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1638 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1639 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
1640
1641 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1642 if( ret != 0 )
1643 return( ret );
1644 break;
1645 #endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
1646
1647 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
1648 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1649 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1650
1651 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1652 if( ret != 0 )
1653 return( ret );
1654 break;
1655 #endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
1656
1657 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1658 case TLS_EXT_TRUNCATED_HMAC:
1659 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1660
1661 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1662 if( ret != 0 )
1663 return( ret );
1664 break;
1665 #endif /* POLARSSL_SSL_TRUNCATED_HMAC */
1666
1667 #if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1668 case TLS_EXT_ENCRYPT_THEN_MAC:
1669 SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
1670
1671 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1672 if( ret != 0 )
1673 return( ret );
1674 break;
1675 #endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1676
1677 #if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1678 case TLS_EXT_EXTENDED_MASTER_SECRET:
1679 SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
1680
1681 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1682 if( ret != 0 )
1683 return( ret );
1684 break;
1685 #endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1686
1687 #if defined(POLARSSL_SSL_SESSION_TICKETS)
1688 case TLS_EXT_SESSION_TICKET:
1689 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1690
1691 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1692 if( ret != 0 )
1693 return( ret );
1694 break;
1695 #endif /* POLARSSL_SSL_SESSION_TICKETS */
1696
1697 #if defined(POLARSSL_SSL_ALPN)
1698 case TLS_EXT_ALPN:
1699 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1700
1701 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1702 if( ret != 0 )
1703 return( ret );
1704 break;
1705 #endif /* POLARSSL_SSL_SESSION_TICKETS */
1706
1707 default:
1708 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1709 ext_id ) );
1710 }
1711
1712 ext_len -= 4 + ext_size;
1713 ext += 4 + ext_size;
1714
1715 if( ext_len > 0 && ext_len < 4 )
1716 {
1717 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1718 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1719 }
Paul Bakker48916f92012-09-16 19:57:18 +00001720 }
Janos Follath307e1812016-05-23 18:52:14 +01001721
1722#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker48916f92012-09-16 19:57:18 +00001723 }
Janos Follath307e1812016-05-23 18:52:14 +01001724#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001725
1726 /*
1727 * Renegotiation security checks
1728 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001729 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001730 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1731 {
1732 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1733 handshake_failure = 1;
1734 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001735#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001736 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1737 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1738 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001739 {
1740 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001741 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001742 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001743 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1744 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1745 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001746 {
1747 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001748 handshake_failure = 1;
1749 }
1750 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1751 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1752 renegotiation_info_seen == 1 )
1753 {
1754 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1755 handshake_failure = 1;
1756 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001757#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001758
1759 if( handshake_failure == 1 )
1760 {
1761 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1762 return( ret );
1763
Paul Bakker48916f92012-09-16 19:57:18 +00001764 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1765 }
Paul Bakker380da532012-04-18 16:10:25 +00001766
Paul Bakker41c83d32013-03-20 14:39:14 +01001767 /*
1768 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001769 * (At the end because we need information from the EC-based extensions
1770 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001771 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001772 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001773 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001774 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001775#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1776 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
1777 {
1778 for( i = 0; ciphersuites[i] != 0; i++ )
1779#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001780 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001781 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001782 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001783#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001784 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001785 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1786 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1787 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001788
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001789 got_common_suite = 1;
1790
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001791 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1792 &ciphersuite_info ) ) != 0 )
1793 return( ret );
1794
1795 if( ciphersuite_info != NULL )
1796 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001797 }
1798 }
1799
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001800 if( got_common_suite )
1801 {
1802 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1803 "but none of them usable" ) );
1804 ssl_send_fatal_handshake_failure( ssl );
1805 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
1806 }
1807 else
1808 {
1809 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1810 ssl_send_fatal_handshake_failure( ssl );
1811 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1812 }
Paul Bakker41c83d32013-03-20 14:39:14 +01001813
1814have_ciphersuite:
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001815 SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
1816
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001817 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001818 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1819 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1820
Paul Bakker5121ce52009-01-03 21:22:43 +00001821 ssl->in_left = 0;
1822 ssl->state++;
1823
1824 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1825
1826 return( 0 );
1827}
1828
Paul Bakker1f2bc622013-08-15 13:45:55 +02001829#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001830static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1831 unsigned char *buf,
1832 size_t *olen )
1833{
1834 unsigned char *p = buf;
1835
1836 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1837 {
1838 *olen = 0;
1839 return;
1840 }
1841
1842 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1843
1844 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1845 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1846
1847 *p++ = 0x00;
1848 *p++ = 0x00;
1849
1850 *olen = 4;
1851}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001852#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001853
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001854#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1855static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
1856 unsigned char *buf,
1857 size_t *olen )
1858{
1859 unsigned char *p = buf;
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01001860 const ssl_ciphersuite_t *suite = NULL;
1861 const cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001862
1863 if( ssl->session_negotiate->encrypt_then_mac == SSL_EXTENDED_MS_DISABLED ||
1864 ssl->minor_ver == SSL_MINOR_VERSION_0 )
1865 {
1866 *olen = 0;
1867 return;
1868 }
1869
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01001870 /*
1871 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
1872 * from a client and then selects a stream or Authenticated Encryption
1873 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
1874 * encrypt-then-MAC response extension back to the client."
1875 */
1876 if( ( suite = ssl_ciphersuite_from_id(
1877 ssl->session_negotiate->ciphersuite ) ) == NULL ||
1878 ( cipher = cipher_info_from_type( suite->cipher ) ) == NULL ||
1879 cipher->mode != POLARSSL_MODE_CBC )
1880 {
1881 *olen = 0;
1882 return;
1883 }
1884
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001885 SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
1886
1887 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
1888 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
1889
1890 *p++ = 0x00;
1891 *p++ = 0x00;
1892
1893 *olen = 4;
1894}
1895#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1896
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001897#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1898static void ssl_write_extended_ms_ext( ssl_context *ssl,
1899 unsigned char *buf,
1900 size_t *olen )
1901{
1902 unsigned char *p = buf;
1903
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +02001904 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_DISABLED ||
1905 ssl->minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001906 {
1907 *olen = 0;
1908 return;
1909 }
1910
1911 SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
1912 "extension" ) );
1913
1914 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
1915 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
1916
1917 *p++ = 0x00;
1918 *p++ = 0x00;
1919
1920 *olen = 4;
1921}
1922#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1923
Paul Bakkera503a632013-08-14 13:48:06 +02001924#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001925static void ssl_write_session_ticket_ext( ssl_context *ssl,
1926 unsigned char *buf,
1927 size_t *olen )
1928{
1929 unsigned char *p = buf;
1930
1931 if( ssl->handshake->new_session_ticket == 0 )
1932 {
1933 *olen = 0;
1934 return;
1935 }
1936
1937 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1938
1939 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1940 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1941
1942 *p++ = 0x00;
1943 *p++ = 0x00;
1944
1945 *olen = 4;
1946}
Paul Bakkera503a632013-08-14 13:48:06 +02001947#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001948
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001949static void ssl_write_renegotiation_ext( ssl_context *ssl,
1950 unsigned char *buf,
1951 size_t *olen )
1952{
1953 unsigned char *p = buf;
1954
1955 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1956 {
1957 *olen = 0;
1958 return;
1959 }
1960
1961 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1962
1963 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1964 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1965
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001966#if defined(POLARSSL_SSL_RENEGOTIATION)
1967 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
1968 {
1969 *p++ = 0x00;
1970 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1971 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001972
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001973 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1974 p += ssl->verify_data_len;
1975 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1976 p += ssl->verify_data_len;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001977
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001978 *olen = 5 + ssl->verify_data_len * 2;
1979 }
1980 else
1981#endif /* POLARSSL_SSL_RENEGOTIATION */
1982 {
1983 *p++ = 0x00;
1984 *p++ = 0x01;
1985 *p++ = 0x00;
1986
1987 *olen = 5;
1988 }
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001989}
1990
Paul Bakker05decb22013-08-15 13:33:48 +02001991#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001992static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1993 unsigned char *buf,
1994 size_t *olen )
1995{
1996 unsigned char *p = buf;
1997
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001998 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1999 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002000 *olen = 0;
2001 return;
2002 }
2003
2004 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
2005
2006 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
2007 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
2008
2009 *p++ = 0x00;
2010 *p++ = 1;
2011
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002012 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002013
2014 *olen = 5;
2015}
Paul Bakker05decb22013-08-15 13:33:48 +02002016#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002017
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002018#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002019static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
2020 unsigned char *buf,
2021 size_t *olen )
2022{
2023 unsigned char *p = buf;
2024 ((void) ssl);
2025
Paul Bakker677377f2013-10-28 12:54:26 +01002026 if( ( ssl->handshake->cli_exts &
2027 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
2028 {
2029 *olen = 0;
2030 return;
2031 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002032
2033 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
2034
2035 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2036 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
2037
2038 *p++ = 0x00;
2039 *p++ = 2;
2040
2041 *p++ = 1;
2042 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
2043
2044 *olen = 6;
2045}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002046#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002047
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002048#if defined(POLARSSL_SSL_ALPN )
2049static void ssl_write_alpn_ext( ssl_context *ssl,
2050 unsigned char *buf, size_t *olen )
2051{
2052 if( ssl->alpn_chosen == NULL )
2053 {
2054 *olen = 0;
2055 return;
2056 }
2057
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02002058 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002059
2060 /*
2061 * 0 . 1 ext identifier
2062 * 2 . 3 ext length
2063 * 4 . 5 protocol list length
2064 * 6 . 6 protocol name length
2065 * 7 . 7+n protocol name
2066 */
2067 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
2068 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
2069
2070 *olen = 7 + strlen( ssl->alpn_chosen );
2071
2072 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2073 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2074
2075 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2076 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2077
2078 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2079
2080 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2081}
2082#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
2083
Paul Bakker5121ce52009-01-03 21:22:43 +00002084static int ssl_write_server_hello( ssl_context *ssl )
2085{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002086#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002087 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002088#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002089 int ret;
2090 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 unsigned char *buf, *p;
2092
2093 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
2094
Paul Bakkera9a028e2013-11-21 17:31:06 +01002095 if( ssl->f_rng == NULL )
2096 {
2097 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2098 return( POLARSSL_ERR_SSL_NO_RNG );
2099 }
2100
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 /*
2102 * 0 . 0 handshake type
2103 * 1 . 3 handshake length
2104 * 4 . 5 protocol version
2105 * 6 . 9 UNIX time()
2106 * 10 . 37 random bytes
2107 */
2108 buf = ssl->out_msg;
2109 p = buf + 4;
2110
2111 *p++ = (unsigned char) ssl->major_ver;
2112 *p++ = (unsigned char) ssl->minor_ver;
2113
2114 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
2115 buf[4], buf[5] ) );
2116
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002117#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002118 t = time( NULL );
2119 *p++ = (unsigned char)( t >> 24 );
2120 *p++ = (unsigned char)( t >> 16 );
2121 *p++ = (unsigned char)( t >> 8 );
2122 *p++ = (unsigned char)( t );
2123
2124 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002125#else
2126 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
2127 return( ret );
2128
2129 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02002130#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002131
Paul Bakkera3d195c2011-11-27 21:07:34 +00002132 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
2133 return( ret );
2134
2135 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002136
Paul Bakker48916f92012-09-16 19:57:18 +00002137 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002138
2139 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
2140
2141 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002142 * Resume is 0 by default, see ssl_handshake_init().
2143 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2144 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002145 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002146 if( ssl->handshake->resume == 0 &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002147#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002148 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002149#endif
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002150 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002151 ssl->f_get_cache != NULL &&
2152 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
2153 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01002154 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002155 ssl->handshake->resume = 1;
2156 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002157
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002158 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002159 {
2160 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002161 * New session, create a new session id,
2162 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002163 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002164 ssl->state++;
2165
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002166#if defined(POLARSSL_HAVE_TIME)
2167 ssl->session_negotiate->start = time( NULL );
2168#endif
2169
Paul Bakkera503a632013-08-14 13:48:06 +02002170#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002171 if( ssl->handshake->new_session_ticket != 0 )
2172 {
2173 ssl->session_negotiate->length = n = 0;
2174 memset( ssl->session_negotiate->id, 0, 32 );
2175 }
2176 else
2177#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002178 {
2179 ssl->session_negotiate->length = n = 32;
2180 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002181 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002182 return( ret );
2183 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002184 }
2185 else
2186 {
2187 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002188 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002189 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02002190 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002191 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002192
2193 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2194 {
2195 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2196 return( ret );
2197 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002198 }
2199
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002200 /*
2201 * 38 . 38 session id length
2202 * 39 . 38+n session id
2203 * 39+n . 40+n chosen ciphersuite
2204 * 41+n . 41+n chosen compression alg.
2205 * 42+n . 43+n extensions length
2206 * 44+n . 43+n+m extensions
2207 */
2208 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00002209 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
2210 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002211
2212 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2213 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2214 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002215 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002216
Paul Bakker48916f92012-09-16 19:57:18 +00002217 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2218 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2219 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002220
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02002221 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2222 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02002223 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002224 ssl->session_negotiate->compression ) );
2225
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002226 /*
2227 * First write extensions, then the total length
2228 */
2229 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2230 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002231
Paul Bakker05decb22013-08-15 13:33:48 +02002232#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002233 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2234 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002235#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002236
Paul Bakker1f2bc622013-08-15 13:45:55 +02002237#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002238 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2239 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002240#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002241
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002242#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2243 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2244 ext_len += olen;
2245#endif
2246
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002247#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2248 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2249 ext_len += olen;
2250#endif
2251
Paul Bakkera503a632013-08-14 13:48:06 +02002252#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002253 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2254 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002255#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002256
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002257#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002258 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2259 ext_len += olen;
2260#endif
2261
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002262#if defined(POLARSSL_SSL_ALPN)
2263 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2264 ext_len += olen;
2265#endif
2266
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002267 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002268
Paul Bakkera7036632014-04-30 10:15:38 +02002269 if( ext_len > 0 )
2270 {
2271 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2272 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2273 p += ext_len;
2274 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002275
2276 ssl->out_msglen = p - buf;
2277 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2278 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
2279
2280 ret = ssl_write_record( ssl );
2281
2282 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2283
2284 return( ret );
2285}
2286
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002287#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2288 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002289 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2290 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002291static int ssl_write_certificate_request( ssl_context *ssl )
2292{
Paul Bakkered27a042013-04-18 22:46:23 +02002293 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002294
2295 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2296
2297 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002298 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002299 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2300 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002301 {
2302 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2303 ssl->state++;
2304 return( 0 );
2305 }
2306
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002307 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2308 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002309}
2310#else
2311static int ssl_write_certificate_request( ssl_context *ssl )
2312{
2313 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2314 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002315 size_t dn_size, total_dn_size; /* excluding length bytes */
2316 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002317 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002318 const x509_crt *crt;
Manuel Pégourié-Gonnardde9c8a52015-10-02 11:16:47 +02002319 const unsigned char * const end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00002320
2321 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2322
2323 ssl->state++;
2324
Paul Bakkerfbb17802013-04-17 19:10:21 +02002325 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002326 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002327 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002328 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002329 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002330 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002331 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002332 return( 0 );
2333 }
2334
2335 /*
2336 * 0 . 0 handshake type
2337 * 1 . 3 handshake length
2338 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002339 * 5 .. m-1 cert types
2340 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002341 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002342 * n .. n+1 length of all DNs
2343 * n+2 .. n+3 length of DN 1
2344 * n+4 .. ... Distinguished Name #1
2345 * ... .. ... length of DN 2, etc.
2346 */
2347 buf = ssl->out_msg;
2348 p = buf + 4;
2349
2350 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002351 * Supported certificate types
2352 *
2353 * ClientCertificateType certificate_types<1..2^8-1>;
2354 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002355 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002356 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002357
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002358#if defined(POLARSSL_RSA_C)
2359 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2360#endif
2361#if defined(POLARSSL_ECDSA_C)
2362 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2363#endif
2364
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002365 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002366 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002367
Paul Bakker577e0062013-08-28 11:57:20 +02002368 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002369#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002370 /*
2371 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002372 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002373 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2374 *
2375 * struct {
2376 * HashAlgorithm hash;
2377 * SignatureAlgorithm signature;
2378 * } SignatureAndHashAlgorithm;
2379 *
2380 * enum { (255) } HashAlgorithm;
2381 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002382 */
Paul Bakker21dca692013-01-03 11:41:08 +01002383 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002384 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002385 /*
2386 * Only use current running hash algorithm that is already required
2387 * for requested ciphersuite.
2388 */
Paul Bakker926af752012-11-23 13:38:07 +01002389 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2390
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002391 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2392 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002393 {
2394 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2395 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002396
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002397 /*
2398 * Supported signature algorithms
2399 */
2400#if defined(POLARSSL_RSA_C)
2401 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2402 p[2 + sa_len++] = SSL_SIG_RSA;
2403#endif
2404#if defined(POLARSSL_ECDSA_C)
2405 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2406 p[2 + sa_len++] = SSL_SIG_ECDSA;
2407#endif
Paul Bakker926af752012-11-23 13:38:07 +01002408
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002409 p[0] = (unsigned char)( sa_len >> 8 );
2410 p[1] = (unsigned char)( sa_len );
2411 sa_len += 2;
2412 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002413 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002414#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002415
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002416 /*
2417 * DistinguishedName certificate_authorities<0..2^16-1>;
2418 * opaque DistinguishedName<1..2^16-1>;
2419 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002420 p += 2;
2421 crt = ssl->ca_chain;
2422
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002423 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002424 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002425 {
Paul Bakker926af752012-11-23 13:38:07 +01002426 dn_size = crt->subject_raw.len;
Manuel Pégourié-Gonnardde9c8a52015-10-02 11:16:47 +02002427
Manuel Pégourié-Gonnardcf16b792015-12-10 14:36:25 +01002428 if( end < p ||
2429 (size_t)( end - p ) < dn_size ||
2430 (size_t)( end - p ) < 2 + dn_size )
Manuel Pégourié-Gonnardde9c8a52015-10-02 11:16:47 +02002431 {
2432 SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) );
2433 break;
2434 }
2435
Paul Bakker926af752012-11-23 13:38:07 +01002436 *p++ = (unsigned char)( dn_size >> 8 );
2437 *p++ = (unsigned char)( dn_size );
2438 memcpy( p, crt->subject_raw.p, dn_size );
2439 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002440
Paul Bakker926af752012-11-23 13:38:07 +01002441 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2442
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002443 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002444 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002445 }
2446
Paul Bakker926af752012-11-23 13:38:07 +01002447 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002448 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2449 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002450 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2451 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002452
2453 ret = ssl_write_record( ssl );
2454
2455 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2456
2457 return( ret );
2458}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002459#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2460 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002461 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2462 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002463
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002464#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2465 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2466static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2467{
2468 int ret;
2469
2470 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2471 {
2472 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2473 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2474 }
2475
2476 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2477 pk_ec( *ssl_own_key( ssl ) ),
2478 POLARSSL_ECDH_OURS ) ) != 0 )
2479 {
2480 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2481 return( ret );
2482 }
2483
2484 return( 0 );
2485}
2486#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2487 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2488
Paul Bakker41c83d32013-03-20 14:39:14 +01002489static int ssl_write_server_key_exchange( ssl_context *ssl )
2490{
Paul Bakker23986e52011-04-24 08:57:21 +00002491 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002492 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002493 const ssl_ciphersuite_t *ciphersuite_info =
2494 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002495
2496#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2497 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2498 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002499 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002500 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002501 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002502 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002503 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002504 ((void) dig_signed);
2505 ((void) dig_signed_len);
2506#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002507
Paul Bakker5121ce52009-01-03 21:22:43 +00002508 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2509
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002510#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2511 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2512 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002513 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2514 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2515 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002516 {
2517 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2518 ssl->state++;
2519 return( 0 );
2520 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002521#endif
2522
2523#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2524 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2525 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2526 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2527 {
2528 ssl_get_ecdh_params_from_cert( ssl );
2529
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002530 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002531 ssl->state++;
2532 return( 0 );
2533 }
2534#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002535
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002536#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2537 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2538 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2539 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002540 {
2541 /* TODO: Support identity hints */
2542 *(p++) = 0x00;
2543 *(p++) = 0x00;
2544
2545 n += 2;
2546 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002547#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2548 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002549
2550#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2551 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2552 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2553 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002554 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002555 /*
2556 * Ephemeral DH parameters:
2557 *
2558 * struct {
2559 * opaque dh_p<1..2^16-1>;
2560 * opaque dh_g<1..2^16-1>;
2561 * opaque dh_Ys<1..2^16-1>;
2562 * } ServerDHParams;
2563 */
2564 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2565 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2566 {
2567 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2568 return( ret );
2569 }
Paul Bakker48916f92012-09-16 19:57:18 +00002570
Paul Bakker41c83d32013-03-20 14:39:14 +01002571 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002572 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2573 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002574 {
2575 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2576 return( ret );
2577 }
2578
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002579 dig_signed = p;
2580 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002581
2582 p += len;
2583 n += len;
2584
Paul Bakker41c83d32013-03-20 14:39:14 +01002585 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2586 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2587 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2588 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2589 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002590#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2591 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002592
Gergely Budai987bfb52014-01-19 21:48:42 +01002593#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002594 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002595 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2596 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002597 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002598 /*
2599 * Ephemeral ECDH parameters:
2600 *
2601 * struct {
2602 * ECParameters curve_params;
2603 * ECPoint public;
2604 * } ServerECDHParams;
2605 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002606 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002607#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002608 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002609
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002610 /* Match our preference list against the offered curves */
2611 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2612 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2613 if( (*curve)->grp_id == *gid )
2614 goto curve_matching_done;
2615
2616curve_matching_done:
2617#else
2618 curve = ssl->handshake->curves;
2619#endif
2620
Manuel Pégourié-Gonnard8e8ae3d2015-06-23 18:57:28 +02002621 if( curve == NULL || *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002622 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002623 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2624 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002625 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002626
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002627 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002628
Paul Bakker41c83d32013-03-20 14:39:14 +01002629 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002630 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002631 {
2632 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2633 return( ret );
2634 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002635
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002636 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2637 p, SSL_MAX_CONTENT_LEN - n,
2638 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002639 {
2640 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2641 return( ret );
2642 }
2643
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002644 dig_signed = p;
2645 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002646
2647 p += len;
2648 n += len;
2649
Paul Bakker41c83d32013-03-20 14:39:14 +01002650 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2651 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002652#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002653
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002654#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002655 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2656 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002657 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002658 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2659 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002660 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002661 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002662 unsigned int hashlen = 0;
2663 unsigned char hash[64];
2664 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002665
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002666 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002667 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2668 */
Paul Bakker577e0062013-08-28 11:57:20 +02002669#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002670 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2671 {
2672 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2673
2674 if( md_alg == POLARSSL_MD_NONE )
2675 {
2676 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002677 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002678 }
2679 }
Paul Bakker577e0062013-08-28 11:57:20 +02002680 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002681#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002682#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2683 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002684 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002685 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2686 {
2687 md_alg = POLARSSL_MD_SHA1;
2688 }
2689 else
Paul Bakker577e0062013-08-28 11:57:20 +02002690#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002691 {
2692 md_alg = POLARSSL_MD_NONE;
2693 }
2694
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002695 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002696 * Compute the hash to be signed
2697 */
Paul Bakker577e0062013-08-28 11:57:20 +02002698#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2699 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002700 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002701 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002702 md5_context md5;
2703 sha1_context sha1;
2704
Paul Bakker5b4af392014-06-26 12:09:34 +02002705 md5_init( &md5 );
2706 sha1_init( &sha1 );
2707
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002708 /*
2709 * digitally-signed struct {
2710 * opaque md5_hash[16];
2711 * opaque sha_hash[20];
2712 * };
2713 *
2714 * md5_hash
2715 * MD5(ClientHello.random + ServerHello.random
2716 * + ServerParams);
2717 * sha_hash
2718 * SHA(ClientHello.random + ServerHello.random
2719 * + ServerParams);
2720 */
2721 md5_starts( &md5 );
2722 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002723 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002724 md5_finish( &md5, hash );
2725
2726 sha1_starts( &sha1 );
2727 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002728 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002729 sha1_finish( &sha1, hash + 16 );
2730
2731 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02002732
2733 md5_free( &md5 );
2734 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002735 }
2736 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002737#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2738 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002739#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2740 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002741 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002742 {
2743 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02002744 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002745
Paul Bakker84bbeb52014-07-01 14:53:22 +02002746 md_init( &ctx );
2747
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002748 /* Info from md_alg will be used instead */
2749 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002750
2751 /*
2752 * digitally-signed struct {
2753 * opaque client_random[32];
2754 * opaque server_random[32];
2755 * ServerDHParams params;
2756 * };
2757 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002758 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002759 {
2760 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2761 return( ret );
2762 }
2763
2764 md_starts( &ctx );
2765 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002766 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002767 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002768 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00002769 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002770 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002771#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2772 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002773 {
2774 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002775 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002776 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002777
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002778 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2779 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002780
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002781 /*
2782 * Make the signature
2783 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002784 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002785 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002786 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2787 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002788 }
Paul Bakker23f36802012-09-28 14:15:14 +00002789
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002790#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002791 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2792 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002793 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002794 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002795
2796 n += 2;
2797 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002798#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002799
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002800 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002801 p + 2 , &signature_len,
2802 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002803 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002804 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002805 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002806 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002807
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002808 *(p++) = (unsigned char)( signature_len >> 8 );
2809 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002810 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002811
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002812 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002813
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002814 p += signature_len;
2815 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002816 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002817#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002818 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2819 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002820
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002821 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002822 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2823 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2824
2825 ssl->state++;
2826
2827 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2828 {
2829 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2830 return( ret );
2831 }
2832
2833 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2834
2835 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002836}
2837
2838static int ssl_write_server_hello_done( ssl_context *ssl )
2839{
2840 int ret;
2841
2842 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2843
2844 ssl->out_msglen = 4;
2845 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2846 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2847
2848 ssl->state++;
2849
2850 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2851 {
2852 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2853 return( ret );
2854 }
2855
2856 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2857
2858 return( 0 );
2859}
2860
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002861#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2862 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2863static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2864 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002865{
2866 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002867 size_t n;
2868
2869 /*
2870 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2871 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002872 if( *p + 2 > end )
2873 {
2874 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2875 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2876 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002877
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002878 n = ( (*p)[0] << 8 ) | (*p)[1];
2879 *p += 2;
2880
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002881 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002882 {
2883 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2884 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2885 }
2886
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002887 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002888 {
2889 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2890 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2891 }
2892
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002893 *p += n;
2894
Paul Bakker70df2fb2013-04-17 17:19:09 +02002895 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2896
Paul Bakker70df2fb2013-04-17 17:19:09 +02002897 return( ret );
2898}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002899#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2900 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002901
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002902#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2903 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002904static int ssl_parse_encrypted_pms( ssl_context *ssl,
2905 const unsigned char *p,
2906 const unsigned char *end,
2907 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002908{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002909 int ret;
2910 size_t len = pk_get_len( ssl_own_key( ssl ) );
2911 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002912 unsigned char fake_pms[48], peer_pms[48];
2913 unsigned char mask;
Manuel Pégourié-Gonnardb26b75e2015-06-23 18:52:09 +02002914 size_t i, peer_pmslen;
2915 unsigned int diff;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002916
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002917 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002918 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002919 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002920 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2921 }
2922
2923 /*
2924 * Decrypt the premaster using own private RSA key
2925 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002926#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2927 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002928 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2929 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002930 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2931 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002932 {
2933 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2934 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2935 }
2936 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002937#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002938
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002939 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002940 {
2941 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2942 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2943 }
2944
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002945 /*
2946 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
2947 * must not cause the connection to end immediately; instead, send a
2948 * bad_record_mac later in the handshake.
2949 * Also, avoid data-dependant branches here to protect against
2950 * timing-based variants.
2951 */
2952 ret = ssl->f_rng( ssl->p_rng, fake_pms, sizeof( fake_pms ) );
2953 if( ret != 0 )
2954 return( ret );
2955
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002956 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02002957 peer_pms, &peer_pmslen,
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002958 sizeof( peer_pms ),
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002959 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002960
Manuel Pégourié-Gonnardb26b75e2015-06-23 18:52:09 +02002961 diff = (unsigned int) ret;
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02002962 diff |= peer_pmslen ^ 48;
2963 diff |= peer_pms[0] ^ ssl->handshake->max_major_ver;
2964 diff |= peer_pms[1] ^ ssl->handshake->max_minor_ver;
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002965
2966#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02002967 if( diff != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002968 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002969#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002970
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002971 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
2972 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
2973 {
2974 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2975 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002976 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002977 ssl->handshake->pmslen = 48;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002978
Manuel Pégourié-Gonnardb26b75e2015-06-23 18:52:09 +02002979 /* mask = diff ? 0xff : 0x00 */
2980 mask = - ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002981 for( i = 0; i < ssl->handshake->pmslen; i++ )
2982 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
2983
2984 return( 0 );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002985}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002986#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2987 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002988
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002989#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002990static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2991 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002992{
Paul Bakker6db455e2013-09-18 17:29:31 +02002993 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002994 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002995
Paul Bakker6db455e2013-09-18 17:29:31 +02002996 if( ssl->f_psk == NULL &&
2997 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2998 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002999 {
3000 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
3001 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3002 }
3003
3004 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003005 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003006 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003007 if( *p + 2 > end )
3008 {
3009 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3010 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3011 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003012
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003013 n = ( (*p)[0] << 8 ) | (*p)[1];
3014 *p += 2;
3015
3016 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003017 {
3018 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3019 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3020 }
3021
Paul Bakker6db455e2013-09-18 17:29:31 +02003022 if( ssl->f_psk != NULL )
3023 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003024 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003025 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3026 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003027 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003028 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003029 /* Identity is not a big secret since clients send it in the clear,
3030 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02003031 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003032 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003033 {
3034 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3035 }
3036 }
3037
3038 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003039 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003040 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02003041 if( ( ret = ssl_send_alert_message( ssl,
3042 SSL_ALERT_LEVEL_FATAL,
3043 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
3044 {
3045 return( ret );
3046 }
3047
3048 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003049 }
3050
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003051 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003052
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003053 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003054}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003055#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003056
Paul Bakker5121ce52009-01-03 21:22:43 +00003057static int ssl_parse_client_key_exchange( ssl_context *ssl )
3058{
Paul Bakker23986e52011-04-24 08:57:21 +00003059 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003060 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003061
Paul Bakker41c83d32013-03-20 14:39:14 +01003062 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003063
3064 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
3065
3066 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3067 {
3068 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3069 return( ret );
3070 }
3071
3072 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3073 {
3074 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003075 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003076 }
3077
3078 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
3079 {
3080 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003081 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003082 }
3083
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003084#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01003085 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003086 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003087 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003088 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003089
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003090 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003091 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02003092 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3093 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003094 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003095
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003096 if( p != end )
3097 {
3098 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3099 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3100 }
3101
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02003102 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003103
3104 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
3105 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003106 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02003107 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003108 {
3109 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
3110 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3111 }
3112
3113 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003114 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003115 else
3116#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003117#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003118 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3119 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3120 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003121 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003122 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
3123 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
3124 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003125 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003126 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003127 ssl->in_msg + 4, ssl->in_hslen - 4 ) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003128 {
3129 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3130 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3131 }
3132
3133 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3134
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003135 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
3136 &ssl->handshake->pmslen,
3137 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02003138 POLARSSL_MPI_MAX_SIZE,
3139 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003140 {
3141 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
3142 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3143 }
3144
3145 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003146 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003147 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003148#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003149 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3150 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3151 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003152#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
3153 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003154 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003155 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003156 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003157
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003158 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003159 {
3160 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3161 return( ret );
3162 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003163
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003164 if( p != end )
3165 {
3166 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3167 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3168 }
3169
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003170 if( ( ret = ssl_psk_derive_premaster( ssl,
3171 ciphersuite_info->key_exchange ) ) != 0 )
3172 {
3173 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3174 return( ret );
3175 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003176 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003177 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003178#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003179#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
3180 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
3181 {
3182 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003183 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003184
3185 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3186 {
3187 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3188 return( ret );
3189 }
3190
3191 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3192 {
3193 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
3194 return( ret );
3195 }
3196
3197 if( ( ret = ssl_psk_derive_premaster( ssl,
3198 ciphersuite_info->key_exchange ) ) != 0 )
3199 {
3200 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3201 return( ret );
3202 }
3203 }
3204 else
3205#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003206#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3207 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3208 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003209 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003210 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003211
3212 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3213 {
3214 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3215 return( ret );
3216 }
3217 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3218 {
3219 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3220 return( ret );
3221 }
3222
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003223 if( p != end )
3224 {
3225 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3226 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3227 }
3228
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003229 if( ( ret = ssl_psk_derive_premaster( ssl,
3230 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003231 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003232 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3233 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003234 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003235 }
3236 else
3237#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003238#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3239 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
3240 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003241 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003242 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003243
3244 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3245 {
3246 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3247 return( ret );
3248 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003249
3250 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
3251 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003252 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003253 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3254 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003255 }
3256
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003257 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3258
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003259 if( ( ret = ssl_psk_derive_premaster( ssl,
3260 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003261 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003262 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003263 return( ret );
3264 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003265 }
3266 else
3267#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003268#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
3269 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003270 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003271 if( ( ret = ssl_parse_encrypted_pms( ssl,
3272 ssl->in_msg + 4,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003273 ssl->in_msg + ssl->in_hslen,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003274 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003275 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003276 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003277 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003278 }
3279 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003280 else
3281#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
3282 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003283 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003284 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003285 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003286
Paul Bakkerff60ee62010-03-16 21:09:09 +00003287 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
3288 {
3289 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
3290 return( ret );
3291 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003292
Paul Bakker5121ce52009-01-03 21:22:43 +00003293 ssl->state++;
3294
3295 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3296
3297 return( 0 );
3298}
3299
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003300#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3301 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003302 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3303 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003304static int ssl_parse_certificate_verify( ssl_context *ssl )
3305{
Paul Bakkerfbb17802013-04-17 19:10:21 +02003306 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003307
3308 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3309
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003310 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003311 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003312 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003313 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02003314 {
3315 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3316 ssl->state++;
3317 return( 0 );
3318 }
3319
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003320 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3321 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003322}
3323#else
3324static int ssl_parse_certificate_verify( ssl_context *ssl )
3325{
3326 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003327 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003328 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003329 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003330 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003331#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003332 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003333#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003334 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003335 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3336
3337 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3338
3339 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003340 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003341 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003342 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3343 {
3344 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3345 ssl->state++;
3346 return( 0 );
3347 }
3348
Paul Bakkered27a042013-04-18 22:46:23 +02003349 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003350 {
3351 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3352 ssl->state++;
3353 return( 0 );
3354 }
3355
Paul Bakker48916f92012-09-16 19:57:18 +00003356 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003357
3358 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3359 {
3360 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3361 return( ret );
3362 }
3363
3364 ssl->state++;
3365
3366 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3367 {
3368 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003369 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003370 }
3371
3372 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
3373 {
3374 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003375 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003376 }
3377
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003378 /*
3379 * 0 . 0 handshake type
3380 * 1 . 3 handshake length
3381 * 4 . 5 sig alg (TLS 1.2 only)
3382 * 4+n . 5+n signature length (n = sa_len)
3383 * 6+n . 6+n+m signature (m = sig_len)
3384 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003385
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003386#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3387 defined(POLARSSL_SSL_PROTO_TLS1_1)
3388 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003389 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003390 sa_len = 0;
3391
Paul Bakkerc70b9822013-04-07 22:00:46 +02003392 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003393 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003394
3395 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3396 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3397 POLARSSL_PK_ECDSA ) )
3398 {
3399 hash_start += 16;
3400 hashlen -= 16;
3401 md_alg = POLARSSL_MD_SHA1;
3402 }
Paul Bakker926af752012-11-23 13:38:07 +01003403 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003404 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003405#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3406 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003407#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3408 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003409 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003410 sa_len = 2;
3411
Paul Bakker5121ce52009-01-03 21:22:43 +00003412 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003413 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003414 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003415 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003416 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003417 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3418 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003419 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3420 }
3421
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003422 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003423
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003424 /* Info from md_alg will be used instead */
3425 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003426
3427 /*
3428 * Signature
3429 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003430 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
3431 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003432 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003433 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3434 " for verify message" ) );
3435 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003436 }
3437
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003438 /*
3439 * Check the certificate's key type matches the signature alg
3440 */
3441 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3442 {
3443 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3444 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3445 }
Paul Bakker577e0062013-08-28 11:57:20 +02003446 }
3447 else
3448#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3449 {
3450 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003451 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003452 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003453
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003454 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01003455
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003456 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003457 {
3458 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003459 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003460 }
3461
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003462 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003463 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003464 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003465 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003466 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003467 return( ret );
3468 }
3469
3470 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3471
Paul Bakkered27a042013-04-18 22:46:23 +02003472 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003473}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003474#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3475 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3476 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003477
Paul Bakkera503a632013-08-14 13:48:06 +02003478#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003479static int ssl_write_new_session_ticket( ssl_context *ssl )
3480{
3481 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003482 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003483 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003484
3485 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3486
3487 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3488 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3489
3490 /*
3491 * struct {
3492 * uint32 ticket_lifetime_hint;
3493 * opaque ticket<0..2^16-1>;
3494 * } NewSessionTicket;
3495 *
3496 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3497 * 8 . 9 ticket_len (n)
3498 * 10 . 9+n ticket content
3499 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003500
3501 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3502 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3503 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3504 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003505
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003506 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3507 {
3508 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3509 tlen = 0;
3510 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003511
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003512 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3513 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003514
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003515 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003516
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003517 /*
3518 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3519 * ChangeCipherSpec share the same state.
3520 */
3521 ssl->handshake->new_session_ticket = 0;
3522
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003523 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3524 {
3525 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3526 return( ret );
3527 }
3528
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003529 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3530
3531 return( 0 );
3532}
Paul Bakkera503a632013-08-14 13:48:06 +02003533#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003534
Paul Bakker5121ce52009-01-03 21:22:43 +00003535/*
Paul Bakker1961b702013-01-25 14:49:24 +01003536 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003537 */
Paul Bakker1961b702013-01-25 14:49:24 +01003538int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003539{
3540 int ret = 0;
3541
Paul Bakker1961b702013-01-25 14:49:24 +01003542 if( ssl->state == SSL_HANDSHAKE_OVER )
3543 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003544
Paul Bakker1961b702013-01-25 14:49:24 +01003545 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3546
3547 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3548 return( ret );
3549
3550 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003551 {
Paul Bakker1961b702013-01-25 14:49:24 +01003552 case SSL_HELLO_REQUEST:
3553 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003554 break;
3555
Paul Bakker1961b702013-01-25 14:49:24 +01003556 /*
3557 * <== ClientHello
3558 */
3559 case SSL_CLIENT_HELLO:
3560 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003561 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003562
3563 /*
3564 * ==> ServerHello
3565 * Certificate
3566 * ( ServerKeyExchange )
3567 * ( CertificateRequest )
3568 * ServerHelloDone
3569 */
3570 case SSL_SERVER_HELLO:
3571 ret = ssl_write_server_hello( ssl );
3572 break;
3573
3574 case SSL_SERVER_CERTIFICATE:
3575 ret = ssl_write_certificate( ssl );
3576 break;
3577
3578 case SSL_SERVER_KEY_EXCHANGE:
3579 ret = ssl_write_server_key_exchange( ssl );
3580 break;
3581
3582 case SSL_CERTIFICATE_REQUEST:
3583 ret = ssl_write_certificate_request( ssl );
3584 break;
3585
3586 case SSL_SERVER_HELLO_DONE:
3587 ret = ssl_write_server_hello_done( ssl );
3588 break;
3589
3590 /*
3591 * <== ( Certificate/Alert )
3592 * ClientKeyExchange
3593 * ( CertificateVerify )
3594 * ChangeCipherSpec
3595 * Finished
3596 */
3597 case SSL_CLIENT_CERTIFICATE:
3598 ret = ssl_parse_certificate( ssl );
3599 break;
3600
3601 case SSL_CLIENT_KEY_EXCHANGE:
3602 ret = ssl_parse_client_key_exchange( ssl );
3603 break;
3604
3605 case SSL_CERTIFICATE_VERIFY:
3606 ret = ssl_parse_certificate_verify( ssl );
3607 break;
3608
3609 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3610 ret = ssl_parse_change_cipher_spec( ssl );
3611 break;
3612
3613 case SSL_CLIENT_FINISHED:
3614 ret = ssl_parse_finished( ssl );
3615 break;
3616
3617 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003618 * ==> ( NewSessionTicket )
3619 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003620 * Finished
3621 */
3622 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003623#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003624 if( ssl->handshake->new_session_ticket != 0 )
3625 ret = ssl_write_new_session_ticket( ssl );
3626 else
Paul Bakkera503a632013-08-14 13:48:06 +02003627#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003628 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003629 break;
3630
3631 case SSL_SERVER_FINISHED:
3632 ret = ssl_write_finished( ssl );
3633 break;
3634
3635 case SSL_FLUSH_BUFFERS:
3636 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3637 ssl->state = SSL_HANDSHAKE_WRAPUP;
3638 break;
3639
3640 case SSL_HANDSHAKE_WRAPUP:
3641 ssl_handshake_wrapup( ssl );
3642 break;
3643
3644 default:
3645 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3646 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003647 }
3648
Paul Bakker5121ce52009-01-03 21:22:43 +00003649 return( ret );
3650}
Paul Bakker9af723c2014-05-01 13:03:14 +02003651#endif /* POLARSSL_SSL_SRV_C */