blob: 473f87d08a0a2709d53ac95a9d0710c32a3025d1 [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
Paul Bakker48916f92012-09-16 19:57:18 +00001567 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001568
1569 while( ext_len )
1570 {
1571 unsigned int ext_id = ( ( ext[0] << 8 )
1572 | ( ext[1] ) );
1573 unsigned int ext_size = ( ( ext[2] << 8 )
1574 | ( ext[3] ) );
1575
1576 if( ext_size + 4 > ext_len )
1577 {
1578 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1579 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1580 }
1581 switch( ext_id )
1582 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001583#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001584 case TLS_EXT_SERVERNAME:
1585 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1586 if( ssl->f_sni == NULL )
1587 break;
1588
1589 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1590 if( ret != 0 )
1591 return( ret );
1592 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001593#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001594
Paul Bakker48916f92012-09-16 19:57:18 +00001595 case TLS_EXT_RENEGOTIATION_INFO:
1596 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001597#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001598 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001599#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001600
Paul Bakker23f36802012-09-28 14:15:14 +00001601 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1602 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001603 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001604 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001605
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001606#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1607 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakker23f36802012-09-28 14:15:14 +00001608 case TLS_EXT_SIG_ALG:
1609 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001610#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker23f36802012-09-28 14:15:14 +00001611 if( ssl->renegotiation == SSL_RENEGOTIATION )
1612 break;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001613#endif
Paul Bakker23f36802012-09-28 14:15:14 +00001614
1615 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1616 if( ret != 0 )
1617 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001618 break;
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001619#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
1620 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001621
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001622#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001623 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1624 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1625
1626 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1627 if( ret != 0 )
1628 return( ret );
1629 break;
1630
1631 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1632 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001633 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001634
1635 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1636 if( ret != 0 )
1637 return( ret );
1638 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001639#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001640
Paul Bakker05decb22013-08-15 13:33:48 +02001641#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001642 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1643 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1644
1645 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1646 if( ret != 0 )
1647 return( ret );
1648 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001649#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001650
Paul Bakker1f2bc622013-08-15 13:45:55 +02001651#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001652 case TLS_EXT_TRUNCATED_HMAC:
1653 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1654
1655 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1656 if( ret != 0 )
1657 return( ret );
1658 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001659#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001660
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001661#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1662 case TLS_EXT_ENCRYPT_THEN_MAC:
1663 SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
1664
1665 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1666 if( ret != 0 )
1667 return( ret );
1668 break;
1669#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1670
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001671#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1672 case TLS_EXT_EXTENDED_MASTER_SECRET:
1673 SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
1674
1675 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1676 if( ret != 0 )
1677 return( ret );
1678 break;
1679#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1680
Paul Bakkera503a632013-08-14 13:48:06 +02001681#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001682 case TLS_EXT_SESSION_TICKET:
1683 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1684
1685 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1686 if( ret != 0 )
1687 return( ret );
1688 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001689#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001690
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001691#if defined(POLARSSL_SSL_ALPN)
1692 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001693 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001694
1695 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1696 if( ret != 0 )
1697 return( ret );
1698 break;
1699#endif /* POLARSSL_SSL_SESSION_TICKETS */
1700
Paul Bakker48916f92012-09-16 19:57:18 +00001701 default:
1702 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1703 ext_id ) );
1704 }
1705
1706 ext_len -= 4 + ext_size;
1707 ext += 4 + ext_size;
1708
1709 if( ext_len > 0 && ext_len < 4 )
1710 {
1711 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1712 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1713 }
1714 }
1715
1716 /*
1717 * Renegotiation security checks
1718 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001719 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001720 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1721 {
1722 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1723 handshake_failure = 1;
1724 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001725#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001726 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1727 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1728 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001729 {
1730 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001731 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001732 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001733 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1734 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1735 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001736 {
1737 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001738 handshake_failure = 1;
1739 }
1740 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1741 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1742 renegotiation_info_seen == 1 )
1743 {
1744 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1745 handshake_failure = 1;
1746 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001747#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001748
1749 if( handshake_failure == 1 )
1750 {
1751 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1752 return( ret );
1753
Paul Bakker48916f92012-09-16 19:57:18 +00001754 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1755 }
Paul Bakker380da532012-04-18 16:10:25 +00001756
Paul Bakker41c83d32013-03-20 14:39:14 +01001757 /*
1758 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001759 * (At the end because we need information from the EC-based extensions
1760 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001761 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001762 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001763 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001764 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001765#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1766 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
1767 {
1768 for( i = 0; ciphersuites[i] != 0; i++ )
1769#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001770 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001771 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001772 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001773#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001774 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001775 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1776 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1777 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001778
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001779 got_common_suite = 1;
1780
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001781 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1782 &ciphersuite_info ) ) != 0 )
1783 return( ret );
1784
1785 if( ciphersuite_info != NULL )
1786 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001787 }
1788 }
1789
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001790 if( got_common_suite )
1791 {
1792 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1793 "but none of them usable" ) );
1794 ssl_send_fatal_handshake_failure( ssl );
1795 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
1796 }
1797 else
1798 {
1799 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1800 ssl_send_fatal_handshake_failure( ssl );
1801 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1802 }
Paul Bakker41c83d32013-03-20 14:39:14 +01001803
1804have_ciphersuite:
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001805 SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
1806
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001807 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001808 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1809 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1810
Paul Bakker5121ce52009-01-03 21:22:43 +00001811 ssl->in_left = 0;
1812 ssl->state++;
1813
1814 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1815
1816 return( 0 );
1817}
1818
Paul Bakker1f2bc622013-08-15 13:45:55 +02001819#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001820static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1821 unsigned char *buf,
1822 size_t *olen )
1823{
1824 unsigned char *p = buf;
1825
1826 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1827 {
1828 *olen = 0;
1829 return;
1830 }
1831
1832 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1833
1834 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1835 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1836
1837 *p++ = 0x00;
1838 *p++ = 0x00;
1839
1840 *olen = 4;
1841}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001842#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001843
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001844#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1845static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
1846 unsigned char *buf,
1847 size_t *olen )
1848{
1849 unsigned char *p = buf;
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01001850 const ssl_ciphersuite_t *suite = NULL;
1851 const cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001852
1853 if( ssl->session_negotiate->encrypt_then_mac == SSL_EXTENDED_MS_DISABLED ||
1854 ssl->minor_ver == SSL_MINOR_VERSION_0 )
1855 {
1856 *olen = 0;
1857 return;
1858 }
1859
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01001860 /*
1861 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
1862 * from a client and then selects a stream or Authenticated Encryption
1863 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
1864 * encrypt-then-MAC response extension back to the client."
1865 */
1866 if( ( suite = ssl_ciphersuite_from_id(
1867 ssl->session_negotiate->ciphersuite ) ) == NULL ||
1868 ( cipher = cipher_info_from_type( suite->cipher ) ) == NULL ||
1869 cipher->mode != POLARSSL_MODE_CBC )
1870 {
1871 *olen = 0;
1872 return;
1873 }
1874
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001875 SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
1876
1877 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
1878 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
1879
1880 *p++ = 0x00;
1881 *p++ = 0x00;
1882
1883 *olen = 4;
1884}
1885#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1886
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001887#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1888static void ssl_write_extended_ms_ext( ssl_context *ssl,
1889 unsigned char *buf,
1890 size_t *olen )
1891{
1892 unsigned char *p = buf;
1893
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +02001894 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_DISABLED ||
1895 ssl->minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001896 {
1897 *olen = 0;
1898 return;
1899 }
1900
1901 SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
1902 "extension" ) );
1903
1904 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
1905 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
1906
1907 *p++ = 0x00;
1908 *p++ = 0x00;
1909
1910 *olen = 4;
1911}
1912#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1913
Paul Bakkera503a632013-08-14 13:48:06 +02001914#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001915static void ssl_write_session_ticket_ext( ssl_context *ssl,
1916 unsigned char *buf,
1917 size_t *olen )
1918{
1919 unsigned char *p = buf;
1920
1921 if( ssl->handshake->new_session_ticket == 0 )
1922 {
1923 *olen = 0;
1924 return;
1925 }
1926
1927 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1928
1929 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1930 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1931
1932 *p++ = 0x00;
1933 *p++ = 0x00;
1934
1935 *olen = 4;
1936}
Paul Bakkera503a632013-08-14 13:48:06 +02001937#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001938
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001939static void ssl_write_renegotiation_ext( ssl_context *ssl,
1940 unsigned char *buf,
1941 size_t *olen )
1942{
1943 unsigned char *p = buf;
1944
1945 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1946 {
1947 *olen = 0;
1948 return;
1949 }
1950
1951 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1952
1953 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1954 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1955
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001956#if defined(POLARSSL_SSL_RENEGOTIATION)
1957 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
1958 {
1959 *p++ = 0x00;
1960 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1961 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001962
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001963 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1964 p += ssl->verify_data_len;
1965 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1966 p += ssl->verify_data_len;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001967
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001968 *olen = 5 + ssl->verify_data_len * 2;
1969 }
1970 else
1971#endif /* POLARSSL_SSL_RENEGOTIATION */
1972 {
1973 *p++ = 0x00;
1974 *p++ = 0x01;
1975 *p++ = 0x00;
1976
1977 *olen = 5;
1978 }
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001979}
1980
Paul Bakker05decb22013-08-15 13:33:48 +02001981#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001982static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1983 unsigned char *buf,
1984 size_t *olen )
1985{
1986 unsigned char *p = buf;
1987
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001988 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1989 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001990 *olen = 0;
1991 return;
1992 }
1993
1994 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1995
1996 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1997 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1998
1999 *p++ = 0x00;
2000 *p++ = 1;
2001
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002002 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002003
2004 *olen = 5;
2005}
Paul Bakker05decb22013-08-15 13:33:48 +02002006#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002007
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002008#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002009static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
2010 unsigned char *buf,
2011 size_t *olen )
2012{
2013 unsigned char *p = buf;
2014 ((void) ssl);
2015
Paul Bakker677377f2013-10-28 12:54:26 +01002016 if( ( ssl->handshake->cli_exts &
2017 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
2018 {
2019 *olen = 0;
2020 return;
2021 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002022
2023 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
2024
2025 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2026 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
2027
2028 *p++ = 0x00;
2029 *p++ = 2;
2030
2031 *p++ = 1;
2032 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
2033
2034 *olen = 6;
2035}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002036#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002037
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002038#if defined(POLARSSL_SSL_ALPN )
2039static void ssl_write_alpn_ext( ssl_context *ssl,
2040 unsigned char *buf, size_t *olen )
2041{
2042 if( ssl->alpn_chosen == NULL )
2043 {
2044 *olen = 0;
2045 return;
2046 }
2047
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02002048 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002049
2050 /*
2051 * 0 . 1 ext identifier
2052 * 2 . 3 ext length
2053 * 4 . 5 protocol list length
2054 * 6 . 6 protocol name length
2055 * 7 . 7+n protocol name
2056 */
2057 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
2058 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
2059
2060 *olen = 7 + strlen( ssl->alpn_chosen );
2061
2062 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2063 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2064
2065 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2066 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2067
2068 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2069
2070 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2071}
2072#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
2073
Paul Bakker5121ce52009-01-03 21:22:43 +00002074static int ssl_write_server_hello( ssl_context *ssl )
2075{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002076#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002077 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002078#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002079 int ret;
2080 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002081 unsigned char *buf, *p;
2082
2083 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
2084
Paul Bakkera9a028e2013-11-21 17:31:06 +01002085 if( ssl->f_rng == NULL )
2086 {
2087 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2088 return( POLARSSL_ERR_SSL_NO_RNG );
2089 }
2090
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 /*
2092 * 0 . 0 handshake type
2093 * 1 . 3 handshake length
2094 * 4 . 5 protocol version
2095 * 6 . 9 UNIX time()
2096 * 10 . 37 random bytes
2097 */
2098 buf = ssl->out_msg;
2099 p = buf + 4;
2100
2101 *p++ = (unsigned char) ssl->major_ver;
2102 *p++ = (unsigned char) ssl->minor_ver;
2103
2104 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
2105 buf[4], buf[5] ) );
2106
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002107#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 t = time( NULL );
2109 *p++ = (unsigned char)( t >> 24 );
2110 *p++ = (unsigned char)( t >> 16 );
2111 *p++ = (unsigned char)( t >> 8 );
2112 *p++ = (unsigned char)( t );
2113
2114 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002115#else
2116 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
2117 return( ret );
2118
2119 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02002120#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002121
Paul Bakkera3d195c2011-11-27 21:07:34 +00002122 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
2123 return( ret );
2124
2125 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002126
Paul Bakker48916f92012-09-16 19:57:18 +00002127 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002128
2129 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
2130
2131 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002132 * Resume is 0 by default, see ssl_handshake_init().
2133 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2134 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002135 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002136 if( ssl->handshake->resume == 0 &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002137#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002138 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002139#endif
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002140 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002141 ssl->f_get_cache != NULL &&
2142 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
2143 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01002144 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002145 ssl->handshake->resume = 1;
2146 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002147
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002148 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002149 {
2150 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002151 * New session, create a new session id,
2152 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002153 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002154 ssl->state++;
2155
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002156#if defined(POLARSSL_HAVE_TIME)
2157 ssl->session_negotiate->start = time( NULL );
2158#endif
2159
Paul Bakkera503a632013-08-14 13:48:06 +02002160#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002161 if( ssl->handshake->new_session_ticket != 0 )
2162 {
2163 ssl->session_negotiate->length = n = 0;
2164 memset( ssl->session_negotiate->id, 0, 32 );
2165 }
2166 else
2167#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002168 {
2169 ssl->session_negotiate->length = n = 32;
2170 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002171 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002172 return( ret );
2173 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002174 }
2175 else
2176 {
2177 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002178 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002179 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02002180 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002181 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002182
2183 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2184 {
2185 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2186 return( ret );
2187 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002188 }
2189
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002190 /*
2191 * 38 . 38 session id length
2192 * 39 . 38+n session id
2193 * 39+n . 40+n chosen ciphersuite
2194 * 41+n . 41+n chosen compression alg.
2195 * 42+n . 43+n extensions length
2196 * 44+n . 43+n+m extensions
2197 */
2198 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00002199 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
2200 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002201
2202 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2203 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2204 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002205 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002206
Paul Bakker48916f92012-09-16 19:57:18 +00002207 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2208 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2209 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002210
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02002211 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2212 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02002213 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002214 ssl->session_negotiate->compression ) );
2215
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002216 /*
2217 * First write extensions, then the total length
2218 */
2219 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2220 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002221
Paul Bakker05decb22013-08-15 13:33:48 +02002222#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002223 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2224 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002225#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002226
Paul Bakker1f2bc622013-08-15 13:45:55 +02002227#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002228 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2229 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002230#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002231
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002232#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2233 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2234 ext_len += olen;
2235#endif
2236
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002237#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2238 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2239 ext_len += olen;
2240#endif
2241
Paul Bakkera503a632013-08-14 13:48:06 +02002242#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002243 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2244 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002245#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002246
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002247#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002248 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2249 ext_len += olen;
2250#endif
2251
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002252#if defined(POLARSSL_SSL_ALPN)
2253 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2254 ext_len += olen;
2255#endif
2256
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002257 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002258
Paul Bakkera7036632014-04-30 10:15:38 +02002259 if( ext_len > 0 )
2260 {
2261 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2262 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2263 p += ext_len;
2264 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002265
2266 ssl->out_msglen = p - buf;
2267 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2268 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
2269
2270 ret = ssl_write_record( ssl );
2271
2272 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2273
2274 return( ret );
2275}
2276
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002277#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2278 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002279 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2280 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002281static int ssl_write_certificate_request( ssl_context *ssl )
2282{
Paul Bakkered27a042013-04-18 22:46:23 +02002283 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002284
2285 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2286
2287 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002288 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002289 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2290 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002291 {
2292 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2293 ssl->state++;
2294 return( 0 );
2295 }
2296
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002297 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2298 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002299}
2300#else
2301static int ssl_write_certificate_request( ssl_context *ssl )
2302{
2303 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2304 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002305 size_t dn_size, total_dn_size; /* excluding length bytes */
2306 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002307 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002308 const x509_crt *crt;
Manuel Pégourié-Gonnardde9c8a52015-10-02 11:16:47 +02002309 const unsigned char * const end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00002310
2311 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2312
2313 ssl->state++;
2314
Paul Bakkerfbb17802013-04-17 19:10:21 +02002315 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002316 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002317 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002318 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002319 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002320 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002321 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002322 return( 0 );
2323 }
2324
2325 /*
2326 * 0 . 0 handshake type
2327 * 1 . 3 handshake length
2328 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002329 * 5 .. m-1 cert types
2330 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002331 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002332 * n .. n+1 length of all DNs
2333 * n+2 .. n+3 length of DN 1
2334 * n+4 .. ... Distinguished Name #1
2335 * ... .. ... length of DN 2, etc.
2336 */
2337 buf = ssl->out_msg;
2338 p = buf + 4;
2339
2340 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002341 * Supported certificate types
2342 *
2343 * ClientCertificateType certificate_types<1..2^8-1>;
2344 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002345 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002346 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002347
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002348#if defined(POLARSSL_RSA_C)
2349 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2350#endif
2351#if defined(POLARSSL_ECDSA_C)
2352 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2353#endif
2354
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002355 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002356 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002357
Paul Bakker577e0062013-08-28 11:57:20 +02002358 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002359#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002360 /*
2361 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002362 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002363 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2364 *
2365 * struct {
2366 * HashAlgorithm hash;
2367 * SignatureAlgorithm signature;
2368 * } SignatureAndHashAlgorithm;
2369 *
2370 * enum { (255) } HashAlgorithm;
2371 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002372 */
Paul Bakker21dca692013-01-03 11:41:08 +01002373 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002374 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002375 /*
2376 * Only use current running hash algorithm that is already required
2377 * for requested ciphersuite.
2378 */
Paul Bakker926af752012-11-23 13:38:07 +01002379 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2380
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002381 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2382 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002383 {
2384 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2385 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002386
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002387 /*
2388 * Supported signature algorithms
2389 */
2390#if defined(POLARSSL_RSA_C)
2391 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2392 p[2 + sa_len++] = SSL_SIG_RSA;
2393#endif
2394#if defined(POLARSSL_ECDSA_C)
2395 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2396 p[2 + sa_len++] = SSL_SIG_ECDSA;
2397#endif
Paul Bakker926af752012-11-23 13:38:07 +01002398
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002399 p[0] = (unsigned char)( sa_len >> 8 );
2400 p[1] = (unsigned char)( sa_len );
2401 sa_len += 2;
2402 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002403 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002404#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002405
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002406 /*
2407 * DistinguishedName certificate_authorities<0..2^16-1>;
2408 * opaque DistinguishedName<1..2^16-1>;
2409 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002410 p += 2;
2411 crt = ssl->ca_chain;
2412
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002413 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002414 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002415 {
Paul Bakker926af752012-11-23 13:38:07 +01002416 dn_size = crt->subject_raw.len;
Manuel Pégourié-Gonnardde9c8a52015-10-02 11:16:47 +02002417
Manuel Pégourié-Gonnardcf16b792015-12-10 14:36:25 +01002418 if( end < p ||
2419 (size_t)( end - p ) < dn_size ||
2420 (size_t)( end - p ) < 2 + dn_size )
Manuel Pégourié-Gonnardde9c8a52015-10-02 11:16:47 +02002421 {
2422 SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) );
2423 break;
2424 }
2425
Paul Bakker926af752012-11-23 13:38:07 +01002426 *p++ = (unsigned char)( dn_size >> 8 );
2427 *p++ = (unsigned char)( dn_size );
2428 memcpy( p, crt->subject_raw.p, dn_size );
2429 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002430
Paul Bakker926af752012-11-23 13:38:07 +01002431 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2432
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002433 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002434 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002435 }
2436
Paul Bakker926af752012-11-23 13:38:07 +01002437 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002438 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2439 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002440 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2441 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002442
2443 ret = ssl_write_record( ssl );
2444
2445 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2446
2447 return( ret );
2448}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002449#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2450 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002451 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2452 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002453
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002454#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2455 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2456static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2457{
2458 int ret;
2459
2460 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2461 {
2462 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2463 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2464 }
2465
2466 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2467 pk_ec( *ssl_own_key( ssl ) ),
2468 POLARSSL_ECDH_OURS ) ) != 0 )
2469 {
2470 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2471 return( ret );
2472 }
2473
2474 return( 0 );
2475}
2476#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2477 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2478
Paul Bakker41c83d32013-03-20 14:39:14 +01002479static int ssl_write_server_key_exchange( ssl_context *ssl )
2480{
Paul Bakker23986e52011-04-24 08:57:21 +00002481 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002482 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002483 const ssl_ciphersuite_t *ciphersuite_info =
2484 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002485
2486#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2487 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2488 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002489 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002490 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002491 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002492 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002493 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002494 ((void) dig_signed);
2495 ((void) dig_signed_len);
2496#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002497
Paul Bakker5121ce52009-01-03 21:22:43 +00002498 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2499
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002500#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2501 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2502 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002503 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2504 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2505 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002506 {
2507 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2508 ssl->state++;
2509 return( 0 );
2510 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002511#endif
2512
2513#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2514 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2515 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2516 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2517 {
2518 ssl_get_ecdh_params_from_cert( ssl );
2519
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002520 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002521 ssl->state++;
2522 return( 0 );
2523 }
2524#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002525
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002526#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2527 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2528 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2529 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002530 {
2531 /* TODO: Support identity hints */
2532 *(p++) = 0x00;
2533 *(p++) = 0x00;
2534
2535 n += 2;
2536 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002537#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2538 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002539
2540#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2541 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2542 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2543 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002544 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002545 /*
2546 * Ephemeral DH parameters:
2547 *
2548 * struct {
2549 * opaque dh_p<1..2^16-1>;
2550 * opaque dh_g<1..2^16-1>;
2551 * opaque dh_Ys<1..2^16-1>;
2552 * } ServerDHParams;
2553 */
2554 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2555 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2556 {
2557 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2558 return( ret );
2559 }
Paul Bakker48916f92012-09-16 19:57:18 +00002560
Paul Bakker41c83d32013-03-20 14:39:14 +01002561 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002562 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2563 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002564 {
2565 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2566 return( ret );
2567 }
2568
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002569 dig_signed = p;
2570 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002571
2572 p += len;
2573 n += len;
2574
Paul Bakker41c83d32013-03-20 14:39:14 +01002575 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2576 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2577 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2578 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2579 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002580#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2581 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002582
Gergely Budai987bfb52014-01-19 21:48:42 +01002583#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002584 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002585 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2586 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002587 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002588 /*
2589 * Ephemeral ECDH parameters:
2590 *
2591 * struct {
2592 * ECParameters curve_params;
2593 * ECPoint public;
2594 * } ServerECDHParams;
2595 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002596 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002597#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002598 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002599
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002600 /* Match our preference list against the offered curves */
2601 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2602 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2603 if( (*curve)->grp_id == *gid )
2604 goto curve_matching_done;
2605
2606curve_matching_done:
2607#else
2608 curve = ssl->handshake->curves;
2609#endif
2610
Manuel Pégourié-Gonnard8e8ae3d2015-06-23 18:57:28 +02002611 if( curve == NULL || *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002612 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002613 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2614 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002615 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002616
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002617 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002618
Paul Bakker41c83d32013-03-20 14:39:14 +01002619 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002620 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002621 {
2622 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2623 return( ret );
2624 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002625
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002626 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2627 p, SSL_MAX_CONTENT_LEN - n,
2628 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002629 {
2630 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2631 return( ret );
2632 }
2633
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002634 dig_signed = p;
2635 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002636
2637 p += len;
2638 n += len;
2639
Paul Bakker41c83d32013-03-20 14:39:14 +01002640 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2641 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002642#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002643
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002644#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002645 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2646 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002647 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002648 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2649 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002650 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002651 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002652 unsigned int hashlen = 0;
2653 unsigned char hash[64];
2654 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002655
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002656 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002657 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2658 */
Paul Bakker577e0062013-08-28 11:57:20 +02002659#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002660 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2661 {
2662 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2663
2664 if( md_alg == POLARSSL_MD_NONE )
2665 {
2666 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002667 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002668 }
2669 }
Paul Bakker577e0062013-08-28 11:57:20 +02002670 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002671#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002672#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2673 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002674 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002675 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2676 {
2677 md_alg = POLARSSL_MD_SHA1;
2678 }
2679 else
Paul Bakker577e0062013-08-28 11:57:20 +02002680#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002681 {
2682 md_alg = POLARSSL_MD_NONE;
2683 }
2684
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002685 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002686 * Compute the hash to be signed
2687 */
Paul Bakker577e0062013-08-28 11:57:20 +02002688#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2689 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002690 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002691 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002692 md5_context md5;
2693 sha1_context sha1;
2694
Paul Bakker5b4af392014-06-26 12:09:34 +02002695 md5_init( &md5 );
2696 sha1_init( &sha1 );
2697
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002698 /*
2699 * digitally-signed struct {
2700 * opaque md5_hash[16];
2701 * opaque sha_hash[20];
2702 * };
2703 *
2704 * md5_hash
2705 * MD5(ClientHello.random + ServerHello.random
2706 * + ServerParams);
2707 * sha_hash
2708 * SHA(ClientHello.random + ServerHello.random
2709 * + ServerParams);
2710 */
2711 md5_starts( &md5 );
2712 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002713 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002714 md5_finish( &md5, hash );
2715
2716 sha1_starts( &sha1 );
2717 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002718 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002719 sha1_finish( &sha1, hash + 16 );
2720
2721 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02002722
2723 md5_free( &md5 );
2724 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002725 }
2726 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002727#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2728 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002729#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2730 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002731 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002732 {
2733 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02002734 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002735
Paul Bakker84bbeb52014-07-01 14:53:22 +02002736 md_init( &ctx );
2737
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002738 /* Info from md_alg will be used instead */
2739 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002740
2741 /*
2742 * digitally-signed struct {
2743 * opaque client_random[32];
2744 * opaque server_random[32];
2745 * ServerDHParams params;
2746 * };
2747 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002748 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002749 {
2750 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2751 return( ret );
2752 }
2753
2754 md_starts( &ctx );
2755 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002756 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002757 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002758 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00002759 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002760 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002761#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2762 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002763 {
2764 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002765 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002766 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002767
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002768 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2769 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002770
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002771 /*
2772 * Make the signature
2773 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002774 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002775 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002776 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2777 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002778 }
Paul Bakker23f36802012-09-28 14:15:14 +00002779
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002780#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002781 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2782 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002783 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002784 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002785
2786 n += 2;
2787 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002788#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002789
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002790 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002791 p + 2 , &signature_len,
2792 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002793 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002794 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002795 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002796 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002797
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002798 *(p++) = (unsigned char)( signature_len >> 8 );
2799 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002800 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002801
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002802 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002803
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002804 p += signature_len;
2805 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002806 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002807#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002808 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2809 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002810
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002811 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002812 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2813 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2814
2815 ssl->state++;
2816
2817 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2818 {
2819 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2820 return( ret );
2821 }
2822
2823 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2824
2825 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002826}
2827
2828static int ssl_write_server_hello_done( ssl_context *ssl )
2829{
2830 int ret;
2831
2832 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2833
2834 ssl->out_msglen = 4;
2835 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2836 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2837
2838 ssl->state++;
2839
2840 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2841 {
2842 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2843 return( ret );
2844 }
2845
2846 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2847
2848 return( 0 );
2849}
2850
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002851#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2852 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2853static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2854 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002855{
2856 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002857 size_t n;
2858
2859 /*
2860 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2861 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002862 if( *p + 2 > end )
2863 {
2864 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2865 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2866 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002867
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002868 n = ( (*p)[0] << 8 ) | (*p)[1];
2869 *p += 2;
2870
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002871 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002872 {
2873 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2874 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2875 }
2876
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002877 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002878 {
2879 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2880 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2881 }
2882
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002883 *p += n;
2884
Paul Bakker70df2fb2013-04-17 17:19:09 +02002885 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2886
Paul Bakker70df2fb2013-04-17 17:19:09 +02002887 return( ret );
2888}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002889#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2890 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002891
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002892#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2893 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002894static int ssl_parse_encrypted_pms( ssl_context *ssl,
2895 const unsigned char *p,
2896 const unsigned char *end,
2897 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002898{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002899 int ret;
2900 size_t len = pk_get_len( ssl_own_key( ssl ) );
2901 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002902 unsigned char fake_pms[48], peer_pms[48];
2903 unsigned char mask;
Manuel Pégourié-Gonnardb26b75e2015-06-23 18:52:09 +02002904 size_t i, peer_pmslen;
2905 unsigned int diff;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002906
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002907 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002908 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002909 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002910 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2911 }
2912
2913 /*
2914 * Decrypt the premaster using own private RSA key
2915 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002916#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2917 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002918 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2919 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002920 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2921 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002922 {
2923 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2924 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2925 }
2926 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002927#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002928
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002929 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002930 {
2931 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2932 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2933 }
2934
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002935 /*
2936 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
2937 * must not cause the connection to end immediately; instead, send a
2938 * bad_record_mac later in the handshake.
2939 * Also, avoid data-dependant branches here to protect against
2940 * timing-based variants.
2941 */
2942 ret = ssl->f_rng( ssl->p_rng, fake_pms, sizeof( fake_pms ) );
2943 if( ret != 0 )
2944 return( ret );
2945
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002946 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02002947 peer_pms, &peer_pmslen,
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002948 sizeof( peer_pms ),
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002949 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002950
Manuel Pégourié-Gonnardb26b75e2015-06-23 18:52:09 +02002951 diff = (unsigned int) ret;
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02002952 diff |= peer_pmslen ^ 48;
2953 diff |= peer_pms[0] ^ ssl->handshake->max_major_ver;
2954 diff |= peer_pms[1] ^ ssl->handshake->max_minor_ver;
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002955
2956#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02002957 if( diff != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002958 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002959#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002960
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002961 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
2962 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
2963 {
2964 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2965 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002966 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002967 ssl->handshake->pmslen = 48;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002968
Manuel Pégourié-Gonnardb26b75e2015-06-23 18:52:09 +02002969 /* mask = diff ? 0xff : 0x00 */
2970 mask = - ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002971 for( i = 0; i < ssl->handshake->pmslen; i++ )
2972 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
2973
2974 return( 0 );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002975}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002976#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2977 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002978
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002979#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002980static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2981 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002982{
Paul Bakker6db455e2013-09-18 17:29:31 +02002983 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002984 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002985
Paul Bakker6db455e2013-09-18 17:29:31 +02002986 if( ssl->f_psk == NULL &&
2987 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2988 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002989 {
2990 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2991 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2992 }
2993
2994 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002995 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002996 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002997 if( *p + 2 > end )
2998 {
2999 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3000 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3001 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003002
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003003 n = ( (*p)[0] << 8 ) | (*p)[1];
3004 *p += 2;
3005
3006 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003007 {
3008 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3009 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3010 }
3011
Paul Bakker6db455e2013-09-18 17:29:31 +02003012 if( ssl->f_psk != NULL )
3013 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003014 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003015 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3016 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003017 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003018 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003019 /* Identity is not a big secret since clients send it in the clear,
3020 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02003021 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003022 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003023 {
3024 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3025 }
3026 }
3027
3028 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003029 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003030 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02003031 if( ( ret = ssl_send_alert_message( ssl,
3032 SSL_ALERT_LEVEL_FATAL,
3033 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
3034 {
3035 return( ret );
3036 }
3037
3038 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003039 }
3040
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003041 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003042
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003043 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003044}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003045#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003046
Paul Bakker5121ce52009-01-03 21:22:43 +00003047static int ssl_parse_client_key_exchange( ssl_context *ssl )
3048{
Paul Bakker23986e52011-04-24 08:57:21 +00003049 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003050 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003051
Paul Bakker41c83d32013-03-20 14:39:14 +01003052 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003053
3054 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
3055
3056 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3057 {
3058 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3059 return( ret );
3060 }
3061
3062 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3063 {
3064 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003065 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003066 }
3067
3068 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
3069 {
3070 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003071 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003072 }
3073
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003074#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01003075 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003076 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003077 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003078 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003079
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003080 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003081 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02003082 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3083 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003084 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003085
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003086 if( p != end )
3087 {
3088 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3089 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3090 }
3091
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02003092 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003093
3094 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
3095 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003096 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02003097 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003098 {
3099 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
3100 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3101 }
3102
3103 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003104 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003105 else
3106#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003107#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003108 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3109 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3110 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003111 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003112 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
3113 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
3114 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003115 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003116 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003117 ssl->in_msg + 4, ssl->in_hslen - 4 ) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003118 {
3119 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3120 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3121 }
3122
3123 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3124
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003125 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
3126 &ssl->handshake->pmslen,
3127 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02003128 POLARSSL_MPI_MAX_SIZE,
3129 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003130 {
3131 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
3132 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3133 }
3134
3135 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003136 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003137 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003138#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003139 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3140 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3141 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003142#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
3143 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003144 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003145 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003146 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003147
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003148 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003149 {
3150 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3151 return( ret );
3152 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003153
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003154 if( p != end )
3155 {
3156 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3157 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3158 }
3159
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003160 if( ( ret = ssl_psk_derive_premaster( ssl,
3161 ciphersuite_info->key_exchange ) ) != 0 )
3162 {
3163 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3164 return( ret );
3165 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003166 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003167 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003168#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003169#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
3170 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
3171 {
3172 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003173 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003174
3175 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3176 {
3177 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3178 return( ret );
3179 }
3180
3181 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3182 {
3183 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
3184 return( ret );
3185 }
3186
3187 if( ( ret = ssl_psk_derive_premaster( ssl,
3188 ciphersuite_info->key_exchange ) ) != 0 )
3189 {
3190 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3191 return( ret );
3192 }
3193 }
3194 else
3195#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003196#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3197 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3198 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003199 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003200 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003201
3202 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3203 {
3204 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3205 return( ret );
3206 }
3207 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3208 {
3209 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3210 return( ret );
3211 }
3212
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003213 if( p != end )
3214 {
3215 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3216 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3217 }
3218
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003219 if( ( ret = ssl_psk_derive_premaster( ssl,
3220 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003221 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003222 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3223 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003224 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003225 }
3226 else
3227#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003228#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3229 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
3230 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003231 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003232 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003233
3234 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3235 {
3236 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3237 return( ret );
3238 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003239
3240 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
3241 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003242 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003243 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3244 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003245 }
3246
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003247 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3248
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003249 if( ( ret = ssl_psk_derive_premaster( ssl,
3250 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003251 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003252 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003253 return( ret );
3254 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003255 }
3256 else
3257#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003258#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
3259 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003260 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003261 if( ( ret = ssl_parse_encrypted_pms( ssl,
3262 ssl->in_msg + 4,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003263 ssl->in_msg + ssl->in_hslen,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003264 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003265 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003266 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003267 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003268 }
3269 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003270 else
3271#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
3272 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003273 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003274 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003275 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003276
Paul Bakkerff60ee62010-03-16 21:09:09 +00003277 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
3278 {
3279 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
3280 return( ret );
3281 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003282
Paul Bakker5121ce52009-01-03 21:22:43 +00003283 ssl->state++;
3284
3285 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3286
3287 return( 0 );
3288}
3289
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003290#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3291 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003292 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3293 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003294static int ssl_parse_certificate_verify( ssl_context *ssl )
3295{
Paul Bakkerfbb17802013-04-17 19:10:21 +02003296 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003297
3298 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3299
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003300 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003301 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003302 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003303 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02003304 {
3305 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3306 ssl->state++;
3307 return( 0 );
3308 }
3309
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003310 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3311 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003312}
3313#else
3314static int ssl_parse_certificate_verify( ssl_context *ssl )
3315{
3316 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003317 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003318 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003319 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003320 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003321#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003322 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003323#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003324 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003325 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3326
3327 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3328
3329 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003330 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003331 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003332 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3333 {
3334 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3335 ssl->state++;
3336 return( 0 );
3337 }
3338
Paul Bakkered27a042013-04-18 22:46:23 +02003339 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003340 {
3341 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3342 ssl->state++;
3343 return( 0 );
3344 }
3345
Paul Bakker48916f92012-09-16 19:57:18 +00003346 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003347
3348 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3349 {
3350 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3351 return( ret );
3352 }
3353
3354 ssl->state++;
3355
3356 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3357 {
3358 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003359 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003360 }
3361
3362 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
3363 {
3364 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003365 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003366 }
3367
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003368 /*
3369 * 0 . 0 handshake type
3370 * 1 . 3 handshake length
3371 * 4 . 5 sig alg (TLS 1.2 only)
3372 * 4+n . 5+n signature length (n = sa_len)
3373 * 6+n . 6+n+m signature (m = sig_len)
3374 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003375
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003376#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3377 defined(POLARSSL_SSL_PROTO_TLS1_1)
3378 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003379 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003380 sa_len = 0;
3381
Paul Bakkerc70b9822013-04-07 22:00:46 +02003382 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003383 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003384
3385 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3386 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3387 POLARSSL_PK_ECDSA ) )
3388 {
3389 hash_start += 16;
3390 hashlen -= 16;
3391 md_alg = POLARSSL_MD_SHA1;
3392 }
Paul Bakker926af752012-11-23 13:38:07 +01003393 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003394 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003395#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3396 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003397#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3398 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003399 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003400 sa_len = 2;
3401
Paul Bakker5121ce52009-01-03 21:22:43 +00003402 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003403 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003404 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003405 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003406 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003407 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3408 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003409 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3410 }
3411
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003412 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003413
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003414 /* Info from md_alg will be used instead */
3415 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003416
3417 /*
3418 * Signature
3419 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003420 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
3421 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003422 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003423 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3424 " for verify message" ) );
3425 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003426 }
3427
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003428 /*
3429 * Check the certificate's key type matches the signature alg
3430 */
3431 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3432 {
3433 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3434 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3435 }
Paul Bakker577e0062013-08-28 11:57:20 +02003436 }
3437 else
3438#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3439 {
3440 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003441 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003442 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003443
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003444 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01003445
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003446 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003447 {
3448 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003449 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003450 }
3451
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003452 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003453 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003454 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003455 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003456 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003457 return( ret );
3458 }
3459
3460 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3461
Paul Bakkered27a042013-04-18 22:46:23 +02003462 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003463}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003464#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3465 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3466 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003467
Paul Bakkera503a632013-08-14 13:48:06 +02003468#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003469static int ssl_write_new_session_ticket( ssl_context *ssl )
3470{
3471 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003472 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003473 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003474
3475 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3476
3477 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3478 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3479
3480 /*
3481 * struct {
3482 * uint32 ticket_lifetime_hint;
3483 * opaque ticket<0..2^16-1>;
3484 * } NewSessionTicket;
3485 *
3486 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3487 * 8 . 9 ticket_len (n)
3488 * 10 . 9+n ticket content
3489 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003490
3491 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3492 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3493 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3494 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003495
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003496 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3497 {
3498 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3499 tlen = 0;
3500 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003501
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003502 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3503 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003504
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003505 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003506
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003507 /*
3508 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3509 * ChangeCipherSpec share the same state.
3510 */
3511 ssl->handshake->new_session_ticket = 0;
3512
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003513 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3514 {
3515 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3516 return( ret );
3517 }
3518
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003519 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3520
3521 return( 0 );
3522}
Paul Bakkera503a632013-08-14 13:48:06 +02003523#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003524
Paul Bakker5121ce52009-01-03 21:22:43 +00003525/*
Paul Bakker1961b702013-01-25 14:49:24 +01003526 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003527 */
Paul Bakker1961b702013-01-25 14:49:24 +01003528int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003529{
3530 int ret = 0;
3531
Paul Bakker1961b702013-01-25 14:49:24 +01003532 if( ssl->state == SSL_HANDSHAKE_OVER )
3533 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003534
Paul Bakker1961b702013-01-25 14:49:24 +01003535 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3536
3537 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3538 return( ret );
3539
3540 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003541 {
Paul Bakker1961b702013-01-25 14:49:24 +01003542 case SSL_HELLO_REQUEST:
3543 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003544 break;
3545
Paul Bakker1961b702013-01-25 14:49:24 +01003546 /*
3547 * <== ClientHello
3548 */
3549 case SSL_CLIENT_HELLO:
3550 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003551 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003552
3553 /*
3554 * ==> ServerHello
3555 * Certificate
3556 * ( ServerKeyExchange )
3557 * ( CertificateRequest )
3558 * ServerHelloDone
3559 */
3560 case SSL_SERVER_HELLO:
3561 ret = ssl_write_server_hello( ssl );
3562 break;
3563
3564 case SSL_SERVER_CERTIFICATE:
3565 ret = ssl_write_certificate( ssl );
3566 break;
3567
3568 case SSL_SERVER_KEY_EXCHANGE:
3569 ret = ssl_write_server_key_exchange( ssl );
3570 break;
3571
3572 case SSL_CERTIFICATE_REQUEST:
3573 ret = ssl_write_certificate_request( ssl );
3574 break;
3575
3576 case SSL_SERVER_HELLO_DONE:
3577 ret = ssl_write_server_hello_done( ssl );
3578 break;
3579
3580 /*
3581 * <== ( Certificate/Alert )
3582 * ClientKeyExchange
3583 * ( CertificateVerify )
3584 * ChangeCipherSpec
3585 * Finished
3586 */
3587 case SSL_CLIENT_CERTIFICATE:
3588 ret = ssl_parse_certificate( ssl );
3589 break;
3590
3591 case SSL_CLIENT_KEY_EXCHANGE:
3592 ret = ssl_parse_client_key_exchange( ssl );
3593 break;
3594
3595 case SSL_CERTIFICATE_VERIFY:
3596 ret = ssl_parse_certificate_verify( ssl );
3597 break;
3598
3599 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3600 ret = ssl_parse_change_cipher_spec( ssl );
3601 break;
3602
3603 case SSL_CLIENT_FINISHED:
3604 ret = ssl_parse_finished( ssl );
3605 break;
3606
3607 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003608 * ==> ( NewSessionTicket )
3609 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003610 * Finished
3611 */
3612 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003613#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003614 if( ssl->handshake->new_session_ticket != 0 )
3615 ret = ssl_write_new_session_ticket( ssl );
3616 else
Paul Bakkera503a632013-08-14 13:48:06 +02003617#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003618 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003619 break;
3620
3621 case SSL_SERVER_FINISHED:
3622 ret = ssl_write_finished( ssl );
3623 break;
3624
3625 case SSL_FLUSH_BUFFERS:
3626 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3627 ssl->state = SSL_HANDSHAKE_WRAPUP;
3628 break;
3629
3630 case SSL_HANDSHAKE_WRAPUP:
3631 ssl_handshake_wrapup( ssl );
3632 break;
3633
3634 default:
3635 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3636 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003637 }
3638
Paul Bakker5121ce52009-01-03 21:22:43 +00003639 return( ret );
3640}
Paul Bakker9af723c2014-05-01 13:03:14 +02003641#endif /* POLARSSL_SSL_SRV_C */