blob: 22000414726a85b0ee6ad38432067d772ef8455a [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)
Hanno Beckerc2b9d982017-05-10 16:37:21 +0100470/*
471 * Status of the implementation of signature-algorithms extension:
472 *
473 * Currently, we are only considering the signature-algorithm extension
474 * to pick a ciphersuite which allows us to send the ServerKeyExchange
475 * message with a signature-hash combination that the user allows.
476 *
477 * We do *not* check whether all certificates in our certificate
478 * chain are signed with an allowed signature-hash pair.
479 * This needs to be done at a later stage.
480 *
481 */
Paul Bakker23f36802012-09-28 14:15:14 +0000482static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
483 const unsigned char *buf,
484 size_t len )
485{
486 size_t sig_alg_list_size;
487 const unsigned char *p;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200488 const unsigned char *end = buf + len;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200489
Hanno Beckerc2b9d982017-05-10 16:37:21 +0100490 md_type_t md_cur;
491 pk_type_t sig_cur;
Paul Bakker23f36802012-09-28 14:15:14 +0000492
493 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
494 if( sig_alg_list_size + 2 != len ||
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200495 sig_alg_list_size % 2 != 0 )
Paul Bakker23f36802012-09-28 14:15:14 +0000496 {
497 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
498 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
499 }
500
Hanno Beckerc2b9d982017-05-10 16:37:21 +0100501 /* Currently we only guarantee signing the ServerKeyExchange message according
502 * to the constraints specified in this extension (see above), so it suffices
503 * to remember only one suitable hash for each possible signature algorithm.
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200504 *
Hanno Beckerc2b9d982017-05-10 16:37:21 +0100505 * This will change when we also consider certificate signatures,
506 * in which case we will need to remember the whole signature-hash
507 * pair list from the extension.
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200508 */
Simon Butcher14400c82016-01-02 00:08:13 +0000509
Hanno Beckerc2b9d982017-05-10 16:37:21 +0100510 for( p = buf + 2; p < end; p += 2 ) {
511
512 /* Silently ignore unknown signature or hash algorithms. */
513
514 if( (sig_cur = ssl_pk_alg_from_sig( p[1] ) ) == POLARSSL_PK_NONE )
515 {
516 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: unknown sig alg encoding %d",
517 p[1] ) );
518 continue;
519 }
520
521 /* Check if we support the hash the user proposes */
522 md_cur = ssl_md_alg_from_hash( p[0] );
523 if( md_cur == POLARSSL_MD_NONE )
524 {
525 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: "
526 "unknown hash alg encoding %d", p[0] ) );
527 continue;
528 }
529
530 if( ssl_check_sig_hash( md_cur ) == 0 )
531 {
532 ssl_sig_hash_set_add( &ssl->handshake->hash_algs, sig_cur, md_cur );
533 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: match sig %d and hash %d",
534 sig_cur, md_cur ) );
535 }
536 else
537 {
538 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: hash alg %d not supported",
539 md_cur ) );
Paul Bakker23f36802012-09-28 14:15:14 +0000540 }
Paul Bakker23f36802012-09-28 14:15:14 +0000541 }
542
Paul Bakker23f36802012-09-28 14:15:14 +0000543 return( 0 );
544}
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100545#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
546 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker23f36802012-09-28 14:15:14 +0000547
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200548#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200549static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
550 const unsigned char *buf,
551 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100552{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200553 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100554 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200555 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100556
557 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
558 if( list_size + 2 != len ||
559 list_size % 2 != 0 )
560 {
561 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
562 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
563 }
564
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200565 /* Should never happen unless client duplicates the extension */
566 if( ssl->handshake->curves != NULL )
567 {
568 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
569 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
570 }
571
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +0100572 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200573 * and leave room for a final 0 */
574 our_size = list_size / 2 + 1;
575 if( our_size > POLARSSL_ECP_DP_MAX )
576 our_size = POLARSSL_ECP_DP_MAX;
577
578 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
579 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
580
Paul Bakker9af723c2014-05-01 13:03:14 +0200581 /* explicit void pointer cast for buggy MS compiler */
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200582 memset( (void *) curves, 0, our_size * sizeof( *curves ) );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200583 ssl->handshake->curves = curves;
584
Paul Bakker41c83d32013-03-20 14:39:14 +0100585 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200586 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100587 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200588 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200589
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200590 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100591 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200592 *curves++ = curve_info;
593 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100594 }
595
596 list_size -= 2;
597 p += 2;
598 }
599
600 return( 0 );
601}
602
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200603static int ssl_parse_supported_point_formats( ssl_context *ssl,
604 const unsigned char *buf,
605 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100606{
607 size_t list_size;
608 const unsigned char *p;
609
610 list_size = buf[0];
611 if( list_size + 1 != len )
612 {
613 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
614 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
615 }
616
Manuel Pégourié-Gonnarda701d2f2015-09-16 11:32:18 +0200617 p = buf + 1;
Paul Bakker41c83d32013-03-20 14:39:14 +0100618 while( list_size > 0 )
619 {
620 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
621 p[0] == POLARSSL_ECP_PF_COMPRESSED )
622 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200623 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200624 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100625 return( 0 );
626 }
627
628 list_size--;
629 p++;
630 }
631
632 return( 0 );
633}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200634#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100635
Paul Bakker05decb22013-08-15 13:33:48 +0200636#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200637static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
638 const unsigned char *buf,
639 size_t len )
640{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200641 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200642 {
643 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
644 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
645 }
646
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200647 ssl->session_negotiate->mfl_code = buf[0];
648
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200649 return( 0 );
650}
Paul Bakker05decb22013-08-15 13:33:48 +0200651#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200652
Paul Bakker1f2bc622013-08-15 13:45:55 +0200653#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200654static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
655 const unsigned char *buf,
656 size_t len )
657{
658 if( len != 0 )
659 {
660 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
661 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
662 }
663
664 ((void) buf);
665
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100666 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
667 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200668
669 return( 0 );
670}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200671#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200672
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100673#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
674static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
675 const unsigned char *buf,
676 size_t len )
677{
678 if( len != 0 )
679 {
680 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
681 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
682 }
683
684 ((void) buf);
685
686 if( ssl->encrypt_then_mac == SSL_ETM_ENABLED &&
687 ssl->minor_ver != SSL_MINOR_VERSION_0 )
688 {
689 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
690 }
691
692 return( 0 );
693}
694#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
695
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200696#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
697static int ssl_parse_extended_ms_ext( ssl_context *ssl,
698 const unsigned char *buf,
699 size_t len )
700{
701 if( len != 0 )
702 {
703 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
704 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
705 }
706
707 ((void) buf);
708
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200709 if( ssl->extended_ms == SSL_EXTENDED_MS_ENABLED &&
710 ssl->minor_ver != SSL_MINOR_VERSION_0 )
711 {
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200712 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200713 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200714
715 return( 0 );
716}
717#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
718
Paul Bakkera503a632013-08-14 13:48:06 +0200719#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200720static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200721 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200722 size_t len )
723{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200724 int ret;
725
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200726 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
727 return( 0 );
728
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200729 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200730 ssl->handshake->new_session_ticket = 1;
731
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200732 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
733
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200734 if( len == 0 )
735 return( 0 );
736
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100737#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200738 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
739 {
740 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
741 return( 0 );
742 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100743#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200744
745 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200746 * Failures are ok: just ignore the ticket and proceed.
747 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200748 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
749 {
750 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200751 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200752 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200753
754 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
755
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200756 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200757
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200758 /* Don't send a new ticket after all, this one is OK */
759 ssl->handshake->new_session_ticket = 0;
760
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200761 return( 0 );
762}
Paul Bakkera503a632013-08-14 13:48:06 +0200763#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200764
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200765#if defined(POLARSSL_SSL_ALPN)
766static int ssl_parse_alpn_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200767 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200768{
Paul Bakker14b16c62014-05-28 11:33:54 +0200769 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200770 const unsigned char *theirs, *start, *end;
771 const char **ours;
772
773 /* If ALPN not configured, just ignore the extension */
774 if( ssl->alpn_list == NULL )
775 return( 0 );
776
777 /*
778 * opaque ProtocolName<1..2^8-1>;
779 *
780 * struct {
781 * ProtocolName protocol_name_list<2..2^16-1>
782 * } ProtocolNameList;
783 */
784
785 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
786 if( len < 4 )
787 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
788
789 list_len = ( buf[0] << 8 ) | buf[1];
790 if( list_len != len - 2 )
791 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
792
793 /*
Manuel Pégourié-Gonnardf472a822018-01-09 10:43:43 +0100794 * Validate peer's list (lengths)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200795 */
796 start = buf + 2;
797 end = buf + len;
Manuel Pégourié-Gonnardf472a822018-01-09 10:43:43 +0100798 for( theirs = start; theirs != end; theirs += cur_len )
799 {
800 cur_len = *theirs++;
801
802 /* Current identifier must fit in list */
803 if( cur_len > (size_t)( end - theirs ) )
804 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
805
806 /* Empty strings MUST NOT be included */
807 if( cur_len == 0 )
808 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
809 }
810
811 /*
812 * Use our order of preference
813 */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200814 for( ours = ssl->alpn_list; *ours != NULL; ours++ )
815 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200816 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200817 for( theirs = start; theirs != end; theirs += cur_len )
818 {
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200819 cur_len = *theirs++;
820
Paul Bakker14b16c62014-05-28 11:33:54 +0200821 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200822 memcmp( theirs, *ours, cur_len ) == 0 )
823 {
824 ssl->alpn_chosen = *ours;
825 return( 0 );
826 }
827 }
828 }
829
830 /* If we get there, no match was found */
831 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
832 SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
833 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
834}
835#endif /* POLARSSL_SSL_ALPN */
836
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100837/*
838 * Auxiliary functions for ServerHello parsing and related actions
839 */
840
841#if defined(POLARSSL_X509_CRT_PARSE_C)
842/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100843 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100844 */
845#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100846static int ssl_check_key_curve( pk_context *pk,
847 const ecp_curve_info **curves )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100848{
849 const ecp_curve_info **crv = curves;
850 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
851
852 while( *crv != NULL )
853 {
854 if( (*crv)->grp_id == grp_id )
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100855 return( 0 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100856 crv++;
857 }
858
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100859 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100860}
861#endif /* POLARSSL_ECDSA_C */
862
863/*
864 * Try picking a certificate for this ciphersuite,
865 * return 0 on success and -1 on failure.
866 */
867static int ssl_pick_cert( ssl_context *ssl,
868 const ssl_ciphersuite_t * ciphersuite_info )
869{
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100870 ssl_key_cert *cur, *list, *fallback = NULL;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100871 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +0200872 int flags;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100873
874#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
875 if( ssl->handshake->sni_key_cert != NULL )
876 list = ssl->handshake->sni_key_cert;
877 else
878#endif
879 list = ssl->handshake->key_cert;
880
881 if( pk_alg == POLARSSL_PK_NONE )
882 return( 0 );
883
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000884 SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) );
885
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100886 for( cur = list; cur != NULL; cur = cur->next )
887 {
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000888 SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate",
889 cur->cert );
890
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100891 if( ! pk_can_do( cur->key, pk_alg ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000892 {
893 SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100894 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000895 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100896
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200897 /*
898 * This avoids sending the client a cert it'll reject based on
899 * keyUsage or other extensions.
900 *
901 * It also allows the user to provision different certificates for
902 * different uses based on keyUsage, eg if they want to avoid signing
903 * and decrypting with the same RSA key.
904 */
905 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +0200906 SSL_IS_SERVER, &flags ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200907 {
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000908 SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
909 "(extended) key usage extension" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200910 continue;
911 }
912
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100913#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100914 if( pk_alg == POLARSSL_PK_ECDSA &&
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100915 ssl_check_key_curve( cur->key, ssl->handshake->curves ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000916 {
917 SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) );
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100918 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000919 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100920#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100921
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100922 /*
923 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
924 * present them a SHA-higher cert rather than failing if it's the only
925 * one we got that satisfies the other conditions.
926 */
927 if( ssl->minor_ver < SSL_MINOR_VERSION_3 &&
928 cur->cert->sig_md != POLARSSL_MD_SHA1 )
929 {
930 if( fallback == NULL )
931 fallback = cur;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000932 {
933 SSL_DEBUG_MSG( 3, ( "certificate not preferred: "
934 "sha-2 with pre-TLS 1.2 client" ) );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100935 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000936 }
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100937 }
938
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100939 /* If we get there, we got a winner */
940 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100941 }
942
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000943 if( cur == NULL )
944 cur = fallback;
945
946
947 /* Do not update ssl->handshake->key_cert unless the is a match */
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100948 if( cur != NULL )
949 {
950 ssl->handshake->key_cert = cur;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000951 SSL_DEBUG_CRT( 3, "selected certificate chain, certificate",
952 ssl->handshake->key_cert->cert );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100953 return( 0 );
954 }
955
956 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100957}
958#endif /* POLARSSL_X509_CRT_PARSE_C */
959
960/*
961 * Check if a given ciphersuite is suitable for use with our config/keys/etc
962 * Sets ciphersuite_info only if the suite matches.
963 */
964static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
965 const ssl_ciphersuite_t **ciphersuite_info )
966{
967 const ssl_ciphersuite_t *suite_info;
968
Hanno Beckerc2b9d982017-05-10 16:37:21 +0100969#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
970 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
971 pk_type_t sig_type;
972#endif
973
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100974 suite_info = ssl_ciphersuite_from_id( suite_id );
975 if( suite_info == NULL )
976 {
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100977 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
978 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100979 }
980
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000981 SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %s", suite_info->name ) );
982
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100983 if( suite_info->min_minor_ver > ssl->minor_ver ||
984 suite_info->max_minor_ver < ssl->minor_ver )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000985 {
986 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100987 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000988 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100989
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100990 if( ssl->arc4_disabled == SSL_ARC4_DISABLED &&
991 suite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000992 {
993 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100994 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000995 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100996
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100997#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
998 if( ssl_ciphersuite_uses_ec( suite_info ) &&
999 ( ssl->handshake->curves == NULL ||
1000 ssl->handshake->curves[0] == NULL ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001001 {
1002 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
1003 "no common elliptic curve" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001004 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001005 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001006#endif
1007
1008#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1009 /* If the ciphersuite requires a pre-shared key and we don't
1010 * have one, skip it now rather than failing later */
1011 if( ssl_ciphersuite_uses_psk( suite_info ) &&
1012 ssl->f_psk == NULL &&
1013 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
1014 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001015 {
1016 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001017 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001018 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001019#endif
1020
Hanno Beckerc2b9d982017-05-10 16:37:21 +01001021#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1022 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
1023 /* If the ciphersuite requires signing, check whether
1024 * a suitable hash algorithm is present. */
1025 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
1026 {
1027 sig_type = ssl_get_ciphersuite_sig_alg( suite_info );
1028 if( sig_type != POLARSSL_PK_NONE &&
1029 ssl_sig_hash_set_find( &ssl->handshake->hash_algs, sig_type ) == POLARSSL_MD_NONE )
1030 {
1031 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no suitable hash algorithm "
1032 "for signature algorithm %d", sig_type ) );
1033 return( 0 );
1034 }
1035 }
1036
1037#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
1038 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
1039
1040
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001041#if defined(POLARSSL_X509_CRT_PARSE_C)
1042 /*
1043 * Final check: if ciphersuite requires us to have a
1044 * certificate/key of a particular type:
1045 * - select the appropriate certificate if we have one, or
1046 * - try the next ciphersuite if we don't
1047 * This must be done last since we modify the key_cert list.
1048 */
1049 if( ssl_pick_cert( ssl, suite_info ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001050 {
1051 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
1052 "no suitable certificate" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001053 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001054 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001055#endif
1056
1057 *ciphersuite_info = suite_info;
1058 return( 0 );
1059}
1060
Paul Bakker78a8c712013-03-06 17:01:52 +01001061#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1062static int ssl_parse_client_hello_v2( ssl_context *ssl )
1063{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001064 int ret, got_common_suite;
Paul Bakker78a8c712013-03-06 17:01:52 +01001065 unsigned int i, j;
1066 size_t n;
1067 unsigned int ciph_len, sess_len, chal_len;
1068 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001069 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +02001070 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001071
1072 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
1073
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001074#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker78a8c712013-03-06 17:01:52 +01001075 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
1076 {
1077 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
1078
1079 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1080 return( ret );
1081
1082 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1083 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001084#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001085
1086 buf = ssl->in_hdr;
1087
1088 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1089
1090 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
1091 buf[2] ) );
1092 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
1093 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
1094 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
1095 buf[3], buf[4] ) );
1096
1097 /*
1098 * SSLv2 Client Hello
1099 *
1100 * Record layer:
1101 * 0 . 1 message length
1102 *
1103 * SSL layer:
1104 * 2 . 2 message type
1105 * 3 . 4 protocol version
1106 */
1107 if( buf[2] != SSL_HS_CLIENT_HELLO ||
1108 buf[3] != SSL_MAJOR_VERSION_3 )
1109 {
1110 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1111 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1112 }
1113
1114 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
1115
1116 if( n < 17 || n > 512 )
1117 {
1118 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1119 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1120 }
1121
1122 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +02001123 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
1124 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +01001125
1126 if( ssl->minor_ver < ssl->min_minor_ver )
1127 {
1128 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001129 " [%d:%d] < [%d:%d]",
1130 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +01001131 ssl->min_major_ver, ssl->min_minor_ver ) );
1132
1133 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1134 SSL_ALERT_MSG_PROTOCOL_VERSION );
1135 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1136 }
1137
Paul Bakker2fbefde2013-06-29 16:01:15 +02001138 ssl->handshake->max_major_ver = buf[3];
1139 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +01001140
1141 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
1142 {
1143 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1144 return( ret );
1145 }
1146
1147 ssl->handshake->update_checksum( ssl, buf + 2, n );
1148
1149 buf = ssl->in_msg;
1150 n = ssl->in_left - 5;
1151
1152 /*
1153 * 0 . 1 ciphersuitelist length
1154 * 2 . 3 session id length
1155 * 4 . 5 challenge length
1156 * 6 . .. ciphersuitelist
1157 * .. . .. session id
1158 * .. . .. challenge
1159 */
1160 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1161
1162 ciph_len = ( buf[0] << 8 ) | buf[1];
1163 sess_len = ( buf[2] << 8 ) | buf[3];
1164 chal_len = ( buf[4] << 8 ) | buf[5];
1165
1166 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
1167 ciph_len, sess_len, chal_len ) );
1168
1169 /*
1170 * Make sure each parameter length is valid
1171 */
1172 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1173 {
1174 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1175 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1176 }
1177
1178 if( sess_len > 32 )
1179 {
1180 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1181 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1182 }
1183
1184 if( chal_len < 8 || chal_len > 32 )
1185 {
1186 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1187 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1188 }
1189
1190 if( n != 6 + ciph_len + sess_len + chal_len )
1191 {
1192 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1193 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1194 }
1195
1196 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1197 buf + 6, ciph_len );
1198 SSL_DEBUG_BUF( 3, "client hello, session id",
1199 buf + 6 + ciph_len, sess_len );
1200 SSL_DEBUG_BUF( 3, "client hello, challenge",
1201 buf + 6 + ciph_len + sess_len, chal_len );
1202
1203 p = buf + 6 + ciph_len;
1204 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001205 memset( ssl->session_negotiate->id, 0,
1206 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001207 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1208
1209 p += sess_len;
1210 memset( ssl->handshake->randbytes, 0, 64 );
1211 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1212
1213 /*
1214 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1215 */
1216 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1217 {
1218 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1219 {
1220 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001221#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker78a8c712013-03-06 17:01:52 +01001222 if( ssl->renegotiation == SSL_RENEGOTIATION )
1223 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001224 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
1225 "during renegotiation" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001226
1227 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1228 return( ret );
1229
1230 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1231 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001232#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001233 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1234 break;
1235 }
1236 }
1237
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001238#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1239 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1240 {
1241 if( p[0] == 0 &&
1242 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1243 p[2] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1244 {
1245 SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
1246
1247 if( ssl->minor_ver < ssl->max_minor_ver )
1248 {
1249 SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
1250
1251 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1252 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1253
1254 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1255 }
1256
1257 break;
1258 }
1259 }
1260#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1261
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001262 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001263 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001264 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001265#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1266 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1267 {
1268 for( i = 0; ciphersuites[i] != 0; i++ )
1269#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001270 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001271 {
1272 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001273#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001274 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001275 if( p[0] != 0 ||
1276 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1277 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1278 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001279
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001280 got_common_suite = 1;
1281
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001282 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1283 &ciphersuite_info ) ) != 0 )
1284 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001285
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001286 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001287 goto have_ciphersuite_v2;
1288 }
1289 }
1290
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001291 if( got_common_suite )
1292 {
1293 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1294 "but none of them usable" ) );
1295 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
1296 }
1297 else
1298 {
1299 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1300 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1301 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001302
1303have_ciphersuite_v2:
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001304 SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
1305
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001306 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001307 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001308 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001309
1310 /*
1311 * SSLv2 Client Hello relevant renegotiation security checks
1312 */
1313 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1314 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1315 {
1316 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1317
1318 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1319 return( ret );
1320
1321 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1322 }
1323
1324 ssl->in_left = 0;
1325 ssl->state++;
1326
1327 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1328
1329 return( 0 );
1330}
1331#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1332
Paul Bakker5121ce52009-01-03 21:22:43 +00001333static int ssl_parse_client_hello( ssl_context *ssl )
1334{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001335 int ret, got_common_suite;
Paul Bakker23986e52011-04-24 08:57:21 +00001336 unsigned int i, j;
1337 size_t n;
1338 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001339 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001340 unsigned int ext_len = 0;
1341 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001342#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001343 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001344#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001345 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001346 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001347 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001348
Hanno Beckerc2b9d982017-05-10 16:37:21 +01001349 /* If there is no signature-algorithm extension present,
1350 * we need to fall back to the default values for allowed
1351 * signature-hash pairs. */
1352#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1353 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
1354 int sig_hash_alg_ext_present = 0;
1355#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
1356 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
1357
Paul Bakker5121ce52009-01-03 21:22:43 +00001358 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1359
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001360#if defined(POLARSSL_SSL_RENEGOTIATION)
1361 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1362#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001363 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001364 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
1365 {
1366 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1367 return( ret );
1368 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001369 }
1370
1371 buf = ssl->in_hdr;
1372
Paul Bakker78a8c712013-03-06 17:01:52 +01001373#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1374 if( ( buf[0] & 0x80 ) != 0 )
1375 return ssl_parse_client_hello_v2( ssl );
1376#endif
1377
Paul Bakkerec636f32012-09-09 19:17:02 +00001378 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1379
1380 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1381 buf[0] ) );
1382 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1383 ( buf[3] << 8 ) | buf[4] ) );
1384 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
1385 buf[1], buf[2] ) );
1386
1387 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001388 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001389 *
1390 * Record layer:
1391 * 0 . 0 message type
1392 * 1 . 2 protocol version
1393 * 3 . 4 message length
1394 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001395
1396 /* According to RFC 5246 Appendix E.1, the version here is typically
1397 * "{03,00}, the lowest version number supported by the client, [or] the
1398 * value of ClientHello.client_version", so the only meaningful check here
1399 * is the major version shouldn't be less than 3 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001400 if( buf[0] != SSL_MSG_HANDSHAKE ||
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001401 buf[1] < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001402 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001403 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1404 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1405 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001406
Paul Bakkerec636f32012-09-09 19:17:02 +00001407 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +00001408
Paul Bakker4f42c112014-04-17 14:48:23 +02001409 if( n < 45 || n > SSL_MAX_CONTENT_LEN )
Paul Bakkerec636f32012-09-09 19:17:02 +00001410 {
1411 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1412 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1413 }
1414
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001415#if defined(POLARSSL_SSL_RENEGOTIATION)
1416 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1417#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001418 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001419 if( ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
1420 {
1421 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1422 return( ret );
1423 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001424 }
1425
1426 buf = ssl->in_msg;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001427#if defined(POLARSSL_SSL_RENEGOTIATION)
1428 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001429 n = ssl->in_msglen;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001430 else
1431#endif
1432 n = ssl->in_left - 5;
Paul Bakkerec636f32012-09-09 19:17:02 +00001433
Paul Bakker48916f92012-09-16 19:57:18 +00001434 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +00001435
1436 /*
1437 * SSL layer:
1438 * 0 . 0 handshake type
1439 * 1 . 3 handshake length
1440 * 4 . 5 protocol version
1441 * 6 . 9 UNIX time()
1442 * 10 . 37 random bytes
1443 * 38 . 38 session id length
1444 * 39 . 38+x session id
1445 * 39+x . 40+x ciphersuitelist length
Paul Bakker0f651c72014-05-22 15:12:19 +02001446 * 41+x . 40+y ciphersuitelist
1447 * 41+y . 41+y compression alg length
1448 * 42+y . 41+z compression algs
Paul Bakkerec636f32012-09-09 19:17:02 +00001449 * .. . .. extensions
1450 */
1451 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1452
1453 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1454 buf[0] ) );
1455 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1456 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1457 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1458 buf[4], buf[5] ) );
1459
1460 /*
1461 * Check the handshake type and protocol version
1462 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001463 if( buf[0] != SSL_HS_CLIENT_HELLO )
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
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001469 ssl->major_ver = buf[4];
1470 ssl->minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001471
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001472 ssl->handshake->max_major_ver = ssl->major_ver;
1473 ssl->handshake->max_minor_ver = ssl->minor_ver;
1474
1475 if( ssl->major_ver < ssl->min_major_ver ||
1476 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001477 {
1478 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001479 " [%d:%d] < [%d:%d]",
1480 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001481 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001482
1483 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1484 SSL_ALERT_MSG_PROTOCOL_VERSION );
1485
1486 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1487 }
1488
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001489 if( ssl->major_ver > ssl->max_major_ver )
1490 {
1491 ssl->major_ver = ssl->max_major_ver;
1492 ssl->minor_ver = ssl->max_minor_ver;
1493 }
1494 else if( ssl->minor_ver > ssl->max_minor_ver )
1495 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001496
Paul Bakker48916f92012-09-16 19:57:18 +00001497 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001498
1499 /*
1500 * Check the handshake message length
1501 */
1502 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1503 {
1504 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1505 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1506 }
1507
1508 /*
1509 * Check the session length
1510 */
1511 sess_len = buf[38];
1512
Paul Bakker0f651c72014-05-22 15:12:19 +02001513 if( sess_len > 32 || sess_len > n - 42 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001514 {
1515 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1516 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1517 }
1518
Paul Bakker48916f92012-09-16 19:57:18 +00001519 ssl->session_negotiate->length = sess_len;
1520 memset( ssl->session_negotiate->id, 0,
1521 sizeof( ssl->session_negotiate->id ) );
1522 memcpy( ssl->session_negotiate->id, buf + 39,
1523 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001524
1525 /*
1526 * Check the ciphersuitelist length
1527 */
1528 ciph_len = ( buf[39 + sess_len] << 8 )
1529 | ( buf[40 + sess_len] );
1530
Paul Bakker0f651c72014-05-22 15:12:19 +02001531 if( ciph_len < 2 || ( ciph_len % 2 ) != 0 || ciph_len > n - 42 - sess_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001532 {
1533 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1534 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1535 }
1536
1537 /*
1538 * Check the compression algorithms length
1539 */
1540 comp_len = buf[41 + sess_len + ciph_len];
1541
Paul Bakker0f651c72014-05-22 15:12:19 +02001542 if( comp_len < 1 || comp_len > 16 ||
1543 comp_len > n - 42 - sess_len - ciph_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001544 {
1545 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1546 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1547 }
1548
Paul Bakker48916f92012-09-16 19:57:18 +00001549 /*
1550 * Check the extension length
1551 */
1552 if( n > 42 + sess_len + ciph_len + comp_len )
1553 {
1554 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1555 | ( buf[43 + sess_len + ciph_len + comp_len] );
1556
1557 if( ( ext_len > 0 && ext_len < 4 ) ||
1558 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1559 {
1560 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1561 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1562 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1563 }
1564 }
1565
1566 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001567#if defined(POLARSSL_ZLIB_SUPPORT)
1568 for( i = 0; i < comp_len; ++i )
1569 {
Paul Bakker48916f92012-09-16 19:57:18 +00001570 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001571 {
Paul Bakker48916f92012-09-16 19:57:18 +00001572 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001573 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001574 }
1575 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001576#endif
1577
Paul Bakkerec636f32012-09-09 19:17:02 +00001578 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1579 buf + 6, 32 );
1580 SSL_DEBUG_BUF( 3, "client hello, session id",
1581 buf + 38, sess_len );
1582 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1583 buf + 41 + sess_len, ciph_len );
1584 SSL_DEBUG_BUF( 3, "client hello, compression",
1585 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001586
Paul Bakkerec636f32012-09-09 19:17:02 +00001587 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001588 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1589 */
1590 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1591 {
1592 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1593 {
1594 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001595#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001596 if( ssl->renegotiation == SSL_RENEGOTIATION )
1597 {
1598 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001599
1600 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1601 return( ret );
1602
Paul Bakker48916f92012-09-16 19:57:18 +00001603 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1604 }
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001605 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001606#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00001607 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1608 break;
1609 }
1610 }
1611
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001612#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1613 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1614 {
1615 if( p[0] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1616 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1617 {
1618 SSL_DEBUG_MSG( 0, ( "received FALLBACK_SCSV" ) );
1619
1620 if( ssl->minor_ver < ssl->max_minor_ver )
1621 {
1622 SSL_DEBUG_MSG( 0, ( "inapropriate fallback" ) );
1623
1624 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1625 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1626
1627 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1628 }
1629
1630 break;
1631 }
1632 }
1633#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1634
Janos Follath307e1812016-05-23 18:52:14 +01001635 /* Do not parse the extensions if the protocol is SSLv3 */
1636#if defined(POLARSSL_SSL_PROTO_SSL3)
1637 if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
Paul Bakker48916f92012-09-16 19:57:18 +00001638 {
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001639#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001640
Janos Follath307e1812016-05-23 18:52:14 +01001641 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001642
Hanno Becker57457782017-06-09 15:30:29 +01001643 SSL_DEBUG_BUF( 3, "client hello extensions", ext, ext_len );
1644
Janos Follath307e1812016-05-23 18:52:14 +01001645 while( ext_len )
Paul Bakker48916f92012-09-16 19:57:18 +00001646 {
Janos Follath307e1812016-05-23 18:52:14 +01001647 unsigned int ext_id = ( ( ext[0] << 8 )
1648 | ( ext[1] ) );
1649 unsigned int ext_size = ( ( ext[2] << 8 )
1650 | ( ext[3] ) );
1651
1652 if( ext_size + 4 > ext_len )
1653 {
1654 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1655 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1656 }
1657 switch( ext_id )
1658 {
1659 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
1660 case TLS_EXT_SERVERNAME:
1661 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1662 if( ssl->f_sni == NULL )
1663 break;
1664
1665 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1666 if( ret != 0 )
1667 return( ret );
1668 break;
1669 #endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
1670
1671 case TLS_EXT_RENEGOTIATION_INFO:
1672 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1673 #if defined(POLARSSL_SSL_RENEGOTIATION)
1674 renegotiation_info_seen = 1;
1675 #endif
1676
1677 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1678 if( ret != 0 )
1679 return( ret );
1680 break;
1681
1682 #if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1683 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
1684 case TLS_EXT_SIG_ALG:
1685 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
Janos Follath307e1812016-05-23 18:52:14 +01001686
1687 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1688 if( ret != 0 )
1689 return( ret );
Hanno Beckerc2b9d982017-05-10 16:37:21 +01001690
1691 sig_hash_alg_ext_present = 1;
Janos Follath307e1812016-05-23 18:52:14 +01001692 break;
1693 #endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
1694 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
1695
1696 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
1697 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1698 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1699
1700 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1701 if( ret != 0 )
1702 return( ret );
1703 break;
1704
1705 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1706 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1707 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
1708
1709 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1710 if( ret != 0 )
1711 return( ret );
1712 break;
1713 #endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
1714
1715 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
1716 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1717 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1718
1719 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1720 if( ret != 0 )
1721 return( ret );
1722 break;
1723 #endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
1724
1725 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1726 case TLS_EXT_TRUNCATED_HMAC:
1727 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1728
1729 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1730 if( ret != 0 )
1731 return( ret );
1732 break;
1733 #endif /* POLARSSL_SSL_TRUNCATED_HMAC */
1734
1735 #if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1736 case TLS_EXT_ENCRYPT_THEN_MAC:
1737 SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
1738
1739 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1740 if( ret != 0 )
1741 return( ret );
1742 break;
1743 #endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1744
1745 #if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1746 case TLS_EXT_EXTENDED_MASTER_SECRET:
1747 SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
1748
1749 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1750 if( ret != 0 )
1751 return( ret );
1752 break;
1753 #endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1754
1755 #if defined(POLARSSL_SSL_SESSION_TICKETS)
1756 case TLS_EXT_SESSION_TICKET:
1757 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1758
1759 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1760 if( ret != 0 )
1761 return( ret );
1762 break;
1763 #endif /* POLARSSL_SSL_SESSION_TICKETS */
1764
1765 #if defined(POLARSSL_SSL_ALPN)
1766 case TLS_EXT_ALPN:
1767 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1768
1769 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1770 if( ret != 0 )
1771 return( ret );
1772 break;
1773 #endif /* POLARSSL_SSL_SESSION_TICKETS */
1774
1775 default:
1776 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1777 ext_id ) );
1778 }
1779
1780 ext_len -= 4 + ext_size;
1781 ext += 4 + ext_size;
1782
1783 if( ext_len > 0 && ext_len < 4 )
1784 {
1785 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1786 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1787 }
Paul Bakker48916f92012-09-16 19:57:18 +00001788 }
Janos Follath307e1812016-05-23 18:52:14 +01001789
1790#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker48916f92012-09-16 19:57:18 +00001791 }
Janos Follath307e1812016-05-23 18:52:14 +01001792#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001793
1794 /*
1795 * Renegotiation security checks
1796 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001797 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001798 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1799 {
1800 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1801 handshake_failure = 1;
1802 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001803#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001804 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1805 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1806 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001807 {
1808 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001809 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001810 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001811 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1812 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1813 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001814 {
1815 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001816 handshake_failure = 1;
1817 }
1818 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1819 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1820 renegotiation_info_seen == 1 )
1821 {
1822 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1823 handshake_failure = 1;
1824 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001825#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001826
1827 if( handshake_failure == 1 )
1828 {
1829 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1830 return( ret );
1831
Paul Bakker48916f92012-09-16 19:57:18 +00001832 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1833 }
Paul Bakker380da532012-04-18 16:10:25 +00001834
Hanno Beckerc2b9d982017-05-10 16:37:21 +01001835
1836#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1837 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
1838
1839 /*
1840 * Try to fall back to default hash SHA1 if the client
1841 * hasn't provided any preferred signature-hash combinations.
1842 */
1843 if( sig_hash_alg_ext_present == 0 )
1844 {
1845 md_type_t md_default = POLARSSL_MD_SHA1;
1846
1847 if( ssl_check_sig_hash( md_default ) != 0 )
1848 md_default = POLARSSL_MD_NONE;
1849
1850 ssl_sig_hash_set_const_hash( &ssl->handshake->hash_algs, md_default );
1851 }
1852
1853#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
1854 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
1855
Paul Bakker41c83d32013-03-20 14:39:14 +01001856 /*
1857 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001858 * (At the end because we need information from the EC-based extensions
1859 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001860 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001861 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001862 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001863 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001864#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1865 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
1866 {
1867 for( i = 0; ciphersuites[i] != 0; i++ )
1868#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001869 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001870 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001871 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001872#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001873 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001874 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1875 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1876 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001877
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001878 got_common_suite = 1;
1879
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001880 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1881 &ciphersuite_info ) ) != 0 )
1882 return( ret );
1883
1884 if( ciphersuite_info != NULL )
1885 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001886 }
1887 }
1888
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001889 if( got_common_suite )
1890 {
1891 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1892 "but none of them usable" ) );
1893 ssl_send_fatal_handshake_failure( ssl );
1894 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
1895 }
1896 else
1897 {
1898 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1899 ssl_send_fatal_handshake_failure( ssl );
1900 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1901 }
Paul Bakker41c83d32013-03-20 14:39:14 +01001902
1903have_ciphersuite:
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001904 SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
1905
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001906 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001907 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1908 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1909
Paul Bakker5121ce52009-01-03 21:22:43 +00001910 ssl->in_left = 0;
1911 ssl->state++;
1912
Hanno Beckerc2b9d982017-05-10 16:37:21 +01001913 /* Debugging-only output for testsuite */
1914#if defined(POLARSSL_DEBUG_C) && \
1915 defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1916 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
1917 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
1918 {
1919 pk_type_t sig_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
1920 if( sig_alg != POLARSSL_PK_NONE )
1921 {
1922 md_type_t md_alg = ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
1923 sig_alg );
1924 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
1925 ssl_hash_from_md_alg( md_alg ) ) );
1926 }
1927 else
1928 {
1929 SSL_DEBUG_MSG( 3, ( "no hash algorithm for signature algorithm %d - should not happen",
1930 sig_alg ) );
1931 }
1932 }
1933#endif
1934
Paul Bakker5121ce52009-01-03 21:22:43 +00001935 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1936
1937 return( 0 );
1938}
1939
Paul Bakker1f2bc622013-08-15 13:45:55 +02001940#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001941static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1942 unsigned char *buf,
1943 size_t *olen )
1944{
1945 unsigned char *p = buf;
1946
1947 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1948 {
1949 *olen = 0;
1950 return;
1951 }
1952
1953 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1954
1955 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1956 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1957
1958 *p++ = 0x00;
1959 *p++ = 0x00;
1960
1961 *olen = 4;
1962}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001963#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001964
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001965#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1966static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
1967 unsigned char *buf,
1968 size_t *olen )
1969{
1970 unsigned char *p = buf;
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01001971 const ssl_ciphersuite_t *suite = NULL;
1972 const cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001973
Hanno Beckerc2f52b42017-10-23 10:28:28 +01001974 if( ssl->session_negotiate->encrypt_then_mac == SSL_ETM_DISABLED ||
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001975 ssl->minor_ver == SSL_MINOR_VERSION_0 )
1976 {
1977 *olen = 0;
1978 return;
1979 }
1980
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01001981 /*
1982 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
1983 * from a client and then selects a stream or Authenticated Encryption
1984 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
1985 * encrypt-then-MAC response extension back to the client."
1986 */
1987 if( ( suite = ssl_ciphersuite_from_id(
1988 ssl->session_negotiate->ciphersuite ) ) == NULL ||
1989 ( cipher = cipher_info_from_type( suite->cipher ) ) == NULL ||
1990 cipher->mode != POLARSSL_MODE_CBC )
1991 {
1992 *olen = 0;
1993 return;
1994 }
1995
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001996 SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
1997
1998 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
1999 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
2000
2001 *p++ = 0x00;
2002 *p++ = 0x00;
2003
2004 *olen = 4;
2005}
2006#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
2007
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002008#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2009static void ssl_write_extended_ms_ext( ssl_context *ssl,
2010 unsigned char *buf,
2011 size_t *olen )
2012{
2013 unsigned char *p = buf;
2014
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +02002015 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_DISABLED ||
2016 ssl->minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002017 {
2018 *olen = 0;
2019 return;
2020 }
2021
2022 SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
2023 "extension" ) );
2024
2025 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
2026 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
2027
2028 *p++ = 0x00;
2029 *p++ = 0x00;
2030
2031 *olen = 4;
2032}
2033#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
2034
Paul Bakkera503a632013-08-14 13:48:06 +02002035#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002036static void ssl_write_session_ticket_ext( ssl_context *ssl,
2037 unsigned char *buf,
2038 size_t *olen )
2039{
2040 unsigned char *p = buf;
2041
2042 if( ssl->handshake->new_session_ticket == 0 )
2043 {
2044 *olen = 0;
2045 return;
2046 }
2047
2048 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
2049
2050 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
2051 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
2052
2053 *p++ = 0x00;
2054 *p++ = 0x00;
2055
2056 *olen = 4;
2057}
Paul Bakkera503a632013-08-14 13:48:06 +02002058#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002059
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002060static void ssl_write_renegotiation_ext( ssl_context *ssl,
2061 unsigned char *buf,
2062 size_t *olen )
2063{
2064 unsigned char *p = buf;
2065
2066 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
2067 {
2068 *olen = 0;
2069 return;
2070 }
2071
2072 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
2073
2074 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
2075 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
2076
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002077#if defined(POLARSSL_SSL_RENEGOTIATION)
2078 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
2079 {
2080 *p++ = 0x00;
2081 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2082 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002083
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002084 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2085 p += ssl->verify_data_len;
2086 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2087 p += ssl->verify_data_len;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002088
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002089 *olen = 5 + ssl->verify_data_len * 2;
2090 }
2091 else
2092#endif /* POLARSSL_SSL_RENEGOTIATION */
2093 {
2094 *p++ = 0x00;
2095 *p++ = 0x01;
2096 *p++ = 0x00;
2097
2098 *olen = 5;
2099 }
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002100}
2101
Paul Bakker05decb22013-08-15 13:33:48 +02002102#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002103static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
2104 unsigned char *buf,
2105 size_t *olen )
2106{
2107 unsigned char *p = buf;
2108
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002109 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
2110 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002111 *olen = 0;
2112 return;
2113 }
2114
2115 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
2116
2117 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
2118 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
2119
2120 *p++ = 0x00;
2121 *p++ = 1;
2122
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002123 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002124
2125 *olen = 5;
2126}
Paul Bakker05decb22013-08-15 13:33:48 +02002127#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002128
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002129#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002130static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
2131 unsigned char *buf,
2132 size_t *olen )
2133{
2134 unsigned char *p = buf;
2135 ((void) ssl);
2136
Paul Bakker677377f2013-10-28 12:54:26 +01002137 if( ( ssl->handshake->cli_exts &
2138 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
2139 {
2140 *olen = 0;
2141 return;
2142 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002143
2144 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
2145
2146 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2147 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
2148
2149 *p++ = 0x00;
2150 *p++ = 2;
2151
2152 *p++ = 1;
2153 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
2154
2155 *olen = 6;
2156}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002157#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002158
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002159#if defined(POLARSSL_SSL_ALPN )
2160static void ssl_write_alpn_ext( ssl_context *ssl,
2161 unsigned char *buf, size_t *olen )
2162{
2163 if( ssl->alpn_chosen == NULL )
2164 {
2165 *olen = 0;
2166 return;
2167 }
2168
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02002169 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002170
2171 /*
2172 * 0 . 1 ext identifier
2173 * 2 . 3 ext length
2174 * 4 . 5 protocol list length
2175 * 6 . 6 protocol name length
2176 * 7 . 7+n protocol name
2177 */
2178 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
2179 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
2180
2181 *olen = 7 + strlen( ssl->alpn_chosen );
2182
2183 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2184 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2185
2186 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2187 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2188
2189 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2190
2191 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2192}
2193#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
2194
Paul Bakker5121ce52009-01-03 21:22:43 +00002195static int ssl_write_server_hello( ssl_context *ssl )
2196{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002197#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002198 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002199#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002200 int ret;
2201 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002202 unsigned char *buf, *p;
2203
2204 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
2205
Paul Bakkera9a028e2013-11-21 17:31:06 +01002206 if( ssl->f_rng == NULL )
2207 {
2208 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2209 return( POLARSSL_ERR_SSL_NO_RNG );
2210 }
2211
Paul Bakker5121ce52009-01-03 21:22:43 +00002212 /*
2213 * 0 . 0 handshake type
2214 * 1 . 3 handshake length
2215 * 4 . 5 protocol version
2216 * 6 . 9 UNIX time()
2217 * 10 . 37 random bytes
2218 */
2219 buf = ssl->out_msg;
2220 p = buf + 4;
2221
2222 *p++ = (unsigned char) ssl->major_ver;
2223 *p++ = (unsigned char) ssl->minor_ver;
2224
2225 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
2226 buf[4], buf[5] ) );
2227
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002228#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002229 t = time( NULL );
2230 *p++ = (unsigned char)( t >> 24 );
2231 *p++ = (unsigned char)( t >> 16 );
2232 *p++ = (unsigned char)( t >> 8 );
2233 *p++ = (unsigned char)( t );
2234
2235 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002236#else
2237 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
2238 return( ret );
2239
2240 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02002241#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002242
Paul Bakkera3d195c2011-11-27 21:07:34 +00002243 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
2244 return( ret );
2245
2246 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002247
Paul Bakker48916f92012-09-16 19:57:18 +00002248 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002249
2250 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
2251
2252 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002253 * Resume is 0 by default, see ssl_handshake_init().
2254 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2255 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002256 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002257 if( ssl->handshake->resume == 0 &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002258#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002259 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002260#endif
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002261 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002262 ssl->f_get_cache != NULL &&
2263 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
2264 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01002265 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002266 ssl->handshake->resume = 1;
2267 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002268
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002269 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002270 {
2271 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002272 * New session, create a new session id,
2273 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002274 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002275 ssl->state++;
2276
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002277#if defined(POLARSSL_HAVE_TIME)
2278 ssl->session_negotiate->start = time( NULL );
2279#endif
2280
Paul Bakkera503a632013-08-14 13:48:06 +02002281#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002282 if( ssl->handshake->new_session_ticket != 0 )
2283 {
2284 ssl->session_negotiate->length = n = 0;
2285 memset( ssl->session_negotiate->id, 0, 32 );
2286 }
2287 else
2288#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002289 {
2290 ssl->session_negotiate->length = n = 32;
2291 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002292 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002293 return( ret );
2294 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002295 }
2296 else
2297 {
2298 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002299 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002300 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02002301 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002302 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002303
2304 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2305 {
2306 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2307 return( ret );
2308 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002309 }
2310
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002311 /*
2312 * 38 . 38 session id length
2313 * 39 . 38+n session id
2314 * 39+n . 40+n chosen ciphersuite
2315 * 41+n . 41+n chosen compression alg.
2316 * 42+n . 43+n extensions length
2317 * 44+n . 43+n+m extensions
2318 */
2319 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00002320 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
2321 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002322
2323 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2324 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2325 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002326 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002327
Paul Bakker48916f92012-09-16 19:57:18 +00002328 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2329 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2330 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002331
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02002332 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2333 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02002334 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002335 ssl->session_negotiate->compression ) );
2336
Hanno Becker57457782017-06-09 15:30:29 +01002337 /* Do not write the extensions if the protocol is SSLv3 */
2338#if defined(POLARSSL_SSL_PROTO_SSL3)
2339 if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
2340 {
2341#endif
2342
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002343 /*
2344 * First write extensions, then the total length
2345 */
2346 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2347 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002348
Paul Bakker05decb22013-08-15 13:33:48 +02002349#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002350 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2351 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002352#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002353
Paul Bakker1f2bc622013-08-15 13:45:55 +02002354#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002355 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2356 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002357#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002358
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002359#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2360 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2361 ext_len += olen;
2362#endif
2363
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002364#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2365 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2366 ext_len += olen;
2367#endif
2368
Paul Bakkera503a632013-08-14 13:48:06 +02002369#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002370 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2371 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002372#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002373
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002374#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002375 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2376 ext_len += olen;
2377#endif
2378
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002379#if defined(POLARSSL_SSL_ALPN)
2380 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2381 ext_len += olen;
2382#endif
2383
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002384 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002385
Paul Bakkera7036632014-04-30 10:15:38 +02002386 if( ext_len > 0 )
2387 {
2388 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2389 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2390 p += ext_len;
2391 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002392
Hanno Becker57457782017-06-09 15:30:29 +01002393#if defined(POLARSSL_SSL_PROTO_SSL3)
2394 }
2395#endif
2396
Paul Bakker5121ce52009-01-03 21:22:43 +00002397 ssl->out_msglen = p - buf;
2398 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2399 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
2400
2401 ret = ssl_write_record( ssl );
2402
2403 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2404
2405 return( ret );
2406}
2407
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002408#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2409 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Simon Butcher7458bc32016-09-05 11:18:39 +03002410 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002411 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
Simon Butcher7458bc32016-09-05 11:18:39 +03002412 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)&& \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002413 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002414static int ssl_write_certificate_request( ssl_context *ssl )
2415{
Paul Bakkered27a042013-04-18 22:46:23 +02002416 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002417
2418 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2419
2420 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002421 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002422 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2423 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002424 {
2425 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2426 ssl->state++;
2427 return( 0 );
2428 }
2429
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002430 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2431 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002432}
2433#else
2434static int ssl_write_certificate_request( ssl_context *ssl )
2435{
2436 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2437 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002438 size_t dn_size, total_dn_size; /* excluding length bytes */
2439 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002440 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002441 const x509_crt *crt;
Manuel Pégourié-Gonnardde9c8a52015-10-02 11:16:47 +02002442 const unsigned char * const end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00002443
2444 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2445
2446 ssl->state++;
2447
Paul Bakkerfbb17802013-04-17 19:10:21 +02002448 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002449 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002450 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002451 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002452 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002453 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002454 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002455 return( 0 );
2456 }
2457
2458 /*
2459 * 0 . 0 handshake type
2460 * 1 . 3 handshake length
2461 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002462 * 5 .. m-1 cert types
2463 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002464 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002465 * n .. n+1 length of all DNs
2466 * n+2 .. n+3 length of DN 1
2467 * n+4 .. ... Distinguished Name #1
2468 * ... .. ... length of DN 2, etc.
2469 */
2470 buf = ssl->out_msg;
2471 p = buf + 4;
2472
2473 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002474 * Supported certificate types
2475 *
2476 * ClientCertificateType certificate_types<1..2^8-1>;
2477 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002478 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002479 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002480
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002481#if defined(POLARSSL_RSA_C)
2482 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2483#endif
2484#if defined(POLARSSL_ECDSA_C)
2485 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2486#endif
2487
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002488 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002489 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002490
Paul Bakker577e0062013-08-28 11:57:20 +02002491 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002492#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002493 /*
2494 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002495 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002496 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2497 *
2498 * struct {
2499 * HashAlgorithm hash;
2500 * SignatureAlgorithm signature;
2501 * } SignatureAndHashAlgorithm;
2502 *
2503 * enum { (255) } HashAlgorithm;
2504 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002505 */
Paul Bakker21dca692013-01-03 11:41:08 +01002506 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002507 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002508 /*
2509 * Only use current running hash algorithm that is already required
2510 * for requested ciphersuite.
2511 */
Paul Bakker926af752012-11-23 13:38:07 +01002512 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2513
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002514 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2515 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002516 {
2517 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2518 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002519
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002520 /*
2521 * Supported signature algorithms
2522 */
2523#if defined(POLARSSL_RSA_C)
2524 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2525 p[2 + sa_len++] = SSL_SIG_RSA;
2526#endif
2527#if defined(POLARSSL_ECDSA_C)
2528 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2529 p[2 + sa_len++] = SSL_SIG_ECDSA;
2530#endif
Paul Bakker926af752012-11-23 13:38:07 +01002531
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002532 p[0] = (unsigned char)( sa_len >> 8 );
2533 p[1] = (unsigned char)( sa_len );
2534 sa_len += 2;
2535 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002536 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002537#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002538
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002539 /*
2540 * DistinguishedName certificate_authorities<0..2^16-1>;
2541 * opaque DistinguishedName<1..2^16-1>;
2542 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002543 p += 2;
2544 crt = ssl->ca_chain;
2545
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002546 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002547 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002548 {
Paul Bakker926af752012-11-23 13:38:07 +01002549 dn_size = crt->subject_raw.len;
Manuel Pégourié-Gonnardde9c8a52015-10-02 11:16:47 +02002550
Manuel Pégourié-Gonnardcf16b792015-12-10 14:36:25 +01002551 if( end < p ||
2552 (size_t)( end - p ) < dn_size ||
2553 (size_t)( end - p ) < 2 + dn_size )
Manuel Pégourié-Gonnardde9c8a52015-10-02 11:16:47 +02002554 {
2555 SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) );
2556 break;
2557 }
2558
Paul Bakker926af752012-11-23 13:38:07 +01002559 *p++ = (unsigned char)( dn_size >> 8 );
2560 *p++ = (unsigned char)( dn_size );
2561 memcpy( p, crt->subject_raw.p, dn_size );
2562 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002563
Paul Bakker926af752012-11-23 13:38:07 +01002564 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2565
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002566 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002567 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002568 }
2569
Paul Bakker926af752012-11-23 13:38:07 +01002570 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002571 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2572 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002573 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2574 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002575
2576 ret = ssl_write_record( ssl );
2577
2578 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2579
2580 return( ret );
2581}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002582#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2583 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Simon Butcher7458bc32016-09-05 11:18:39 +03002584 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002585 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
Simon Butcher7458bc32016-09-05 11:18:39 +03002586 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002587 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002588
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002589#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2590 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2591static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2592{
2593 int ret;
2594
2595 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2596 {
2597 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2598 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2599 }
2600
2601 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2602 pk_ec( *ssl_own_key( ssl ) ),
2603 POLARSSL_ECDH_OURS ) ) != 0 )
2604 {
2605 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2606 return( ret );
2607 }
2608
2609 return( 0 );
2610}
2611#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2612 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2613
Paul Bakker41c83d32013-03-20 14:39:14 +01002614static int ssl_write_server_key_exchange( ssl_context *ssl )
2615{
Paul Bakker23986e52011-04-24 08:57:21 +00002616 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002617 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002618 const ssl_ciphersuite_t *ciphersuite_info =
2619 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002620
2621#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2622 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2623 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002624 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002625 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002626 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002627 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002628 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002629 ((void) dig_signed);
2630 ((void) dig_signed_len);
2631#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002632
Paul Bakker5121ce52009-01-03 21:22:43 +00002633 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2634
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002635#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2636 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2637 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002638 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2639 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2640 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002641 {
2642 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2643 ssl->state++;
2644 return( 0 );
2645 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002646#endif
2647
2648#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2649 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2650 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2651 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2652 {
2653 ssl_get_ecdh_params_from_cert( ssl );
2654
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002655 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002656 ssl->state++;
2657 return( 0 );
2658 }
2659#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002660
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002661#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2662 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2663 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2664 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002665 {
2666 /* TODO: Support identity hints */
2667 *(p++) = 0x00;
2668 *(p++) = 0x00;
2669
2670 n += 2;
2671 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002672#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2673 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002674
2675#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2676 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2677 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2678 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002679 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002680 /*
2681 * Ephemeral DH parameters:
2682 *
2683 * struct {
2684 * opaque dh_p<1..2^16-1>;
2685 * opaque dh_g<1..2^16-1>;
2686 * opaque dh_Ys<1..2^16-1>;
2687 * } ServerDHParams;
2688 */
2689 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2690 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2691 {
2692 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2693 return( ret );
2694 }
Paul Bakker48916f92012-09-16 19:57:18 +00002695
Paul Bakker41c83d32013-03-20 14:39:14 +01002696 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002697 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2698 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002699 {
2700 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2701 return( ret );
2702 }
2703
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002704 dig_signed = p;
2705 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002706
2707 p += len;
2708 n += len;
2709
Paul Bakker41c83d32013-03-20 14:39:14 +01002710 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2711 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2712 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2713 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2714 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002715#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2716 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002717
Gergely Budai987bfb52014-01-19 21:48:42 +01002718#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002719 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002720 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2721 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002722 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002723 /*
2724 * Ephemeral ECDH parameters:
2725 *
2726 * struct {
2727 * ECParameters curve_params;
2728 * ECPoint public;
2729 * } ServerECDHParams;
2730 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002731 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002732#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002733 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002734
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002735 /* Match our preference list against the offered curves */
2736 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2737 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2738 if( (*curve)->grp_id == *gid )
2739 goto curve_matching_done;
2740
2741curve_matching_done:
2742#else
2743 curve = ssl->handshake->curves;
2744#endif
2745
Manuel Pégourié-Gonnard8e8ae3d2015-06-23 18:57:28 +02002746 if( curve == NULL || *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002747 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002748 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2749 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002750 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002751
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002752 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002753
Paul Bakker41c83d32013-03-20 14:39:14 +01002754 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002755 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002756 {
2757 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2758 return( ret );
2759 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002760
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002761 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2762 p, SSL_MAX_CONTENT_LEN - n,
2763 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002764 {
2765 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2766 return( ret );
2767 }
2768
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002769 dig_signed = p;
2770 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002771
2772 p += len;
2773 n += len;
2774
Paul Bakker41c83d32013-03-20 14:39:14 +01002775 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2776 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002777#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002778
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002779#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002780 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2781 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002782 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002783 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2784 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002785 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002786 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002787 unsigned int hashlen = 0;
2788 unsigned char hash[64];
Paul Bakker23f36802012-09-28 14:15:14 +00002789
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002790 /*
Hanno Beckerc2b9d982017-05-10 16:37:21 +01002791 * Choose hash algorithm:
2792 * - For TLS 1.2, obey signature-hash-algorithm extension to choose appropriate hash.
2793 * - For SSL3, TLS1.0, TLS1.1 and ECDHE_ECDSA, use SHA1 (RFC 4492, Sec. 5.4)
2794 * - Otherwise, use MD5 + SHA1 (RFC 4346, Sec. 7.4.3)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002795 */
Hanno Beckerc2b9d982017-05-10 16:37:21 +01002796
2797 md_type_t md_alg;
2798
Paul Bakker577e0062013-08-28 11:57:20 +02002799#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Hanno Beckerc2b9d982017-05-10 16:37:21 +01002800 pk_type_t sig_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002801 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2802 {
Hanno Beckerc2b9d982017-05-10 16:37:21 +01002803 /* For TLS 1.2, obey signature-hash-algorithm extension
2804 * (RFC 5246, Sec. 7.4.1.4.1). */
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002805
Hanno Beckerc2b9d982017-05-10 16:37:21 +01002806 if( sig_alg == POLARSSL_PK_NONE ||
2807 ( md_alg = ssl_sig_hash_set_find( &ssl->handshake->hash_algs, sig_alg ) ) == POLARSSL_MD_NONE )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002808 {
2809 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002810 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002811 }
2812 }
Paul Bakker577e0062013-08-28 11:57:20 +02002813 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002814#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002815#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2816 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002817 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002818 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2819 {
2820 md_alg = POLARSSL_MD_SHA1;
2821 }
2822 else
Paul Bakker577e0062013-08-28 11:57:20 +02002823#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002824 {
2825 md_alg = POLARSSL_MD_NONE;
2826 }
2827
Hanno Beckerc2b9d982017-05-10 16:37:21 +01002828 SSL_DEBUG_MSG( 3, ( "pick hash algorithm %d for signing", md_alg ) );
2829
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002830 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002831 * Compute the hash to be signed
2832 */
Paul Bakker577e0062013-08-28 11:57:20 +02002833#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2834 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002835 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002836 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002837 md5_context md5;
2838 sha1_context sha1;
2839
Paul Bakker5b4af392014-06-26 12:09:34 +02002840 md5_init( &md5 );
2841 sha1_init( &sha1 );
2842
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002843 /*
2844 * digitally-signed struct {
2845 * opaque md5_hash[16];
2846 * opaque sha_hash[20];
2847 * };
2848 *
2849 * md5_hash
2850 * MD5(ClientHello.random + ServerHello.random
2851 * + ServerParams);
2852 * sha_hash
2853 * SHA(ClientHello.random + ServerHello.random
2854 * + ServerParams);
2855 */
2856 md5_starts( &md5 );
2857 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002858 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002859 md5_finish( &md5, hash );
2860
2861 sha1_starts( &sha1 );
2862 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002863 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002864 sha1_finish( &sha1, hash + 16 );
2865
2866 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02002867
2868 md5_free( &md5 );
2869 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002870 }
2871 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002872#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2873 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002874#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2875 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002876 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002877 {
2878 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02002879 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002880
Paul Bakker84bbeb52014-07-01 14:53:22 +02002881 md_init( &ctx );
2882
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002883 /* Info from md_alg will be used instead */
2884 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002885
2886 /*
2887 * digitally-signed struct {
2888 * opaque client_random[32];
2889 * opaque server_random[32];
2890 * ServerDHParams params;
2891 * };
2892 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002893 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002894 {
2895 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2896 return( ret );
2897 }
2898
2899 md_starts( &ctx );
2900 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002901 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002902 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002903 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00002904 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002905 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002906#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2907 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002908 {
2909 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002910 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002911 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002912
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002913 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2914 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002915
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002916 /*
2917 * Make the signature
2918 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002919 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002920 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002921 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2922 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002923 }
Paul Bakker23f36802012-09-28 14:15:14 +00002924
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002925#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002926 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2927 {
Hanno Beckerc2b9d982017-05-10 16:37:21 +01002928 *(p++) = ssl_hash_from_md_alg( md_alg );
2929 *(p++) = ssl_sig_from_pk_alg( sig_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002930
2931 n += 2;
2932 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002933#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002934
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002935 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002936 p + 2 , &signature_len,
2937 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002938 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002939 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002940 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002941 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002942
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002943 *(p++) = (unsigned char)( signature_len >> 8 );
2944 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002945 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002946
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002947 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002948
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002949 p += signature_len;
2950 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002951 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002952#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002953 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2954 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002955
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002956 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002957 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2958 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2959
2960 ssl->state++;
2961
2962 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2963 {
2964 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2965 return( ret );
2966 }
2967
2968 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2969
2970 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002971}
2972
2973static int ssl_write_server_hello_done( ssl_context *ssl )
2974{
2975 int ret;
2976
2977 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2978
2979 ssl->out_msglen = 4;
2980 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2981 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2982
2983 ssl->state++;
2984
2985 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2986 {
2987 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2988 return( ret );
2989 }
2990
2991 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2992
2993 return( 0 );
2994}
2995
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002996#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2997 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2998static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2999 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003000{
3001 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003002 size_t n;
3003
3004 /*
3005 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3006 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003007 if( *p + 2 > end )
3008 {
3009 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3010 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3011 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003012
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003013 n = ( (*p)[0] << 8 ) | (*p)[1];
3014 *p += 2;
3015
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003016 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003017 {
3018 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3019 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3020 }
3021
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003022 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003023 {
3024 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
3025 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3026 }
3027
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003028 *p += n;
3029
Paul Bakker70df2fb2013-04-17 17:19:09 +02003030 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
3031
Paul Bakker70df2fb2013-04-17 17:19:09 +02003032 return( ret );
3033}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003034#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3035 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003036
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003037#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
3038 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003039static int ssl_parse_encrypted_pms( ssl_context *ssl,
3040 const unsigned char *p,
3041 const unsigned char *end,
3042 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003043{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003044 int ret;
3045 size_t len = pk_get_len( ssl_own_key( ssl ) );
3046 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003047 unsigned char fake_pms[48], peer_pms[48];
3048 unsigned char mask;
Manuel Pégourié-Gonnardb26b75e2015-06-23 18:52:09 +02003049 size_t i, peer_pmslen;
3050 unsigned int diff;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003051
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003052 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003053 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02003054 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003055 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3056 }
3057
3058 /*
3059 * Decrypt the premaster using own private RSA key
3060 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003061#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3062 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02003063 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
3064 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003065 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
3066 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003067 {
3068 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3069 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3070 }
3071 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003072#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003073
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003074 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003075 {
3076 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3077 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3078 }
3079
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003080 /*
3081 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
3082 * must not cause the connection to end immediately; instead, send a
3083 * bad_record_mac later in the handshake.
3084 * Also, avoid data-dependant branches here to protect against
3085 * timing-based variants.
3086 */
3087 ret = ssl->f_rng( ssl->p_rng, fake_pms, sizeof( fake_pms ) );
3088 if( ret != 0 )
3089 return( ret );
3090
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003091 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02003092 peer_pms, &peer_pmslen,
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003093 sizeof( peer_pms ),
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003094 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003095
Manuel Pégourié-Gonnardb26b75e2015-06-23 18:52:09 +02003096 diff = (unsigned int) ret;
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02003097 diff |= peer_pmslen ^ 48;
3098 diff |= peer_pms[0] ^ ssl->handshake->max_major_ver;
3099 diff |= peer_pms[1] ^ ssl->handshake->max_minor_ver;
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003100
3101#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02003102 if( diff != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003103 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003104#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003105
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003106 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
3107 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
3108 {
3109 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3110 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003111 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003112 ssl->handshake->pmslen = 48;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003113
Manuel Pégourié-Gonnardb26b75e2015-06-23 18:52:09 +02003114 /* mask = diff ? 0xff : 0x00 */
Simon Ba697bf52016-11-10 13:19:42 +00003115 /* MSVC has a warning about unary minus on unsigned, but this is
3116 * well-defined and precisely what we want to do here */
3117#if defined(_MSC_VER)
3118#pragma warning( push )
3119#pragma warning( disable : 4146 )
3120#endif
Manuel Pégourié-Gonnardb26b75e2015-06-23 18:52:09 +02003121 mask = - ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 );
Simon Ba697bf52016-11-10 13:19:42 +00003122#if defined(_MSC_VER)
3123#pragma warning( pop )
3124#endif
3125
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003126 for( i = 0; i < ssl->handshake->pmslen; i++ )
3127 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
3128
3129 return( 0 );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003130}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003131#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
3132 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003133
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003134#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003135static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
3136 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003137{
Paul Bakker6db455e2013-09-18 17:29:31 +02003138 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003139 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003140
Paul Bakker6db455e2013-09-18 17:29:31 +02003141 if( ssl->f_psk == NULL &&
3142 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
3143 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003144 {
3145 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
3146 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3147 }
3148
3149 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003150 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003151 */
Hanno Beckerb2ee6b42017-06-26 13:52:14 +01003152 if( end - *p < 2 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003153 {
3154 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3155 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3156 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003157
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003158 n = ( (*p)[0] << 8 ) | (*p)[1];
3159 *p += 2;
3160
Hanno Beckerb2ee6b42017-06-26 13:52:14 +01003161 if( n < 1 || n > 65535 || n > (size_t) ( end - *p ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003162 {
3163 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3164 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3165 }
3166
Paul Bakker6db455e2013-09-18 17:29:31 +02003167 if( ssl->f_psk != NULL )
3168 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003169 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003170 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3171 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003172 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003173 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003174 /* Identity is not a big secret since clients send it in the clear,
3175 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02003176 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003177 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003178 {
3179 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3180 }
3181 }
3182
3183 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003184 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003185 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02003186 if( ( ret = ssl_send_alert_message( ssl,
3187 SSL_ALERT_LEVEL_FATAL,
3188 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
3189 {
3190 return( ret );
3191 }
3192
3193 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003194 }
3195
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003196 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003197
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003198 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003199}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003200#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003201
Paul Bakker5121ce52009-01-03 21:22:43 +00003202static int ssl_parse_client_key_exchange( ssl_context *ssl )
3203{
Paul Bakker23986e52011-04-24 08:57:21 +00003204 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003205 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003206
Paul Bakker41c83d32013-03-20 14:39:14 +01003207 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003208
3209 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
3210
3211 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3212 {
3213 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3214 return( ret );
3215 }
3216
3217 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3218 {
3219 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003220 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003221 }
3222
3223 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
3224 {
3225 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003226 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003227 }
3228
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003229#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01003230 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003231 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003232 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003233 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003234
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003235 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003236 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02003237 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3238 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003239 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003240
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003241 if( p != end )
3242 {
3243 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3244 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3245 }
3246
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02003247 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003248
3249 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
3250 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003251 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02003252 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003253 {
3254 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
3255 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3256 }
3257
3258 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003259 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003260 else
3261#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003262#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003263 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3264 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3265 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003266 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003267 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
3268 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
3269 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003270 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003271 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003272 ssl->in_msg + 4, ssl->in_hslen - 4 ) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003273 {
3274 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3275 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3276 }
3277
3278 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3279
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003280 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
3281 &ssl->handshake->pmslen,
3282 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02003283 POLARSSL_MPI_MAX_SIZE,
3284 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003285 {
3286 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
3287 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3288 }
3289
3290 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003291 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003292 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003293#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003294 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3295 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3296 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003297#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
3298 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003299 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003300 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003301 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003302
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003303 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003304 {
3305 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3306 return( ret );
3307 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003308
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003309 if( p != end )
3310 {
3311 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3312 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3313 }
3314
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003315 if( ( ret = ssl_psk_derive_premaster( ssl,
3316 ciphersuite_info->key_exchange ) ) != 0 )
3317 {
3318 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3319 return( ret );
3320 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003321 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003322 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003323#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003324#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
3325 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
3326 {
3327 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003328 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003329
3330 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3331 {
3332 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3333 return( ret );
3334 }
3335
3336 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3337 {
3338 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
3339 return( ret );
3340 }
3341
3342 if( ( ret = ssl_psk_derive_premaster( ssl,
3343 ciphersuite_info->key_exchange ) ) != 0 )
3344 {
3345 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3346 return( ret );
3347 }
3348 }
3349 else
3350#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003351#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3352 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3353 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003354 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003355 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003356
3357 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3358 {
3359 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3360 return( ret );
3361 }
3362 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3363 {
3364 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3365 return( ret );
3366 }
3367
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003368 if( p != end )
3369 {
3370 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3371 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3372 }
3373
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003374 if( ( ret = ssl_psk_derive_premaster( ssl,
3375 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003376 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003377 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3378 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003379 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003380 }
3381 else
3382#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003383#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3384 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
3385 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003386 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003387 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003388
3389 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3390 {
3391 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3392 return( ret );
3393 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003394
3395 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
3396 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003397 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003398 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3399 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003400 }
3401
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003402 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3403
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003404 if( ( ret = ssl_psk_derive_premaster( ssl,
3405 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003406 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003407 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003408 return( ret );
3409 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003410 }
3411 else
3412#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003413#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
3414 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003415 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003416 if( ( ret = ssl_parse_encrypted_pms( ssl,
3417 ssl->in_msg + 4,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003418 ssl->in_msg + ssl->in_hslen,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003419 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003420 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003421 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003422 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003423 }
3424 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003425 else
3426#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
3427 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003428 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003429 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003430 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003431
Paul Bakkerff60ee62010-03-16 21:09:09 +00003432 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
3433 {
3434 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
3435 return( ret );
3436 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003437
Paul Bakker5121ce52009-01-03 21:22:43 +00003438 ssl->state++;
3439
3440 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3441
3442 return( 0 );
3443}
3444
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003445#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3446 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Simon Butcher7458bc32016-09-05 11:18:39 +03003447 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003448 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
Simon Butcher7458bc32016-09-05 11:18:39 +03003449 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)&& \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003450 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003451static int ssl_parse_certificate_verify( ssl_context *ssl )
3452{
Paul Bakkerfbb17802013-04-17 19:10:21 +02003453 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003454
3455 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3456
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003457 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003458 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003459 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003460 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02003461 {
3462 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3463 ssl->state++;
3464 return( 0 );
3465 }
3466
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003467 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3468 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003469}
3470#else
3471static int ssl_parse_certificate_verify( ssl_context *ssl )
3472{
3473 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003474 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003475 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003476 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003477 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003478#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003479 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003480#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003481 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003482 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3483
3484 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3485
3486 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003487 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003488 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003489 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3490 {
3491 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3492 ssl->state++;
3493 return( 0 );
3494 }
3495
Paul Bakkered27a042013-04-18 22:46:23 +02003496 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003497 {
3498 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3499 ssl->state++;
3500 return( 0 );
3501 }
3502
Paul Bakker48916f92012-09-16 19:57:18 +00003503 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003504
3505 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3506 {
3507 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3508 return( ret );
3509 }
3510
3511 ssl->state++;
3512
3513 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3514 {
3515 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003516 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003517 }
3518
3519 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
3520 {
3521 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003522 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003523 }
3524
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003525 /*
3526 * 0 . 0 handshake type
3527 * 1 . 3 handshake length
3528 * 4 . 5 sig alg (TLS 1.2 only)
3529 * 4+n . 5+n signature length (n = sa_len)
3530 * 6+n . 6+n+m signature (m = sig_len)
3531 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003532
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003533#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3534 defined(POLARSSL_SSL_PROTO_TLS1_1)
3535 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003536 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003537 sa_len = 0;
3538
Paul Bakkerc70b9822013-04-07 22:00:46 +02003539 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003540 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003541
3542 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3543 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3544 POLARSSL_PK_ECDSA ) )
3545 {
3546 hash_start += 16;
3547 hashlen -= 16;
3548 md_alg = POLARSSL_MD_SHA1;
3549 }
Paul Bakker926af752012-11-23 13:38:07 +01003550 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003551 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003552#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3553 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003554#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3555 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003556 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003557 sa_len = 2;
3558
Paul Bakker5121ce52009-01-03 21:22:43 +00003559 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003560 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003561 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003562 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003563 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003564 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3565 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003566 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3567 }
3568
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003569 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003570
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003571 /* Info from md_alg will be used instead */
3572 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003573
3574 /*
3575 * Signature
3576 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003577 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
3578 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003579 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003580 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3581 " for verify message" ) );
3582 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003583 }
3584
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003585 /*
3586 * Check the certificate's key type matches the signature alg
3587 */
3588 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3589 {
3590 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3591 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3592 }
Paul Bakker577e0062013-08-28 11:57:20 +02003593 }
3594 else
3595#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3596 {
3597 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003598 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003599 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003600
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003601 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01003602
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003603 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003604 {
3605 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003606 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003607 }
3608
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003609 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003610 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003611 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003612 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003613 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003614 return( ret );
3615 }
3616
3617 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3618
Paul Bakkered27a042013-04-18 22:46:23 +02003619 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003620}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003621#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3622 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Simon Butcher7458bc32016-09-05 11:18:39 +03003623 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED &&
3624 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
3625 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED &&
3626 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003627
Paul Bakkera503a632013-08-14 13:48:06 +02003628#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003629static int ssl_write_new_session_ticket( ssl_context *ssl )
3630{
3631 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003632 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003633 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003634
3635 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3636
3637 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3638 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3639
3640 /*
3641 * struct {
3642 * uint32 ticket_lifetime_hint;
3643 * opaque ticket<0..2^16-1>;
3644 * } NewSessionTicket;
3645 *
3646 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3647 * 8 . 9 ticket_len (n)
3648 * 10 . 9+n ticket content
3649 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003650
3651 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3652 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3653 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3654 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003655
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003656 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3657 {
3658 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3659 tlen = 0;
3660 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003661
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003662 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3663 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003664
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003665 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003666
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003667 /*
3668 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3669 * ChangeCipherSpec share the same state.
3670 */
3671 ssl->handshake->new_session_ticket = 0;
3672
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003673 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3674 {
3675 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3676 return( ret );
3677 }
3678
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003679 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3680
3681 return( 0 );
3682}
Paul Bakkera503a632013-08-14 13:48:06 +02003683#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003684
Paul Bakker5121ce52009-01-03 21:22:43 +00003685/*
Paul Bakker1961b702013-01-25 14:49:24 +01003686 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003687 */
Paul Bakker1961b702013-01-25 14:49:24 +01003688int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003689{
3690 int ret = 0;
3691
Paul Bakker1961b702013-01-25 14:49:24 +01003692 if( ssl->state == SSL_HANDSHAKE_OVER )
3693 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003694
Paul Bakker1961b702013-01-25 14:49:24 +01003695 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3696
3697 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3698 return( ret );
3699
3700 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003701 {
Paul Bakker1961b702013-01-25 14:49:24 +01003702 case SSL_HELLO_REQUEST:
3703 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003704 break;
3705
Paul Bakker1961b702013-01-25 14:49:24 +01003706 /*
3707 * <== ClientHello
3708 */
3709 case SSL_CLIENT_HELLO:
3710 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003711 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003712
3713 /*
3714 * ==> ServerHello
3715 * Certificate
3716 * ( ServerKeyExchange )
3717 * ( CertificateRequest )
3718 * ServerHelloDone
3719 */
3720 case SSL_SERVER_HELLO:
3721 ret = ssl_write_server_hello( ssl );
3722 break;
3723
3724 case SSL_SERVER_CERTIFICATE:
3725 ret = ssl_write_certificate( ssl );
3726 break;
3727
3728 case SSL_SERVER_KEY_EXCHANGE:
3729 ret = ssl_write_server_key_exchange( ssl );
3730 break;
3731
3732 case SSL_CERTIFICATE_REQUEST:
3733 ret = ssl_write_certificate_request( ssl );
3734 break;
3735
3736 case SSL_SERVER_HELLO_DONE:
3737 ret = ssl_write_server_hello_done( ssl );
3738 break;
3739
3740 /*
3741 * <== ( Certificate/Alert )
3742 * ClientKeyExchange
3743 * ( CertificateVerify )
3744 * ChangeCipherSpec
3745 * Finished
3746 */
3747 case SSL_CLIENT_CERTIFICATE:
3748 ret = ssl_parse_certificate( ssl );
3749 break;
3750
3751 case SSL_CLIENT_KEY_EXCHANGE:
3752 ret = ssl_parse_client_key_exchange( ssl );
3753 break;
3754
3755 case SSL_CERTIFICATE_VERIFY:
3756 ret = ssl_parse_certificate_verify( ssl );
3757 break;
3758
3759 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3760 ret = ssl_parse_change_cipher_spec( ssl );
3761 break;
3762
3763 case SSL_CLIENT_FINISHED:
3764 ret = ssl_parse_finished( ssl );
3765 break;
3766
3767 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003768 * ==> ( NewSessionTicket )
3769 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003770 * Finished
3771 */
3772 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003773#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003774 if( ssl->handshake->new_session_ticket != 0 )
3775 ret = ssl_write_new_session_ticket( ssl );
3776 else
Paul Bakkera503a632013-08-14 13:48:06 +02003777#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003778 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003779 break;
3780
3781 case SSL_SERVER_FINISHED:
3782 ret = ssl_write_finished( ssl );
3783 break;
3784
3785 case SSL_FLUSH_BUFFERS:
3786 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3787 ssl->state = SSL_HANDSHAKE_WRAPUP;
3788 break;
3789
3790 case SSL_HANDSHAKE_WRAPUP:
3791 ssl_handshake_wrapup( ssl );
3792 break;
3793
3794 default:
3795 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3796 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003797 }
3798
Paul Bakker5121ce52009-01-03 21:22:43 +00003799 return( ret );
3800}
Paul Bakker9af723c2014-05-01 13:03:14 +02003801#endif /* POLARSSL_SSL_SRV_C */