blob: 90d5ac7ff0139476d9ae21de6adb7d4e5aef839e [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) && \
Simon Butcher7458bc32016-09-05 11:18:39 +03002289 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002290 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
Simon Butcher7458bc32016-09-05 11:18:39 +03002291 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)&& \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002292 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002293static int ssl_write_certificate_request( ssl_context *ssl )
2294{
Paul Bakkered27a042013-04-18 22:46:23 +02002295 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002296
2297 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2298
2299 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002300 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002301 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2302 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002303 {
2304 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2305 ssl->state++;
2306 return( 0 );
2307 }
2308
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002309 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2310 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002311}
2312#else
2313static int ssl_write_certificate_request( ssl_context *ssl )
2314{
2315 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2316 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002317 size_t dn_size, total_dn_size; /* excluding length bytes */
2318 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002319 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002320 const x509_crt *crt;
Manuel Pégourié-Gonnardde9c8a52015-10-02 11:16:47 +02002321 const unsigned char * const end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00002322
2323 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2324
2325 ssl->state++;
2326
Paul Bakkerfbb17802013-04-17 19:10:21 +02002327 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002328 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002329 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002330 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002331 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002332 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002333 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002334 return( 0 );
2335 }
2336
2337 /*
2338 * 0 . 0 handshake type
2339 * 1 . 3 handshake length
2340 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002341 * 5 .. m-1 cert types
2342 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002343 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002344 * n .. n+1 length of all DNs
2345 * n+2 .. n+3 length of DN 1
2346 * n+4 .. ... Distinguished Name #1
2347 * ... .. ... length of DN 2, etc.
2348 */
2349 buf = ssl->out_msg;
2350 p = buf + 4;
2351
2352 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002353 * Supported certificate types
2354 *
2355 * ClientCertificateType certificate_types<1..2^8-1>;
2356 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002357 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002358 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002359
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002360#if defined(POLARSSL_RSA_C)
2361 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2362#endif
2363#if defined(POLARSSL_ECDSA_C)
2364 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2365#endif
2366
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002367 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002368 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002369
Paul Bakker577e0062013-08-28 11:57:20 +02002370 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002371#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002372 /*
2373 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002374 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002375 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2376 *
2377 * struct {
2378 * HashAlgorithm hash;
2379 * SignatureAlgorithm signature;
2380 * } SignatureAndHashAlgorithm;
2381 *
2382 * enum { (255) } HashAlgorithm;
2383 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002384 */
Paul Bakker21dca692013-01-03 11:41:08 +01002385 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002386 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002387 /*
2388 * Only use current running hash algorithm that is already required
2389 * for requested ciphersuite.
2390 */
Paul Bakker926af752012-11-23 13:38:07 +01002391 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2392
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002393 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2394 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002395 {
2396 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2397 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002398
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002399 /*
2400 * Supported signature algorithms
2401 */
2402#if defined(POLARSSL_RSA_C)
2403 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2404 p[2 + sa_len++] = SSL_SIG_RSA;
2405#endif
2406#if defined(POLARSSL_ECDSA_C)
2407 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2408 p[2 + sa_len++] = SSL_SIG_ECDSA;
2409#endif
Paul Bakker926af752012-11-23 13:38:07 +01002410
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002411 p[0] = (unsigned char)( sa_len >> 8 );
2412 p[1] = (unsigned char)( sa_len );
2413 sa_len += 2;
2414 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002415 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002416#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002417
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002418 /*
2419 * DistinguishedName certificate_authorities<0..2^16-1>;
2420 * opaque DistinguishedName<1..2^16-1>;
2421 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002422 p += 2;
2423 crt = ssl->ca_chain;
2424
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002425 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002426 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002427 {
Paul Bakker926af752012-11-23 13:38:07 +01002428 dn_size = crt->subject_raw.len;
Manuel Pégourié-Gonnardde9c8a52015-10-02 11:16:47 +02002429
Manuel Pégourié-Gonnardcf16b792015-12-10 14:36:25 +01002430 if( end < p ||
2431 (size_t)( end - p ) < dn_size ||
2432 (size_t)( end - p ) < 2 + dn_size )
Manuel Pégourié-Gonnardde9c8a52015-10-02 11:16:47 +02002433 {
2434 SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) );
2435 break;
2436 }
2437
Paul Bakker926af752012-11-23 13:38:07 +01002438 *p++ = (unsigned char)( dn_size >> 8 );
2439 *p++ = (unsigned char)( dn_size );
2440 memcpy( p, crt->subject_raw.p, dn_size );
2441 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002442
Paul Bakker926af752012-11-23 13:38:07 +01002443 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2444
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002445 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002446 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002447 }
2448
Paul Bakker926af752012-11-23 13:38:07 +01002449 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002450 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2451 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002452 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2453 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002454
2455 ret = ssl_write_record( ssl );
2456
2457 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2458
2459 return( ret );
2460}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002461#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2462 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Simon Butcher7458bc32016-09-05 11:18:39 +03002463 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002464 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
Simon Butcher7458bc32016-09-05 11:18:39 +03002465 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002466 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002467
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002468#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2469 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2470static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2471{
2472 int ret;
2473
2474 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2475 {
2476 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2477 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2478 }
2479
2480 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2481 pk_ec( *ssl_own_key( ssl ) ),
2482 POLARSSL_ECDH_OURS ) ) != 0 )
2483 {
2484 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2485 return( ret );
2486 }
2487
2488 return( 0 );
2489}
2490#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2491 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2492
Paul Bakker41c83d32013-03-20 14:39:14 +01002493static int ssl_write_server_key_exchange( ssl_context *ssl )
2494{
Paul Bakker23986e52011-04-24 08:57:21 +00002495 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002496 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002497 const ssl_ciphersuite_t *ciphersuite_info =
2498 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002499
2500#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2501 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2502 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002503 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002504 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002505 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002506 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002507 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002508 ((void) dig_signed);
2509 ((void) dig_signed_len);
2510#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002511
Paul Bakker5121ce52009-01-03 21:22:43 +00002512 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2513
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002514#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2515 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2516 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002517 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2518 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2519 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002520 {
2521 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2522 ssl->state++;
2523 return( 0 );
2524 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002525#endif
2526
2527#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2528 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2529 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2530 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2531 {
2532 ssl_get_ecdh_params_from_cert( ssl );
2533
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002534 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002535 ssl->state++;
2536 return( 0 );
2537 }
2538#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002539
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002540#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2541 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2542 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2543 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002544 {
2545 /* TODO: Support identity hints */
2546 *(p++) = 0x00;
2547 *(p++) = 0x00;
2548
2549 n += 2;
2550 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002551#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2552 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002553
2554#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2555 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2556 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2557 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002558 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002559 /*
2560 * Ephemeral DH parameters:
2561 *
2562 * struct {
2563 * opaque dh_p<1..2^16-1>;
2564 * opaque dh_g<1..2^16-1>;
2565 * opaque dh_Ys<1..2^16-1>;
2566 * } ServerDHParams;
2567 */
2568 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2569 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2570 {
2571 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2572 return( ret );
2573 }
Paul Bakker48916f92012-09-16 19:57:18 +00002574
Paul Bakker41c83d32013-03-20 14:39:14 +01002575 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002576 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2577 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002578 {
2579 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2580 return( ret );
2581 }
2582
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002583 dig_signed = p;
2584 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002585
2586 p += len;
2587 n += len;
2588
Paul Bakker41c83d32013-03-20 14:39:14 +01002589 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2590 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2591 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2592 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2593 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002594#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2595 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002596
Gergely Budai987bfb52014-01-19 21:48:42 +01002597#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002598 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002599 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2600 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002601 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002602 /*
2603 * Ephemeral ECDH parameters:
2604 *
2605 * struct {
2606 * ECParameters curve_params;
2607 * ECPoint public;
2608 * } ServerECDHParams;
2609 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002610 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002611#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002612 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002613
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002614 /* Match our preference list against the offered curves */
2615 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2616 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2617 if( (*curve)->grp_id == *gid )
2618 goto curve_matching_done;
2619
2620curve_matching_done:
2621#else
2622 curve = ssl->handshake->curves;
2623#endif
2624
Manuel Pégourié-Gonnard8e8ae3d2015-06-23 18:57:28 +02002625 if( curve == NULL || *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002626 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002627 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2628 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002629 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002630
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002631 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002632
Paul Bakker41c83d32013-03-20 14:39:14 +01002633 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002634 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002635 {
2636 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2637 return( ret );
2638 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002639
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002640 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2641 p, SSL_MAX_CONTENT_LEN - n,
2642 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002643 {
2644 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2645 return( ret );
2646 }
2647
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002648 dig_signed = p;
2649 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002650
2651 p += len;
2652 n += len;
2653
Paul Bakker41c83d32013-03-20 14:39:14 +01002654 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2655 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002656#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002657
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002658#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002659 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2660 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002661 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002662 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2663 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002664 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002665 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002666 unsigned int hashlen = 0;
2667 unsigned char hash[64];
2668 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002669
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002670 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002671 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2672 */
Paul Bakker577e0062013-08-28 11:57:20 +02002673#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002674 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2675 {
2676 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2677
2678 if( md_alg == POLARSSL_MD_NONE )
2679 {
2680 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002681 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002682 }
2683 }
Paul Bakker577e0062013-08-28 11:57:20 +02002684 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002685#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002686#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2687 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002688 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002689 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2690 {
2691 md_alg = POLARSSL_MD_SHA1;
2692 }
2693 else
Paul Bakker577e0062013-08-28 11:57:20 +02002694#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002695 {
2696 md_alg = POLARSSL_MD_NONE;
2697 }
2698
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002699 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002700 * Compute the hash to be signed
2701 */
Paul Bakker577e0062013-08-28 11:57:20 +02002702#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2703 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002704 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002705 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002706 md5_context md5;
2707 sha1_context sha1;
2708
Paul Bakker5b4af392014-06-26 12:09:34 +02002709 md5_init( &md5 );
2710 sha1_init( &sha1 );
2711
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002712 /*
2713 * digitally-signed struct {
2714 * opaque md5_hash[16];
2715 * opaque sha_hash[20];
2716 * };
2717 *
2718 * md5_hash
2719 * MD5(ClientHello.random + ServerHello.random
2720 * + ServerParams);
2721 * sha_hash
2722 * SHA(ClientHello.random + ServerHello.random
2723 * + ServerParams);
2724 */
2725 md5_starts( &md5 );
2726 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002727 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002728 md5_finish( &md5, hash );
2729
2730 sha1_starts( &sha1 );
2731 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002732 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002733 sha1_finish( &sha1, hash + 16 );
2734
2735 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02002736
2737 md5_free( &md5 );
2738 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002739 }
2740 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002741#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2742 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002743#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2744 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002745 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002746 {
2747 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02002748 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002749
Paul Bakker84bbeb52014-07-01 14:53:22 +02002750 md_init( &ctx );
2751
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002752 /* Info from md_alg will be used instead */
2753 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002754
2755 /*
2756 * digitally-signed struct {
2757 * opaque client_random[32];
2758 * opaque server_random[32];
2759 * ServerDHParams params;
2760 * };
2761 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002762 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002763 {
2764 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2765 return( ret );
2766 }
2767
2768 md_starts( &ctx );
2769 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002770 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002771 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002772 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00002773 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002774 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002775#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2776 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002777 {
2778 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002779 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002780 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002781
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002782 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2783 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002784
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002785 /*
2786 * Make the signature
2787 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002788 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002789 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002790 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2791 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002792 }
Paul Bakker23f36802012-09-28 14:15:14 +00002793
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002794#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002795 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2796 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002797 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002798 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002799
2800 n += 2;
2801 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002802#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002803
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002804 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002805 p + 2 , &signature_len,
2806 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002807 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002808 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002809 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002810 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002811
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002812 *(p++) = (unsigned char)( signature_len >> 8 );
2813 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002814 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002815
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002816 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002817
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002818 p += signature_len;
2819 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002820 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002821#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002822 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2823 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002824
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002825 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002826 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2827 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2828
2829 ssl->state++;
2830
2831 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2832 {
2833 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2834 return( ret );
2835 }
2836
2837 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2838
2839 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002840}
2841
2842static int ssl_write_server_hello_done( ssl_context *ssl )
2843{
2844 int ret;
2845
2846 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2847
2848 ssl->out_msglen = 4;
2849 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2850 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2851
2852 ssl->state++;
2853
2854 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2855 {
2856 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2857 return( ret );
2858 }
2859
2860 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2861
2862 return( 0 );
2863}
2864
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002865#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2866 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2867static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2868 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002869{
2870 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002871 size_t n;
2872
2873 /*
2874 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2875 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002876 if( *p + 2 > end )
2877 {
2878 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2879 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2880 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002881
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002882 n = ( (*p)[0] << 8 ) | (*p)[1];
2883 *p += 2;
2884
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002885 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002886 {
2887 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2888 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2889 }
2890
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002891 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002892 {
2893 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2894 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2895 }
2896
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002897 *p += n;
2898
Paul Bakker70df2fb2013-04-17 17:19:09 +02002899 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2900
Paul Bakker70df2fb2013-04-17 17:19:09 +02002901 return( ret );
2902}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002903#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2904 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002905
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002906#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2907 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002908static int ssl_parse_encrypted_pms( ssl_context *ssl,
2909 const unsigned char *p,
2910 const unsigned char *end,
2911 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002912{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002913 int ret;
2914 size_t len = pk_get_len( ssl_own_key( ssl ) );
2915 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002916 unsigned char fake_pms[48], peer_pms[48];
2917 unsigned char mask;
Manuel Pégourié-Gonnardb26b75e2015-06-23 18:52:09 +02002918 size_t i, peer_pmslen;
2919 unsigned int diff;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002920
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002921 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002922 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002923 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002924 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2925 }
2926
2927 /*
2928 * Decrypt the premaster using own private RSA key
2929 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002930#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2931 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002932 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2933 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002934 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2935 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002936 {
2937 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2938 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2939 }
2940 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002941#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002942
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002943 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002944 {
2945 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2946 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2947 }
2948
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002949 /*
2950 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
2951 * must not cause the connection to end immediately; instead, send a
2952 * bad_record_mac later in the handshake.
2953 * Also, avoid data-dependant branches here to protect against
2954 * timing-based variants.
2955 */
2956 ret = ssl->f_rng( ssl->p_rng, fake_pms, sizeof( fake_pms ) );
2957 if( ret != 0 )
2958 return( ret );
2959
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002960 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02002961 peer_pms, &peer_pmslen,
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002962 sizeof( peer_pms ),
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002963 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002964
Manuel Pégourié-Gonnardb26b75e2015-06-23 18:52:09 +02002965 diff = (unsigned int) ret;
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02002966 diff |= peer_pmslen ^ 48;
2967 diff |= peer_pms[0] ^ ssl->handshake->max_major_ver;
2968 diff |= peer_pms[1] ^ ssl->handshake->max_minor_ver;
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002969
2970#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02002971 if( diff != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002972 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002973#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002974
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002975 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
2976 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
2977 {
2978 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2979 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002980 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002981 ssl->handshake->pmslen = 48;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002982
Manuel Pégourié-Gonnardb26b75e2015-06-23 18:52:09 +02002983 /* mask = diff ? 0xff : 0x00 */
Simon Ba697bf52016-11-10 13:19:42 +00002984 /* MSVC has a warning about unary minus on unsigned, but this is
2985 * well-defined and precisely what we want to do here */
2986#if defined(_MSC_VER)
2987#pragma warning( push )
2988#pragma warning( disable : 4146 )
2989#endif
Manuel Pégourié-Gonnardb26b75e2015-06-23 18:52:09 +02002990 mask = - ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 );
Simon Ba697bf52016-11-10 13:19:42 +00002991#if defined(_MSC_VER)
2992#pragma warning( pop )
2993#endif
2994
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002995 for( i = 0; i < ssl->handshake->pmslen; i++ )
2996 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
2997
2998 return( 0 );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002999}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003000#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
3001 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003002
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003003#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003004static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
3005 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003006{
Paul Bakker6db455e2013-09-18 17:29:31 +02003007 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003008 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003009
Paul Bakker6db455e2013-09-18 17:29:31 +02003010 if( ssl->f_psk == NULL &&
3011 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
3012 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003013 {
3014 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
3015 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3016 }
3017
3018 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003019 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003020 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003021 if( *p + 2 > end )
3022 {
3023 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3024 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3025 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003026
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003027 n = ( (*p)[0] << 8 ) | (*p)[1];
3028 *p += 2;
3029
3030 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003031 {
3032 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3033 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3034 }
3035
Paul Bakker6db455e2013-09-18 17:29:31 +02003036 if( ssl->f_psk != NULL )
3037 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003038 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003039 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3040 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003041 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003042 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003043 /* Identity is not a big secret since clients send it in the clear,
3044 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02003045 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003046 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003047 {
3048 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3049 }
3050 }
3051
3052 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003053 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003054 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02003055 if( ( ret = ssl_send_alert_message( ssl,
3056 SSL_ALERT_LEVEL_FATAL,
3057 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
3058 {
3059 return( ret );
3060 }
3061
3062 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003063 }
3064
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003065 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003066
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003067 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003068}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003069#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003070
Paul Bakker5121ce52009-01-03 21:22:43 +00003071static int ssl_parse_client_key_exchange( ssl_context *ssl )
3072{
Paul Bakker23986e52011-04-24 08:57:21 +00003073 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003074 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003075
Paul Bakker41c83d32013-03-20 14:39:14 +01003076 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003077
3078 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
3079
3080 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3081 {
3082 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3083 return( ret );
3084 }
3085
3086 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3087 {
3088 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003089 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003090 }
3091
3092 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
3093 {
3094 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003095 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003096 }
3097
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003098#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01003099 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003100 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003101 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003102 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003103
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003104 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003105 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02003106 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3107 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003108 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003109
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003110 if( p != end )
3111 {
3112 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3113 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3114 }
3115
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02003116 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003117
3118 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
3119 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003120 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02003121 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003122 {
3123 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
3124 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3125 }
3126
3127 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003128 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003129 else
3130#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003131#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003132 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3133 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3134 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003135 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003136 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
3137 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
3138 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003139 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003140 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003141 ssl->in_msg + 4, ssl->in_hslen - 4 ) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003142 {
3143 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3144 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3145 }
3146
3147 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3148
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003149 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
3150 &ssl->handshake->pmslen,
3151 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02003152 POLARSSL_MPI_MAX_SIZE,
3153 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003154 {
3155 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
3156 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3157 }
3158
3159 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003160 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003161 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003162#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003163 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3164 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3165 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003166#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
3167 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003168 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003169 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003170 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003171
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003172 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003173 {
3174 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3175 return( ret );
3176 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003177
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003178 if( p != end )
3179 {
3180 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3181 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3182 }
3183
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003184 if( ( ret = ssl_psk_derive_premaster( ssl,
3185 ciphersuite_info->key_exchange ) ) != 0 )
3186 {
3187 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3188 return( ret );
3189 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003190 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003191 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003192#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003193#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
3194 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
3195 {
3196 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003197 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003198
3199 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3200 {
3201 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3202 return( ret );
3203 }
3204
3205 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3206 {
3207 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
3208 return( ret );
3209 }
3210
3211 if( ( ret = ssl_psk_derive_premaster( ssl,
3212 ciphersuite_info->key_exchange ) ) != 0 )
3213 {
3214 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3215 return( ret );
3216 }
3217 }
3218 else
3219#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003220#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3221 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3222 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003223 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003224 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003225
3226 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3227 {
3228 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3229 return( ret );
3230 }
3231 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3232 {
3233 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3234 return( ret );
3235 }
3236
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003237 if( p != end )
3238 {
3239 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3240 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3241 }
3242
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003243 if( ( ret = ssl_psk_derive_premaster( ssl,
3244 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003245 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003246 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3247 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003248 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003249 }
3250 else
3251#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003252#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3253 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
3254 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003255 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003256 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003257
3258 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3259 {
3260 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3261 return( ret );
3262 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003263
3264 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
3265 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003266 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003267 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3268 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003269 }
3270
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003271 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3272
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003273 if( ( ret = ssl_psk_derive_premaster( ssl,
3274 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003275 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003276 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003277 return( ret );
3278 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003279 }
3280 else
3281#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003282#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
3283 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003284 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003285 if( ( ret = ssl_parse_encrypted_pms( ssl,
3286 ssl->in_msg + 4,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003287 ssl->in_msg + ssl->in_hslen,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003288 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003289 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003290 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003291 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003292 }
3293 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003294 else
3295#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
3296 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003297 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003298 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003299 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003300
Paul Bakkerff60ee62010-03-16 21:09:09 +00003301 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
3302 {
3303 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
3304 return( ret );
3305 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003306
Paul Bakker5121ce52009-01-03 21:22:43 +00003307 ssl->state++;
3308
3309 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3310
3311 return( 0 );
3312}
3313
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003314#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3315 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Simon Butcher7458bc32016-09-05 11:18:39 +03003316 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003317 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
Simon Butcher7458bc32016-09-05 11:18:39 +03003318 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)&& \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003319 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003320static int ssl_parse_certificate_verify( ssl_context *ssl )
3321{
Paul Bakkerfbb17802013-04-17 19:10:21 +02003322 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003323
3324 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3325
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003326 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003327 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003328 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003329 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02003330 {
3331 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3332 ssl->state++;
3333 return( 0 );
3334 }
3335
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003336 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3337 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003338}
3339#else
3340static int ssl_parse_certificate_verify( ssl_context *ssl )
3341{
3342 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003343 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003344 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003345 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003346 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003347#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003348 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003349#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003350 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003351 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3352
3353 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3354
3355 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003356 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003357 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003358 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3359 {
3360 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3361 ssl->state++;
3362 return( 0 );
3363 }
3364
Paul Bakkered27a042013-04-18 22:46:23 +02003365 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003366 {
3367 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3368 ssl->state++;
3369 return( 0 );
3370 }
3371
Paul Bakker48916f92012-09-16 19:57:18 +00003372 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003373
3374 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3375 {
3376 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3377 return( ret );
3378 }
3379
3380 ssl->state++;
3381
3382 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3383 {
3384 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003385 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003386 }
3387
3388 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
3389 {
3390 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003391 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003392 }
3393
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003394 /*
3395 * 0 . 0 handshake type
3396 * 1 . 3 handshake length
3397 * 4 . 5 sig alg (TLS 1.2 only)
3398 * 4+n . 5+n signature length (n = sa_len)
3399 * 6+n . 6+n+m signature (m = sig_len)
3400 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003401
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003402#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3403 defined(POLARSSL_SSL_PROTO_TLS1_1)
3404 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003405 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003406 sa_len = 0;
3407
Paul Bakkerc70b9822013-04-07 22:00:46 +02003408 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003409 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003410
3411 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3412 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3413 POLARSSL_PK_ECDSA ) )
3414 {
3415 hash_start += 16;
3416 hashlen -= 16;
3417 md_alg = POLARSSL_MD_SHA1;
3418 }
Paul Bakker926af752012-11-23 13:38:07 +01003419 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003420 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003421#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3422 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003423#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3424 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003425 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003426 sa_len = 2;
3427
Paul Bakker5121ce52009-01-03 21:22:43 +00003428 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003429 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003430 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003431 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003432 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003433 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3434 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003435 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3436 }
3437
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003438 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003439
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003440 /* Info from md_alg will be used instead */
3441 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003442
3443 /*
3444 * Signature
3445 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003446 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
3447 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003448 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003449 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3450 " for verify message" ) );
3451 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003452 }
3453
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003454 /*
3455 * Check the certificate's key type matches the signature alg
3456 */
3457 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3458 {
3459 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3460 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3461 }
Paul Bakker577e0062013-08-28 11:57:20 +02003462 }
3463 else
3464#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3465 {
3466 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003467 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003468 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003469
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003470 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01003471
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003472 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003473 {
3474 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003475 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003476 }
3477
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003478 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003479 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003480 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003481 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003482 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003483 return( ret );
3484 }
3485
3486 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3487
Paul Bakkered27a042013-04-18 22:46:23 +02003488 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003489}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003490#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3491 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Simon Butcher7458bc32016-09-05 11:18:39 +03003492 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED &&
3493 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
3494 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED &&
3495 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003496
Paul Bakkera503a632013-08-14 13:48:06 +02003497#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003498static int ssl_write_new_session_ticket( ssl_context *ssl )
3499{
3500 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003501 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003502 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003503
3504 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3505
3506 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3507 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3508
3509 /*
3510 * struct {
3511 * uint32 ticket_lifetime_hint;
3512 * opaque ticket<0..2^16-1>;
3513 * } NewSessionTicket;
3514 *
3515 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3516 * 8 . 9 ticket_len (n)
3517 * 10 . 9+n ticket content
3518 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003519
3520 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3521 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3522 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3523 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003524
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003525 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3526 {
3527 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3528 tlen = 0;
3529 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003530
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003531 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3532 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003533
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003534 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003535
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003536 /*
3537 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3538 * ChangeCipherSpec share the same state.
3539 */
3540 ssl->handshake->new_session_ticket = 0;
3541
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003542 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3543 {
3544 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3545 return( ret );
3546 }
3547
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003548 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3549
3550 return( 0 );
3551}
Paul Bakkera503a632013-08-14 13:48:06 +02003552#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003553
Paul Bakker5121ce52009-01-03 21:22:43 +00003554/*
Paul Bakker1961b702013-01-25 14:49:24 +01003555 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003556 */
Paul Bakker1961b702013-01-25 14:49:24 +01003557int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003558{
3559 int ret = 0;
3560
Paul Bakker1961b702013-01-25 14:49:24 +01003561 if( ssl->state == SSL_HANDSHAKE_OVER )
3562 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003563
Paul Bakker1961b702013-01-25 14:49:24 +01003564 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3565
3566 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3567 return( ret );
3568
3569 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003570 {
Paul Bakker1961b702013-01-25 14:49:24 +01003571 case SSL_HELLO_REQUEST:
3572 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003573 break;
3574
Paul Bakker1961b702013-01-25 14:49:24 +01003575 /*
3576 * <== ClientHello
3577 */
3578 case SSL_CLIENT_HELLO:
3579 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003580 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003581
3582 /*
3583 * ==> ServerHello
3584 * Certificate
3585 * ( ServerKeyExchange )
3586 * ( CertificateRequest )
3587 * ServerHelloDone
3588 */
3589 case SSL_SERVER_HELLO:
3590 ret = ssl_write_server_hello( ssl );
3591 break;
3592
3593 case SSL_SERVER_CERTIFICATE:
3594 ret = ssl_write_certificate( ssl );
3595 break;
3596
3597 case SSL_SERVER_KEY_EXCHANGE:
3598 ret = ssl_write_server_key_exchange( ssl );
3599 break;
3600
3601 case SSL_CERTIFICATE_REQUEST:
3602 ret = ssl_write_certificate_request( ssl );
3603 break;
3604
3605 case SSL_SERVER_HELLO_DONE:
3606 ret = ssl_write_server_hello_done( ssl );
3607 break;
3608
3609 /*
3610 * <== ( Certificate/Alert )
3611 * ClientKeyExchange
3612 * ( CertificateVerify )
3613 * ChangeCipherSpec
3614 * Finished
3615 */
3616 case SSL_CLIENT_CERTIFICATE:
3617 ret = ssl_parse_certificate( ssl );
3618 break;
3619
3620 case SSL_CLIENT_KEY_EXCHANGE:
3621 ret = ssl_parse_client_key_exchange( ssl );
3622 break;
3623
3624 case SSL_CERTIFICATE_VERIFY:
3625 ret = ssl_parse_certificate_verify( ssl );
3626 break;
3627
3628 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3629 ret = ssl_parse_change_cipher_spec( ssl );
3630 break;
3631
3632 case SSL_CLIENT_FINISHED:
3633 ret = ssl_parse_finished( ssl );
3634 break;
3635
3636 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003637 * ==> ( NewSessionTicket )
3638 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003639 * Finished
3640 */
3641 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003642#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003643 if( ssl->handshake->new_session_ticket != 0 )
3644 ret = ssl_write_new_session_ticket( ssl );
3645 else
Paul Bakkera503a632013-08-14 13:48:06 +02003646#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003647 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003648 break;
3649
3650 case SSL_SERVER_FINISHED:
3651 ret = ssl_write_finished( ssl );
3652 break;
3653
3654 case SSL_FLUSH_BUFFERS:
3655 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3656 ssl->state = SSL_HANDSHAKE_WRAPUP;
3657 break;
3658
3659 case SSL_HANDSHAKE_WRAPUP:
3660 ssl_handshake_wrapup( ssl );
3661 break;
3662
3663 default:
3664 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3665 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003666 }
3667
Paul Bakker5121ce52009-01-03 21:22:43 +00003668 return( ret );
3669}
Paul Bakker9af723c2014-05-01 13:03:14 +02003670#endif /* POLARSSL_SSL_SRV_C */