blob: 0f0369aa4020107747ef0a3d667684a917cecc4f [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
Hanno Becker57457782017-06-09 15:30:29 +01001635 SSL_DEBUG_BUF( 3, "client hello extensions", ext, ext_len );
1636
Janos Follath307e1812016-05-23 18:52:14 +01001637 while( ext_len )
Paul Bakker48916f92012-09-16 19:57:18 +00001638 {
Janos Follath307e1812016-05-23 18:52:14 +01001639 unsigned int ext_id = ( ( ext[0] << 8 )
1640 | ( ext[1] ) );
1641 unsigned int ext_size = ( ( ext[2] << 8 )
1642 | ( ext[3] ) );
1643
1644 if( ext_size + 4 > ext_len )
1645 {
1646 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1647 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1648 }
1649 switch( ext_id )
1650 {
1651 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
1652 case TLS_EXT_SERVERNAME:
1653 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1654 if( ssl->f_sni == NULL )
1655 break;
1656
1657 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1658 if( ret != 0 )
1659 return( ret );
1660 break;
1661 #endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
1662
1663 case TLS_EXT_RENEGOTIATION_INFO:
1664 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1665 #if defined(POLARSSL_SSL_RENEGOTIATION)
1666 renegotiation_info_seen = 1;
1667 #endif
1668
1669 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1670 if( ret != 0 )
1671 return( ret );
1672 break;
1673
1674 #if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1675 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
1676 case TLS_EXT_SIG_ALG:
1677 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
Janos Follath307e1812016-05-23 18:52:14 +01001678
1679 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1680 if( ret != 0 )
1681 return( ret );
Hanno Beckerc2b9d982017-05-10 16:37:21 +01001682
1683 sig_hash_alg_ext_present = 1;
Janos Follath307e1812016-05-23 18:52:14 +01001684 break;
1685 #endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
1686 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
1687
1688 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
1689 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1690 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1691
1692 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1693 if( ret != 0 )
1694 return( ret );
1695 break;
1696
1697 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1698 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1699 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
1700
1701 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1702 if( ret != 0 )
1703 return( ret );
1704 break;
1705 #endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
1706
1707 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
1708 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1709 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1710
1711 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1712 if( ret != 0 )
1713 return( ret );
1714 break;
1715 #endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
1716
1717 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1718 case TLS_EXT_TRUNCATED_HMAC:
1719 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1720
1721 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1722 if( ret != 0 )
1723 return( ret );
1724 break;
1725 #endif /* POLARSSL_SSL_TRUNCATED_HMAC */
1726
1727 #if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1728 case TLS_EXT_ENCRYPT_THEN_MAC:
1729 SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
1730
1731 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1732 if( ret != 0 )
1733 return( ret );
1734 break;
1735 #endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1736
1737 #if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1738 case TLS_EXT_EXTENDED_MASTER_SECRET:
1739 SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
1740
1741 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1742 if( ret != 0 )
1743 return( ret );
1744 break;
1745 #endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1746
1747 #if defined(POLARSSL_SSL_SESSION_TICKETS)
1748 case TLS_EXT_SESSION_TICKET:
1749 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1750
1751 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1752 if( ret != 0 )
1753 return( ret );
1754 break;
1755 #endif /* POLARSSL_SSL_SESSION_TICKETS */
1756
1757 #if defined(POLARSSL_SSL_ALPN)
1758 case TLS_EXT_ALPN:
1759 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1760
1761 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1762 if( ret != 0 )
1763 return( ret );
1764 break;
1765 #endif /* POLARSSL_SSL_SESSION_TICKETS */
1766
1767 default:
1768 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1769 ext_id ) );
1770 }
1771
1772 ext_len -= 4 + ext_size;
1773 ext += 4 + ext_size;
1774
1775 if( ext_len > 0 && ext_len < 4 )
1776 {
1777 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1778 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1779 }
Paul Bakker48916f92012-09-16 19:57:18 +00001780 }
Janos Follath307e1812016-05-23 18:52:14 +01001781
1782#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker48916f92012-09-16 19:57:18 +00001783 }
Janos Follath307e1812016-05-23 18:52:14 +01001784#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001785
1786 /*
1787 * Renegotiation security checks
1788 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001789 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001790 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1791 {
1792 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1793 handshake_failure = 1;
1794 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001795#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001796 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1797 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1798 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001799 {
1800 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001801 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001802 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001803 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1804 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1805 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001806 {
1807 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001808 handshake_failure = 1;
1809 }
1810 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1811 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1812 renegotiation_info_seen == 1 )
1813 {
1814 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1815 handshake_failure = 1;
1816 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001817#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001818
1819 if( handshake_failure == 1 )
1820 {
1821 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1822 return( ret );
1823
Paul Bakker48916f92012-09-16 19:57:18 +00001824 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1825 }
Paul Bakker380da532012-04-18 16:10:25 +00001826
Hanno Beckerc2b9d982017-05-10 16:37:21 +01001827
1828#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1829 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
1830
1831 /*
1832 * Try to fall back to default hash SHA1 if the client
1833 * hasn't provided any preferred signature-hash combinations.
1834 */
1835 if( sig_hash_alg_ext_present == 0 )
1836 {
1837 md_type_t md_default = POLARSSL_MD_SHA1;
1838
1839 if( ssl_check_sig_hash( md_default ) != 0 )
1840 md_default = POLARSSL_MD_NONE;
1841
1842 ssl_sig_hash_set_const_hash( &ssl->handshake->hash_algs, md_default );
1843 }
1844
1845#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
1846 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
1847
Paul Bakker41c83d32013-03-20 14:39:14 +01001848 /*
1849 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001850 * (At the end because we need information from the EC-based extensions
1851 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001852 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001853 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001854 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001855 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001856#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1857 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
1858 {
1859 for( i = 0; ciphersuites[i] != 0; i++ )
1860#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001861 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001862 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001863 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001864#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001865 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001866 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1867 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1868 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001869
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001870 got_common_suite = 1;
1871
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001872 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1873 &ciphersuite_info ) ) != 0 )
1874 return( ret );
1875
1876 if( ciphersuite_info != NULL )
1877 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001878 }
1879 }
1880
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001881 if( got_common_suite )
1882 {
1883 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1884 "but none of them usable" ) );
1885 ssl_send_fatal_handshake_failure( ssl );
1886 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
1887 }
1888 else
1889 {
1890 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1891 ssl_send_fatal_handshake_failure( ssl );
1892 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1893 }
Paul Bakker41c83d32013-03-20 14:39:14 +01001894
1895have_ciphersuite:
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001896 SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
1897
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001898 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001899 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1900 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1901
Paul Bakker5121ce52009-01-03 21:22:43 +00001902 ssl->in_left = 0;
1903 ssl->state++;
1904
Hanno Beckerc2b9d982017-05-10 16:37:21 +01001905 /* Debugging-only output for testsuite */
1906#if defined(POLARSSL_DEBUG_C) && \
1907 defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1908 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
1909 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
1910 {
1911 pk_type_t sig_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
1912 if( sig_alg != POLARSSL_PK_NONE )
1913 {
1914 md_type_t md_alg = ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
1915 sig_alg );
1916 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
1917 ssl_hash_from_md_alg( md_alg ) ) );
1918 }
1919 else
1920 {
1921 SSL_DEBUG_MSG( 3, ( "no hash algorithm for signature algorithm %d - should not happen",
1922 sig_alg ) );
1923 }
1924 }
1925#endif
1926
Paul Bakker5121ce52009-01-03 21:22:43 +00001927 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1928
1929 return( 0 );
1930}
1931
Paul Bakker1f2bc622013-08-15 13:45:55 +02001932#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001933static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1934 unsigned char *buf,
1935 size_t *olen )
1936{
1937 unsigned char *p = buf;
1938
1939 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1940 {
1941 *olen = 0;
1942 return;
1943 }
1944
1945 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1946
1947 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1948 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1949
1950 *p++ = 0x00;
1951 *p++ = 0x00;
1952
1953 *olen = 4;
1954}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001955#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001956
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001957#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1958static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
1959 unsigned char *buf,
1960 size_t *olen )
1961{
1962 unsigned char *p = buf;
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01001963 const ssl_ciphersuite_t *suite = NULL;
1964 const cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001965
1966 if( ssl->session_negotiate->encrypt_then_mac == SSL_EXTENDED_MS_DISABLED ||
1967 ssl->minor_ver == SSL_MINOR_VERSION_0 )
1968 {
1969 *olen = 0;
1970 return;
1971 }
1972
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01001973 /*
1974 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
1975 * from a client and then selects a stream or Authenticated Encryption
1976 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
1977 * encrypt-then-MAC response extension back to the client."
1978 */
1979 if( ( suite = ssl_ciphersuite_from_id(
1980 ssl->session_negotiate->ciphersuite ) ) == NULL ||
1981 ( cipher = cipher_info_from_type( suite->cipher ) ) == NULL ||
1982 cipher->mode != POLARSSL_MODE_CBC )
1983 {
1984 *olen = 0;
1985 return;
1986 }
1987
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001988 SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
1989
1990 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
1991 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
1992
1993 *p++ = 0x00;
1994 *p++ = 0x00;
1995
1996 *olen = 4;
1997}
1998#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1999
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002000#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2001static void ssl_write_extended_ms_ext( ssl_context *ssl,
2002 unsigned char *buf,
2003 size_t *olen )
2004{
2005 unsigned char *p = buf;
2006
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +02002007 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_DISABLED ||
2008 ssl->minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002009 {
2010 *olen = 0;
2011 return;
2012 }
2013
2014 SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
2015 "extension" ) );
2016
2017 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
2018 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
2019
2020 *p++ = 0x00;
2021 *p++ = 0x00;
2022
2023 *olen = 4;
2024}
2025#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
2026
Paul Bakkera503a632013-08-14 13:48:06 +02002027#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002028static void ssl_write_session_ticket_ext( ssl_context *ssl,
2029 unsigned char *buf,
2030 size_t *olen )
2031{
2032 unsigned char *p = buf;
2033
2034 if( ssl->handshake->new_session_ticket == 0 )
2035 {
2036 *olen = 0;
2037 return;
2038 }
2039
2040 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
2041
2042 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
2043 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
2044
2045 *p++ = 0x00;
2046 *p++ = 0x00;
2047
2048 *olen = 4;
2049}
Paul Bakkera503a632013-08-14 13:48:06 +02002050#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002051
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002052static void ssl_write_renegotiation_ext( ssl_context *ssl,
2053 unsigned char *buf,
2054 size_t *olen )
2055{
2056 unsigned char *p = buf;
2057
2058 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
2059 {
2060 *olen = 0;
2061 return;
2062 }
2063
2064 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
2065
2066 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
2067 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
2068
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002069#if defined(POLARSSL_SSL_RENEGOTIATION)
2070 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
2071 {
2072 *p++ = 0x00;
2073 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2074 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002075
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002076 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2077 p += ssl->verify_data_len;
2078 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2079 p += ssl->verify_data_len;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002080
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002081 *olen = 5 + ssl->verify_data_len * 2;
2082 }
2083 else
2084#endif /* POLARSSL_SSL_RENEGOTIATION */
2085 {
2086 *p++ = 0x00;
2087 *p++ = 0x01;
2088 *p++ = 0x00;
2089
2090 *olen = 5;
2091 }
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002092}
2093
Paul Bakker05decb22013-08-15 13:33:48 +02002094#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002095static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
2096 unsigned char *buf,
2097 size_t *olen )
2098{
2099 unsigned char *p = buf;
2100
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002101 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
2102 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002103 *olen = 0;
2104 return;
2105 }
2106
2107 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
2108
2109 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
2110 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
2111
2112 *p++ = 0x00;
2113 *p++ = 1;
2114
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002115 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002116
2117 *olen = 5;
2118}
Paul Bakker05decb22013-08-15 13:33:48 +02002119#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002120
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002121#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002122static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
2123 unsigned char *buf,
2124 size_t *olen )
2125{
2126 unsigned char *p = buf;
2127 ((void) ssl);
2128
Paul Bakker677377f2013-10-28 12:54:26 +01002129 if( ( ssl->handshake->cli_exts &
2130 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
2131 {
2132 *olen = 0;
2133 return;
2134 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002135
2136 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
2137
2138 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2139 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
2140
2141 *p++ = 0x00;
2142 *p++ = 2;
2143
2144 *p++ = 1;
2145 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
2146
2147 *olen = 6;
2148}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002149#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002150
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002151#if defined(POLARSSL_SSL_ALPN )
2152static void ssl_write_alpn_ext( ssl_context *ssl,
2153 unsigned char *buf, size_t *olen )
2154{
2155 if( ssl->alpn_chosen == NULL )
2156 {
2157 *olen = 0;
2158 return;
2159 }
2160
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02002161 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002162
2163 /*
2164 * 0 . 1 ext identifier
2165 * 2 . 3 ext length
2166 * 4 . 5 protocol list length
2167 * 6 . 6 protocol name length
2168 * 7 . 7+n protocol name
2169 */
2170 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
2171 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
2172
2173 *olen = 7 + strlen( ssl->alpn_chosen );
2174
2175 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2176 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2177
2178 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2179 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2180
2181 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2182
2183 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2184}
2185#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
2186
Paul Bakker5121ce52009-01-03 21:22:43 +00002187static int ssl_write_server_hello( ssl_context *ssl )
2188{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002189#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002190 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002191#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002192 int ret;
2193 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002194 unsigned char *buf, *p;
2195
2196 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
2197
Paul Bakkera9a028e2013-11-21 17:31:06 +01002198 if( ssl->f_rng == NULL )
2199 {
2200 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2201 return( POLARSSL_ERR_SSL_NO_RNG );
2202 }
2203
Paul Bakker5121ce52009-01-03 21:22:43 +00002204 /*
2205 * 0 . 0 handshake type
2206 * 1 . 3 handshake length
2207 * 4 . 5 protocol version
2208 * 6 . 9 UNIX time()
2209 * 10 . 37 random bytes
2210 */
2211 buf = ssl->out_msg;
2212 p = buf + 4;
2213
2214 *p++ = (unsigned char) ssl->major_ver;
2215 *p++ = (unsigned char) ssl->minor_ver;
2216
2217 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
2218 buf[4], buf[5] ) );
2219
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002220#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002221 t = time( NULL );
2222 *p++ = (unsigned char)( t >> 24 );
2223 *p++ = (unsigned char)( t >> 16 );
2224 *p++ = (unsigned char)( t >> 8 );
2225 *p++ = (unsigned char)( t );
2226
2227 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002228#else
2229 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
2230 return( ret );
2231
2232 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02002233#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002234
Paul Bakkera3d195c2011-11-27 21:07:34 +00002235 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
2236 return( ret );
2237
2238 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002239
Paul Bakker48916f92012-09-16 19:57:18 +00002240 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002241
2242 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
2243
2244 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002245 * Resume is 0 by default, see ssl_handshake_init().
2246 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2247 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002248 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002249 if( ssl->handshake->resume == 0 &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002250#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002251 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002252#endif
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002253 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002254 ssl->f_get_cache != NULL &&
2255 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
2256 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01002257 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002258 ssl->handshake->resume = 1;
2259 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002260
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002261 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002262 {
2263 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002264 * New session, create a new session id,
2265 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002266 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002267 ssl->state++;
2268
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002269#if defined(POLARSSL_HAVE_TIME)
2270 ssl->session_negotiate->start = time( NULL );
2271#endif
2272
Paul Bakkera503a632013-08-14 13:48:06 +02002273#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002274 if( ssl->handshake->new_session_ticket != 0 )
2275 {
2276 ssl->session_negotiate->length = n = 0;
2277 memset( ssl->session_negotiate->id, 0, 32 );
2278 }
2279 else
2280#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002281 {
2282 ssl->session_negotiate->length = n = 32;
2283 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002284 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002285 return( ret );
2286 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002287 }
2288 else
2289 {
2290 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002291 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002292 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02002293 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002294 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002295
2296 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2297 {
2298 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2299 return( ret );
2300 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002301 }
2302
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002303 /*
2304 * 38 . 38 session id length
2305 * 39 . 38+n session id
2306 * 39+n . 40+n chosen ciphersuite
2307 * 41+n . 41+n chosen compression alg.
2308 * 42+n . 43+n extensions length
2309 * 44+n . 43+n+m extensions
2310 */
2311 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00002312 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
2313 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002314
2315 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2316 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2317 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002318 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002319
Paul Bakker48916f92012-09-16 19:57:18 +00002320 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2321 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2322 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002323
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02002324 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2325 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02002326 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002327 ssl->session_negotiate->compression ) );
2328
Hanno Becker57457782017-06-09 15:30:29 +01002329 /* Do not write the extensions if the protocol is SSLv3 */
2330#if defined(POLARSSL_SSL_PROTO_SSL3)
2331 if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
2332 {
2333#endif
2334
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002335 /*
2336 * First write extensions, then the total length
2337 */
2338 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2339 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002340
Paul Bakker05decb22013-08-15 13:33:48 +02002341#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002342 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2343 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002344#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002345
Paul Bakker1f2bc622013-08-15 13:45:55 +02002346#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002347 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2348 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002349#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002350
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002351#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2352 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2353 ext_len += olen;
2354#endif
2355
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002356#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2357 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2358 ext_len += olen;
2359#endif
2360
Paul Bakkera503a632013-08-14 13:48:06 +02002361#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002362 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2363 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002364#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002365
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002366#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002367 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2368 ext_len += olen;
2369#endif
2370
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002371#if defined(POLARSSL_SSL_ALPN)
2372 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2373 ext_len += olen;
2374#endif
2375
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002376 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002377
Paul Bakkera7036632014-04-30 10:15:38 +02002378 if( ext_len > 0 )
2379 {
2380 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2381 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2382 p += ext_len;
2383 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002384
Hanno Becker57457782017-06-09 15:30:29 +01002385#if defined(POLARSSL_SSL_PROTO_SSL3)
2386 }
2387#endif
2388
Paul Bakker5121ce52009-01-03 21:22:43 +00002389 ssl->out_msglen = p - buf;
2390 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2391 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
2392
2393 ret = ssl_write_record( ssl );
2394
2395 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2396
2397 return( ret );
2398}
2399
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002400#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2401 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Simon Butcher7458bc32016-09-05 11:18:39 +03002402 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002403 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
Simon Butcher7458bc32016-09-05 11:18:39 +03002404 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)&& \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002405 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002406static int ssl_write_certificate_request( ssl_context *ssl )
2407{
Paul Bakkered27a042013-04-18 22:46:23 +02002408 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002409
2410 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2411
2412 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002413 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002414 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2415 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002416 {
2417 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2418 ssl->state++;
2419 return( 0 );
2420 }
2421
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002422 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2423 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002424}
2425#else
2426static int ssl_write_certificate_request( ssl_context *ssl )
2427{
2428 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2429 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002430 size_t dn_size, total_dn_size; /* excluding length bytes */
2431 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002432 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002433 const x509_crt *crt;
Manuel Pégourié-Gonnardde9c8a52015-10-02 11:16:47 +02002434 const unsigned char * const end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00002435
2436 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2437
2438 ssl->state++;
2439
Paul Bakkerfbb17802013-04-17 19:10:21 +02002440 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002441 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002442 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002443 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002444 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002445 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002446 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002447 return( 0 );
2448 }
2449
2450 /*
2451 * 0 . 0 handshake type
2452 * 1 . 3 handshake length
2453 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002454 * 5 .. m-1 cert types
2455 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002456 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002457 * n .. n+1 length of all DNs
2458 * n+2 .. n+3 length of DN 1
2459 * n+4 .. ... Distinguished Name #1
2460 * ... .. ... length of DN 2, etc.
2461 */
2462 buf = ssl->out_msg;
2463 p = buf + 4;
2464
2465 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002466 * Supported certificate types
2467 *
2468 * ClientCertificateType certificate_types<1..2^8-1>;
2469 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002470 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002471 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002472
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002473#if defined(POLARSSL_RSA_C)
2474 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2475#endif
2476#if defined(POLARSSL_ECDSA_C)
2477 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2478#endif
2479
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002480 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002481 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002482
Paul Bakker577e0062013-08-28 11:57:20 +02002483 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002484#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002485 /*
2486 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002487 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002488 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2489 *
2490 * struct {
2491 * HashAlgorithm hash;
2492 * SignatureAlgorithm signature;
2493 * } SignatureAndHashAlgorithm;
2494 *
2495 * enum { (255) } HashAlgorithm;
2496 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002497 */
Paul Bakker21dca692013-01-03 11:41:08 +01002498 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002499 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002500 /*
2501 * Only use current running hash algorithm that is already required
2502 * for requested ciphersuite.
2503 */
Paul Bakker926af752012-11-23 13:38:07 +01002504 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2505
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002506 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2507 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002508 {
2509 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2510 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002511
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002512 /*
2513 * Supported signature algorithms
2514 */
2515#if defined(POLARSSL_RSA_C)
2516 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2517 p[2 + sa_len++] = SSL_SIG_RSA;
2518#endif
2519#if defined(POLARSSL_ECDSA_C)
2520 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2521 p[2 + sa_len++] = SSL_SIG_ECDSA;
2522#endif
Paul Bakker926af752012-11-23 13:38:07 +01002523
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002524 p[0] = (unsigned char)( sa_len >> 8 );
2525 p[1] = (unsigned char)( sa_len );
2526 sa_len += 2;
2527 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002528 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002529#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002530
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002531 /*
2532 * DistinguishedName certificate_authorities<0..2^16-1>;
2533 * opaque DistinguishedName<1..2^16-1>;
2534 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002535 p += 2;
2536 crt = ssl->ca_chain;
2537
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002538 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002539 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002540 {
Paul Bakker926af752012-11-23 13:38:07 +01002541 dn_size = crt->subject_raw.len;
Manuel Pégourié-Gonnardde9c8a52015-10-02 11:16:47 +02002542
Manuel Pégourié-Gonnardcf16b792015-12-10 14:36:25 +01002543 if( end < p ||
2544 (size_t)( end - p ) < dn_size ||
2545 (size_t)( end - p ) < 2 + dn_size )
Manuel Pégourié-Gonnardde9c8a52015-10-02 11:16:47 +02002546 {
2547 SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) );
2548 break;
2549 }
2550
Paul Bakker926af752012-11-23 13:38:07 +01002551 *p++ = (unsigned char)( dn_size >> 8 );
2552 *p++ = (unsigned char)( dn_size );
2553 memcpy( p, crt->subject_raw.p, dn_size );
2554 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002555
Paul Bakker926af752012-11-23 13:38:07 +01002556 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2557
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002558 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002559 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002560 }
2561
Paul Bakker926af752012-11-23 13:38:07 +01002562 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002563 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2564 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002565 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2566 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002567
2568 ret = ssl_write_record( ssl );
2569
2570 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2571
2572 return( ret );
2573}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002574#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2575 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Simon Butcher7458bc32016-09-05 11:18:39 +03002576 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002577 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
Simon Butcher7458bc32016-09-05 11:18:39 +03002578 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002579 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002580
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002581#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2582 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2583static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2584{
2585 int ret;
2586
2587 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2588 {
2589 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2590 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2591 }
2592
2593 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2594 pk_ec( *ssl_own_key( ssl ) ),
2595 POLARSSL_ECDH_OURS ) ) != 0 )
2596 {
2597 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2598 return( ret );
2599 }
2600
2601 return( 0 );
2602}
2603#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2604 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2605
Paul Bakker41c83d32013-03-20 14:39:14 +01002606static int ssl_write_server_key_exchange( ssl_context *ssl )
2607{
Paul Bakker23986e52011-04-24 08:57:21 +00002608 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002609 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002610 const ssl_ciphersuite_t *ciphersuite_info =
2611 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002612
2613#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2614 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2615 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002616 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002617 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002618 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002619 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002620 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002621 ((void) dig_signed);
2622 ((void) dig_signed_len);
2623#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002624
Paul Bakker5121ce52009-01-03 21:22:43 +00002625 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2626
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002627#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2628 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2629 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002630 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2631 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2632 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002633 {
2634 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2635 ssl->state++;
2636 return( 0 );
2637 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002638#endif
2639
2640#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2641 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2642 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2643 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2644 {
2645 ssl_get_ecdh_params_from_cert( ssl );
2646
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002647 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002648 ssl->state++;
2649 return( 0 );
2650 }
2651#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002652
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002653#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2654 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2655 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2656 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002657 {
2658 /* TODO: Support identity hints */
2659 *(p++) = 0x00;
2660 *(p++) = 0x00;
2661
2662 n += 2;
2663 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002664#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2665 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002666
2667#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2668 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2669 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2670 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002671 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002672 /*
2673 * Ephemeral DH parameters:
2674 *
2675 * struct {
2676 * opaque dh_p<1..2^16-1>;
2677 * opaque dh_g<1..2^16-1>;
2678 * opaque dh_Ys<1..2^16-1>;
2679 * } ServerDHParams;
2680 */
2681 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2682 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2683 {
2684 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2685 return( ret );
2686 }
Paul Bakker48916f92012-09-16 19:57:18 +00002687
Paul Bakker41c83d32013-03-20 14:39:14 +01002688 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002689 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2690 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002691 {
2692 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2693 return( ret );
2694 }
2695
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002696 dig_signed = p;
2697 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002698
2699 p += len;
2700 n += len;
2701
Paul Bakker41c83d32013-03-20 14:39:14 +01002702 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2703 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2704 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2705 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2706 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002707#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2708 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002709
Gergely Budai987bfb52014-01-19 21:48:42 +01002710#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002711 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002712 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2713 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002714 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002715 /*
2716 * Ephemeral ECDH parameters:
2717 *
2718 * struct {
2719 * ECParameters curve_params;
2720 * ECPoint public;
2721 * } ServerECDHParams;
2722 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002723 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002724#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002725 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002726
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002727 /* Match our preference list against the offered curves */
2728 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2729 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2730 if( (*curve)->grp_id == *gid )
2731 goto curve_matching_done;
2732
2733curve_matching_done:
2734#else
2735 curve = ssl->handshake->curves;
2736#endif
2737
Manuel Pégourié-Gonnard8e8ae3d2015-06-23 18:57:28 +02002738 if( curve == NULL || *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002739 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002740 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2741 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002742 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002743
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002744 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002745
Paul Bakker41c83d32013-03-20 14:39:14 +01002746 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002747 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002748 {
2749 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2750 return( ret );
2751 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002752
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002753 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2754 p, SSL_MAX_CONTENT_LEN - n,
2755 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002756 {
2757 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2758 return( ret );
2759 }
2760
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002761 dig_signed = p;
2762 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002763
2764 p += len;
2765 n += len;
2766
Paul Bakker41c83d32013-03-20 14:39:14 +01002767 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2768 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002769#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002770
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002771#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002772 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2773 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002774 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002775 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2776 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002777 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002778 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002779 unsigned int hashlen = 0;
2780 unsigned char hash[64];
Paul Bakker23f36802012-09-28 14:15:14 +00002781
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002782 /*
Hanno Beckerc2b9d982017-05-10 16:37:21 +01002783 * Choose hash algorithm:
2784 * - For TLS 1.2, obey signature-hash-algorithm extension to choose appropriate hash.
2785 * - For SSL3, TLS1.0, TLS1.1 and ECDHE_ECDSA, use SHA1 (RFC 4492, Sec. 5.4)
2786 * - Otherwise, use MD5 + SHA1 (RFC 4346, Sec. 7.4.3)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002787 */
Hanno Beckerc2b9d982017-05-10 16:37:21 +01002788
2789 md_type_t md_alg;
2790
Paul Bakker577e0062013-08-28 11:57:20 +02002791#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Hanno Beckerc2b9d982017-05-10 16:37:21 +01002792 pk_type_t sig_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002793 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2794 {
Hanno Beckerc2b9d982017-05-10 16:37:21 +01002795 /* For TLS 1.2, obey signature-hash-algorithm extension
2796 * (RFC 5246, Sec. 7.4.1.4.1). */
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002797
Hanno Beckerc2b9d982017-05-10 16:37:21 +01002798 if( sig_alg == POLARSSL_PK_NONE ||
2799 ( 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 +02002800 {
2801 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002802 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002803 }
2804 }
Paul Bakker577e0062013-08-28 11:57:20 +02002805 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002806#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002807#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2808 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002809 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002810 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2811 {
2812 md_alg = POLARSSL_MD_SHA1;
2813 }
2814 else
Paul Bakker577e0062013-08-28 11:57:20 +02002815#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002816 {
2817 md_alg = POLARSSL_MD_NONE;
2818 }
2819
Hanno Beckerc2b9d982017-05-10 16:37:21 +01002820 SSL_DEBUG_MSG( 3, ( "pick hash algorithm %d for signing", md_alg ) );
2821
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002822 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002823 * Compute the hash to be signed
2824 */
Paul Bakker577e0062013-08-28 11:57:20 +02002825#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2826 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002827 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002828 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002829 md5_context md5;
2830 sha1_context sha1;
2831
Paul Bakker5b4af392014-06-26 12:09:34 +02002832 md5_init( &md5 );
2833 sha1_init( &sha1 );
2834
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002835 /*
2836 * digitally-signed struct {
2837 * opaque md5_hash[16];
2838 * opaque sha_hash[20];
2839 * };
2840 *
2841 * md5_hash
2842 * MD5(ClientHello.random + ServerHello.random
2843 * + ServerParams);
2844 * sha_hash
2845 * SHA(ClientHello.random + ServerHello.random
2846 * + ServerParams);
2847 */
2848 md5_starts( &md5 );
2849 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002850 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002851 md5_finish( &md5, hash );
2852
2853 sha1_starts( &sha1 );
2854 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002855 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002856 sha1_finish( &sha1, hash + 16 );
2857
2858 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02002859
2860 md5_free( &md5 );
2861 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002862 }
2863 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002864#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2865 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002866#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2867 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002868 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002869 {
2870 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02002871 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002872
Paul Bakker84bbeb52014-07-01 14:53:22 +02002873 md_init( &ctx );
2874
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002875 /* Info from md_alg will be used instead */
2876 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002877
2878 /*
2879 * digitally-signed struct {
2880 * opaque client_random[32];
2881 * opaque server_random[32];
2882 * ServerDHParams params;
2883 * };
2884 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002885 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002886 {
2887 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2888 return( ret );
2889 }
2890
2891 md_starts( &ctx );
2892 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002893 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002894 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002895 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00002896 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002897 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002898#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2899 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002900 {
2901 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002902 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002903 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002904
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002905 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2906 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002907
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002908 /*
2909 * Make the signature
2910 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002911 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002912 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002913 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2914 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002915 }
Paul Bakker23f36802012-09-28 14:15:14 +00002916
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002917#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002918 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2919 {
Hanno Beckerc2b9d982017-05-10 16:37:21 +01002920 *(p++) = ssl_hash_from_md_alg( md_alg );
2921 *(p++) = ssl_sig_from_pk_alg( sig_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002922
2923 n += 2;
2924 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002925#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002926
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002927 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002928 p + 2 , &signature_len,
2929 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002930 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002931 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002932 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002933 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002934
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002935 *(p++) = (unsigned char)( signature_len >> 8 );
2936 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002937 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002938
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002939 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002940
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002941 p += signature_len;
2942 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002943 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002944#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002945 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2946 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002947
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002948 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002949 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2950 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2951
2952 ssl->state++;
2953
2954 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2955 {
2956 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2957 return( ret );
2958 }
2959
2960 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2961
2962 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002963}
2964
2965static int ssl_write_server_hello_done( ssl_context *ssl )
2966{
2967 int ret;
2968
2969 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2970
2971 ssl->out_msglen = 4;
2972 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2973 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2974
2975 ssl->state++;
2976
2977 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2978 {
2979 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2980 return( ret );
2981 }
2982
2983 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2984
2985 return( 0 );
2986}
2987
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002988#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2989 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2990static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2991 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002992{
2993 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002994 size_t n;
2995
2996 /*
2997 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2998 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002999 if( *p + 2 > end )
3000 {
3001 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3002 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3003 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003004
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003005 n = ( (*p)[0] << 8 ) | (*p)[1];
3006 *p += 2;
3007
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003008 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003009 {
3010 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3011 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3012 }
3013
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003014 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003015 {
3016 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
3017 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3018 }
3019
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003020 *p += n;
3021
Paul Bakker70df2fb2013-04-17 17:19:09 +02003022 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
3023
Paul Bakker70df2fb2013-04-17 17:19:09 +02003024 return( ret );
3025}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003026#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3027 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003028
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003029#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
3030 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003031static int ssl_parse_encrypted_pms( ssl_context *ssl,
3032 const unsigned char *p,
3033 const unsigned char *end,
3034 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003035{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003036 int ret;
3037 size_t len = pk_get_len( ssl_own_key( ssl ) );
3038 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003039 unsigned char fake_pms[48], peer_pms[48];
3040 unsigned char mask;
Manuel Pégourié-Gonnardb26b75e2015-06-23 18:52:09 +02003041 size_t i, peer_pmslen;
3042 unsigned int diff;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003043
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003044 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003045 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02003046 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003047 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3048 }
3049
3050 /*
3051 * Decrypt the premaster using own private RSA key
3052 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003053#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3054 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02003055 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
3056 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003057 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
3058 *p++ != ( ( len ) & 0xFF ) )
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 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003064#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003065
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003066 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003067 {
3068 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3069 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3070 }
3071
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003072 /*
3073 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
3074 * must not cause the connection to end immediately; instead, send a
3075 * bad_record_mac later in the handshake.
3076 * Also, avoid data-dependant branches here to protect against
3077 * timing-based variants.
3078 */
3079 ret = ssl->f_rng( ssl->p_rng, fake_pms, sizeof( fake_pms ) );
3080 if( ret != 0 )
3081 return( ret );
3082
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003083 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02003084 peer_pms, &peer_pmslen,
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003085 sizeof( peer_pms ),
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003086 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003087
Manuel Pégourié-Gonnardb26b75e2015-06-23 18:52:09 +02003088 diff = (unsigned int) ret;
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02003089 diff |= peer_pmslen ^ 48;
3090 diff |= peer_pms[0] ^ ssl->handshake->max_major_ver;
3091 diff |= peer_pms[1] ^ ssl->handshake->max_minor_ver;
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003092
3093#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02003094 if( diff != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003095 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003096#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003097
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003098 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
3099 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
3100 {
3101 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3102 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003103 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003104 ssl->handshake->pmslen = 48;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003105
Manuel Pégourié-Gonnardb26b75e2015-06-23 18:52:09 +02003106 /* mask = diff ? 0xff : 0x00 */
Simon Ba697bf52016-11-10 13:19:42 +00003107 /* MSVC has a warning about unary minus on unsigned, but this is
3108 * well-defined and precisely what we want to do here */
3109#if defined(_MSC_VER)
3110#pragma warning( push )
3111#pragma warning( disable : 4146 )
3112#endif
Manuel Pégourié-Gonnardb26b75e2015-06-23 18:52:09 +02003113 mask = - ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 );
Simon Ba697bf52016-11-10 13:19:42 +00003114#if defined(_MSC_VER)
3115#pragma warning( pop )
3116#endif
3117
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003118 for( i = 0; i < ssl->handshake->pmslen; i++ )
3119 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
3120
3121 return( 0 );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003122}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003123#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
3124 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003125
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003126#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003127static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
3128 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003129{
Paul Bakker6db455e2013-09-18 17:29:31 +02003130 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003131 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003132
Paul Bakker6db455e2013-09-18 17:29:31 +02003133 if( ssl->f_psk == NULL &&
3134 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
3135 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003136 {
3137 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
3138 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3139 }
3140
3141 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003142 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003143 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003144 if( *p + 2 > end )
3145 {
3146 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3147 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3148 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003149
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003150 n = ( (*p)[0] << 8 ) | (*p)[1];
3151 *p += 2;
3152
3153 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003154 {
3155 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3156 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3157 }
3158
Paul Bakker6db455e2013-09-18 17:29:31 +02003159 if( ssl->f_psk != NULL )
3160 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003161 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003162 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3163 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003164 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003165 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003166 /* Identity is not a big secret since clients send it in the clear,
3167 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02003168 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003169 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003170 {
3171 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3172 }
3173 }
3174
3175 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003176 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003177 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02003178 if( ( ret = ssl_send_alert_message( ssl,
3179 SSL_ALERT_LEVEL_FATAL,
3180 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
3181 {
3182 return( ret );
3183 }
3184
3185 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003186 }
3187
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003188 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003189
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003190 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003191}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003192#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003193
Paul Bakker5121ce52009-01-03 21:22:43 +00003194static int ssl_parse_client_key_exchange( ssl_context *ssl )
3195{
Paul Bakker23986e52011-04-24 08:57:21 +00003196 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003197 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003198
Paul Bakker41c83d32013-03-20 14:39:14 +01003199 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003200
3201 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
3202
3203 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3204 {
3205 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3206 return( ret );
3207 }
3208
3209 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3210 {
3211 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003212 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003213 }
3214
3215 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
3216 {
3217 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003218 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003219 }
3220
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003221#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01003222 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003223 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003224 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003225 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003226
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003227 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003228 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02003229 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3230 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003231 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003232
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003233 if( p != end )
3234 {
3235 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3236 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3237 }
3238
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02003239 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003240
3241 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
3242 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003243 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02003244 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003245 {
3246 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
3247 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3248 }
3249
3250 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003251 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003252 else
3253#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003254#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003255 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3256 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3257 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003258 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003259 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
3260 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
3261 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003262 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003263 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003264 ssl->in_msg + 4, ssl->in_hslen - 4 ) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003265 {
3266 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3267 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3268 }
3269
3270 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3271
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003272 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
3273 &ssl->handshake->pmslen,
3274 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02003275 POLARSSL_MPI_MAX_SIZE,
3276 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003277 {
3278 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
3279 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3280 }
3281
3282 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003283 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003284 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003285#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003286 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3287 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3288 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003289#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
3290 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003291 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003292 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003293 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003294
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003295 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003296 {
3297 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3298 return( ret );
3299 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003300
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003301 if( p != end )
3302 {
3303 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3304 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3305 }
3306
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003307 if( ( ret = ssl_psk_derive_premaster( ssl,
3308 ciphersuite_info->key_exchange ) ) != 0 )
3309 {
3310 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3311 return( ret );
3312 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003313 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003314 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003315#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003316#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
3317 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
3318 {
3319 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003320 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003321
3322 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3323 {
3324 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3325 return( ret );
3326 }
3327
3328 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3329 {
3330 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
3331 return( ret );
3332 }
3333
3334 if( ( ret = ssl_psk_derive_premaster( ssl,
3335 ciphersuite_info->key_exchange ) ) != 0 )
3336 {
3337 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3338 return( ret );
3339 }
3340 }
3341 else
3342#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003343#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3344 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3345 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003346 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003347 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003348
3349 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3350 {
3351 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3352 return( ret );
3353 }
3354 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3355 {
3356 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3357 return( ret );
3358 }
3359
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003360 if( p != end )
3361 {
3362 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3363 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3364 }
3365
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003366 if( ( ret = ssl_psk_derive_premaster( ssl,
3367 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003368 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003369 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3370 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003371 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003372 }
3373 else
3374#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003375#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3376 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
3377 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003378 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003379 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003380
3381 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3382 {
3383 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3384 return( ret );
3385 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003386
3387 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
3388 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003389 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003390 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3391 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003392 }
3393
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003394 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3395
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003396 if( ( ret = ssl_psk_derive_premaster( ssl,
3397 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003398 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003399 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003400 return( ret );
3401 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003402 }
3403 else
3404#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003405#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
3406 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003407 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003408 if( ( ret = ssl_parse_encrypted_pms( ssl,
3409 ssl->in_msg + 4,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003410 ssl->in_msg + ssl->in_hslen,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003411 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003412 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003413 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003414 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003415 }
3416 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003417 else
3418#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
3419 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003420 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003421 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003422 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003423
Paul Bakkerff60ee62010-03-16 21:09:09 +00003424 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
3425 {
3426 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
3427 return( ret );
3428 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003429
Paul Bakker5121ce52009-01-03 21:22:43 +00003430 ssl->state++;
3431
3432 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3433
3434 return( 0 );
3435}
3436
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003437#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3438 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Simon Butcher7458bc32016-09-05 11:18:39 +03003439 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003440 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
Simon Butcher7458bc32016-09-05 11:18:39 +03003441 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)&& \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003442 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003443static int ssl_parse_certificate_verify( ssl_context *ssl )
3444{
Paul Bakkerfbb17802013-04-17 19:10:21 +02003445 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003446
3447 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3448
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003449 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003450 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003451 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003452 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02003453 {
3454 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3455 ssl->state++;
3456 return( 0 );
3457 }
3458
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003459 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3460 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003461}
3462#else
3463static int ssl_parse_certificate_verify( ssl_context *ssl )
3464{
3465 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003466 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003467 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003468 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003469 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003470#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003471 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003472#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003473 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003474 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3475
3476 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3477
3478 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003479 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003480 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003481 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3482 {
3483 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3484 ssl->state++;
3485 return( 0 );
3486 }
3487
Paul Bakkered27a042013-04-18 22:46:23 +02003488 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003489 {
3490 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3491 ssl->state++;
3492 return( 0 );
3493 }
3494
Paul Bakker48916f92012-09-16 19:57:18 +00003495 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003496
3497 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3498 {
3499 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3500 return( ret );
3501 }
3502
3503 ssl->state++;
3504
3505 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3506 {
3507 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003508 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003509 }
3510
3511 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
3512 {
3513 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003514 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003515 }
3516
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003517 /*
3518 * 0 . 0 handshake type
3519 * 1 . 3 handshake length
3520 * 4 . 5 sig alg (TLS 1.2 only)
3521 * 4+n . 5+n signature length (n = sa_len)
3522 * 6+n . 6+n+m signature (m = sig_len)
3523 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003524
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003525#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3526 defined(POLARSSL_SSL_PROTO_TLS1_1)
3527 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003528 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003529 sa_len = 0;
3530
Paul Bakkerc70b9822013-04-07 22:00:46 +02003531 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003532 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003533
3534 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3535 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3536 POLARSSL_PK_ECDSA ) )
3537 {
3538 hash_start += 16;
3539 hashlen -= 16;
3540 md_alg = POLARSSL_MD_SHA1;
3541 }
Paul Bakker926af752012-11-23 13:38:07 +01003542 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003543 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003544#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3545 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003546#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3547 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003548 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003549 sa_len = 2;
3550
Paul Bakker5121ce52009-01-03 21:22:43 +00003551 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003552 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003553 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003554 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003555 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003556 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3557 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003558 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3559 }
3560
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003561 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003562
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003563 /* Info from md_alg will be used instead */
3564 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003565
3566 /*
3567 * Signature
3568 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003569 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
3570 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003571 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003572 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3573 " for verify message" ) );
3574 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003575 }
3576
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003577 /*
3578 * Check the certificate's key type matches the signature alg
3579 */
3580 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3581 {
3582 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3583 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3584 }
Paul Bakker577e0062013-08-28 11:57:20 +02003585 }
3586 else
3587#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3588 {
3589 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003590 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003591 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003592
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003593 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01003594
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003595 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003596 {
3597 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003598 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003599 }
3600
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003601 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003602 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003603 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003604 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003605 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003606 return( ret );
3607 }
3608
3609 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3610
Paul Bakkered27a042013-04-18 22:46:23 +02003611 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003612}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003613#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3614 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Simon Butcher7458bc32016-09-05 11:18:39 +03003615 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED &&
3616 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
3617 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED &&
3618 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003619
Paul Bakkera503a632013-08-14 13:48:06 +02003620#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003621static int ssl_write_new_session_ticket( ssl_context *ssl )
3622{
3623 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003624 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003625 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003626
3627 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3628
3629 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3630 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3631
3632 /*
3633 * struct {
3634 * uint32 ticket_lifetime_hint;
3635 * opaque ticket<0..2^16-1>;
3636 * } NewSessionTicket;
3637 *
3638 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3639 * 8 . 9 ticket_len (n)
3640 * 10 . 9+n ticket content
3641 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003642
3643 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3644 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3645 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3646 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003647
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003648 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3649 {
3650 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3651 tlen = 0;
3652 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003653
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003654 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3655 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003656
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003657 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003658
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003659 /*
3660 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3661 * ChangeCipherSpec share the same state.
3662 */
3663 ssl->handshake->new_session_ticket = 0;
3664
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003665 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3666 {
3667 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3668 return( ret );
3669 }
3670
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003671 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3672
3673 return( 0 );
3674}
Paul Bakkera503a632013-08-14 13:48:06 +02003675#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003676
Paul Bakker5121ce52009-01-03 21:22:43 +00003677/*
Paul Bakker1961b702013-01-25 14:49:24 +01003678 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003679 */
Paul Bakker1961b702013-01-25 14:49:24 +01003680int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003681{
3682 int ret = 0;
3683
Paul Bakker1961b702013-01-25 14:49:24 +01003684 if( ssl->state == SSL_HANDSHAKE_OVER )
3685 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003686
Paul Bakker1961b702013-01-25 14:49:24 +01003687 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3688
3689 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3690 return( ret );
3691
3692 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003693 {
Paul Bakker1961b702013-01-25 14:49:24 +01003694 case SSL_HELLO_REQUEST:
3695 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003696 break;
3697
Paul Bakker1961b702013-01-25 14:49:24 +01003698 /*
3699 * <== ClientHello
3700 */
3701 case SSL_CLIENT_HELLO:
3702 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003703 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003704
3705 /*
3706 * ==> ServerHello
3707 * Certificate
3708 * ( ServerKeyExchange )
3709 * ( CertificateRequest )
3710 * ServerHelloDone
3711 */
3712 case SSL_SERVER_HELLO:
3713 ret = ssl_write_server_hello( ssl );
3714 break;
3715
3716 case SSL_SERVER_CERTIFICATE:
3717 ret = ssl_write_certificate( ssl );
3718 break;
3719
3720 case SSL_SERVER_KEY_EXCHANGE:
3721 ret = ssl_write_server_key_exchange( ssl );
3722 break;
3723
3724 case SSL_CERTIFICATE_REQUEST:
3725 ret = ssl_write_certificate_request( ssl );
3726 break;
3727
3728 case SSL_SERVER_HELLO_DONE:
3729 ret = ssl_write_server_hello_done( ssl );
3730 break;
3731
3732 /*
3733 * <== ( Certificate/Alert )
3734 * ClientKeyExchange
3735 * ( CertificateVerify )
3736 * ChangeCipherSpec
3737 * Finished
3738 */
3739 case SSL_CLIENT_CERTIFICATE:
3740 ret = ssl_parse_certificate( ssl );
3741 break;
3742
3743 case SSL_CLIENT_KEY_EXCHANGE:
3744 ret = ssl_parse_client_key_exchange( ssl );
3745 break;
3746
3747 case SSL_CERTIFICATE_VERIFY:
3748 ret = ssl_parse_certificate_verify( ssl );
3749 break;
3750
3751 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3752 ret = ssl_parse_change_cipher_spec( ssl );
3753 break;
3754
3755 case SSL_CLIENT_FINISHED:
3756 ret = ssl_parse_finished( ssl );
3757 break;
3758
3759 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003760 * ==> ( NewSessionTicket )
3761 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003762 * Finished
3763 */
3764 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003765#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003766 if( ssl->handshake->new_session_ticket != 0 )
3767 ret = ssl_write_new_session_ticket( ssl );
3768 else
Paul Bakkera503a632013-08-14 13:48:06 +02003769#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003770 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003771 break;
3772
3773 case SSL_SERVER_FINISHED:
3774 ret = ssl_write_finished( ssl );
3775 break;
3776
3777 case SSL_FLUSH_BUFFERS:
3778 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3779 ssl->state = SSL_HANDSHAKE_WRAPUP;
3780 break;
3781
3782 case SSL_HANDSHAKE_WRAPUP:
3783 ssl_handshake_wrapup( ssl );
3784 break;
3785
3786 default:
3787 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3788 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003789 }
3790
Paul Bakker5121ce52009-01-03 21:22:43 +00003791 return( ret );
3792}
Paul Bakker9af723c2014-05-01 13:03:14 +02003793#endif /* POLARSSL_SSL_SRV_C */