blob: 8ad990b1a3667207ab6671907776304f325a138c [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 /*
794 * Use our order of preference
795 */
796 start = buf + 2;
797 end = buf + len;
798 for( ours = ssl->alpn_list; *ours != NULL; ours++ )
799 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200800 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200801 for( theirs = start; theirs != end; theirs += cur_len )
802 {
803 /* If the list is well formed, we should get equality first */
804 if( theirs > end )
805 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
806
807 cur_len = *theirs++;
808
809 /* Empty strings MUST NOT be included */
810 if( cur_len == 0 )
811 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
812
Paul Bakker14b16c62014-05-28 11:33:54 +0200813 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200814 memcmp( theirs, *ours, cur_len ) == 0 )
815 {
816 ssl->alpn_chosen = *ours;
817 return( 0 );
818 }
819 }
820 }
821
822 /* If we get there, no match was found */
823 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
824 SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
825 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
826}
827#endif /* POLARSSL_SSL_ALPN */
828
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100829/*
830 * Auxiliary functions for ServerHello parsing and related actions
831 */
832
833#if defined(POLARSSL_X509_CRT_PARSE_C)
834/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100835 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100836 */
837#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100838static int ssl_check_key_curve( pk_context *pk,
839 const ecp_curve_info **curves )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100840{
841 const ecp_curve_info **crv = curves;
842 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
843
844 while( *crv != NULL )
845 {
846 if( (*crv)->grp_id == grp_id )
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100847 return( 0 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100848 crv++;
849 }
850
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100851 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100852}
853#endif /* POLARSSL_ECDSA_C */
854
855/*
856 * Try picking a certificate for this ciphersuite,
857 * return 0 on success and -1 on failure.
858 */
859static int ssl_pick_cert( ssl_context *ssl,
860 const ssl_ciphersuite_t * ciphersuite_info )
861{
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100862 ssl_key_cert *cur, *list, *fallback = NULL;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100863 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +0200864 int flags;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100865
866#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
867 if( ssl->handshake->sni_key_cert != NULL )
868 list = ssl->handshake->sni_key_cert;
869 else
870#endif
871 list = ssl->handshake->key_cert;
872
873 if( pk_alg == POLARSSL_PK_NONE )
874 return( 0 );
875
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000876 SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) );
877
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100878 for( cur = list; cur != NULL; cur = cur->next )
879 {
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000880 SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate",
881 cur->cert );
882
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100883 if( ! pk_can_do( cur->key, pk_alg ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000884 {
885 SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100886 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000887 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100888
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200889 /*
890 * This avoids sending the client a cert it'll reject based on
891 * keyUsage or other extensions.
892 *
893 * It also allows the user to provision different certificates for
894 * different uses based on keyUsage, eg if they want to avoid signing
895 * and decrypting with the same RSA key.
896 */
897 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +0200898 SSL_IS_SERVER, &flags ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200899 {
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000900 SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
901 "(extended) key usage extension" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200902 continue;
903 }
904
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100905#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100906 if( pk_alg == POLARSSL_PK_ECDSA &&
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100907 ssl_check_key_curve( cur->key, ssl->handshake->curves ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000908 {
909 SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) );
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100910 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000911 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100912#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100913
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100914 /*
915 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
916 * present them a SHA-higher cert rather than failing if it's the only
917 * one we got that satisfies the other conditions.
918 */
919 if( ssl->minor_ver < SSL_MINOR_VERSION_3 &&
920 cur->cert->sig_md != POLARSSL_MD_SHA1 )
921 {
922 if( fallback == NULL )
923 fallback = cur;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000924 {
925 SSL_DEBUG_MSG( 3, ( "certificate not preferred: "
926 "sha-2 with pre-TLS 1.2 client" ) );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100927 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000928 }
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100929 }
930
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100931 /* If we get there, we got a winner */
932 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100933 }
934
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000935 if( cur == NULL )
936 cur = fallback;
937
938
939 /* Do not update ssl->handshake->key_cert unless the is a match */
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100940 if( cur != NULL )
941 {
942 ssl->handshake->key_cert = cur;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000943 SSL_DEBUG_CRT( 3, "selected certificate chain, certificate",
944 ssl->handshake->key_cert->cert );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100945 return( 0 );
946 }
947
948 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100949}
950#endif /* POLARSSL_X509_CRT_PARSE_C */
951
952/*
953 * Check if a given ciphersuite is suitable for use with our config/keys/etc
954 * Sets ciphersuite_info only if the suite matches.
955 */
956static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
957 const ssl_ciphersuite_t **ciphersuite_info )
958{
959 const ssl_ciphersuite_t *suite_info;
960
Hanno Beckerc2b9d982017-05-10 16:37:21 +0100961#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
962 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
963 pk_type_t sig_type;
964#endif
965
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100966 suite_info = ssl_ciphersuite_from_id( suite_id );
967 if( suite_info == NULL )
968 {
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100969 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
970 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100971 }
972
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000973 SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %s", suite_info->name ) );
974
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100975 if( suite_info->min_minor_ver > ssl->minor_ver ||
976 suite_info->max_minor_ver < ssl->minor_ver )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000977 {
978 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100979 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000980 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100981
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100982 if( ssl->arc4_disabled == SSL_ARC4_DISABLED &&
983 suite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000984 {
985 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100986 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000987 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100988
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100989#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
990 if( ssl_ciphersuite_uses_ec( suite_info ) &&
991 ( ssl->handshake->curves == NULL ||
992 ssl->handshake->curves[0] == NULL ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000993 {
994 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
995 "no common elliptic curve" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100996 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000997 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100998#endif
999
1000#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1001 /* If the ciphersuite requires a pre-shared key and we don't
1002 * have one, skip it now rather than failing later */
1003 if( ssl_ciphersuite_uses_psk( suite_info ) &&
1004 ssl->f_psk == NULL &&
1005 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
1006 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001007 {
1008 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001009 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001010 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001011#endif
1012
Hanno Beckerc2b9d982017-05-10 16:37:21 +01001013#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1014 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
1015 /* If the ciphersuite requires signing, check whether
1016 * a suitable hash algorithm is present. */
1017 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
1018 {
1019 sig_type = ssl_get_ciphersuite_sig_alg( suite_info );
1020 if( sig_type != POLARSSL_PK_NONE &&
1021 ssl_sig_hash_set_find( &ssl->handshake->hash_algs, sig_type ) == POLARSSL_MD_NONE )
1022 {
1023 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no suitable hash algorithm "
1024 "for signature algorithm %d", sig_type ) );
1025 return( 0 );
1026 }
1027 }
1028
1029#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
1030 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
1031
1032
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001033#if defined(POLARSSL_X509_CRT_PARSE_C)
1034 /*
1035 * Final check: if ciphersuite requires us to have a
1036 * certificate/key of a particular type:
1037 * - select the appropriate certificate if we have one, or
1038 * - try the next ciphersuite if we don't
1039 * This must be done last since we modify the key_cert list.
1040 */
1041 if( ssl_pick_cert( ssl, suite_info ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001042 {
1043 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
1044 "no suitable certificate" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001045 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001046 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001047#endif
1048
1049 *ciphersuite_info = suite_info;
1050 return( 0 );
1051}
1052
Paul Bakker78a8c712013-03-06 17:01:52 +01001053#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1054static int ssl_parse_client_hello_v2( ssl_context *ssl )
1055{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001056 int ret, got_common_suite;
Paul Bakker78a8c712013-03-06 17:01:52 +01001057 unsigned int i, j;
1058 size_t n;
1059 unsigned int ciph_len, sess_len, chal_len;
1060 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001061 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +02001062 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001063
1064 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
1065
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001066#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker78a8c712013-03-06 17:01:52 +01001067 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
1068 {
1069 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
1070
1071 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1072 return( ret );
1073
1074 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1075 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001076#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001077
1078 buf = ssl->in_hdr;
1079
1080 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1081
1082 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
1083 buf[2] ) );
1084 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
1085 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
1086 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
1087 buf[3], buf[4] ) );
1088
1089 /*
1090 * SSLv2 Client Hello
1091 *
1092 * Record layer:
1093 * 0 . 1 message length
1094 *
1095 * SSL layer:
1096 * 2 . 2 message type
1097 * 3 . 4 protocol version
1098 */
1099 if( buf[2] != SSL_HS_CLIENT_HELLO ||
1100 buf[3] != SSL_MAJOR_VERSION_3 )
1101 {
1102 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1103 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1104 }
1105
1106 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
1107
1108 if( n < 17 || n > 512 )
1109 {
1110 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1111 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1112 }
1113
1114 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +02001115 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
1116 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +01001117
1118 if( ssl->minor_ver < ssl->min_minor_ver )
1119 {
1120 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001121 " [%d:%d] < [%d:%d]",
1122 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +01001123 ssl->min_major_ver, ssl->min_minor_ver ) );
1124
1125 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1126 SSL_ALERT_MSG_PROTOCOL_VERSION );
1127 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1128 }
1129
Paul Bakker2fbefde2013-06-29 16:01:15 +02001130 ssl->handshake->max_major_ver = buf[3];
1131 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +01001132
1133 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
1134 {
1135 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1136 return( ret );
1137 }
1138
1139 ssl->handshake->update_checksum( ssl, buf + 2, n );
1140
1141 buf = ssl->in_msg;
1142 n = ssl->in_left - 5;
1143
1144 /*
1145 * 0 . 1 ciphersuitelist length
1146 * 2 . 3 session id length
1147 * 4 . 5 challenge length
1148 * 6 . .. ciphersuitelist
1149 * .. . .. session id
1150 * .. . .. challenge
1151 */
1152 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1153
1154 ciph_len = ( buf[0] << 8 ) | buf[1];
1155 sess_len = ( buf[2] << 8 ) | buf[3];
1156 chal_len = ( buf[4] << 8 ) | buf[5];
1157
1158 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
1159 ciph_len, sess_len, chal_len ) );
1160
1161 /*
1162 * Make sure each parameter length is valid
1163 */
1164 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1165 {
1166 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1167 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1168 }
1169
1170 if( sess_len > 32 )
1171 {
1172 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1173 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1174 }
1175
1176 if( chal_len < 8 || chal_len > 32 )
1177 {
1178 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1179 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1180 }
1181
1182 if( n != 6 + ciph_len + sess_len + chal_len )
1183 {
1184 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1185 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1186 }
1187
1188 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1189 buf + 6, ciph_len );
1190 SSL_DEBUG_BUF( 3, "client hello, session id",
1191 buf + 6 + ciph_len, sess_len );
1192 SSL_DEBUG_BUF( 3, "client hello, challenge",
1193 buf + 6 + ciph_len + sess_len, chal_len );
1194
1195 p = buf + 6 + ciph_len;
1196 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001197 memset( ssl->session_negotiate->id, 0,
1198 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001199 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1200
1201 p += sess_len;
1202 memset( ssl->handshake->randbytes, 0, 64 );
1203 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1204
1205 /*
1206 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1207 */
1208 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1209 {
1210 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1211 {
1212 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001213#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker78a8c712013-03-06 17:01:52 +01001214 if( ssl->renegotiation == SSL_RENEGOTIATION )
1215 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001216 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
1217 "during renegotiation" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001218
1219 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1220 return( ret );
1221
1222 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1223 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001224#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001225 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1226 break;
1227 }
1228 }
1229
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001230#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1231 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1232 {
1233 if( p[0] == 0 &&
1234 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1235 p[2] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1236 {
1237 SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
1238
1239 if( ssl->minor_ver < ssl->max_minor_ver )
1240 {
1241 SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
1242
1243 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1244 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1245
1246 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1247 }
1248
1249 break;
1250 }
1251 }
1252#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1253
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001254 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001255 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001256 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001257#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1258 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1259 {
1260 for( i = 0; ciphersuites[i] != 0; i++ )
1261#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001262 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001263 {
1264 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001265#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001266 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001267 if( p[0] != 0 ||
1268 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1269 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1270 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001271
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001272 got_common_suite = 1;
1273
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001274 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1275 &ciphersuite_info ) ) != 0 )
1276 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001277
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001278 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001279 goto have_ciphersuite_v2;
1280 }
1281 }
1282
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001283 if( got_common_suite )
1284 {
1285 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1286 "but none of them usable" ) );
1287 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
1288 }
1289 else
1290 {
1291 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1292 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1293 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001294
1295have_ciphersuite_v2:
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001296 SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
1297
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001298 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001299 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001300 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001301
1302 /*
1303 * SSLv2 Client Hello relevant renegotiation security checks
1304 */
1305 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1306 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1307 {
1308 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1309
1310 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1311 return( ret );
1312
1313 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1314 }
1315
1316 ssl->in_left = 0;
1317 ssl->state++;
1318
1319 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1320
1321 return( 0 );
1322}
1323#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1324
Paul Bakker5121ce52009-01-03 21:22:43 +00001325static int ssl_parse_client_hello( ssl_context *ssl )
1326{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001327 int ret, got_common_suite;
Paul Bakker23986e52011-04-24 08:57:21 +00001328 unsigned int i, j;
1329 size_t n;
1330 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001331 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001332 unsigned int ext_len = 0;
1333 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001334#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001335 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001336#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001337 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001338 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001339 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001340
Hanno Beckerc2b9d982017-05-10 16:37:21 +01001341 /* If there is no signature-algorithm extension present,
1342 * we need to fall back to the default values for allowed
1343 * signature-hash pairs. */
1344#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1345 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
1346 int sig_hash_alg_ext_present = 0;
1347#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
1348 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
1349
Paul Bakker5121ce52009-01-03 21:22:43 +00001350 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1351
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001352#if defined(POLARSSL_SSL_RENEGOTIATION)
1353 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1354#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001355 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001356 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
1357 {
1358 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1359 return( ret );
1360 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001361 }
1362
1363 buf = ssl->in_hdr;
1364
Paul Bakker78a8c712013-03-06 17:01:52 +01001365#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1366 if( ( buf[0] & 0x80 ) != 0 )
1367 return ssl_parse_client_hello_v2( ssl );
1368#endif
1369
Paul Bakkerec636f32012-09-09 19:17:02 +00001370 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1371
1372 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1373 buf[0] ) );
1374 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1375 ( buf[3] << 8 ) | buf[4] ) );
1376 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
1377 buf[1], buf[2] ) );
1378
1379 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001380 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001381 *
1382 * Record layer:
1383 * 0 . 0 message type
1384 * 1 . 2 protocol version
1385 * 3 . 4 message length
1386 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001387
1388 /* According to RFC 5246 Appendix E.1, the version here is typically
1389 * "{03,00}, the lowest version number supported by the client, [or] the
1390 * value of ClientHello.client_version", so the only meaningful check here
1391 * is the major version shouldn't be less than 3 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001392 if( buf[0] != SSL_MSG_HANDSHAKE ||
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001393 buf[1] < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001394 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001395 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1396 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1397 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001398
Paul Bakkerec636f32012-09-09 19:17:02 +00001399 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +00001400
Paul Bakker4f42c112014-04-17 14:48:23 +02001401 if( n < 45 || n > SSL_MAX_CONTENT_LEN )
Paul Bakkerec636f32012-09-09 19:17:02 +00001402 {
1403 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1404 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1405 }
1406
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001407#if defined(POLARSSL_SSL_RENEGOTIATION)
1408 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1409#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001410 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001411 if( ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
1412 {
1413 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1414 return( ret );
1415 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001416 }
1417
1418 buf = ssl->in_msg;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001419#if defined(POLARSSL_SSL_RENEGOTIATION)
1420 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001421 n = ssl->in_msglen;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001422 else
1423#endif
1424 n = ssl->in_left - 5;
Paul Bakkerec636f32012-09-09 19:17:02 +00001425
Paul Bakker48916f92012-09-16 19:57:18 +00001426 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +00001427
1428 /*
1429 * SSL layer:
1430 * 0 . 0 handshake type
1431 * 1 . 3 handshake length
1432 * 4 . 5 protocol version
1433 * 6 . 9 UNIX time()
1434 * 10 . 37 random bytes
1435 * 38 . 38 session id length
1436 * 39 . 38+x session id
1437 * 39+x . 40+x ciphersuitelist length
Paul Bakker0f651c72014-05-22 15:12:19 +02001438 * 41+x . 40+y ciphersuitelist
1439 * 41+y . 41+y compression alg length
1440 * 42+y . 41+z compression algs
Paul Bakkerec636f32012-09-09 19:17:02 +00001441 * .. . .. extensions
1442 */
1443 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1444
1445 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1446 buf[0] ) );
1447 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1448 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1449 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1450 buf[4], buf[5] ) );
1451
1452 /*
1453 * Check the handshake type and protocol version
1454 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001455 if( buf[0] != SSL_HS_CLIENT_HELLO )
Paul Bakkerec636f32012-09-09 19:17:02 +00001456 {
1457 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1458 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1459 }
1460
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001461 ssl->major_ver = buf[4];
1462 ssl->minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001463
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001464 ssl->handshake->max_major_ver = ssl->major_ver;
1465 ssl->handshake->max_minor_ver = ssl->minor_ver;
1466
1467 if( ssl->major_ver < ssl->min_major_ver ||
1468 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001469 {
1470 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001471 " [%d:%d] < [%d:%d]",
1472 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001473 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001474
1475 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1476 SSL_ALERT_MSG_PROTOCOL_VERSION );
1477
1478 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1479 }
1480
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001481 if( ssl->major_ver > ssl->max_major_ver )
1482 {
1483 ssl->major_ver = ssl->max_major_ver;
1484 ssl->minor_ver = ssl->max_minor_ver;
1485 }
1486 else if( ssl->minor_ver > ssl->max_minor_ver )
1487 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001488
Paul Bakker48916f92012-09-16 19:57:18 +00001489 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001490
1491 /*
1492 * Check the handshake message length
1493 */
1494 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1495 {
1496 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1497 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1498 }
1499
1500 /*
1501 * Check the session length
1502 */
1503 sess_len = buf[38];
1504
Paul Bakker0f651c72014-05-22 15:12:19 +02001505 if( sess_len > 32 || sess_len > n - 42 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001506 {
1507 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1508 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1509 }
1510
Paul Bakker48916f92012-09-16 19:57:18 +00001511 ssl->session_negotiate->length = sess_len;
1512 memset( ssl->session_negotiate->id, 0,
1513 sizeof( ssl->session_negotiate->id ) );
1514 memcpy( ssl->session_negotiate->id, buf + 39,
1515 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001516
1517 /*
1518 * Check the ciphersuitelist length
1519 */
1520 ciph_len = ( buf[39 + sess_len] << 8 )
1521 | ( buf[40 + sess_len] );
1522
Paul Bakker0f651c72014-05-22 15:12:19 +02001523 if( ciph_len < 2 || ( ciph_len % 2 ) != 0 || ciph_len > n - 42 - sess_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001524 {
1525 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1526 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1527 }
1528
1529 /*
1530 * Check the compression algorithms length
1531 */
1532 comp_len = buf[41 + sess_len + ciph_len];
1533
Paul Bakker0f651c72014-05-22 15:12:19 +02001534 if( comp_len < 1 || comp_len > 16 ||
1535 comp_len > n - 42 - sess_len - ciph_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001536 {
1537 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1538 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1539 }
1540
Paul Bakker48916f92012-09-16 19:57:18 +00001541 /*
1542 * Check the extension length
1543 */
1544 if( n > 42 + sess_len + ciph_len + comp_len )
1545 {
1546 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1547 | ( buf[43 + sess_len + ciph_len + comp_len] );
1548
1549 if( ( ext_len > 0 && ext_len < 4 ) ||
1550 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1551 {
1552 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1553 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1554 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1555 }
1556 }
1557
1558 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001559#if defined(POLARSSL_ZLIB_SUPPORT)
1560 for( i = 0; i < comp_len; ++i )
1561 {
Paul Bakker48916f92012-09-16 19:57:18 +00001562 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001563 {
Paul Bakker48916f92012-09-16 19:57:18 +00001564 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001565 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001566 }
1567 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001568#endif
1569
Paul Bakkerec636f32012-09-09 19:17:02 +00001570 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1571 buf + 6, 32 );
1572 SSL_DEBUG_BUF( 3, "client hello, session id",
1573 buf + 38, sess_len );
1574 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1575 buf + 41 + sess_len, ciph_len );
1576 SSL_DEBUG_BUF( 3, "client hello, compression",
1577 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001578
Paul Bakkerec636f32012-09-09 19:17:02 +00001579 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001580 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1581 */
1582 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1583 {
1584 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1585 {
1586 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001587#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001588 if( ssl->renegotiation == SSL_RENEGOTIATION )
1589 {
1590 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001591
1592 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1593 return( ret );
1594
Paul Bakker48916f92012-09-16 19:57:18 +00001595 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1596 }
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001597 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001598#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00001599 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1600 break;
1601 }
1602 }
1603
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001604#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1605 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1606 {
1607 if( p[0] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1608 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1609 {
1610 SSL_DEBUG_MSG( 0, ( "received FALLBACK_SCSV" ) );
1611
1612 if( ssl->minor_ver < ssl->max_minor_ver )
1613 {
1614 SSL_DEBUG_MSG( 0, ( "inapropriate fallback" ) );
1615
1616 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1617 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1618
1619 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1620 }
1621
1622 break;
1623 }
1624 }
1625#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1626
Janos Follath307e1812016-05-23 18:52:14 +01001627 /* Do not parse the extensions if the protocol is SSLv3 */
1628#if defined(POLARSSL_SSL_PROTO_SSL3)
1629 if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
Paul Bakker48916f92012-09-16 19:57:18 +00001630 {
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001631#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001632
Janos Follath307e1812016-05-23 18:52:14 +01001633 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001634
Janos Follath307e1812016-05-23 18:52:14 +01001635 while( ext_len )
Paul Bakker48916f92012-09-16 19:57:18 +00001636 {
Janos Follath307e1812016-05-23 18:52:14 +01001637 unsigned int ext_id = ( ( ext[0] << 8 )
1638 | ( ext[1] ) );
1639 unsigned int ext_size = ( ( ext[2] << 8 )
1640 | ( ext[3] ) );
1641
1642 if( ext_size + 4 > ext_len )
1643 {
1644 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1645 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1646 }
1647 switch( ext_id )
1648 {
1649 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
1650 case TLS_EXT_SERVERNAME:
1651 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1652 if( ssl->f_sni == NULL )
1653 break;
1654
1655 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1656 if( ret != 0 )
1657 return( ret );
1658 break;
1659 #endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
1660
1661 case TLS_EXT_RENEGOTIATION_INFO:
1662 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1663 #if defined(POLARSSL_SSL_RENEGOTIATION)
1664 renegotiation_info_seen = 1;
1665 #endif
1666
1667 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1668 if( ret != 0 )
1669 return( ret );
1670 break;
1671
1672 #if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1673 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
1674 case TLS_EXT_SIG_ALG:
1675 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1676 #if defined(POLARSSL_SSL_RENEGOTIATION)
1677 if( ssl->renegotiation == SSL_RENEGOTIATION )
1678 break;
1679 #endif
1680
1681 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1682 if( ret != 0 )
1683 return( ret );
Hanno Beckerc2b9d982017-05-10 16:37:21 +01001684
1685 sig_hash_alg_ext_present = 1;
Janos Follath307e1812016-05-23 18:52:14 +01001686 break;
1687 #endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
1688 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
1689
1690 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
1691 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1692 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1693
1694 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1695 if( ret != 0 )
1696 return( ret );
1697 break;
1698
1699 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1700 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1701 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
1702
1703 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1704 if( ret != 0 )
1705 return( ret );
1706 break;
1707 #endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
1708
1709 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
1710 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1711 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1712
1713 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1714 if( ret != 0 )
1715 return( ret );
1716 break;
1717 #endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
1718
1719 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1720 case TLS_EXT_TRUNCATED_HMAC:
1721 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1722
1723 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1724 if( ret != 0 )
1725 return( ret );
1726 break;
1727 #endif /* POLARSSL_SSL_TRUNCATED_HMAC */
1728
1729 #if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1730 case TLS_EXT_ENCRYPT_THEN_MAC:
1731 SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
1732
1733 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1734 if( ret != 0 )
1735 return( ret );
1736 break;
1737 #endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1738
1739 #if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1740 case TLS_EXT_EXTENDED_MASTER_SECRET:
1741 SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
1742
1743 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1744 if( ret != 0 )
1745 return( ret );
1746 break;
1747 #endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1748
1749 #if defined(POLARSSL_SSL_SESSION_TICKETS)
1750 case TLS_EXT_SESSION_TICKET:
1751 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1752
1753 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1754 if( ret != 0 )
1755 return( ret );
1756 break;
1757 #endif /* POLARSSL_SSL_SESSION_TICKETS */
1758
1759 #if defined(POLARSSL_SSL_ALPN)
1760 case TLS_EXT_ALPN:
1761 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1762
1763 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1764 if( ret != 0 )
1765 return( ret );
1766 break;
1767 #endif /* POLARSSL_SSL_SESSION_TICKETS */
1768
1769 default:
1770 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1771 ext_id ) );
1772 }
1773
1774 ext_len -= 4 + ext_size;
1775 ext += 4 + ext_size;
1776
1777 if( ext_len > 0 && ext_len < 4 )
1778 {
1779 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1780 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1781 }
Paul Bakker48916f92012-09-16 19:57:18 +00001782 }
Janos Follath307e1812016-05-23 18:52:14 +01001783
1784#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker48916f92012-09-16 19:57:18 +00001785 }
Janos Follath307e1812016-05-23 18:52:14 +01001786#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001787
1788 /*
1789 * Renegotiation security checks
1790 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001791 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001792 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1793 {
1794 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1795 handshake_failure = 1;
1796 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001797#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001798 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1799 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1800 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001801 {
1802 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001803 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001804 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001805 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1806 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1807 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001808 {
1809 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001810 handshake_failure = 1;
1811 }
1812 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1813 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1814 renegotiation_info_seen == 1 )
1815 {
1816 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1817 handshake_failure = 1;
1818 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001819#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001820
1821 if( handshake_failure == 1 )
1822 {
1823 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1824 return( ret );
1825
Paul Bakker48916f92012-09-16 19:57:18 +00001826 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1827 }
Paul Bakker380da532012-04-18 16:10:25 +00001828
Hanno Beckerc2b9d982017-05-10 16:37:21 +01001829
1830#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1831 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
1832
1833 /*
1834 * Try to fall back to default hash SHA1 if the client
1835 * hasn't provided any preferred signature-hash combinations.
1836 */
1837 if( sig_hash_alg_ext_present == 0 )
1838 {
1839 md_type_t md_default = POLARSSL_MD_SHA1;
1840
1841 if( ssl_check_sig_hash( md_default ) != 0 )
1842 md_default = POLARSSL_MD_NONE;
1843
1844 ssl_sig_hash_set_const_hash( &ssl->handshake->hash_algs, md_default );
1845 }
1846
1847#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
1848 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
1849
Paul Bakker41c83d32013-03-20 14:39:14 +01001850 /*
1851 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001852 * (At the end because we need information from the EC-based extensions
1853 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001854 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001855 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001856 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001857 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001858#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1859 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
1860 {
1861 for( i = 0; ciphersuites[i] != 0; i++ )
1862#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001863 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001864 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001865 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001866#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001867 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001868 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1869 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1870 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001871
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001872 got_common_suite = 1;
1873
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001874 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1875 &ciphersuite_info ) ) != 0 )
1876 return( ret );
1877
1878 if( ciphersuite_info != NULL )
1879 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001880 }
1881 }
1882
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001883 if( got_common_suite )
1884 {
1885 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1886 "but none of them usable" ) );
1887 ssl_send_fatal_handshake_failure( ssl );
1888 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
1889 }
1890 else
1891 {
1892 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1893 ssl_send_fatal_handshake_failure( ssl );
1894 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1895 }
Paul Bakker41c83d32013-03-20 14:39:14 +01001896
1897have_ciphersuite:
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001898 SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
1899
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001900 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001901 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1902 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1903
Paul Bakker5121ce52009-01-03 21:22:43 +00001904 ssl->in_left = 0;
1905 ssl->state++;
1906
Hanno Beckerc2b9d982017-05-10 16:37:21 +01001907 /* Debugging-only output for testsuite */
1908#if defined(POLARSSL_DEBUG_C) && \
1909 defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1910 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
1911 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
1912 {
1913 pk_type_t sig_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
1914 if( sig_alg != POLARSSL_PK_NONE )
1915 {
1916 md_type_t md_alg = ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
1917 sig_alg );
1918 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
1919 ssl_hash_from_md_alg( md_alg ) ) );
1920 }
1921 else
1922 {
1923 SSL_DEBUG_MSG( 3, ( "no hash algorithm for signature algorithm %d - should not happen",
1924 sig_alg ) );
1925 }
1926 }
1927#endif
1928
Paul Bakker5121ce52009-01-03 21:22:43 +00001929 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1930
1931 return( 0 );
1932}
1933
Paul Bakker1f2bc622013-08-15 13:45:55 +02001934#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001935static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1936 unsigned char *buf,
1937 size_t *olen )
1938{
1939 unsigned char *p = buf;
1940
1941 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1942 {
1943 *olen = 0;
1944 return;
1945 }
1946
1947 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1948
1949 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1950 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1951
1952 *p++ = 0x00;
1953 *p++ = 0x00;
1954
1955 *olen = 4;
1956}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001957#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001958
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001959#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1960static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
1961 unsigned char *buf,
1962 size_t *olen )
1963{
1964 unsigned char *p = buf;
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01001965 const ssl_ciphersuite_t *suite = NULL;
1966 const cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001967
1968 if( ssl->session_negotiate->encrypt_then_mac == SSL_EXTENDED_MS_DISABLED ||
1969 ssl->minor_ver == SSL_MINOR_VERSION_0 )
1970 {
1971 *olen = 0;
1972 return;
1973 }
1974
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01001975 /*
1976 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
1977 * from a client and then selects a stream or Authenticated Encryption
1978 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
1979 * encrypt-then-MAC response extension back to the client."
1980 */
1981 if( ( suite = ssl_ciphersuite_from_id(
1982 ssl->session_negotiate->ciphersuite ) ) == NULL ||
1983 ( cipher = cipher_info_from_type( suite->cipher ) ) == NULL ||
1984 cipher->mode != POLARSSL_MODE_CBC )
1985 {
1986 *olen = 0;
1987 return;
1988 }
1989
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001990 SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
1991
1992 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
1993 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
1994
1995 *p++ = 0x00;
1996 *p++ = 0x00;
1997
1998 *olen = 4;
1999}
2000#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
2001
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002002#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2003static void ssl_write_extended_ms_ext( ssl_context *ssl,
2004 unsigned char *buf,
2005 size_t *olen )
2006{
2007 unsigned char *p = buf;
2008
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +02002009 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_DISABLED ||
2010 ssl->minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002011 {
2012 *olen = 0;
2013 return;
2014 }
2015
2016 SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
2017 "extension" ) );
2018
2019 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
2020 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
2021
2022 *p++ = 0x00;
2023 *p++ = 0x00;
2024
2025 *olen = 4;
2026}
2027#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
2028
Paul Bakkera503a632013-08-14 13:48:06 +02002029#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002030static void ssl_write_session_ticket_ext( ssl_context *ssl,
2031 unsigned char *buf,
2032 size_t *olen )
2033{
2034 unsigned char *p = buf;
2035
2036 if( ssl->handshake->new_session_ticket == 0 )
2037 {
2038 *olen = 0;
2039 return;
2040 }
2041
2042 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
2043
2044 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
2045 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
2046
2047 *p++ = 0x00;
2048 *p++ = 0x00;
2049
2050 *olen = 4;
2051}
Paul Bakkera503a632013-08-14 13:48:06 +02002052#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002053
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002054static void ssl_write_renegotiation_ext( ssl_context *ssl,
2055 unsigned char *buf,
2056 size_t *olen )
2057{
2058 unsigned char *p = buf;
2059
2060 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
2061 {
2062 *olen = 0;
2063 return;
2064 }
2065
2066 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
2067
2068 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
2069 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
2070
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002071#if defined(POLARSSL_SSL_RENEGOTIATION)
2072 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
2073 {
2074 *p++ = 0x00;
2075 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2076 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002077
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002078 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2079 p += ssl->verify_data_len;
2080 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2081 p += ssl->verify_data_len;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002082
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002083 *olen = 5 + ssl->verify_data_len * 2;
2084 }
2085 else
2086#endif /* POLARSSL_SSL_RENEGOTIATION */
2087 {
2088 *p++ = 0x00;
2089 *p++ = 0x01;
2090 *p++ = 0x00;
2091
2092 *olen = 5;
2093 }
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002094}
2095
Paul Bakker05decb22013-08-15 13:33:48 +02002096#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002097static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
2098 unsigned char *buf,
2099 size_t *olen )
2100{
2101 unsigned char *p = buf;
2102
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002103 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
2104 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002105 *olen = 0;
2106 return;
2107 }
2108
2109 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
2110
2111 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
2112 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
2113
2114 *p++ = 0x00;
2115 *p++ = 1;
2116
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002117 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002118
2119 *olen = 5;
2120}
Paul Bakker05decb22013-08-15 13:33:48 +02002121#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002122
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002123#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002124static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
2125 unsigned char *buf,
2126 size_t *olen )
2127{
2128 unsigned char *p = buf;
2129 ((void) ssl);
2130
Paul Bakker677377f2013-10-28 12:54:26 +01002131 if( ( ssl->handshake->cli_exts &
2132 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
2133 {
2134 *olen = 0;
2135 return;
2136 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002137
2138 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
2139
2140 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2141 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
2142
2143 *p++ = 0x00;
2144 *p++ = 2;
2145
2146 *p++ = 1;
2147 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
2148
2149 *olen = 6;
2150}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002151#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002152
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002153#if defined(POLARSSL_SSL_ALPN )
2154static void ssl_write_alpn_ext( ssl_context *ssl,
2155 unsigned char *buf, size_t *olen )
2156{
2157 if( ssl->alpn_chosen == NULL )
2158 {
2159 *olen = 0;
2160 return;
2161 }
2162
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02002163 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002164
2165 /*
2166 * 0 . 1 ext identifier
2167 * 2 . 3 ext length
2168 * 4 . 5 protocol list length
2169 * 6 . 6 protocol name length
2170 * 7 . 7+n protocol name
2171 */
2172 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
2173 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
2174
2175 *olen = 7 + strlen( ssl->alpn_chosen );
2176
2177 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2178 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2179
2180 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2181 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2182
2183 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2184
2185 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2186}
2187#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
2188
Paul Bakker5121ce52009-01-03 21:22:43 +00002189static int ssl_write_server_hello( ssl_context *ssl )
2190{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002191#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002192 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002193#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002194 int ret;
2195 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002196 unsigned char *buf, *p;
2197
2198 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
2199
Paul Bakkera9a028e2013-11-21 17:31:06 +01002200 if( ssl->f_rng == NULL )
2201 {
2202 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2203 return( POLARSSL_ERR_SSL_NO_RNG );
2204 }
2205
Paul Bakker5121ce52009-01-03 21:22:43 +00002206 /*
2207 * 0 . 0 handshake type
2208 * 1 . 3 handshake length
2209 * 4 . 5 protocol version
2210 * 6 . 9 UNIX time()
2211 * 10 . 37 random bytes
2212 */
2213 buf = ssl->out_msg;
2214 p = buf + 4;
2215
2216 *p++ = (unsigned char) ssl->major_ver;
2217 *p++ = (unsigned char) ssl->minor_ver;
2218
2219 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
2220 buf[4], buf[5] ) );
2221
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002222#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002223 t = time( NULL );
2224 *p++ = (unsigned char)( t >> 24 );
2225 *p++ = (unsigned char)( t >> 16 );
2226 *p++ = (unsigned char)( t >> 8 );
2227 *p++ = (unsigned char)( t );
2228
2229 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002230#else
2231 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
2232 return( ret );
2233
2234 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02002235#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002236
Paul Bakkera3d195c2011-11-27 21:07:34 +00002237 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
2238 return( ret );
2239
2240 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002241
Paul Bakker48916f92012-09-16 19:57:18 +00002242 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002243
2244 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
2245
2246 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002247 * Resume is 0 by default, see ssl_handshake_init().
2248 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2249 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002250 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002251 if( ssl->handshake->resume == 0 &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002252#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002253 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002254#endif
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002255 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002256 ssl->f_get_cache != NULL &&
2257 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
2258 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01002259 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002260 ssl->handshake->resume = 1;
2261 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002262
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002263 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002264 {
2265 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002266 * New session, create a new session id,
2267 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002268 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002269 ssl->state++;
2270
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002271#if defined(POLARSSL_HAVE_TIME)
2272 ssl->session_negotiate->start = time( NULL );
2273#endif
2274
Paul Bakkera503a632013-08-14 13:48:06 +02002275#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002276 if( ssl->handshake->new_session_ticket != 0 )
2277 {
2278 ssl->session_negotiate->length = n = 0;
2279 memset( ssl->session_negotiate->id, 0, 32 );
2280 }
2281 else
2282#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002283 {
2284 ssl->session_negotiate->length = n = 32;
2285 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002286 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002287 return( ret );
2288 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002289 }
2290 else
2291 {
2292 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002293 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002294 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02002295 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002296 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002297
2298 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2299 {
2300 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2301 return( ret );
2302 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002303 }
2304
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002305 /*
2306 * 38 . 38 session id length
2307 * 39 . 38+n session id
2308 * 39+n . 40+n chosen ciphersuite
2309 * 41+n . 41+n chosen compression alg.
2310 * 42+n . 43+n extensions length
2311 * 44+n . 43+n+m extensions
2312 */
2313 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00002314 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
2315 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002316
2317 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2318 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2319 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002320 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002321
Paul Bakker48916f92012-09-16 19:57:18 +00002322 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2323 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2324 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002325
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02002326 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2327 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02002328 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002329 ssl->session_negotiate->compression ) );
2330
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002331 /*
2332 * First write extensions, then the total length
2333 */
2334 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2335 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002336
Paul Bakker05decb22013-08-15 13:33:48 +02002337#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002338 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2339 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002340#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002341
Paul Bakker1f2bc622013-08-15 13:45:55 +02002342#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002343 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2344 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002345#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002346
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002347#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2348 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2349 ext_len += olen;
2350#endif
2351
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002352#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2353 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2354 ext_len += olen;
2355#endif
2356
Paul Bakkera503a632013-08-14 13:48:06 +02002357#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002358 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2359 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002360#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002361
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002362#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002363 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2364 ext_len += olen;
2365#endif
2366
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002367#if defined(POLARSSL_SSL_ALPN)
2368 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2369 ext_len += olen;
2370#endif
2371
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002372 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002373
Paul Bakkera7036632014-04-30 10:15:38 +02002374 if( ext_len > 0 )
2375 {
2376 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2377 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2378 p += ext_len;
2379 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002380
2381 ssl->out_msglen = p - buf;
2382 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2383 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
2384
2385 ret = ssl_write_record( ssl );
2386
2387 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2388
2389 return( ret );
2390}
2391
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002392#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2393 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Simon Butcher7458bc32016-09-05 11:18:39 +03002394 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002395 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
Simon Butcher7458bc32016-09-05 11:18:39 +03002396 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)&& \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002397 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002398static int ssl_write_certificate_request( ssl_context *ssl )
2399{
Paul Bakkered27a042013-04-18 22:46:23 +02002400 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002401
2402 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2403
2404 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002405 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002406 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2407 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002408 {
2409 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2410 ssl->state++;
2411 return( 0 );
2412 }
2413
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002414 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2415 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002416}
2417#else
2418static int ssl_write_certificate_request( ssl_context *ssl )
2419{
2420 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2421 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002422 size_t dn_size, total_dn_size; /* excluding length bytes */
2423 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002425 const x509_crt *crt;
Manuel Pégourié-Gonnardde9c8a52015-10-02 11:16:47 +02002426 const unsigned char * const end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00002427
2428 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2429
2430 ssl->state++;
2431
Paul Bakkerfbb17802013-04-17 19:10:21 +02002432 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002433 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002434 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002435 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002436 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002437 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002438 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002439 return( 0 );
2440 }
2441
2442 /*
2443 * 0 . 0 handshake type
2444 * 1 . 3 handshake length
2445 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002446 * 5 .. m-1 cert types
2447 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002448 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002449 * n .. n+1 length of all DNs
2450 * n+2 .. n+3 length of DN 1
2451 * n+4 .. ... Distinguished Name #1
2452 * ... .. ... length of DN 2, etc.
2453 */
2454 buf = ssl->out_msg;
2455 p = buf + 4;
2456
2457 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002458 * Supported certificate types
2459 *
2460 * ClientCertificateType certificate_types<1..2^8-1>;
2461 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002462 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002463 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002464
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002465#if defined(POLARSSL_RSA_C)
2466 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2467#endif
2468#if defined(POLARSSL_ECDSA_C)
2469 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2470#endif
2471
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002472 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002473 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002474
Paul Bakker577e0062013-08-28 11:57:20 +02002475 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002476#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002477 /*
2478 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002479 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002480 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2481 *
2482 * struct {
2483 * HashAlgorithm hash;
2484 * SignatureAlgorithm signature;
2485 * } SignatureAndHashAlgorithm;
2486 *
2487 * enum { (255) } HashAlgorithm;
2488 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002489 */
Paul Bakker21dca692013-01-03 11:41:08 +01002490 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002491 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002492 /*
2493 * Only use current running hash algorithm that is already required
2494 * for requested ciphersuite.
2495 */
Paul Bakker926af752012-11-23 13:38:07 +01002496 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2497
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002498 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2499 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002500 {
2501 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2502 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002503
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002504 /*
2505 * Supported signature algorithms
2506 */
2507#if defined(POLARSSL_RSA_C)
2508 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2509 p[2 + sa_len++] = SSL_SIG_RSA;
2510#endif
2511#if defined(POLARSSL_ECDSA_C)
2512 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2513 p[2 + sa_len++] = SSL_SIG_ECDSA;
2514#endif
Paul Bakker926af752012-11-23 13:38:07 +01002515
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002516 p[0] = (unsigned char)( sa_len >> 8 );
2517 p[1] = (unsigned char)( sa_len );
2518 sa_len += 2;
2519 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002520 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002521#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002522
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002523 /*
2524 * DistinguishedName certificate_authorities<0..2^16-1>;
2525 * opaque DistinguishedName<1..2^16-1>;
2526 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002527 p += 2;
2528 crt = ssl->ca_chain;
2529
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002530 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002531 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002532 {
Paul Bakker926af752012-11-23 13:38:07 +01002533 dn_size = crt->subject_raw.len;
Manuel Pégourié-Gonnardde9c8a52015-10-02 11:16:47 +02002534
Manuel Pégourié-Gonnardcf16b792015-12-10 14:36:25 +01002535 if( end < p ||
2536 (size_t)( end - p ) < dn_size ||
2537 (size_t)( end - p ) < 2 + dn_size )
Manuel Pégourié-Gonnardde9c8a52015-10-02 11:16:47 +02002538 {
2539 SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) );
2540 break;
2541 }
2542
Paul Bakker926af752012-11-23 13:38:07 +01002543 *p++ = (unsigned char)( dn_size >> 8 );
2544 *p++ = (unsigned char)( dn_size );
2545 memcpy( p, crt->subject_raw.p, dn_size );
2546 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002547
Paul Bakker926af752012-11-23 13:38:07 +01002548 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2549
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002550 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002551 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002552 }
2553
Paul Bakker926af752012-11-23 13:38:07 +01002554 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002555 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2556 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002557 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2558 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002559
2560 ret = ssl_write_record( ssl );
2561
2562 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2563
2564 return( ret );
2565}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002566#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2567 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Simon Butcher7458bc32016-09-05 11:18:39 +03002568 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002569 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
Simon Butcher7458bc32016-09-05 11:18:39 +03002570 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002571 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002572
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002573#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2574 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2575static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2576{
2577 int ret;
2578
2579 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2580 {
2581 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2582 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2583 }
2584
2585 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2586 pk_ec( *ssl_own_key( ssl ) ),
2587 POLARSSL_ECDH_OURS ) ) != 0 )
2588 {
2589 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2590 return( ret );
2591 }
2592
2593 return( 0 );
2594}
2595#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2596 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2597
Paul Bakker41c83d32013-03-20 14:39:14 +01002598static int ssl_write_server_key_exchange( ssl_context *ssl )
2599{
Paul Bakker23986e52011-04-24 08:57:21 +00002600 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002601 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002602 const ssl_ciphersuite_t *ciphersuite_info =
2603 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002604
2605#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2606 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2607 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002608 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002609 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002610 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002611 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002612 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002613 ((void) dig_signed);
2614 ((void) dig_signed_len);
2615#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002616
Paul Bakker5121ce52009-01-03 21:22:43 +00002617 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2618
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002619#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2620 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2621 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002622 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2623 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2624 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002625 {
2626 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2627 ssl->state++;
2628 return( 0 );
2629 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002630#endif
2631
2632#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2633 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2634 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2635 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2636 {
2637 ssl_get_ecdh_params_from_cert( ssl );
2638
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002639 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002640 ssl->state++;
2641 return( 0 );
2642 }
2643#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002644
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002645#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2646 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2647 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2648 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002649 {
2650 /* TODO: Support identity hints */
2651 *(p++) = 0x00;
2652 *(p++) = 0x00;
2653
2654 n += 2;
2655 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002656#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2657 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002658
2659#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2660 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2661 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2662 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002663 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002664 /*
2665 * Ephemeral DH parameters:
2666 *
2667 * struct {
2668 * opaque dh_p<1..2^16-1>;
2669 * opaque dh_g<1..2^16-1>;
2670 * opaque dh_Ys<1..2^16-1>;
2671 * } ServerDHParams;
2672 */
2673 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2674 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2675 {
2676 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2677 return( ret );
2678 }
Paul Bakker48916f92012-09-16 19:57:18 +00002679
Paul Bakker41c83d32013-03-20 14:39:14 +01002680 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002681 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2682 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002683 {
2684 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2685 return( ret );
2686 }
2687
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002688 dig_signed = p;
2689 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002690
2691 p += len;
2692 n += len;
2693
Paul Bakker41c83d32013-03-20 14:39:14 +01002694 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2695 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2696 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2697 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2698 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002699#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2700 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002701
Gergely Budai987bfb52014-01-19 21:48:42 +01002702#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002703 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002704 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2705 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002706 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002707 /*
2708 * Ephemeral ECDH parameters:
2709 *
2710 * struct {
2711 * ECParameters curve_params;
2712 * ECPoint public;
2713 * } ServerECDHParams;
2714 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002715 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002716#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002717 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002718
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002719 /* Match our preference list against the offered curves */
2720 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2721 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2722 if( (*curve)->grp_id == *gid )
2723 goto curve_matching_done;
2724
2725curve_matching_done:
2726#else
2727 curve = ssl->handshake->curves;
2728#endif
2729
Manuel Pégourié-Gonnard8e8ae3d2015-06-23 18:57:28 +02002730 if( curve == NULL || *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002731 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002732 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2733 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002734 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002735
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002736 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002737
Paul Bakker41c83d32013-03-20 14:39:14 +01002738 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002739 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002740 {
2741 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2742 return( ret );
2743 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002744
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002745 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2746 p, SSL_MAX_CONTENT_LEN - n,
2747 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002748 {
2749 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2750 return( ret );
2751 }
2752
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002753 dig_signed = p;
2754 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002755
2756 p += len;
2757 n += len;
2758
Paul Bakker41c83d32013-03-20 14:39:14 +01002759 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2760 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002761#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002762
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002763#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002764 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2765 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002766 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002767 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2768 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002769 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002770 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002771 unsigned int hashlen = 0;
2772 unsigned char hash[64];
Paul Bakker23f36802012-09-28 14:15:14 +00002773
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002774 /*
Hanno Beckerc2b9d982017-05-10 16:37:21 +01002775 * Choose hash algorithm:
2776 * - For TLS 1.2, obey signature-hash-algorithm extension to choose appropriate hash.
2777 * - For SSL3, TLS1.0, TLS1.1 and ECDHE_ECDSA, use SHA1 (RFC 4492, Sec. 5.4)
2778 * - Otherwise, use MD5 + SHA1 (RFC 4346, Sec. 7.4.3)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002779 */
Hanno Beckerc2b9d982017-05-10 16:37:21 +01002780
2781 md_type_t md_alg;
2782
Paul Bakker577e0062013-08-28 11:57:20 +02002783#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Hanno Beckerc2b9d982017-05-10 16:37:21 +01002784 pk_type_t sig_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002785 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2786 {
Hanno Beckerc2b9d982017-05-10 16:37:21 +01002787 /* For TLS 1.2, obey signature-hash-algorithm extension
2788 * (RFC 5246, Sec. 7.4.1.4.1). */
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002789
Hanno Beckerc2b9d982017-05-10 16:37:21 +01002790 if( sig_alg == POLARSSL_PK_NONE ||
2791 ( 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 +02002792 {
2793 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002794 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002795 }
2796 }
Paul Bakker577e0062013-08-28 11:57:20 +02002797 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002798#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002799#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2800 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002801 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002802 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2803 {
2804 md_alg = POLARSSL_MD_SHA1;
2805 }
2806 else
Paul Bakker577e0062013-08-28 11:57:20 +02002807#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002808 {
2809 md_alg = POLARSSL_MD_NONE;
2810 }
2811
Hanno Beckerc2b9d982017-05-10 16:37:21 +01002812 SSL_DEBUG_MSG( 3, ( "pick hash algorithm %d for signing", md_alg ) );
2813
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002814 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002815 * Compute the hash to be signed
2816 */
Paul Bakker577e0062013-08-28 11:57:20 +02002817#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2818 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002819 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002820 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002821 md5_context md5;
2822 sha1_context sha1;
2823
Paul Bakker5b4af392014-06-26 12:09:34 +02002824 md5_init( &md5 );
2825 sha1_init( &sha1 );
2826
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002827 /*
2828 * digitally-signed struct {
2829 * opaque md5_hash[16];
2830 * opaque sha_hash[20];
2831 * };
2832 *
2833 * md5_hash
2834 * MD5(ClientHello.random + ServerHello.random
2835 * + ServerParams);
2836 * sha_hash
2837 * SHA(ClientHello.random + ServerHello.random
2838 * + ServerParams);
2839 */
2840 md5_starts( &md5 );
2841 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002842 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002843 md5_finish( &md5, hash );
2844
2845 sha1_starts( &sha1 );
2846 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002847 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002848 sha1_finish( &sha1, hash + 16 );
2849
2850 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02002851
2852 md5_free( &md5 );
2853 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002854 }
2855 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002856#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2857 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002858#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2859 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002860 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002861 {
2862 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02002863 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002864
Paul Bakker84bbeb52014-07-01 14:53:22 +02002865 md_init( &ctx );
2866
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002867 /* Info from md_alg will be used instead */
2868 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002869
2870 /*
2871 * digitally-signed struct {
2872 * opaque client_random[32];
2873 * opaque server_random[32];
2874 * ServerDHParams params;
2875 * };
2876 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002877 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002878 {
2879 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2880 return( ret );
2881 }
2882
2883 md_starts( &ctx );
2884 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002885 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002886 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002887 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00002888 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002889 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002890#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2891 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002892 {
2893 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002894 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002895 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002896
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002897 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2898 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002899
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002900 /*
2901 * Make the signature
2902 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002903 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002904 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002905 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2906 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002907 }
Paul Bakker23f36802012-09-28 14:15:14 +00002908
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002909#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002910 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2911 {
Hanno Beckerc2b9d982017-05-10 16:37:21 +01002912 *(p++) = ssl_hash_from_md_alg( md_alg );
2913 *(p++) = ssl_sig_from_pk_alg( sig_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002914
2915 n += 2;
2916 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002917#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002918
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002919 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002920 p + 2 , &signature_len,
2921 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002922 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002923 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002924 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002925 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002926
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002927 *(p++) = (unsigned char)( signature_len >> 8 );
2928 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002929 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002930
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002931 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002932
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002933 p += signature_len;
2934 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002935 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002936#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002937 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2938 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002939
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002940 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002941 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2942 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2943
2944 ssl->state++;
2945
2946 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2947 {
2948 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2949 return( ret );
2950 }
2951
2952 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2953
2954 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002955}
2956
2957static int ssl_write_server_hello_done( ssl_context *ssl )
2958{
2959 int ret;
2960
2961 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2962
2963 ssl->out_msglen = 4;
2964 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2965 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2966
2967 ssl->state++;
2968
2969 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2970 {
2971 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2972 return( ret );
2973 }
2974
2975 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2976
2977 return( 0 );
2978}
2979
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002980#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2981 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2982static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2983 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002984{
2985 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002986 size_t n;
2987
2988 /*
2989 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2990 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002991 if( *p + 2 > end )
2992 {
2993 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2994 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2995 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002996
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002997 n = ( (*p)[0] << 8 ) | (*p)[1];
2998 *p += 2;
2999
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003000 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003001 {
3002 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3003 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3004 }
3005
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003006 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003007 {
3008 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
3009 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3010 }
3011
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003012 *p += n;
3013
Paul Bakker70df2fb2013-04-17 17:19:09 +02003014 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
3015
Paul Bakker70df2fb2013-04-17 17:19:09 +02003016 return( ret );
3017}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003018#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3019 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003020
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003021#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
3022 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003023static int ssl_parse_encrypted_pms( ssl_context *ssl,
3024 const unsigned char *p,
3025 const unsigned char *end,
3026 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003027{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003028 int ret;
3029 size_t len = pk_get_len( ssl_own_key( ssl ) );
3030 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003031 unsigned char fake_pms[48], peer_pms[48];
3032 unsigned char mask;
Manuel Pégourié-Gonnardb26b75e2015-06-23 18:52:09 +02003033 size_t i, peer_pmslen;
3034 unsigned int diff;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003035
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003036 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003037 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02003038 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003039 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3040 }
3041
3042 /*
3043 * Decrypt the premaster using own private RSA key
3044 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003045#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3046 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02003047 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
3048 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003049 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
3050 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003051 {
3052 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3053 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3054 }
3055 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003056#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003057
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003058 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003059 {
3060 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3061 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3062 }
3063
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003064 /*
3065 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
3066 * must not cause the connection to end immediately; instead, send a
3067 * bad_record_mac later in the handshake.
3068 * Also, avoid data-dependant branches here to protect against
3069 * timing-based variants.
3070 */
3071 ret = ssl->f_rng( ssl->p_rng, fake_pms, sizeof( fake_pms ) );
3072 if( ret != 0 )
3073 return( ret );
3074
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003075 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02003076 peer_pms, &peer_pmslen,
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003077 sizeof( peer_pms ),
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003078 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003079
Manuel Pégourié-Gonnardb26b75e2015-06-23 18:52:09 +02003080 diff = (unsigned int) ret;
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02003081 diff |= peer_pmslen ^ 48;
3082 diff |= peer_pms[0] ^ ssl->handshake->max_major_ver;
3083 diff |= peer_pms[1] ^ ssl->handshake->max_minor_ver;
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003084
3085#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02003086 if( diff != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003087 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003088#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003089
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003090 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
3091 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
3092 {
3093 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3094 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003095 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003096 ssl->handshake->pmslen = 48;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003097
Manuel Pégourié-Gonnardb26b75e2015-06-23 18:52:09 +02003098 /* mask = diff ? 0xff : 0x00 */
Simon Ba697bf52016-11-10 13:19:42 +00003099 /* MSVC has a warning about unary minus on unsigned, but this is
3100 * well-defined and precisely what we want to do here */
3101#if defined(_MSC_VER)
3102#pragma warning( push )
3103#pragma warning( disable : 4146 )
3104#endif
Manuel Pégourié-Gonnardb26b75e2015-06-23 18:52:09 +02003105 mask = - ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 );
Simon Ba697bf52016-11-10 13:19:42 +00003106#if defined(_MSC_VER)
3107#pragma warning( pop )
3108#endif
3109
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003110 for( i = 0; i < ssl->handshake->pmslen; i++ )
3111 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
3112
3113 return( 0 );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003114}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003115#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
3116 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003117
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003118#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003119static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
3120 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003121{
Paul Bakker6db455e2013-09-18 17:29:31 +02003122 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003123 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003124
Paul Bakker6db455e2013-09-18 17:29:31 +02003125 if( ssl->f_psk == NULL &&
3126 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
3127 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003128 {
3129 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
3130 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3131 }
3132
3133 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003134 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003135 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003136 if( *p + 2 > end )
3137 {
3138 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3139 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3140 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003141
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003142 n = ( (*p)[0] << 8 ) | (*p)[1];
3143 *p += 2;
3144
3145 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003146 {
3147 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3148 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3149 }
3150
Paul Bakker6db455e2013-09-18 17:29:31 +02003151 if( ssl->f_psk != NULL )
3152 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003153 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003154 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3155 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003156 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003157 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003158 /* Identity is not a big secret since clients send it in the clear,
3159 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02003160 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003161 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003162 {
3163 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3164 }
3165 }
3166
3167 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003168 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003169 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02003170 if( ( ret = ssl_send_alert_message( ssl,
3171 SSL_ALERT_LEVEL_FATAL,
3172 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
3173 {
3174 return( ret );
3175 }
3176
3177 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003178 }
3179
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003180 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003181
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003182 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003183}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003184#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003185
Paul Bakker5121ce52009-01-03 21:22:43 +00003186static int ssl_parse_client_key_exchange( ssl_context *ssl )
3187{
Paul Bakker23986e52011-04-24 08:57:21 +00003188 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003189 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003190
Paul Bakker41c83d32013-03-20 14:39:14 +01003191 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003192
3193 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
3194
3195 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3196 {
3197 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3198 return( ret );
3199 }
3200
3201 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3202 {
3203 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003204 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003205 }
3206
3207 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
3208 {
3209 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003210 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003211 }
3212
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003213#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01003214 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003215 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003216 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003217 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003218
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003219 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003220 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02003221 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3222 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003223 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003224
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003225 if( p != end )
3226 {
3227 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3228 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3229 }
3230
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02003231 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003232
3233 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
3234 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003235 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02003236 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003237 {
3238 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
3239 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3240 }
3241
3242 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003243 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003244 else
3245#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003246#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003247 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3248 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3249 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003250 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003251 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
3252 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
3253 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003254 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003255 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003256 ssl->in_msg + 4, ssl->in_hslen - 4 ) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003257 {
3258 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3259 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3260 }
3261
3262 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3263
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003264 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
3265 &ssl->handshake->pmslen,
3266 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02003267 POLARSSL_MPI_MAX_SIZE,
3268 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003269 {
3270 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
3271 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3272 }
3273
3274 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003275 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003276 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003277#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003278 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3279 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3280 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003281#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
3282 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003283 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003284 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003285 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003286
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003287 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003288 {
3289 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3290 return( ret );
3291 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003292
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003293 if( p != end )
3294 {
3295 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3296 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3297 }
3298
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003299 if( ( ret = ssl_psk_derive_premaster( ssl,
3300 ciphersuite_info->key_exchange ) ) != 0 )
3301 {
3302 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3303 return( ret );
3304 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003305 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003306 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003307#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003308#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
3309 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
3310 {
3311 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003312 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003313
3314 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3315 {
3316 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3317 return( ret );
3318 }
3319
3320 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3321 {
3322 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
3323 return( ret );
3324 }
3325
3326 if( ( ret = ssl_psk_derive_premaster( ssl,
3327 ciphersuite_info->key_exchange ) ) != 0 )
3328 {
3329 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3330 return( ret );
3331 }
3332 }
3333 else
3334#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003335#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3336 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3337 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003338 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003339 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003340
3341 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3342 {
3343 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3344 return( ret );
3345 }
3346 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3347 {
3348 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3349 return( ret );
3350 }
3351
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003352 if( p != end )
3353 {
3354 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3355 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3356 }
3357
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003358 if( ( ret = ssl_psk_derive_premaster( ssl,
3359 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003360 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003361 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3362 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003363 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003364 }
3365 else
3366#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003367#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3368 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
3369 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003370 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003371 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003372
3373 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3374 {
3375 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3376 return( ret );
3377 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003378
3379 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
3380 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003381 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003382 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3383 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003384 }
3385
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003386 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3387
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003388 if( ( ret = ssl_psk_derive_premaster( ssl,
3389 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003390 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003391 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003392 return( ret );
3393 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003394 }
3395 else
3396#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003397#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
3398 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003399 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003400 if( ( ret = ssl_parse_encrypted_pms( ssl,
3401 ssl->in_msg + 4,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003402 ssl->in_msg + ssl->in_hslen,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003403 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003404 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003405 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003406 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003407 }
3408 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003409 else
3410#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
3411 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003412 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003413 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003414 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003415
Paul Bakkerff60ee62010-03-16 21:09:09 +00003416 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
3417 {
3418 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
3419 return( ret );
3420 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003421
Paul Bakker5121ce52009-01-03 21:22:43 +00003422 ssl->state++;
3423
3424 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3425
3426 return( 0 );
3427}
3428
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003429#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3430 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Simon Butcher7458bc32016-09-05 11:18:39 +03003431 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003432 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
Simon Butcher7458bc32016-09-05 11:18:39 +03003433 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)&& \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003434 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003435static int ssl_parse_certificate_verify( ssl_context *ssl )
3436{
Paul Bakkerfbb17802013-04-17 19:10:21 +02003437 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003438
3439 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3440
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003441 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003442 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003443 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003444 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02003445 {
3446 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3447 ssl->state++;
3448 return( 0 );
3449 }
3450
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003451 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3452 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003453}
3454#else
3455static int ssl_parse_certificate_verify( ssl_context *ssl )
3456{
3457 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003458 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003459 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003460 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003461 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003462#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003463 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003464#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003465 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003466 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3467
3468 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3469
3470 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003471 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003472 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003473 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3474 {
3475 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3476 ssl->state++;
3477 return( 0 );
3478 }
3479
Paul Bakkered27a042013-04-18 22:46:23 +02003480 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003481 {
3482 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3483 ssl->state++;
3484 return( 0 );
3485 }
3486
Paul Bakker48916f92012-09-16 19:57:18 +00003487 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003488
3489 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3490 {
3491 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3492 return( ret );
3493 }
3494
3495 ssl->state++;
3496
3497 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3498 {
3499 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003500 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003501 }
3502
3503 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
3504 {
3505 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003506 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003507 }
3508
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003509 /*
3510 * 0 . 0 handshake type
3511 * 1 . 3 handshake length
3512 * 4 . 5 sig alg (TLS 1.2 only)
3513 * 4+n . 5+n signature length (n = sa_len)
3514 * 6+n . 6+n+m signature (m = sig_len)
3515 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003516
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003517#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3518 defined(POLARSSL_SSL_PROTO_TLS1_1)
3519 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003520 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003521 sa_len = 0;
3522
Paul Bakkerc70b9822013-04-07 22:00:46 +02003523 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003524 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003525
3526 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3527 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3528 POLARSSL_PK_ECDSA ) )
3529 {
3530 hash_start += 16;
3531 hashlen -= 16;
3532 md_alg = POLARSSL_MD_SHA1;
3533 }
Paul Bakker926af752012-11-23 13:38:07 +01003534 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003535 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003536#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3537 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003538#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3539 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003540 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003541 sa_len = 2;
3542
Paul Bakker5121ce52009-01-03 21:22:43 +00003543 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003544 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003545 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003546 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003547 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003548 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3549 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003550 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3551 }
3552
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003553 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003554
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003555 /* Info from md_alg will be used instead */
3556 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003557
3558 /*
3559 * Signature
3560 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003561 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
3562 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003563 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003564 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3565 " for verify message" ) );
3566 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003567 }
3568
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003569 /*
3570 * Check the certificate's key type matches the signature alg
3571 */
3572 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3573 {
3574 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3575 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3576 }
Paul Bakker577e0062013-08-28 11:57:20 +02003577 }
3578 else
3579#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3580 {
3581 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003582 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003583 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003584
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003585 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01003586
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003587 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003588 {
3589 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003590 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003591 }
3592
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003593 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003594 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003595 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003596 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003597 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003598 return( ret );
3599 }
3600
3601 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3602
Paul Bakkered27a042013-04-18 22:46:23 +02003603 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003604}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003605#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3606 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Simon Butcher7458bc32016-09-05 11:18:39 +03003607 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED &&
3608 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
3609 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED &&
3610 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003611
Paul Bakkera503a632013-08-14 13:48:06 +02003612#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003613static int ssl_write_new_session_ticket( ssl_context *ssl )
3614{
3615 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003616 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003617 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003618
3619 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3620
3621 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3622 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3623
3624 /*
3625 * struct {
3626 * uint32 ticket_lifetime_hint;
3627 * opaque ticket<0..2^16-1>;
3628 * } NewSessionTicket;
3629 *
3630 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3631 * 8 . 9 ticket_len (n)
3632 * 10 . 9+n ticket content
3633 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003634
3635 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3636 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3637 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3638 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003639
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003640 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3641 {
3642 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3643 tlen = 0;
3644 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003645
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003646 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3647 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003648
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003649 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003650
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003651 /*
3652 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3653 * ChangeCipherSpec share the same state.
3654 */
3655 ssl->handshake->new_session_ticket = 0;
3656
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003657 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3658 {
3659 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3660 return( ret );
3661 }
3662
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003663 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3664
3665 return( 0 );
3666}
Paul Bakkera503a632013-08-14 13:48:06 +02003667#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003668
Paul Bakker5121ce52009-01-03 21:22:43 +00003669/*
Paul Bakker1961b702013-01-25 14:49:24 +01003670 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003671 */
Paul Bakker1961b702013-01-25 14:49:24 +01003672int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003673{
3674 int ret = 0;
3675
Paul Bakker1961b702013-01-25 14:49:24 +01003676 if( ssl->state == SSL_HANDSHAKE_OVER )
3677 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003678
Paul Bakker1961b702013-01-25 14:49:24 +01003679 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3680
3681 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3682 return( ret );
3683
3684 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003685 {
Paul Bakker1961b702013-01-25 14:49:24 +01003686 case SSL_HELLO_REQUEST:
3687 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003688 break;
3689
Paul Bakker1961b702013-01-25 14:49:24 +01003690 /*
3691 * <== ClientHello
3692 */
3693 case SSL_CLIENT_HELLO:
3694 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003695 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003696
3697 /*
3698 * ==> ServerHello
3699 * Certificate
3700 * ( ServerKeyExchange )
3701 * ( CertificateRequest )
3702 * ServerHelloDone
3703 */
3704 case SSL_SERVER_HELLO:
3705 ret = ssl_write_server_hello( ssl );
3706 break;
3707
3708 case SSL_SERVER_CERTIFICATE:
3709 ret = ssl_write_certificate( ssl );
3710 break;
3711
3712 case SSL_SERVER_KEY_EXCHANGE:
3713 ret = ssl_write_server_key_exchange( ssl );
3714 break;
3715
3716 case SSL_CERTIFICATE_REQUEST:
3717 ret = ssl_write_certificate_request( ssl );
3718 break;
3719
3720 case SSL_SERVER_HELLO_DONE:
3721 ret = ssl_write_server_hello_done( ssl );
3722 break;
3723
3724 /*
3725 * <== ( Certificate/Alert )
3726 * ClientKeyExchange
3727 * ( CertificateVerify )
3728 * ChangeCipherSpec
3729 * Finished
3730 */
3731 case SSL_CLIENT_CERTIFICATE:
3732 ret = ssl_parse_certificate( ssl );
3733 break;
3734
3735 case SSL_CLIENT_KEY_EXCHANGE:
3736 ret = ssl_parse_client_key_exchange( ssl );
3737 break;
3738
3739 case SSL_CERTIFICATE_VERIFY:
3740 ret = ssl_parse_certificate_verify( ssl );
3741 break;
3742
3743 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3744 ret = ssl_parse_change_cipher_spec( ssl );
3745 break;
3746
3747 case SSL_CLIENT_FINISHED:
3748 ret = ssl_parse_finished( ssl );
3749 break;
3750
3751 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003752 * ==> ( NewSessionTicket )
3753 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003754 * Finished
3755 */
3756 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003757#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003758 if( ssl->handshake->new_session_ticket != 0 )
3759 ret = ssl_write_new_session_ticket( ssl );
3760 else
Paul Bakkera503a632013-08-14 13:48:06 +02003761#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003762 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003763 break;
3764
3765 case SSL_SERVER_FINISHED:
3766 ret = ssl_write_finished( ssl );
3767 break;
3768
3769 case SSL_FLUSH_BUFFERS:
3770 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3771 ssl->state = SSL_HANDSHAKE_WRAPUP;
3772 break;
3773
3774 case SSL_HANDSHAKE_WRAPUP:
3775 ssl_handshake_wrapup( ssl );
3776 break;
3777
3778 default:
3779 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3780 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003781 }
3782
Paul Bakker5121ce52009-01-03 21:22:43 +00003783 return( ret );
3784}
Paul Bakker9af723c2014-05-01 13:03:14 +02003785#endif /* POLARSSL_SSL_SRV_C */