blob: 57ad2629929482a512d4b0fb1e2aa82bcfc0c1a6 [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" ) );
1678 #if defined(POLARSSL_SSL_RENEGOTIATION)
1679 if( ssl->renegotiation == SSL_RENEGOTIATION )
1680 break;
1681 #endif
1682
1683 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1684 if( ret != 0 )
1685 return( ret );
Hanno Beckerc2b9d982017-05-10 16:37:21 +01001686
1687 sig_hash_alg_ext_present = 1;
Janos Follath307e1812016-05-23 18:52:14 +01001688 break;
1689 #endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
1690 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
1691
1692 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
1693 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1694 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1695
1696 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1697 if( ret != 0 )
1698 return( ret );
1699 break;
1700
1701 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1702 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1703 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
1704
1705 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1706 if( ret != 0 )
1707 return( ret );
1708 break;
1709 #endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
1710
1711 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
1712 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1713 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1714
1715 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1716 if( ret != 0 )
1717 return( ret );
1718 break;
1719 #endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
1720
1721 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1722 case TLS_EXT_TRUNCATED_HMAC:
1723 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1724
1725 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1726 if( ret != 0 )
1727 return( ret );
1728 break;
1729 #endif /* POLARSSL_SSL_TRUNCATED_HMAC */
1730
1731 #if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1732 case TLS_EXT_ENCRYPT_THEN_MAC:
1733 SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
1734
1735 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1736 if( ret != 0 )
1737 return( ret );
1738 break;
1739 #endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1740
1741 #if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1742 case TLS_EXT_EXTENDED_MASTER_SECRET:
1743 SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
1744
1745 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1746 if( ret != 0 )
1747 return( ret );
1748 break;
1749 #endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1750
1751 #if defined(POLARSSL_SSL_SESSION_TICKETS)
1752 case TLS_EXT_SESSION_TICKET:
1753 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1754
1755 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1756 if( ret != 0 )
1757 return( ret );
1758 break;
1759 #endif /* POLARSSL_SSL_SESSION_TICKETS */
1760
1761 #if defined(POLARSSL_SSL_ALPN)
1762 case TLS_EXT_ALPN:
1763 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1764
1765 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1766 if( ret != 0 )
1767 return( ret );
1768 break;
1769 #endif /* POLARSSL_SSL_SESSION_TICKETS */
1770
1771 default:
1772 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1773 ext_id ) );
1774 }
1775
1776 ext_len -= 4 + ext_size;
1777 ext += 4 + ext_size;
1778
1779 if( ext_len > 0 && ext_len < 4 )
1780 {
1781 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1782 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1783 }
Paul Bakker48916f92012-09-16 19:57:18 +00001784 }
Janos Follath307e1812016-05-23 18:52:14 +01001785
1786#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker48916f92012-09-16 19:57:18 +00001787 }
Janos Follath307e1812016-05-23 18:52:14 +01001788#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001789
1790 /*
1791 * Renegotiation security checks
1792 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001793 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001794 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1795 {
1796 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1797 handshake_failure = 1;
1798 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001799#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001800 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1801 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1802 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001803 {
1804 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001805 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001806 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001807 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1808 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1809 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001810 {
1811 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001812 handshake_failure = 1;
1813 }
1814 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1815 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1816 renegotiation_info_seen == 1 )
1817 {
1818 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1819 handshake_failure = 1;
1820 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001821#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001822
1823 if( handshake_failure == 1 )
1824 {
1825 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1826 return( ret );
1827
Paul Bakker48916f92012-09-16 19:57:18 +00001828 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1829 }
Paul Bakker380da532012-04-18 16:10:25 +00001830
Hanno Beckerc2b9d982017-05-10 16:37:21 +01001831
1832#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1833 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
1834
1835 /*
1836 * Try to fall back to default hash SHA1 if the client
1837 * hasn't provided any preferred signature-hash combinations.
1838 */
1839 if( sig_hash_alg_ext_present == 0 )
1840 {
1841 md_type_t md_default = POLARSSL_MD_SHA1;
1842
1843 if( ssl_check_sig_hash( md_default ) != 0 )
1844 md_default = POLARSSL_MD_NONE;
1845
1846 ssl_sig_hash_set_const_hash( &ssl->handshake->hash_algs, md_default );
1847 }
1848
1849#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
1850 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
1851
Paul Bakker41c83d32013-03-20 14:39:14 +01001852 /*
1853 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001854 * (At the end because we need information from the EC-based extensions
1855 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001856 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001857 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001858 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001859 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001860#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1861 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
1862 {
1863 for( i = 0; ciphersuites[i] != 0; i++ )
1864#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001865 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001866 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001867 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001868#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001869 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001870 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1871 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1872 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001873
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001874 got_common_suite = 1;
1875
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001876 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1877 &ciphersuite_info ) ) != 0 )
1878 return( ret );
1879
1880 if( ciphersuite_info != NULL )
1881 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001882 }
1883 }
1884
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001885 if( got_common_suite )
1886 {
1887 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1888 "but none of them usable" ) );
1889 ssl_send_fatal_handshake_failure( ssl );
1890 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
1891 }
1892 else
1893 {
1894 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1895 ssl_send_fatal_handshake_failure( ssl );
1896 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1897 }
Paul Bakker41c83d32013-03-20 14:39:14 +01001898
1899have_ciphersuite:
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001900 SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
1901
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001902 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001903 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1904 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1905
Paul Bakker5121ce52009-01-03 21:22:43 +00001906 ssl->in_left = 0;
1907 ssl->state++;
1908
Hanno Beckerc2b9d982017-05-10 16:37:21 +01001909 /* Debugging-only output for testsuite */
1910#if defined(POLARSSL_DEBUG_C) && \
1911 defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1912 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
1913 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
1914 {
1915 pk_type_t sig_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
1916 if( sig_alg != POLARSSL_PK_NONE )
1917 {
1918 md_type_t md_alg = ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
1919 sig_alg );
1920 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
1921 ssl_hash_from_md_alg( md_alg ) ) );
1922 }
1923 else
1924 {
1925 SSL_DEBUG_MSG( 3, ( "no hash algorithm for signature algorithm %d - should not happen",
1926 sig_alg ) );
1927 }
1928 }
1929#endif
1930
Paul Bakker5121ce52009-01-03 21:22:43 +00001931 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1932
1933 return( 0 );
1934}
1935
Paul Bakker1f2bc622013-08-15 13:45:55 +02001936#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001937static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1938 unsigned char *buf,
1939 size_t *olen )
1940{
1941 unsigned char *p = buf;
1942
1943 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1944 {
1945 *olen = 0;
1946 return;
1947 }
1948
1949 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1950
1951 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1952 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1953
1954 *p++ = 0x00;
1955 *p++ = 0x00;
1956
1957 *olen = 4;
1958}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001959#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001960
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001961#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1962static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
1963 unsigned char *buf,
1964 size_t *olen )
1965{
1966 unsigned char *p = buf;
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01001967 const ssl_ciphersuite_t *suite = NULL;
1968 const cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001969
1970 if( ssl->session_negotiate->encrypt_then_mac == SSL_EXTENDED_MS_DISABLED ||
1971 ssl->minor_ver == SSL_MINOR_VERSION_0 )
1972 {
1973 *olen = 0;
1974 return;
1975 }
1976
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01001977 /*
1978 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
1979 * from a client and then selects a stream or Authenticated Encryption
1980 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
1981 * encrypt-then-MAC response extension back to the client."
1982 */
1983 if( ( suite = ssl_ciphersuite_from_id(
1984 ssl->session_negotiate->ciphersuite ) ) == NULL ||
1985 ( cipher = cipher_info_from_type( suite->cipher ) ) == NULL ||
1986 cipher->mode != POLARSSL_MODE_CBC )
1987 {
1988 *olen = 0;
1989 return;
1990 }
1991
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001992 SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
1993
1994 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
1995 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
1996
1997 *p++ = 0x00;
1998 *p++ = 0x00;
1999
2000 *olen = 4;
2001}
2002#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
2003
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002004#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2005static void ssl_write_extended_ms_ext( ssl_context *ssl,
2006 unsigned char *buf,
2007 size_t *olen )
2008{
2009 unsigned char *p = buf;
2010
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +02002011 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_DISABLED ||
2012 ssl->minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002013 {
2014 *olen = 0;
2015 return;
2016 }
2017
2018 SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
2019 "extension" ) );
2020
2021 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
2022 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
2023
2024 *p++ = 0x00;
2025 *p++ = 0x00;
2026
2027 *olen = 4;
2028}
2029#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
2030
Paul Bakkera503a632013-08-14 13:48:06 +02002031#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002032static void ssl_write_session_ticket_ext( ssl_context *ssl,
2033 unsigned char *buf,
2034 size_t *olen )
2035{
2036 unsigned char *p = buf;
2037
2038 if( ssl->handshake->new_session_ticket == 0 )
2039 {
2040 *olen = 0;
2041 return;
2042 }
2043
2044 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
2045
2046 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
2047 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
2048
2049 *p++ = 0x00;
2050 *p++ = 0x00;
2051
2052 *olen = 4;
2053}
Paul Bakkera503a632013-08-14 13:48:06 +02002054#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002055
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002056static void ssl_write_renegotiation_ext( ssl_context *ssl,
2057 unsigned char *buf,
2058 size_t *olen )
2059{
2060 unsigned char *p = buf;
2061
2062 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
2063 {
2064 *olen = 0;
2065 return;
2066 }
2067
2068 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
2069
2070 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
2071 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
2072
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002073#if defined(POLARSSL_SSL_RENEGOTIATION)
2074 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
2075 {
2076 *p++ = 0x00;
2077 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2078 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002079
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002080 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2081 p += ssl->verify_data_len;
2082 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2083 p += ssl->verify_data_len;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002084
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002085 *olen = 5 + ssl->verify_data_len * 2;
2086 }
2087 else
2088#endif /* POLARSSL_SSL_RENEGOTIATION */
2089 {
2090 *p++ = 0x00;
2091 *p++ = 0x01;
2092 *p++ = 0x00;
2093
2094 *olen = 5;
2095 }
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002096}
2097
Paul Bakker05decb22013-08-15 13:33:48 +02002098#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002099static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
2100 unsigned char *buf,
2101 size_t *olen )
2102{
2103 unsigned char *p = buf;
2104
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002105 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
2106 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002107 *olen = 0;
2108 return;
2109 }
2110
2111 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
2112
2113 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
2114 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
2115
2116 *p++ = 0x00;
2117 *p++ = 1;
2118
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002119 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002120
2121 *olen = 5;
2122}
Paul Bakker05decb22013-08-15 13:33:48 +02002123#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002124
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002125#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002126static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
2127 unsigned char *buf,
2128 size_t *olen )
2129{
2130 unsigned char *p = buf;
2131 ((void) ssl);
2132
Paul Bakker677377f2013-10-28 12:54:26 +01002133 if( ( ssl->handshake->cli_exts &
2134 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
2135 {
2136 *olen = 0;
2137 return;
2138 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002139
2140 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
2141
2142 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2143 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
2144
2145 *p++ = 0x00;
2146 *p++ = 2;
2147
2148 *p++ = 1;
2149 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
2150
2151 *olen = 6;
2152}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002153#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002154
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002155#if defined(POLARSSL_SSL_ALPN )
2156static void ssl_write_alpn_ext( ssl_context *ssl,
2157 unsigned char *buf, size_t *olen )
2158{
2159 if( ssl->alpn_chosen == NULL )
2160 {
2161 *olen = 0;
2162 return;
2163 }
2164
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02002165 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002166
2167 /*
2168 * 0 . 1 ext identifier
2169 * 2 . 3 ext length
2170 * 4 . 5 protocol list length
2171 * 6 . 6 protocol name length
2172 * 7 . 7+n protocol name
2173 */
2174 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
2175 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
2176
2177 *olen = 7 + strlen( ssl->alpn_chosen );
2178
2179 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2180 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2181
2182 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2183 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2184
2185 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2186
2187 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2188}
2189#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
2190
Paul Bakker5121ce52009-01-03 21:22:43 +00002191static int ssl_write_server_hello( ssl_context *ssl )
2192{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002193#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002194 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002195#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002196 int ret;
2197 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002198 unsigned char *buf, *p;
2199
2200 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
2201
Paul Bakkera9a028e2013-11-21 17:31:06 +01002202 if( ssl->f_rng == NULL )
2203 {
2204 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2205 return( POLARSSL_ERR_SSL_NO_RNG );
2206 }
2207
Paul Bakker5121ce52009-01-03 21:22:43 +00002208 /*
2209 * 0 . 0 handshake type
2210 * 1 . 3 handshake length
2211 * 4 . 5 protocol version
2212 * 6 . 9 UNIX time()
2213 * 10 . 37 random bytes
2214 */
2215 buf = ssl->out_msg;
2216 p = buf + 4;
2217
2218 *p++ = (unsigned char) ssl->major_ver;
2219 *p++ = (unsigned char) ssl->minor_ver;
2220
2221 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
2222 buf[4], buf[5] ) );
2223
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002224#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002225 t = time( NULL );
2226 *p++ = (unsigned char)( t >> 24 );
2227 *p++ = (unsigned char)( t >> 16 );
2228 *p++ = (unsigned char)( t >> 8 );
2229 *p++ = (unsigned char)( t );
2230
2231 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002232#else
2233 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
2234 return( ret );
2235
2236 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02002237#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002238
Paul Bakkera3d195c2011-11-27 21:07:34 +00002239 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
2240 return( ret );
2241
2242 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002243
Paul Bakker48916f92012-09-16 19:57:18 +00002244 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002245
2246 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
2247
2248 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002249 * Resume is 0 by default, see ssl_handshake_init().
2250 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2251 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002252 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002253 if( ssl->handshake->resume == 0 &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002254#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002255 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002256#endif
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002257 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002258 ssl->f_get_cache != NULL &&
2259 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
2260 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01002261 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002262 ssl->handshake->resume = 1;
2263 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002264
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002265 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002266 {
2267 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002268 * New session, create a new session id,
2269 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002270 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002271 ssl->state++;
2272
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002273#if defined(POLARSSL_HAVE_TIME)
2274 ssl->session_negotiate->start = time( NULL );
2275#endif
2276
Paul Bakkera503a632013-08-14 13:48:06 +02002277#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002278 if( ssl->handshake->new_session_ticket != 0 )
2279 {
2280 ssl->session_negotiate->length = n = 0;
2281 memset( ssl->session_negotiate->id, 0, 32 );
2282 }
2283 else
2284#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002285 {
2286 ssl->session_negotiate->length = n = 32;
2287 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002288 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002289 return( ret );
2290 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002291 }
2292 else
2293 {
2294 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002295 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002296 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02002297 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002298 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002299
2300 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2301 {
2302 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2303 return( ret );
2304 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002305 }
2306
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002307 /*
2308 * 38 . 38 session id length
2309 * 39 . 38+n session id
2310 * 39+n . 40+n chosen ciphersuite
2311 * 41+n . 41+n chosen compression alg.
2312 * 42+n . 43+n extensions length
2313 * 44+n . 43+n+m extensions
2314 */
2315 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00002316 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
2317 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002318
2319 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2320 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2321 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002322 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002323
Paul Bakker48916f92012-09-16 19:57:18 +00002324 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2325 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2326 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002327
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02002328 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2329 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02002330 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002331 ssl->session_negotiate->compression ) );
2332
Hanno Becker57457782017-06-09 15:30:29 +01002333 /* Do not write the extensions if the protocol is SSLv3 */
2334#if defined(POLARSSL_SSL_PROTO_SSL3)
2335 if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
2336 {
2337#endif
2338
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002339 /*
2340 * First write extensions, then the total length
2341 */
2342 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2343 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002344
Paul Bakker05decb22013-08-15 13:33:48 +02002345#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002346 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2347 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002348#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002349
Paul Bakker1f2bc622013-08-15 13:45:55 +02002350#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002351 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2352 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002353#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002354
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002355#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2356 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2357 ext_len += olen;
2358#endif
2359
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002360#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2361 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2362 ext_len += olen;
2363#endif
2364
Paul Bakkera503a632013-08-14 13:48:06 +02002365#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002366 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2367 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002368#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002369
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002370#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002371 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2372 ext_len += olen;
2373#endif
2374
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002375#if defined(POLARSSL_SSL_ALPN)
2376 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2377 ext_len += olen;
2378#endif
2379
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002380 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002381
Paul Bakkera7036632014-04-30 10:15:38 +02002382 if( ext_len > 0 )
2383 {
2384 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2385 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2386 p += ext_len;
2387 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002388
Hanno Becker57457782017-06-09 15:30:29 +01002389#if defined(POLARSSL_SSL_PROTO_SSL3)
2390 }
2391#endif
2392
Paul Bakker5121ce52009-01-03 21:22:43 +00002393 ssl->out_msglen = p - buf;
2394 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2395 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
2396
2397 ret = ssl_write_record( ssl );
2398
2399 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2400
2401 return( ret );
2402}
2403
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002404#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2405 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Simon Butcher7458bc32016-09-05 11:18:39 +03002406 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002407 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
Simon Butcher7458bc32016-09-05 11:18:39 +03002408 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)&& \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002409 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002410static int ssl_write_certificate_request( ssl_context *ssl )
2411{
Paul Bakkered27a042013-04-18 22:46:23 +02002412 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002413
2414 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2415
2416 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002417 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002418 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2419 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002420 {
2421 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2422 ssl->state++;
2423 return( 0 );
2424 }
2425
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002426 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2427 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002428}
2429#else
2430static int ssl_write_certificate_request( ssl_context *ssl )
2431{
2432 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2433 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002434 size_t dn_size, total_dn_size; /* excluding length bytes */
2435 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002436 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002437 const x509_crt *crt;
Manuel Pégourié-Gonnardde9c8a52015-10-02 11:16:47 +02002438 const unsigned char * const end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00002439
2440 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2441
2442 ssl->state++;
2443
Paul Bakkerfbb17802013-04-17 19:10:21 +02002444 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002445 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002446 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002447 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002448 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002449 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002450 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002451 return( 0 );
2452 }
2453
2454 /*
2455 * 0 . 0 handshake type
2456 * 1 . 3 handshake length
2457 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002458 * 5 .. m-1 cert types
2459 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002460 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 * n .. n+1 length of all DNs
2462 * n+2 .. n+3 length of DN 1
2463 * n+4 .. ... Distinguished Name #1
2464 * ... .. ... length of DN 2, etc.
2465 */
2466 buf = ssl->out_msg;
2467 p = buf + 4;
2468
2469 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002470 * Supported certificate types
2471 *
2472 * ClientCertificateType certificate_types<1..2^8-1>;
2473 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002474 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002475 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002476
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002477#if defined(POLARSSL_RSA_C)
2478 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2479#endif
2480#if defined(POLARSSL_ECDSA_C)
2481 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2482#endif
2483
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002484 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002485 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002486
Paul Bakker577e0062013-08-28 11:57:20 +02002487 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002488#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002489 /*
2490 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002491 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002492 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2493 *
2494 * struct {
2495 * HashAlgorithm hash;
2496 * SignatureAlgorithm signature;
2497 * } SignatureAndHashAlgorithm;
2498 *
2499 * enum { (255) } HashAlgorithm;
2500 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002501 */
Paul Bakker21dca692013-01-03 11:41:08 +01002502 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002503 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002504 /*
2505 * Only use current running hash algorithm that is already required
2506 * for requested ciphersuite.
2507 */
Paul Bakker926af752012-11-23 13:38:07 +01002508 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2509
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002510 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2511 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002512 {
2513 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2514 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002515
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002516 /*
2517 * Supported signature algorithms
2518 */
2519#if defined(POLARSSL_RSA_C)
2520 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2521 p[2 + sa_len++] = SSL_SIG_RSA;
2522#endif
2523#if defined(POLARSSL_ECDSA_C)
2524 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2525 p[2 + sa_len++] = SSL_SIG_ECDSA;
2526#endif
Paul Bakker926af752012-11-23 13:38:07 +01002527
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002528 p[0] = (unsigned char)( sa_len >> 8 );
2529 p[1] = (unsigned char)( sa_len );
2530 sa_len += 2;
2531 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002532 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002533#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002534
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002535 /*
2536 * DistinguishedName certificate_authorities<0..2^16-1>;
2537 * opaque DistinguishedName<1..2^16-1>;
2538 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002539 p += 2;
2540 crt = ssl->ca_chain;
2541
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002542 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002543 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002544 {
Paul Bakker926af752012-11-23 13:38:07 +01002545 dn_size = crt->subject_raw.len;
Manuel Pégourié-Gonnardde9c8a52015-10-02 11:16:47 +02002546
Manuel Pégourié-Gonnardcf16b792015-12-10 14:36:25 +01002547 if( end < p ||
2548 (size_t)( end - p ) < dn_size ||
2549 (size_t)( end - p ) < 2 + dn_size )
Manuel Pégourié-Gonnardde9c8a52015-10-02 11:16:47 +02002550 {
2551 SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) );
2552 break;
2553 }
2554
Paul Bakker926af752012-11-23 13:38:07 +01002555 *p++ = (unsigned char)( dn_size >> 8 );
2556 *p++ = (unsigned char)( dn_size );
2557 memcpy( p, crt->subject_raw.p, dn_size );
2558 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002559
Paul Bakker926af752012-11-23 13:38:07 +01002560 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2561
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002562 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002563 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002564 }
2565
Paul Bakker926af752012-11-23 13:38:07 +01002566 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002567 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2568 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002569 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2570 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002571
2572 ret = ssl_write_record( ssl );
2573
2574 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2575
2576 return( ret );
2577}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002578#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2579 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Simon Butcher7458bc32016-09-05 11:18:39 +03002580 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002581 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
Simon Butcher7458bc32016-09-05 11:18:39 +03002582 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002583 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002584
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002585#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2586 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2587static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2588{
2589 int ret;
2590
2591 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2592 {
2593 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2594 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2595 }
2596
2597 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2598 pk_ec( *ssl_own_key( ssl ) ),
2599 POLARSSL_ECDH_OURS ) ) != 0 )
2600 {
2601 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2602 return( ret );
2603 }
2604
2605 return( 0 );
2606}
2607#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2608 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2609
Paul Bakker41c83d32013-03-20 14:39:14 +01002610static int ssl_write_server_key_exchange( ssl_context *ssl )
2611{
Paul Bakker23986e52011-04-24 08:57:21 +00002612 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002613 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002614 const ssl_ciphersuite_t *ciphersuite_info =
2615 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002616
2617#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2618 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2619 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002620 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002621 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002622 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002623 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002624 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002625 ((void) dig_signed);
2626 ((void) dig_signed_len);
2627#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002628
Paul Bakker5121ce52009-01-03 21:22:43 +00002629 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2630
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002631#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2632 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2633 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002634 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2635 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2636 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002637 {
2638 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2639 ssl->state++;
2640 return( 0 );
2641 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002642#endif
2643
2644#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2645 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2646 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2647 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2648 {
2649 ssl_get_ecdh_params_from_cert( ssl );
2650
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002651 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002652 ssl->state++;
2653 return( 0 );
2654 }
2655#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002656
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002657#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2658 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2659 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2660 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002661 {
2662 /* TODO: Support identity hints */
2663 *(p++) = 0x00;
2664 *(p++) = 0x00;
2665
2666 n += 2;
2667 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002668#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2669 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002670
2671#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2672 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2673 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2674 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002675 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002676 /*
2677 * Ephemeral DH parameters:
2678 *
2679 * struct {
2680 * opaque dh_p<1..2^16-1>;
2681 * opaque dh_g<1..2^16-1>;
2682 * opaque dh_Ys<1..2^16-1>;
2683 * } ServerDHParams;
2684 */
2685 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2686 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2687 {
2688 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2689 return( ret );
2690 }
Paul Bakker48916f92012-09-16 19:57:18 +00002691
Paul Bakker41c83d32013-03-20 14:39:14 +01002692 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002693 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2694 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002695 {
2696 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2697 return( ret );
2698 }
2699
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002700 dig_signed = p;
2701 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002702
2703 p += len;
2704 n += len;
2705
Paul Bakker41c83d32013-03-20 14:39:14 +01002706 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2707 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2708 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2709 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2710 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002711#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2712 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002713
Gergely Budai987bfb52014-01-19 21:48:42 +01002714#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002715 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002716 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2717 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002718 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002719 /*
2720 * Ephemeral ECDH parameters:
2721 *
2722 * struct {
2723 * ECParameters curve_params;
2724 * ECPoint public;
2725 * } ServerECDHParams;
2726 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002727 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002728#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002729 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002730
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002731 /* Match our preference list against the offered curves */
2732 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2733 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2734 if( (*curve)->grp_id == *gid )
2735 goto curve_matching_done;
2736
2737curve_matching_done:
2738#else
2739 curve = ssl->handshake->curves;
2740#endif
2741
Manuel Pégourié-Gonnard8e8ae3d2015-06-23 18:57:28 +02002742 if( curve == NULL || *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002743 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002744 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2745 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002746 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002747
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002748 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002749
Paul Bakker41c83d32013-03-20 14:39:14 +01002750 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002751 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002752 {
2753 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2754 return( ret );
2755 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002756
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002757 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2758 p, SSL_MAX_CONTENT_LEN - n,
2759 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002760 {
2761 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2762 return( ret );
2763 }
2764
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002765 dig_signed = p;
2766 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002767
2768 p += len;
2769 n += len;
2770
Paul Bakker41c83d32013-03-20 14:39:14 +01002771 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2772 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002773#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002774
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002775#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002776 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2777 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002778 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002779 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2780 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002781 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002782 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002783 unsigned int hashlen = 0;
2784 unsigned char hash[64];
Paul Bakker23f36802012-09-28 14:15:14 +00002785
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002786 /*
Hanno Beckerc2b9d982017-05-10 16:37:21 +01002787 * Choose hash algorithm:
2788 * - For TLS 1.2, obey signature-hash-algorithm extension to choose appropriate hash.
2789 * - For SSL3, TLS1.0, TLS1.1 and ECDHE_ECDSA, use SHA1 (RFC 4492, Sec. 5.4)
2790 * - Otherwise, use MD5 + SHA1 (RFC 4346, Sec. 7.4.3)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002791 */
Hanno Beckerc2b9d982017-05-10 16:37:21 +01002792
2793 md_type_t md_alg;
2794
Paul Bakker577e0062013-08-28 11:57:20 +02002795#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Hanno Beckerc2b9d982017-05-10 16:37:21 +01002796 pk_type_t sig_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002797 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2798 {
Hanno Beckerc2b9d982017-05-10 16:37:21 +01002799 /* For TLS 1.2, obey signature-hash-algorithm extension
2800 * (RFC 5246, Sec. 7.4.1.4.1). */
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002801
Hanno Beckerc2b9d982017-05-10 16:37:21 +01002802 if( sig_alg == POLARSSL_PK_NONE ||
2803 ( 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 +02002804 {
2805 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002806 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002807 }
2808 }
Paul Bakker577e0062013-08-28 11:57:20 +02002809 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002810#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002811#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2812 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002813 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002814 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2815 {
2816 md_alg = POLARSSL_MD_SHA1;
2817 }
2818 else
Paul Bakker577e0062013-08-28 11:57:20 +02002819#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002820 {
2821 md_alg = POLARSSL_MD_NONE;
2822 }
2823
Hanno Beckerc2b9d982017-05-10 16:37:21 +01002824 SSL_DEBUG_MSG( 3, ( "pick hash algorithm %d for signing", md_alg ) );
2825
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002826 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002827 * Compute the hash to be signed
2828 */
Paul Bakker577e0062013-08-28 11:57:20 +02002829#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2830 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002831 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002832 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002833 md5_context md5;
2834 sha1_context sha1;
2835
Paul Bakker5b4af392014-06-26 12:09:34 +02002836 md5_init( &md5 );
2837 sha1_init( &sha1 );
2838
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002839 /*
2840 * digitally-signed struct {
2841 * opaque md5_hash[16];
2842 * opaque sha_hash[20];
2843 * };
2844 *
2845 * md5_hash
2846 * MD5(ClientHello.random + ServerHello.random
2847 * + ServerParams);
2848 * sha_hash
2849 * SHA(ClientHello.random + ServerHello.random
2850 * + ServerParams);
2851 */
2852 md5_starts( &md5 );
2853 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002854 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002855 md5_finish( &md5, hash );
2856
2857 sha1_starts( &sha1 );
2858 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002859 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002860 sha1_finish( &sha1, hash + 16 );
2861
2862 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02002863
2864 md5_free( &md5 );
2865 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002866 }
2867 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002868#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2869 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002870#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2871 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002872 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002873 {
2874 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02002875 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002876
Paul Bakker84bbeb52014-07-01 14:53:22 +02002877 md_init( &ctx );
2878
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002879 /* Info from md_alg will be used instead */
2880 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002881
2882 /*
2883 * digitally-signed struct {
2884 * opaque client_random[32];
2885 * opaque server_random[32];
2886 * ServerDHParams params;
2887 * };
2888 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002889 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002890 {
2891 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2892 return( ret );
2893 }
2894
2895 md_starts( &ctx );
2896 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002897 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002898 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002899 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00002900 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002901 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002902#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2903 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002904 {
2905 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002906 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002907 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002908
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002909 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2910 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002911
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002912 /*
2913 * Make the signature
2914 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002915 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002916 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002917 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2918 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002919 }
Paul Bakker23f36802012-09-28 14:15:14 +00002920
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002921#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002922 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2923 {
Hanno Beckerc2b9d982017-05-10 16:37:21 +01002924 *(p++) = ssl_hash_from_md_alg( md_alg );
2925 *(p++) = ssl_sig_from_pk_alg( sig_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002926
2927 n += 2;
2928 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002929#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002930
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002931 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002932 p + 2 , &signature_len,
2933 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002934 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002935 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002936 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002937 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002938
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002939 *(p++) = (unsigned char)( signature_len >> 8 );
2940 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002941 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002942
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002943 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002944
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002945 p += signature_len;
2946 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002947 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002948#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002949 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2950 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002951
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002952 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002953 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2954 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2955
2956 ssl->state++;
2957
2958 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2959 {
2960 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2961 return( ret );
2962 }
2963
2964 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2965
2966 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002967}
2968
2969static int ssl_write_server_hello_done( ssl_context *ssl )
2970{
2971 int ret;
2972
2973 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2974
2975 ssl->out_msglen = 4;
2976 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2977 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2978
2979 ssl->state++;
2980
2981 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2982 {
2983 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2984 return( ret );
2985 }
2986
2987 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2988
2989 return( 0 );
2990}
2991
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002992#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2993 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2994static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2995 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002996{
2997 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002998 size_t n;
2999
3000 /*
3001 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3002 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003003 if( *p + 2 > end )
3004 {
3005 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3006 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3007 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003008
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003009 n = ( (*p)[0] << 8 ) | (*p)[1];
3010 *p += 2;
3011
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003012 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003013 {
3014 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3015 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3016 }
3017
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003018 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003019 {
3020 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
3021 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3022 }
3023
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003024 *p += n;
3025
Paul Bakker70df2fb2013-04-17 17:19:09 +02003026 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
3027
Paul Bakker70df2fb2013-04-17 17:19:09 +02003028 return( ret );
3029}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003030#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3031 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003032
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003033#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
3034 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003035static int ssl_parse_encrypted_pms( ssl_context *ssl,
3036 const unsigned char *p,
3037 const unsigned char *end,
3038 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003039{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003040 int ret;
3041 size_t len = pk_get_len( ssl_own_key( ssl ) );
3042 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003043 unsigned char fake_pms[48], peer_pms[48];
3044 unsigned char mask;
Manuel Pégourié-Gonnardb26b75e2015-06-23 18:52:09 +02003045 size_t i, peer_pmslen;
3046 unsigned int diff;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003047
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003048 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003049 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02003050 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003051 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3052 }
3053
3054 /*
3055 * Decrypt the premaster using own private RSA key
3056 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003057#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3058 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02003059 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
3060 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003061 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
3062 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003063 {
3064 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3065 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3066 }
3067 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003068#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003069
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003070 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003071 {
3072 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3073 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3074 }
3075
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003076 /*
3077 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
3078 * must not cause the connection to end immediately; instead, send a
3079 * bad_record_mac later in the handshake.
3080 * Also, avoid data-dependant branches here to protect against
3081 * timing-based variants.
3082 */
3083 ret = ssl->f_rng( ssl->p_rng, fake_pms, sizeof( fake_pms ) );
3084 if( ret != 0 )
3085 return( ret );
3086
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003087 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02003088 peer_pms, &peer_pmslen,
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003089 sizeof( peer_pms ),
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003090 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003091
Manuel Pégourié-Gonnardb26b75e2015-06-23 18:52:09 +02003092 diff = (unsigned int) ret;
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02003093 diff |= peer_pmslen ^ 48;
3094 diff |= peer_pms[0] ^ ssl->handshake->max_major_ver;
3095 diff |= peer_pms[1] ^ ssl->handshake->max_minor_ver;
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003096
3097#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02003098 if( diff != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003099 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003100#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003101
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003102 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
3103 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
3104 {
3105 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3106 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003107 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003108 ssl->handshake->pmslen = 48;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003109
Manuel Pégourié-Gonnardb26b75e2015-06-23 18:52:09 +02003110 /* mask = diff ? 0xff : 0x00 */
Simon Ba697bf52016-11-10 13:19:42 +00003111 /* MSVC has a warning about unary minus on unsigned, but this is
3112 * well-defined and precisely what we want to do here */
3113#if defined(_MSC_VER)
3114#pragma warning( push )
3115#pragma warning( disable : 4146 )
3116#endif
Manuel Pégourié-Gonnardb26b75e2015-06-23 18:52:09 +02003117 mask = - ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 );
Simon Ba697bf52016-11-10 13:19:42 +00003118#if defined(_MSC_VER)
3119#pragma warning( pop )
3120#endif
3121
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003122 for( i = 0; i < ssl->handshake->pmslen; i++ )
3123 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
3124
3125 return( 0 );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003126}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003127#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
3128 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003129
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003130#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003131static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
3132 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003133{
Paul Bakker6db455e2013-09-18 17:29:31 +02003134 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003135 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003136
Paul Bakker6db455e2013-09-18 17:29:31 +02003137 if( ssl->f_psk == NULL &&
3138 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
3139 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003140 {
3141 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
3142 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3143 }
3144
3145 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003146 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003147 */
Hanno Beckerb2ee6b42017-06-26 13:52:14 +01003148 if( end - *p < 2 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003149 {
3150 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3151 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3152 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003153
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003154 n = ( (*p)[0] << 8 ) | (*p)[1];
3155 *p += 2;
3156
Hanno Beckerb2ee6b42017-06-26 13:52:14 +01003157 if( n < 1 || n > 65535 || n > (size_t) ( end - *p ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003158 {
3159 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3160 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3161 }
3162
Paul Bakker6db455e2013-09-18 17:29:31 +02003163 if( ssl->f_psk != NULL )
3164 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003165 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003166 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3167 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003168 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003169 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003170 /* Identity is not a big secret since clients send it in the clear,
3171 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02003172 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003173 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003174 {
3175 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3176 }
3177 }
3178
3179 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003180 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003181 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02003182 if( ( ret = ssl_send_alert_message( ssl,
3183 SSL_ALERT_LEVEL_FATAL,
3184 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
3185 {
3186 return( ret );
3187 }
3188
3189 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003190 }
3191
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003192 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003193
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003194 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003195}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003196#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003197
Paul Bakker5121ce52009-01-03 21:22:43 +00003198static int ssl_parse_client_key_exchange( ssl_context *ssl )
3199{
Paul Bakker23986e52011-04-24 08:57:21 +00003200 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003201 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003202
Paul Bakker41c83d32013-03-20 14:39:14 +01003203 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003204
3205 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
3206
3207 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3208 {
3209 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3210 return( ret );
3211 }
3212
3213 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3214 {
3215 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003216 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003217 }
3218
3219 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
3220 {
3221 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003222 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003223 }
3224
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003225#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01003226 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003227 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003228 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003229 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003230
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003231 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003232 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02003233 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3234 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003235 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003236
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003237 if( p != end )
3238 {
3239 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3240 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3241 }
3242
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02003243 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003244
3245 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
3246 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003247 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02003248 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003249 {
3250 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
3251 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3252 }
3253
3254 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003255 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003256 else
3257#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003258#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003259 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3260 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3261 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003262 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003263 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
3264 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
3265 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003266 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003267 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003268 ssl->in_msg + 4, ssl->in_hslen - 4 ) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003269 {
3270 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3271 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3272 }
3273
3274 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3275
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003276 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
3277 &ssl->handshake->pmslen,
3278 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02003279 POLARSSL_MPI_MAX_SIZE,
3280 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003281 {
3282 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
3283 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3284 }
3285
3286 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003287 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003288 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003289#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003290 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3291 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3292 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003293#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
3294 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003295 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003296 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003297 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003298
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003299 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003300 {
3301 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3302 return( ret );
3303 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003304
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003305 if( p != end )
3306 {
3307 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3308 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3309 }
3310
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003311 if( ( ret = ssl_psk_derive_premaster( ssl,
3312 ciphersuite_info->key_exchange ) ) != 0 )
3313 {
3314 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3315 return( ret );
3316 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003317 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003318 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003319#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003320#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
3321 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
3322 {
3323 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003324 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003325
3326 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3327 {
3328 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3329 return( ret );
3330 }
3331
3332 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3333 {
3334 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
3335 return( ret );
3336 }
3337
3338 if( ( ret = ssl_psk_derive_premaster( ssl,
3339 ciphersuite_info->key_exchange ) ) != 0 )
3340 {
3341 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3342 return( ret );
3343 }
3344 }
3345 else
3346#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003347#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3348 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3349 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003350 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003351 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003352
3353 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3354 {
3355 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3356 return( ret );
3357 }
3358 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3359 {
3360 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3361 return( ret );
3362 }
3363
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003364 if( p != end )
3365 {
3366 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3367 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3368 }
3369
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003370 if( ( ret = ssl_psk_derive_premaster( ssl,
3371 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003372 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003373 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3374 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003375 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003376 }
3377 else
3378#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003379#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3380 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
3381 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003382 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003383 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003384
3385 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3386 {
3387 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3388 return( ret );
3389 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003390
3391 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
3392 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003393 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003394 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3395 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003396 }
3397
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003398 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3399
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003400 if( ( ret = ssl_psk_derive_premaster( ssl,
3401 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003402 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003403 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003404 return( ret );
3405 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003406 }
3407 else
3408#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003409#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
3410 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003411 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003412 if( ( ret = ssl_parse_encrypted_pms( ssl,
3413 ssl->in_msg + 4,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003414 ssl->in_msg + ssl->in_hslen,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003415 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003416 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003417 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003418 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003419 }
3420 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003421 else
3422#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
3423 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003424 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003425 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003426 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003427
Paul Bakkerff60ee62010-03-16 21:09:09 +00003428 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
3429 {
3430 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
3431 return( ret );
3432 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003433
Paul Bakker5121ce52009-01-03 21:22:43 +00003434 ssl->state++;
3435
3436 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3437
3438 return( 0 );
3439}
3440
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003441#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3442 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Simon Butcher7458bc32016-09-05 11:18:39 +03003443 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003444 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
Simon Butcher7458bc32016-09-05 11:18:39 +03003445 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)&& \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003446 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003447static int ssl_parse_certificate_verify( ssl_context *ssl )
3448{
Paul Bakkerfbb17802013-04-17 19:10:21 +02003449 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003450
3451 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3452
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003453 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003454 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003455 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003456 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02003457 {
3458 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3459 ssl->state++;
3460 return( 0 );
3461 }
3462
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003463 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3464 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003465}
3466#else
3467static int ssl_parse_certificate_verify( ssl_context *ssl )
3468{
3469 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003470 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003471 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003472 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003473 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003474#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003475 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003476#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003477 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003478 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3479
3480 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3481
3482 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003483 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003484 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003485 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3486 {
3487 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3488 ssl->state++;
3489 return( 0 );
3490 }
3491
Paul Bakkered27a042013-04-18 22:46:23 +02003492 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003493 {
3494 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3495 ssl->state++;
3496 return( 0 );
3497 }
3498
Paul Bakker48916f92012-09-16 19:57:18 +00003499 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003500
3501 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3502 {
3503 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3504 return( ret );
3505 }
3506
3507 ssl->state++;
3508
3509 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3510 {
3511 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003512 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003513 }
3514
3515 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
3516 {
3517 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003518 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003519 }
3520
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003521 /*
3522 * 0 . 0 handshake type
3523 * 1 . 3 handshake length
3524 * 4 . 5 sig alg (TLS 1.2 only)
3525 * 4+n . 5+n signature length (n = sa_len)
3526 * 6+n . 6+n+m signature (m = sig_len)
3527 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003528
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003529#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3530 defined(POLARSSL_SSL_PROTO_TLS1_1)
3531 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003532 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003533 sa_len = 0;
3534
Paul Bakkerc70b9822013-04-07 22:00:46 +02003535 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003536 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003537
3538 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3539 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3540 POLARSSL_PK_ECDSA ) )
3541 {
3542 hash_start += 16;
3543 hashlen -= 16;
3544 md_alg = POLARSSL_MD_SHA1;
3545 }
Paul Bakker926af752012-11-23 13:38:07 +01003546 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003547 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003548#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3549 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003550#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3551 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003552 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003553 sa_len = 2;
3554
Paul Bakker5121ce52009-01-03 21:22:43 +00003555 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003556 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003557 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003558 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003559 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003560 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3561 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003562 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3563 }
3564
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003565 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003566
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003567 /* Info from md_alg will be used instead */
3568 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003569
3570 /*
3571 * Signature
3572 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003573 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
3574 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003575 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003576 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3577 " for verify message" ) );
3578 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003579 }
3580
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003581 /*
3582 * Check the certificate's key type matches the signature alg
3583 */
3584 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3585 {
3586 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3587 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3588 }
Paul Bakker577e0062013-08-28 11:57:20 +02003589 }
3590 else
3591#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3592 {
3593 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003594 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003595 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003596
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003597 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01003598
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003599 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003600 {
3601 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003602 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003603 }
3604
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003605 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003606 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003607 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003608 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003609 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003610 return( ret );
3611 }
3612
3613 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3614
Paul Bakkered27a042013-04-18 22:46:23 +02003615 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003616}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003617#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3618 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Simon Butcher7458bc32016-09-05 11:18:39 +03003619 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED &&
3620 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
3621 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED &&
3622 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003623
Paul Bakkera503a632013-08-14 13:48:06 +02003624#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003625static int ssl_write_new_session_ticket( ssl_context *ssl )
3626{
3627 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003628 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003629 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003630
3631 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3632
3633 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3634 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3635
3636 /*
3637 * struct {
3638 * uint32 ticket_lifetime_hint;
3639 * opaque ticket<0..2^16-1>;
3640 * } NewSessionTicket;
3641 *
3642 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3643 * 8 . 9 ticket_len (n)
3644 * 10 . 9+n ticket content
3645 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003646
3647 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3648 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3649 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3650 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003651
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003652 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3653 {
3654 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3655 tlen = 0;
3656 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003657
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003658 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3659 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003660
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003661 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003662
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003663 /*
3664 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3665 * ChangeCipherSpec share the same state.
3666 */
3667 ssl->handshake->new_session_ticket = 0;
3668
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003669 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3670 {
3671 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3672 return( ret );
3673 }
3674
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003675 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3676
3677 return( 0 );
3678}
Paul Bakkera503a632013-08-14 13:48:06 +02003679#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003680
Paul Bakker5121ce52009-01-03 21:22:43 +00003681/*
Paul Bakker1961b702013-01-25 14:49:24 +01003682 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003683 */
Paul Bakker1961b702013-01-25 14:49:24 +01003684int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003685{
3686 int ret = 0;
3687
Paul Bakker1961b702013-01-25 14:49:24 +01003688 if( ssl->state == SSL_HANDSHAKE_OVER )
3689 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003690
Paul Bakker1961b702013-01-25 14:49:24 +01003691 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3692
3693 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3694 return( ret );
3695
3696 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003697 {
Paul Bakker1961b702013-01-25 14:49:24 +01003698 case SSL_HELLO_REQUEST:
3699 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003700 break;
3701
Paul Bakker1961b702013-01-25 14:49:24 +01003702 /*
3703 * <== ClientHello
3704 */
3705 case SSL_CLIENT_HELLO:
3706 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003707 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003708
3709 /*
3710 * ==> ServerHello
3711 * Certificate
3712 * ( ServerKeyExchange )
3713 * ( CertificateRequest )
3714 * ServerHelloDone
3715 */
3716 case SSL_SERVER_HELLO:
3717 ret = ssl_write_server_hello( ssl );
3718 break;
3719
3720 case SSL_SERVER_CERTIFICATE:
3721 ret = ssl_write_certificate( ssl );
3722 break;
3723
3724 case SSL_SERVER_KEY_EXCHANGE:
3725 ret = ssl_write_server_key_exchange( ssl );
3726 break;
3727
3728 case SSL_CERTIFICATE_REQUEST:
3729 ret = ssl_write_certificate_request( ssl );
3730 break;
3731
3732 case SSL_SERVER_HELLO_DONE:
3733 ret = ssl_write_server_hello_done( ssl );
3734 break;
3735
3736 /*
3737 * <== ( Certificate/Alert )
3738 * ClientKeyExchange
3739 * ( CertificateVerify )
3740 * ChangeCipherSpec
3741 * Finished
3742 */
3743 case SSL_CLIENT_CERTIFICATE:
3744 ret = ssl_parse_certificate( ssl );
3745 break;
3746
3747 case SSL_CLIENT_KEY_EXCHANGE:
3748 ret = ssl_parse_client_key_exchange( ssl );
3749 break;
3750
3751 case SSL_CERTIFICATE_VERIFY:
3752 ret = ssl_parse_certificate_verify( ssl );
3753 break;
3754
3755 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3756 ret = ssl_parse_change_cipher_spec( ssl );
3757 break;
3758
3759 case SSL_CLIENT_FINISHED:
3760 ret = ssl_parse_finished( ssl );
3761 break;
3762
3763 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003764 * ==> ( NewSessionTicket )
3765 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003766 * Finished
3767 */
3768 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003769#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003770 if( ssl->handshake->new_session_ticket != 0 )
3771 ret = ssl_write_new_session_ticket( ssl );
3772 else
Paul Bakkera503a632013-08-14 13:48:06 +02003773#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003774 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003775 break;
3776
3777 case SSL_SERVER_FINISHED:
3778 ret = ssl_write_finished( ssl );
3779 break;
3780
3781 case SSL_FLUSH_BUFFERS:
3782 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3783 ssl->state = SSL_HANDSHAKE_WRAPUP;
3784 break;
3785
3786 case SSL_HANDSHAKE_WRAPUP:
3787 ssl_handshake_wrapup( ssl );
3788 break;
3789
3790 default:
3791 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3792 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003793 }
3794
Paul Bakker5121ce52009-01-03 21:22:43 +00003795 return( ret );
3796}
Paul Bakker9af723c2014-05-01 13:03:14 +02003797#endif /* POLARSSL_SSL_SRV_C */