blob: 82fa2d46ef00c142132640dbd368ad9f61b04809 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 server-side functions
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Paul Bakker40e46942009-01-03 21:51:57 +000029#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Paul Bakker40e46942009-01-03 21:51:57 +000031#include "polarssl/debug.h"
32#include "polarssl/ssl.h"
Rich Evans00ab4702015-02-06 13:43:58 +000033
34#include <string.h>
35
Paul Bakker41c83d32013-03-20 14:39:14 +010036#if defined(POLARSSL_ECP_C)
37#include "polarssl/ecp.h"
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker7dc4c442014-02-01 22:50:26 +010040#if defined(POLARSSL_PLATFORM_C)
41#include "polarssl/platform.h"
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020042#else
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <stdlib.h>
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020044#define polarssl_malloc malloc
45#define polarssl_free free
46#endif
47
Paul Bakkerfa9b1002013-07-03 15:31:03 +020048#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000049#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020050#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000051
Paul Bakkera503a632013-08-14 13:48:06 +020052#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker34617722014-06-13 17:20:13 +020053/* Implementation that should never be optimized out by the compiler */
54static void polarssl_zeroize( void *v, size_t n ) {
55 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
56}
57
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020058/*
59 * Serialize a session in the following format:
60 * 0 . n-1 session structure, n = sizeof(ssl_session)
61 * n . n+2 peer_cert length = m (0 if no certificate)
62 * n+3 . n+2+m peer cert ASN.1
63 *
64 * Assumes ticket is NULL (always true on server side).
65 */
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020066static int ssl_save_session( const ssl_session *session,
67 unsigned char *buf, size_t buf_len,
68 size_t *olen )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020069{
70 unsigned char *p = buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020071 size_t left = buf_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020072#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020073 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020075
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020076 if( left < sizeof( ssl_session ) )
77 return( -1 );
78
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020079 memcpy( p, session, sizeof( ssl_session ) );
80 p += sizeof( ssl_session );
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020081 left -= sizeof( ssl_session );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020082
Paul Bakker7c6b2c32013-09-16 13:49:26 +020083#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020084 if( session->peer_cert == NULL )
85 cert_len = 0;
86 else
87 cert_len = session->peer_cert->raw.len;
88
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020089 if( left < 3 + cert_len )
90 return( -1 );
91
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020092 *p++ = (unsigned char)( cert_len >> 16 & 0xFF );
93 *p++ = (unsigned char)( cert_len >> 8 & 0xFF );
94 *p++ = (unsigned char)( cert_len & 0xFF );
95
96 if( session->peer_cert != NULL )
97 memcpy( p, session->peer_cert->raw.p, cert_len );
98
99 p += cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200100#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200101
102 *olen = p - buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200103
104 return( 0 );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200105}
106
107/*
108 * Unserialise session, see ssl_save_session()
109 */
110static int ssl_load_session( ssl_session *session,
111 const unsigned char *buf, size_t len )
112{
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200113 const unsigned char *p = buf;
114 const unsigned char * const end = buf + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200115#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200116 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200117#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200118
119 if( p + sizeof( ssl_session ) > end )
120 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
121
122 memcpy( session, p, sizeof( ssl_session ) );
123 p += sizeof( ssl_session );
124
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200125#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200126 if( p + 3 > end )
127 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
128
129 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
130 p += 3;
131
132 if( cert_len == 0 )
133 {
134 session->peer_cert = NULL;
135 }
136 else
137 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200138 int ret;
139
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200140 if( p + cert_len > end )
141 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
142
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200143 session->peer_cert = polarssl_malloc( sizeof( x509_crt ) );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200144
145 if( session->peer_cert == NULL )
146 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
147
Paul Bakkerb6b09562013-09-18 14:17:41 +0200148 x509_crt_init( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200149
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200150 if( ( ret = x509_crt_parse_der( session->peer_cert,
151 p, cert_len ) ) != 0 )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200152 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200153 x509_crt_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200154 polarssl_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200155 session->peer_cert = NULL;
156 return( ret );
157 }
158
159 p += cert_len;
160 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200161#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200162
163 if( p != end )
164 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
165
166 return( 0 );
167}
168
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200169/*
170 * Create session ticket, secured as recommended in RFC 5077 section 4:
171 *
172 * struct {
173 * opaque key_name[16];
174 * opaque iv[16];
175 * opaque encrypted_state<0..2^16-1>;
176 * opaque mac[32];
177 * } ticket;
178 *
179 * (the internal state structure differs, however).
180 */
181static int ssl_write_ticket( ssl_context *ssl, size_t *tlen )
182{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200183 int ret;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200184 unsigned char * const start = ssl->out_msg + 10;
185 unsigned char *p = start;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200186 unsigned char *state;
187 unsigned char iv[16];
188 size_t clear_len, enc_len, pad_len, i;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200189
Manuel Pégourié-Gonnard0a201712013-08-23 16:25:16 +0200190 *tlen = 0;
191
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200192 if( ssl->ticket_keys == NULL )
193 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
194
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200195 /* Write key name */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200196 memcpy( p, ssl->ticket_keys->key_name, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200197 p += 16;
198
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200199 /* Generate and write IV (with a copy for aes_crypt) */
200 if( ( ret = ssl->f_rng( ssl->p_rng, p, 16 ) ) != 0 )
201 return( ret );
202 memcpy( iv, p, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200203 p += 16;
204
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200205 /*
206 * Dump session state
207 *
208 * After the session state itself, we still need room for 16 bytes of
209 * padding and 32 bytes of MAC, so there's only so much room left
210 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200211 state = p + 2;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200212 if( ssl_save_session( ssl->session_negotiate, state,
Paul Bakker66d5d072014-06-17 16:39:18 +0200213 SSL_MAX_CONTENT_LEN - ( state - ssl->out_ctr ) - 48,
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200214 &clear_len ) != 0 )
215 {
216 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
217 }
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200218 SSL_DEBUG_BUF( 3, "session ticket cleartext", state, clear_len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200219
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200220 /* Apply PKCS padding */
221 pad_len = 16 - clear_len % 16;
222 enc_len = clear_len + pad_len;
223 for( i = clear_len; i < enc_len; i++ )
224 state[i] = (unsigned char) pad_len;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200225
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200226 /* Encrypt */
227 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->enc, AES_ENCRYPT,
228 enc_len, iv, state, state ) ) != 0 )
229 {
230 return( ret );
231 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200232
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200233 /* Write length */
234 *p++ = (unsigned char)( ( enc_len >> 8 ) & 0xFF );
235 *p++ = (unsigned char)( ( enc_len ) & 0xFF );
236 p = state + enc_len;
237
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200238 /* Compute and write MAC( key_name + iv + enc_state_len + enc_state ) */
239 sha256_hmac( ssl->ticket_keys->mac_key, 16, start, p - start, p, 0 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200240 p += 32;
241
242 *tlen = p - start;
243
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200244 SSL_DEBUG_BUF( 3, "session ticket structure", start, *tlen );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200245
246 return( 0 );
247}
248
249/*
250 * Load session ticket (see ssl_write_ticket for structure)
251 */
252static int ssl_parse_ticket( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200253 unsigned char *buf,
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200254 size_t len )
255{
256 int ret;
257 ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200258 unsigned char *key_name = buf;
259 unsigned char *iv = buf + 16;
260 unsigned char *enc_len_p = iv + 16;
261 unsigned char *ticket = enc_len_p + 2;
262 unsigned char *mac;
Manuel Pégourié-Gonnard34ced2d2013-09-20 11:37:39 +0200263 unsigned char computed_mac[32];
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200264 size_t enc_len, clear_len, i;
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100265 unsigned char pad_len, diff;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200266
267 SSL_DEBUG_BUF( 3, "session ticket structure", buf, len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200268
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200269 if( len < 34 || ssl->ticket_keys == NULL )
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200270 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
271
272 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
273 mac = ticket + enc_len;
274
275 if( len != enc_len + 66 )
276 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
277
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100278 /* Check name, in constant time though it's not a big secret */
279 diff = 0;
280 for( i = 0; i < 16; i++ )
281 diff |= key_name[i] ^ ssl->ticket_keys->key_name[i];
282 /* don't return yet, check the MAC anyway */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200283
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100284 /* Check mac, with constant-time buffer comparison */
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200285 sha256_hmac( ssl->ticket_keys->mac_key, 16, buf, len - 32,
286 computed_mac, 0 );
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100287
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200288 for( i = 0; i < 32; i++ )
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100289 diff |= mac[i] ^ computed_mac[i];
290
291 /* Now return if ticket is not authentic, since we want to avoid
292 * decrypting arbitrary attacker-chosen data */
293 if( diff != 0 )
294 return( POLARSSL_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200295
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200296 /* Decrypt */
297 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->dec, AES_DECRYPT,
298 enc_len, iv, ticket, ticket ) ) != 0 )
299 {
300 return( ret );
301 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200302
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200303 /* Check PKCS padding */
304 pad_len = ticket[enc_len - 1];
305
306 ret = 0;
307 for( i = 2; i < pad_len; i++ )
308 if( ticket[enc_len - i] != pad_len )
309 ret = POLARSSL_ERR_SSL_BAD_INPUT_DATA;
310 if( ret != 0 )
311 return( ret );
312
313 clear_len = enc_len - pad_len;
314
315 SSL_DEBUG_BUF( 3, "session ticket cleartext", ticket, clear_len );
316
317 /* Actually load session */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200318 if( ( ret = ssl_load_session( &session, ticket, clear_len ) ) != 0 )
319 {
320 SSL_DEBUG_MSG( 1, ( "failed to parse ticket content" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100321 ssl_session_free( &session );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200322 return( ret );
323 }
324
Paul Bakker606b4ba2013-08-14 16:52:14 +0200325#if defined(POLARSSL_HAVE_TIME)
326 /* Check if still valid */
327 if( (int) ( time( NULL) - session.start ) > ssl->ticket_lifetime )
328 {
329 SSL_DEBUG_MSG( 1, ( "session ticket expired" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100330 ssl_session_free( &session );
Paul Bakker606b4ba2013-08-14 16:52:14 +0200331 return( POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED );
332 }
333#endif
334
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200335 /*
336 * Keep the session ID sent by the client, since we MUST send it back to
337 * inform him we're accepting the ticket (RFC 5077 section 3.4)
338 */
339 session.length = ssl->session_negotiate->length;
340 memcpy( &session.id, ssl->session_negotiate->id, session.length );
341
342 ssl_session_free( ssl->session_negotiate );
343 memcpy( ssl->session_negotiate, &session, sizeof( ssl_session ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200344
345 /* Zeroize instead of free as we copied the content */
Paul Bakker34617722014-06-13 17:20:13 +0200346 polarssl_zeroize( &session, sizeof( ssl_session ) );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200347
348 return( 0 );
349}
Paul Bakkera503a632013-08-14 13:48:06 +0200350#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200351
Paul Bakker0be444a2013-08-27 21:55:01 +0200352#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200353/*
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200354 * Wrapper around f_sni, allowing use of ssl_set_own_cert() but
355 * making it act on ssl->hanshake->sni_key_cert instead.
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200356 */
357static int ssl_sni_wrapper( ssl_context *ssl,
358 const unsigned char* name, size_t len )
359{
360 int ret;
361 ssl_key_cert *key_cert_ori = ssl->key_cert;
362
363 ssl->key_cert = NULL;
364 ret = ssl->f_sni( ssl->p_sni, ssl, name, len );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200365 ssl->handshake->sni_key_cert = ssl->key_cert;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200366
367 ssl->key_cert = key_cert_ori;
368
369 return( ret );
370}
371
Paul Bakker5701cdc2012-09-27 21:49:42 +0000372static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000373 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000374 size_t len )
375{
376 int ret;
377 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000378 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000379
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100380 SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
381
Paul Bakker5701cdc2012-09-27 21:49:42 +0000382 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
383 if( servername_list_size + 2 != len )
384 {
385 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
386 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
387 }
388
389 p = buf + 2;
390 while( servername_list_size > 0 )
391 {
392 hostname_len = ( ( p[1] << 8 ) | p[2] );
393 if( hostname_len + 3 > servername_list_size )
394 {
395 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
396 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
397 }
398
399 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
400 {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200401 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000402 if( ret != 0 )
403 {
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100404 SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000405 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
406 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
407 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
408 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000409 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000410 }
411
412 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000413 p += hostname_len + 3;
414 }
415
416 if( servername_list_size != 0 )
417 {
418 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
419 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000420 }
421
422 return( 0 );
423}
Paul Bakker0be444a2013-08-27 21:55:01 +0200424#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000425
Paul Bakker48916f92012-09-16 19:57:18 +0000426static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000427 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000428 size_t len )
429{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000430 int ret;
431
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100432#if defined(POLARSSL_SSL_RENEGOTIATION)
433 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
434 {
435 /* Check verify-data in constant-time. The length OTOH is no secret */
436 if( len != 1 + ssl->verify_data_len ||
437 buf[0] != ssl->verify_data_len ||
438 safer_memcmp( buf + 1, ssl->peer_verify_data,
439 ssl->verify_data_len ) != 0 )
440 {
441 SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
442
443 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
444 return( ret );
445
446 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
447 }
448 }
449 else
450#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +0000451 {
452 if( len != 1 || buf[0] != 0x0 )
453 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100454 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000455
456 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
457 return( ret );
458
Paul Bakker48916f92012-09-16 19:57:18 +0000459 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
460 }
461
462 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
463 }
Paul Bakker48916f92012-09-16 19:57:18 +0000464
465 return( 0 );
466}
467
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100468#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
469 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakker23f36802012-09-28 14:15:14 +0000470static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
471 const unsigned char *buf,
472 size_t len )
473{
474 size_t sig_alg_list_size;
475 const unsigned char *p;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200476 const unsigned char *end = buf + len;
477 const int *md_cur;
478
Paul Bakker23f36802012-09-28 14:15:14 +0000479
480 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
481 if( sig_alg_list_size + 2 != len ||
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200482 sig_alg_list_size % 2 != 0 )
Paul Bakker23f36802012-09-28 14:15:14 +0000483 {
484 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
485 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
486 }
487
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200488 /*
489 * For now, ignore the SignatureAlgorithm part and rely on offered
490 * ciphersuites only for that part. To be fixed later.
491 *
492 * So, just look at the HashAlgorithm part.
493 */
494 for( md_cur = md_list(); *md_cur != POLARSSL_MD_NONE; md_cur++ ) {
495 for( p = buf + 2; p < end; p += 2 ) {
496 if( *md_cur == (int) ssl_md_alg_from_hash( p[0] ) ) {
497 ssl->handshake->sig_alg = p[0];
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200498 goto have_sig_alg;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200499 }
Paul Bakker23f36802012-09-28 14:15:14 +0000500 }
Paul Bakker23f36802012-09-28 14:15:14 +0000501 }
502
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200503 /* Some key echanges do not need signatures at all */
504 SSL_DEBUG_MSG( 3, ( "no signature_algorithm in common" ) );
505 return( 0 );
506
507have_sig_alg:
Paul Bakker23f36802012-09-28 14:15:14 +0000508 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
509 ssl->handshake->sig_alg ) );
510
511 return( 0 );
512}
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100513#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
514 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker23f36802012-09-28 14:15:14 +0000515
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200516#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200517static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
518 const unsigned char *buf,
519 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100520{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200521 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100522 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200523 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100524
525 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
526 if( list_size + 2 != len ||
527 list_size % 2 != 0 )
528 {
529 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
530 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
531 }
532
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200533 /* Should never happen unless client duplicates the extension */
534 if( ssl->handshake->curves != NULL )
535 {
536 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
537 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
538 }
539
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +0100540 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200541 * and leave room for a final 0 */
542 our_size = list_size / 2 + 1;
543 if( our_size > POLARSSL_ECP_DP_MAX )
544 our_size = POLARSSL_ECP_DP_MAX;
545
546 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
547 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
548
Paul Bakker9af723c2014-05-01 13:03:14 +0200549 /* explicit void pointer cast for buggy MS compiler */
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200550 memset( (void *) curves, 0, our_size * sizeof( *curves ) );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200551 ssl->handshake->curves = curves;
552
Paul Bakker41c83d32013-03-20 14:39:14 +0100553 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200554 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100555 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200556 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200557
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200558 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100559 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200560 *curves++ = curve_info;
561 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100562 }
563
564 list_size -= 2;
565 p += 2;
566 }
567
568 return( 0 );
569}
570
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200571static int ssl_parse_supported_point_formats( ssl_context *ssl,
572 const unsigned char *buf,
573 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100574{
575 size_t list_size;
576 const unsigned char *p;
577
578 list_size = buf[0];
579 if( list_size + 1 != len )
580 {
581 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
582 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
583 }
584
Manuel Pégourié-Gonnarda701d2f2015-09-16 11:32:18 +0200585 p = buf + 1;
Paul Bakker41c83d32013-03-20 14:39:14 +0100586 while( list_size > 0 )
587 {
588 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
589 p[0] == POLARSSL_ECP_PF_COMPRESSED )
590 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200591 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200592 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100593 return( 0 );
594 }
595
596 list_size--;
597 p++;
598 }
599
600 return( 0 );
601}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200602#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100603
Paul Bakker05decb22013-08-15 13:33:48 +0200604#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200605static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
606 const unsigned char *buf,
607 size_t len )
608{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200609 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200610 {
611 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
612 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
613 }
614
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200615 ssl->session_negotiate->mfl_code = buf[0];
616
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200617 return( 0 );
618}
Paul Bakker05decb22013-08-15 13:33:48 +0200619#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200620
Paul Bakker1f2bc622013-08-15 13:45:55 +0200621#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200622static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
623 const unsigned char *buf,
624 size_t len )
625{
626 if( len != 0 )
627 {
628 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
629 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
630 }
631
632 ((void) buf);
633
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100634 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
635 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200636
637 return( 0 );
638}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200639#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200640
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100641#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
642static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
643 const unsigned char *buf,
644 size_t len )
645{
646 if( len != 0 )
647 {
648 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
649 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
650 }
651
652 ((void) buf);
653
654 if( ssl->encrypt_then_mac == SSL_ETM_ENABLED &&
655 ssl->minor_ver != SSL_MINOR_VERSION_0 )
656 {
657 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
658 }
659
660 return( 0 );
661}
662#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
663
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200664#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
665static int ssl_parse_extended_ms_ext( ssl_context *ssl,
666 const unsigned char *buf,
667 size_t len )
668{
669 if( len != 0 )
670 {
671 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
672 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
673 }
674
675 ((void) buf);
676
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200677 if( ssl->extended_ms == SSL_EXTENDED_MS_ENABLED &&
678 ssl->minor_ver != SSL_MINOR_VERSION_0 )
679 {
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200680 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200681 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200682
683 return( 0 );
684}
685#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
686
Paul Bakkera503a632013-08-14 13:48:06 +0200687#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200688static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200689 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200690 size_t len )
691{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200692 int ret;
693
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200694 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
695 return( 0 );
696
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200697 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200698 ssl->handshake->new_session_ticket = 1;
699
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200700 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
701
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200702 if( len == 0 )
703 return( 0 );
704
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100705#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200706 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
707 {
708 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
709 return( 0 );
710 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100711#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200712
713 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200714 * Failures are ok: just ignore the ticket and proceed.
715 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200716 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
717 {
718 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200719 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200720 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200721
722 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
723
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200724 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200725
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200726 /* Don't send a new ticket after all, this one is OK */
727 ssl->handshake->new_session_ticket = 0;
728
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200729 return( 0 );
730}
Paul Bakkera503a632013-08-14 13:48:06 +0200731#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200732
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200733#if defined(POLARSSL_SSL_ALPN)
734static int ssl_parse_alpn_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200735 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200736{
Paul Bakker14b16c62014-05-28 11:33:54 +0200737 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200738 const unsigned char *theirs, *start, *end;
739 const char **ours;
740
741 /* If ALPN not configured, just ignore the extension */
742 if( ssl->alpn_list == NULL )
743 return( 0 );
744
745 /*
746 * opaque ProtocolName<1..2^8-1>;
747 *
748 * struct {
749 * ProtocolName protocol_name_list<2..2^16-1>
750 * } ProtocolNameList;
751 */
752
753 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
754 if( len < 4 )
755 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
756
757 list_len = ( buf[0] << 8 ) | buf[1];
758 if( list_len != len - 2 )
759 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
760
761 /*
762 * Use our order of preference
763 */
764 start = buf + 2;
765 end = buf + len;
766 for( ours = ssl->alpn_list; *ours != NULL; ours++ )
767 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200768 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200769 for( theirs = start; theirs != end; theirs += cur_len )
770 {
771 /* If the list is well formed, we should get equality first */
772 if( theirs > end )
773 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
774
775 cur_len = *theirs++;
776
777 /* Empty strings MUST NOT be included */
778 if( cur_len == 0 )
779 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
780
Paul Bakker14b16c62014-05-28 11:33:54 +0200781 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200782 memcmp( theirs, *ours, cur_len ) == 0 )
783 {
784 ssl->alpn_chosen = *ours;
785 return( 0 );
786 }
787 }
788 }
789
790 /* If we get there, no match was found */
791 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
792 SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
793 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
794}
795#endif /* POLARSSL_SSL_ALPN */
796
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100797/*
798 * Auxiliary functions for ServerHello parsing and related actions
799 */
800
801#if defined(POLARSSL_X509_CRT_PARSE_C)
802/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100803 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100804 */
805#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100806static int ssl_check_key_curve( pk_context *pk,
807 const ecp_curve_info **curves )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100808{
809 const ecp_curve_info **crv = curves;
810 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
811
812 while( *crv != NULL )
813 {
814 if( (*crv)->grp_id == grp_id )
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100815 return( 0 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100816 crv++;
817 }
818
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100819 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100820}
821#endif /* POLARSSL_ECDSA_C */
822
823/*
824 * Try picking a certificate for this ciphersuite,
825 * return 0 on success and -1 on failure.
826 */
827static int ssl_pick_cert( ssl_context *ssl,
828 const ssl_ciphersuite_t * ciphersuite_info )
829{
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100830 ssl_key_cert *cur, *list, *fallback = NULL;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100831 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +0200832 int flags;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100833
834#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
835 if( ssl->handshake->sni_key_cert != NULL )
836 list = ssl->handshake->sni_key_cert;
837 else
838#endif
839 list = ssl->handshake->key_cert;
840
841 if( pk_alg == POLARSSL_PK_NONE )
842 return( 0 );
843
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000844 SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) );
845
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100846 for( cur = list; cur != NULL; cur = cur->next )
847 {
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000848 SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate",
849 cur->cert );
850
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100851 if( ! pk_can_do( cur->key, pk_alg ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000852 {
853 SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100854 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000855 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100856
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200857 /*
858 * This avoids sending the client a cert it'll reject based on
859 * keyUsage or other extensions.
860 *
861 * It also allows the user to provision different certificates for
862 * different uses based on keyUsage, eg if they want to avoid signing
863 * and decrypting with the same RSA key.
864 */
865 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +0200866 SSL_IS_SERVER, &flags ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200867 {
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000868 SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
869 "(extended) key usage extension" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200870 continue;
871 }
872
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100873#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100874 if( pk_alg == POLARSSL_PK_ECDSA &&
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100875 ssl_check_key_curve( cur->key, ssl->handshake->curves ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000876 {
877 SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) );
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100878 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000879 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100880#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100881
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100882 /*
883 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
884 * present them a SHA-higher cert rather than failing if it's the only
885 * one we got that satisfies the other conditions.
886 */
887 if( ssl->minor_ver < SSL_MINOR_VERSION_3 &&
888 cur->cert->sig_md != POLARSSL_MD_SHA1 )
889 {
890 if( fallback == NULL )
891 fallback = cur;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000892 {
893 SSL_DEBUG_MSG( 3, ( "certificate not preferred: "
894 "sha-2 with pre-TLS 1.2 client" ) );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100895 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000896 }
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100897 }
898
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100899 /* If we get there, we got a winner */
900 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100901 }
902
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000903 if( cur == NULL )
904 cur = fallback;
905
906
907 /* Do not update ssl->handshake->key_cert unless the is a match */
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100908 if( cur != NULL )
909 {
910 ssl->handshake->key_cert = cur;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000911 SSL_DEBUG_CRT( 3, "selected certificate chain, certificate",
912 ssl->handshake->key_cert->cert );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100913 return( 0 );
914 }
915
916 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100917}
918#endif /* POLARSSL_X509_CRT_PARSE_C */
919
920/*
921 * Check if a given ciphersuite is suitable for use with our config/keys/etc
922 * Sets ciphersuite_info only if the suite matches.
923 */
924static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
925 const ssl_ciphersuite_t **ciphersuite_info )
926{
927 const ssl_ciphersuite_t *suite_info;
928
929 suite_info = ssl_ciphersuite_from_id( suite_id );
930 if( suite_info == NULL )
931 {
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100932 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
933 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100934 }
935
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000936 SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %s", suite_info->name ) );
937
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100938 if( suite_info->min_minor_ver > ssl->minor_ver ||
939 suite_info->max_minor_ver < ssl->minor_ver )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000940 {
941 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100942 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000943 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100944
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100945 if( ssl->arc4_disabled == SSL_ARC4_DISABLED &&
946 suite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000947 {
948 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100949 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000950 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100951
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100952#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
953 if( ssl_ciphersuite_uses_ec( suite_info ) &&
954 ( ssl->handshake->curves == NULL ||
955 ssl->handshake->curves[0] == NULL ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000956 {
957 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
958 "no common elliptic curve" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100959 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000960 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100961#endif
962
963#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
964 /* If the ciphersuite requires a pre-shared key and we don't
965 * have one, skip it now rather than failing later */
966 if( ssl_ciphersuite_uses_psk( suite_info ) &&
967 ssl->f_psk == NULL &&
968 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
969 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000970 {
971 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100972 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000973 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100974#endif
975
976#if defined(POLARSSL_X509_CRT_PARSE_C)
977 /*
978 * Final check: if ciphersuite requires us to have a
979 * certificate/key of a particular type:
980 * - select the appropriate certificate if we have one, or
981 * - try the next ciphersuite if we don't
982 * This must be done last since we modify the key_cert list.
983 */
984 if( ssl_pick_cert( ssl, suite_info ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000985 {
986 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
987 "no suitable certificate" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100988 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000989 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100990#endif
991
992 *ciphersuite_info = suite_info;
993 return( 0 );
994}
995
Paul Bakker78a8c712013-03-06 17:01:52 +0100996#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
997static int ssl_parse_client_hello_v2( ssl_context *ssl )
998{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +0100999 int ret, got_common_suite;
Paul Bakker78a8c712013-03-06 17:01:52 +01001000 unsigned int i, j;
1001 size_t n;
1002 unsigned int ciph_len, sess_len, chal_len;
1003 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001004 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +02001005 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001006
1007 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
1008
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001009#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker78a8c712013-03-06 17:01:52 +01001010 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
1011 {
1012 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
1013
1014 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1015 return( ret );
1016
1017 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1018 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001019#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001020
1021 buf = ssl->in_hdr;
1022
1023 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1024
1025 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
1026 buf[2] ) );
1027 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
1028 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
1029 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
1030 buf[3], buf[4] ) );
1031
1032 /*
1033 * SSLv2 Client Hello
1034 *
1035 * Record layer:
1036 * 0 . 1 message length
1037 *
1038 * SSL layer:
1039 * 2 . 2 message type
1040 * 3 . 4 protocol version
1041 */
1042 if( buf[2] != SSL_HS_CLIENT_HELLO ||
1043 buf[3] != SSL_MAJOR_VERSION_3 )
1044 {
1045 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1046 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1047 }
1048
1049 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
1050
1051 if( n < 17 || n > 512 )
1052 {
1053 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1054 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1055 }
1056
1057 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +02001058 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
1059 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +01001060
1061 if( ssl->minor_ver < ssl->min_minor_ver )
1062 {
1063 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001064 " [%d:%d] < [%d:%d]",
1065 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +01001066 ssl->min_major_ver, ssl->min_minor_ver ) );
1067
1068 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1069 SSL_ALERT_MSG_PROTOCOL_VERSION );
1070 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1071 }
1072
Paul Bakker2fbefde2013-06-29 16:01:15 +02001073 ssl->handshake->max_major_ver = buf[3];
1074 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +01001075
1076 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
1077 {
1078 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1079 return( ret );
1080 }
1081
1082 ssl->handshake->update_checksum( ssl, buf + 2, n );
1083
1084 buf = ssl->in_msg;
1085 n = ssl->in_left - 5;
1086
1087 /*
1088 * 0 . 1 ciphersuitelist length
1089 * 2 . 3 session id length
1090 * 4 . 5 challenge length
1091 * 6 . .. ciphersuitelist
1092 * .. . .. session id
1093 * .. . .. challenge
1094 */
1095 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1096
1097 ciph_len = ( buf[0] << 8 ) | buf[1];
1098 sess_len = ( buf[2] << 8 ) | buf[3];
1099 chal_len = ( buf[4] << 8 ) | buf[5];
1100
1101 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
1102 ciph_len, sess_len, chal_len ) );
1103
1104 /*
1105 * Make sure each parameter length is valid
1106 */
1107 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1108 {
1109 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1110 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1111 }
1112
1113 if( sess_len > 32 )
1114 {
1115 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1116 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1117 }
1118
1119 if( chal_len < 8 || chal_len > 32 )
1120 {
1121 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1122 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1123 }
1124
1125 if( n != 6 + ciph_len + sess_len + chal_len )
1126 {
1127 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1128 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1129 }
1130
1131 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1132 buf + 6, ciph_len );
1133 SSL_DEBUG_BUF( 3, "client hello, session id",
1134 buf + 6 + ciph_len, sess_len );
1135 SSL_DEBUG_BUF( 3, "client hello, challenge",
1136 buf + 6 + ciph_len + sess_len, chal_len );
1137
1138 p = buf + 6 + ciph_len;
1139 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001140 memset( ssl->session_negotiate->id, 0,
1141 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001142 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1143
1144 p += sess_len;
1145 memset( ssl->handshake->randbytes, 0, 64 );
1146 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1147
1148 /*
1149 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1150 */
1151 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1152 {
1153 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1154 {
1155 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001156#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker78a8c712013-03-06 17:01:52 +01001157 if( ssl->renegotiation == SSL_RENEGOTIATION )
1158 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001159 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
1160 "during renegotiation" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001161
1162 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1163 return( ret );
1164
1165 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1166 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001167#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001168 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1169 break;
1170 }
1171 }
1172
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001173#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1174 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1175 {
1176 if( p[0] == 0 &&
1177 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1178 p[2] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1179 {
1180 SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
1181
1182 if( ssl->minor_ver < ssl->max_minor_ver )
1183 {
1184 SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
1185
1186 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1187 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1188
1189 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1190 }
1191
1192 break;
1193 }
1194 }
1195#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1196
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001197 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001198 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001199 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001200#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1201 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1202 {
1203 for( i = 0; ciphersuites[i] != 0; i++ )
1204#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001205 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001206 {
1207 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001208#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001209 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001210 if( p[0] != 0 ||
1211 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1212 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1213 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001214
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001215 got_common_suite = 1;
1216
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001217 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1218 &ciphersuite_info ) ) != 0 )
1219 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001220
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001221 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001222 goto have_ciphersuite_v2;
1223 }
1224 }
1225
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001226 if( got_common_suite )
1227 {
1228 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1229 "but none of them usable" ) );
1230 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
1231 }
1232 else
1233 {
1234 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1235 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1236 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001237
1238have_ciphersuite_v2:
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001239 SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
1240
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001241 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001242 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001243 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001244
1245 /*
1246 * SSLv2 Client Hello relevant renegotiation security checks
1247 */
1248 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1249 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1250 {
1251 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1252
1253 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1254 return( ret );
1255
1256 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1257 }
1258
1259 ssl->in_left = 0;
1260 ssl->state++;
1261
1262 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1263
1264 return( 0 );
1265}
1266#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1267
Paul Bakker5121ce52009-01-03 21:22:43 +00001268static int ssl_parse_client_hello( ssl_context *ssl )
1269{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001270 int ret, got_common_suite;
Paul Bakker23986e52011-04-24 08:57:21 +00001271 unsigned int i, j;
1272 size_t n;
1273 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001274 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001275 unsigned int ext_len = 0;
1276 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001277#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001278 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001279#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001280 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001281 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001282 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001283
1284 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1285
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001286#if defined(POLARSSL_SSL_RENEGOTIATION)
1287 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1288#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001289 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001290 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
1291 {
1292 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1293 return( ret );
1294 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001295 }
1296
1297 buf = ssl->in_hdr;
1298
Paul Bakker78a8c712013-03-06 17:01:52 +01001299#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1300 if( ( buf[0] & 0x80 ) != 0 )
1301 return ssl_parse_client_hello_v2( ssl );
1302#endif
1303
Paul Bakkerec636f32012-09-09 19:17:02 +00001304 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1305
1306 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1307 buf[0] ) );
1308 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1309 ( buf[3] << 8 ) | buf[4] ) );
1310 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
1311 buf[1], buf[2] ) );
1312
1313 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001314 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001315 *
1316 * Record layer:
1317 * 0 . 0 message type
1318 * 1 . 2 protocol version
1319 * 3 . 4 message length
1320 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001321
1322 /* According to RFC 5246 Appendix E.1, the version here is typically
1323 * "{03,00}, the lowest version number supported by the client, [or] the
1324 * value of ClientHello.client_version", so the only meaningful check here
1325 * is the major version shouldn't be less than 3 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001326 if( buf[0] != SSL_MSG_HANDSHAKE ||
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001327 buf[1] < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001328 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001329 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1330 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1331 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001332
Paul Bakkerec636f32012-09-09 19:17:02 +00001333 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +00001334
Paul Bakker4f42c112014-04-17 14:48:23 +02001335 if( n < 45 || n > SSL_MAX_CONTENT_LEN )
Paul Bakkerec636f32012-09-09 19:17:02 +00001336 {
1337 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1338 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1339 }
1340
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001341#if defined(POLARSSL_SSL_RENEGOTIATION)
1342 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1343#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001344 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001345 if( ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
1346 {
1347 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1348 return( ret );
1349 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001350 }
1351
1352 buf = ssl->in_msg;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001353#if defined(POLARSSL_SSL_RENEGOTIATION)
1354 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001355 n = ssl->in_msglen;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001356 else
1357#endif
1358 n = ssl->in_left - 5;
Paul Bakkerec636f32012-09-09 19:17:02 +00001359
Paul Bakker48916f92012-09-16 19:57:18 +00001360 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +00001361
1362 /*
1363 * SSL layer:
1364 * 0 . 0 handshake type
1365 * 1 . 3 handshake length
1366 * 4 . 5 protocol version
1367 * 6 . 9 UNIX time()
1368 * 10 . 37 random bytes
1369 * 38 . 38 session id length
1370 * 39 . 38+x session id
1371 * 39+x . 40+x ciphersuitelist length
Paul Bakker0f651c72014-05-22 15:12:19 +02001372 * 41+x . 40+y ciphersuitelist
1373 * 41+y . 41+y compression alg length
1374 * 42+y . 41+z compression algs
Paul Bakkerec636f32012-09-09 19:17:02 +00001375 * .. . .. extensions
1376 */
1377 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1378
1379 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1380 buf[0] ) );
1381 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1382 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1383 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1384 buf[4], buf[5] ) );
1385
1386 /*
1387 * Check the handshake type and protocol version
1388 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001389 if( buf[0] != SSL_HS_CLIENT_HELLO )
Paul Bakkerec636f32012-09-09 19:17:02 +00001390 {
1391 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1392 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1393 }
1394
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001395 ssl->major_ver = buf[4];
1396 ssl->minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001397
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001398 ssl->handshake->max_major_ver = ssl->major_ver;
1399 ssl->handshake->max_minor_ver = ssl->minor_ver;
1400
1401 if( ssl->major_ver < ssl->min_major_ver ||
1402 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001403 {
1404 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001405 " [%d:%d] < [%d:%d]",
1406 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001407 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001408
1409 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1410 SSL_ALERT_MSG_PROTOCOL_VERSION );
1411
1412 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1413 }
1414
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001415 if( ssl->major_ver > ssl->max_major_ver )
1416 {
1417 ssl->major_ver = ssl->max_major_ver;
1418 ssl->minor_ver = ssl->max_minor_ver;
1419 }
1420 else if( ssl->minor_ver > ssl->max_minor_ver )
1421 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001422
Paul Bakker48916f92012-09-16 19:57:18 +00001423 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001424
1425 /*
1426 * Check the handshake message length
1427 */
1428 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1429 {
1430 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1431 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1432 }
1433
1434 /*
1435 * Check the session length
1436 */
1437 sess_len = buf[38];
1438
Paul Bakker0f651c72014-05-22 15:12:19 +02001439 if( sess_len > 32 || sess_len > n - 42 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001440 {
1441 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1442 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1443 }
1444
Paul Bakker48916f92012-09-16 19:57:18 +00001445 ssl->session_negotiate->length = sess_len;
1446 memset( ssl->session_negotiate->id, 0,
1447 sizeof( ssl->session_negotiate->id ) );
1448 memcpy( ssl->session_negotiate->id, buf + 39,
1449 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001450
1451 /*
1452 * Check the ciphersuitelist length
1453 */
1454 ciph_len = ( buf[39 + sess_len] << 8 )
1455 | ( buf[40 + sess_len] );
1456
Paul Bakker0f651c72014-05-22 15:12:19 +02001457 if( ciph_len < 2 || ( ciph_len % 2 ) != 0 || ciph_len > n - 42 - sess_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001458 {
1459 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1460 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1461 }
1462
1463 /*
1464 * Check the compression algorithms length
1465 */
1466 comp_len = buf[41 + sess_len + ciph_len];
1467
Paul Bakker0f651c72014-05-22 15:12:19 +02001468 if( comp_len < 1 || comp_len > 16 ||
1469 comp_len > n - 42 - sess_len - ciph_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001470 {
1471 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1472 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1473 }
1474
Paul Bakker48916f92012-09-16 19:57:18 +00001475 /*
1476 * Check the extension length
1477 */
1478 if( n > 42 + sess_len + ciph_len + comp_len )
1479 {
1480 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1481 | ( buf[43 + sess_len + ciph_len + comp_len] );
1482
1483 if( ( ext_len > 0 && ext_len < 4 ) ||
1484 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1485 {
1486 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1487 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1488 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1489 }
1490 }
1491
1492 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001493#if defined(POLARSSL_ZLIB_SUPPORT)
1494 for( i = 0; i < comp_len; ++i )
1495 {
Paul Bakker48916f92012-09-16 19:57:18 +00001496 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001497 {
Paul Bakker48916f92012-09-16 19:57:18 +00001498 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001499 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001500 }
1501 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001502#endif
1503
Paul Bakkerec636f32012-09-09 19:17:02 +00001504 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1505 buf + 6, 32 );
1506 SSL_DEBUG_BUF( 3, "client hello, session id",
1507 buf + 38, sess_len );
1508 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1509 buf + 41 + sess_len, ciph_len );
1510 SSL_DEBUG_BUF( 3, "client hello, compression",
1511 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001512
Paul Bakkerec636f32012-09-09 19:17:02 +00001513 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001514 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1515 */
1516 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1517 {
1518 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1519 {
1520 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001521#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001522 if( ssl->renegotiation == SSL_RENEGOTIATION )
1523 {
1524 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001525
1526 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1527 return( ret );
1528
Paul Bakker48916f92012-09-16 19:57:18 +00001529 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1530 }
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001531 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001532#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00001533 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1534 break;
1535 }
1536 }
1537
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001538#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1539 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1540 {
1541 if( p[0] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1542 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1543 {
1544 SSL_DEBUG_MSG( 0, ( "received FALLBACK_SCSV" ) );
1545
1546 if( ssl->minor_ver < ssl->max_minor_ver )
1547 {
1548 SSL_DEBUG_MSG( 0, ( "inapropriate fallback" ) );
1549
1550 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1551 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1552
1553 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1554 }
1555
1556 break;
1557 }
1558 }
1559#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1560
Paul Bakker48916f92012-09-16 19:57:18 +00001561 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001562
1563 while( ext_len )
1564 {
1565 unsigned int ext_id = ( ( ext[0] << 8 )
1566 | ( ext[1] ) );
1567 unsigned int ext_size = ( ( ext[2] << 8 )
1568 | ( ext[3] ) );
1569
1570 if( ext_size + 4 > ext_len )
1571 {
1572 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1573 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1574 }
1575 switch( ext_id )
1576 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001577#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001578 case TLS_EXT_SERVERNAME:
1579 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1580 if( ssl->f_sni == NULL )
1581 break;
1582
1583 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1584 if( ret != 0 )
1585 return( ret );
1586 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001587#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001588
Paul Bakker48916f92012-09-16 19:57:18 +00001589 case TLS_EXT_RENEGOTIATION_INFO:
1590 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001591#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001592 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001593#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001594
Paul Bakker23f36802012-09-28 14:15:14 +00001595 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1596 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001597 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001598 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001599
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001600#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1601 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakker23f36802012-09-28 14:15:14 +00001602 case TLS_EXT_SIG_ALG:
1603 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001604#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker23f36802012-09-28 14:15:14 +00001605 if( ssl->renegotiation == SSL_RENEGOTIATION )
1606 break;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001607#endif
Paul Bakker23f36802012-09-28 14:15:14 +00001608
1609 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1610 if( ret != 0 )
1611 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001612 break;
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001613#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
1614 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001615
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001616#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001617 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1618 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1619
1620 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1621 if( ret != 0 )
1622 return( ret );
1623 break;
1624
1625 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1626 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001627 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001628
1629 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1630 if( ret != 0 )
1631 return( ret );
1632 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001633#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001634
Paul Bakker05decb22013-08-15 13:33:48 +02001635#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001636 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1637 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1638
1639 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1640 if( ret != 0 )
1641 return( ret );
1642 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001643#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001644
Paul Bakker1f2bc622013-08-15 13:45:55 +02001645#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001646 case TLS_EXT_TRUNCATED_HMAC:
1647 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1648
1649 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1650 if( ret != 0 )
1651 return( ret );
1652 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001653#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001654
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001655#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1656 case TLS_EXT_ENCRYPT_THEN_MAC:
1657 SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
1658
1659 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1660 if( ret != 0 )
1661 return( ret );
1662 break;
1663#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1664
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001665#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1666 case TLS_EXT_EXTENDED_MASTER_SECRET:
1667 SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
1668
1669 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1670 if( ret != 0 )
1671 return( ret );
1672 break;
1673#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1674
Paul Bakkera503a632013-08-14 13:48:06 +02001675#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001676 case TLS_EXT_SESSION_TICKET:
1677 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1678
1679 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1680 if( ret != 0 )
1681 return( ret );
1682 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001683#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001684
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001685#if defined(POLARSSL_SSL_ALPN)
1686 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001687 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001688
1689 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1690 if( ret != 0 )
1691 return( ret );
1692 break;
1693#endif /* POLARSSL_SSL_SESSION_TICKETS */
1694
Paul Bakker48916f92012-09-16 19:57:18 +00001695 default:
1696 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1697 ext_id ) );
1698 }
1699
1700 ext_len -= 4 + ext_size;
1701 ext += 4 + ext_size;
1702
1703 if( ext_len > 0 && ext_len < 4 )
1704 {
1705 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1706 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1707 }
1708 }
1709
1710 /*
1711 * Renegotiation security checks
1712 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001713 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001714 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1715 {
1716 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1717 handshake_failure = 1;
1718 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001719#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001720 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1721 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1722 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001723 {
1724 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001725 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001726 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001727 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1728 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1729 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001730 {
1731 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001732 handshake_failure = 1;
1733 }
1734 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1735 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1736 renegotiation_info_seen == 1 )
1737 {
1738 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1739 handshake_failure = 1;
1740 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001741#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001742
1743 if( handshake_failure == 1 )
1744 {
1745 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1746 return( ret );
1747
Paul Bakker48916f92012-09-16 19:57:18 +00001748 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1749 }
Paul Bakker380da532012-04-18 16:10:25 +00001750
Paul Bakker41c83d32013-03-20 14:39:14 +01001751 /*
1752 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001753 * (At the end because we need information from the EC-based extensions
1754 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001755 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001756 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001757 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001758 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001759#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1760 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
1761 {
1762 for( i = 0; ciphersuites[i] != 0; i++ )
1763#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001764 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001765 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001766 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001767#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001768 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001769 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1770 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1771 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001772
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001773 got_common_suite = 1;
1774
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001775 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1776 &ciphersuite_info ) ) != 0 )
1777 return( ret );
1778
1779 if( ciphersuite_info != NULL )
1780 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001781 }
1782 }
1783
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001784 if( got_common_suite )
1785 {
1786 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1787 "but none of them usable" ) );
1788 ssl_send_fatal_handshake_failure( ssl );
1789 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
1790 }
1791 else
1792 {
1793 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1794 ssl_send_fatal_handshake_failure( ssl );
1795 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1796 }
Paul Bakker41c83d32013-03-20 14:39:14 +01001797
1798have_ciphersuite:
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001799 SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
1800
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001801 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001802 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1803 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1804
Paul Bakker5121ce52009-01-03 21:22:43 +00001805 ssl->in_left = 0;
1806 ssl->state++;
1807
1808 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1809
1810 return( 0 );
1811}
1812
Paul Bakker1f2bc622013-08-15 13:45:55 +02001813#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001814static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1815 unsigned char *buf,
1816 size_t *olen )
1817{
1818 unsigned char *p = buf;
1819
1820 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1821 {
1822 *olen = 0;
1823 return;
1824 }
1825
1826 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1827
1828 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1829 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1830
1831 *p++ = 0x00;
1832 *p++ = 0x00;
1833
1834 *olen = 4;
1835}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001836#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001837
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001838#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1839static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
1840 unsigned char *buf,
1841 size_t *olen )
1842{
1843 unsigned char *p = buf;
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01001844 const ssl_ciphersuite_t *suite = NULL;
1845 const cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001846
1847 if( ssl->session_negotiate->encrypt_then_mac == SSL_EXTENDED_MS_DISABLED ||
1848 ssl->minor_ver == SSL_MINOR_VERSION_0 )
1849 {
1850 *olen = 0;
1851 return;
1852 }
1853
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01001854 /*
1855 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
1856 * from a client and then selects a stream or Authenticated Encryption
1857 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
1858 * encrypt-then-MAC response extension back to the client."
1859 */
1860 if( ( suite = ssl_ciphersuite_from_id(
1861 ssl->session_negotiate->ciphersuite ) ) == NULL ||
1862 ( cipher = cipher_info_from_type( suite->cipher ) ) == NULL ||
1863 cipher->mode != POLARSSL_MODE_CBC )
1864 {
1865 *olen = 0;
1866 return;
1867 }
1868
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001869 SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
1870
1871 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
1872 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
1873
1874 *p++ = 0x00;
1875 *p++ = 0x00;
1876
1877 *olen = 4;
1878}
1879#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1880
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001881#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1882static void ssl_write_extended_ms_ext( ssl_context *ssl,
1883 unsigned char *buf,
1884 size_t *olen )
1885{
1886 unsigned char *p = buf;
1887
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +02001888 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_DISABLED ||
1889 ssl->minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001890 {
1891 *olen = 0;
1892 return;
1893 }
1894
1895 SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
1896 "extension" ) );
1897
1898 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
1899 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
1900
1901 *p++ = 0x00;
1902 *p++ = 0x00;
1903
1904 *olen = 4;
1905}
1906#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1907
Paul Bakkera503a632013-08-14 13:48:06 +02001908#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001909static void ssl_write_session_ticket_ext( ssl_context *ssl,
1910 unsigned char *buf,
1911 size_t *olen )
1912{
1913 unsigned char *p = buf;
1914
1915 if( ssl->handshake->new_session_ticket == 0 )
1916 {
1917 *olen = 0;
1918 return;
1919 }
1920
1921 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1922
1923 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1924 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1925
1926 *p++ = 0x00;
1927 *p++ = 0x00;
1928
1929 *olen = 4;
1930}
Paul Bakkera503a632013-08-14 13:48:06 +02001931#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001932
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001933static void ssl_write_renegotiation_ext( ssl_context *ssl,
1934 unsigned char *buf,
1935 size_t *olen )
1936{
1937 unsigned char *p = buf;
1938
1939 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1940 {
1941 *olen = 0;
1942 return;
1943 }
1944
1945 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1946
1947 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1948 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1949
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001950#if defined(POLARSSL_SSL_RENEGOTIATION)
1951 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
1952 {
1953 *p++ = 0x00;
1954 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1955 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001956
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001957 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1958 p += ssl->verify_data_len;
1959 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1960 p += ssl->verify_data_len;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001961
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001962 *olen = 5 + ssl->verify_data_len * 2;
1963 }
1964 else
1965#endif /* POLARSSL_SSL_RENEGOTIATION */
1966 {
1967 *p++ = 0x00;
1968 *p++ = 0x01;
1969 *p++ = 0x00;
1970
1971 *olen = 5;
1972 }
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001973}
1974
Paul Bakker05decb22013-08-15 13:33:48 +02001975#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001976static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1977 unsigned char *buf,
1978 size_t *olen )
1979{
1980 unsigned char *p = buf;
1981
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001982 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1983 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001984 *olen = 0;
1985 return;
1986 }
1987
1988 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1989
1990 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1991 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1992
1993 *p++ = 0x00;
1994 *p++ = 1;
1995
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001996 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001997
1998 *olen = 5;
1999}
Paul Bakker05decb22013-08-15 13:33:48 +02002000#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002001
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002002#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002003static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
2004 unsigned char *buf,
2005 size_t *olen )
2006{
2007 unsigned char *p = buf;
2008 ((void) ssl);
2009
Paul Bakker677377f2013-10-28 12:54:26 +01002010 if( ( ssl->handshake->cli_exts &
2011 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
2012 {
2013 *olen = 0;
2014 return;
2015 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002016
2017 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
2018
2019 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2020 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
2021
2022 *p++ = 0x00;
2023 *p++ = 2;
2024
2025 *p++ = 1;
2026 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
2027
2028 *olen = 6;
2029}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002030#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002031
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002032#if defined(POLARSSL_SSL_ALPN )
2033static void ssl_write_alpn_ext( ssl_context *ssl,
2034 unsigned char *buf, size_t *olen )
2035{
2036 if( ssl->alpn_chosen == NULL )
2037 {
2038 *olen = 0;
2039 return;
2040 }
2041
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02002042 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002043
2044 /*
2045 * 0 . 1 ext identifier
2046 * 2 . 3 ext length
2047 * 4 . 5 protocol list length
2048 * 6 . 6 protocol name length
2049 * 7 . 7+n protocol name
2050 */
2051 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
2052 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
2053
2054 *olen = 7 + strlen( ssl->alpn_chosen );
2055
2056 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2057 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2058
2059 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2060 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2061
2062 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2063
2064 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2065}
2066#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
2067
Paul Bakker5121ce52009-01-03 21:22:43 +00002068static int ssl_write_server_hello( ssl_context *ssl )
2069{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002070#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002071 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002072#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002073 int ret;
2074 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002075 unsigned char *buf, *p;
2076
2077 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
2078
Paul Bakkera9a028e2013-11-21 17:31:06 +01002079 if( ssl->f_rng == NULL )
2080 {
2081 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2082 return( POLARSSL_ERR_SSL_NO_RNG );
2083 }
2084
Paul Bakker5121ce52009-01-03 21:22:43 +00002085 /*
2086 * 0 . 0 handshake type
2087 * 1 . 3 handshake length
2088 * 4 . 5 protocol version
2089 * 6 . 9 UNIX time()
2090 * 10 . 37 random bytes
2091 */
2092 buf = ssl->out_msg;
2093 p = buf + 4;
2094
2095 *p++ = (unsigned char) ssl->major_ver;
2096 *p++ = (unsigned char) ssl->minor_ver;
2097
2098 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
2099 buf[4], buf[5] ) );
2100
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002101#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002102 t = time( NULL );
2103 *p++ = (unsigned char)( t >> 24 );
2104 *p++ = (unsigned char)( t >> 16 );
2105 *p++ = (unsigned char)( t >> 8 );
2106 *p++ = (unsigned char)( t );
2107
2108 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002109#else
2110 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
2111 return( ret );
2112
2113 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02002114#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002115
Paul Bakkera3d195c2011-11-27 21:07:34 +00002116 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
2117 return( ret );
2118
2119 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002120
Paul Bakker48916f92012-09-16 19:57:18 +00002121 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002122
2123 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
2124
2125 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002126 * Resume is 0 by default, see ssl_handshake_init().
2127 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2128 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002129 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002130 if( ssl->handshake->resume == 0 &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002131#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002132 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002133#endif
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002134 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002135 ssl->f_get_cache != NULL &&
2136 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
2137 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01002138 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002139 ssl->handshake->resume = 1;
2140 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002141
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002142 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002143 {
2144 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002145 * New session, create a new session id,
2146 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002147 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002148 ssl->state++;
2149
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002150#if defined(POLARSSL_HAVE_TIME)
2151 ssl->session_negotiate->start = time( NULL );
2152#endif
2153
Paul Bakkera503a632013-08-14 13:48:06 +02002154#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002155 if( ssl->handshake->new_session_ticket != 0 )
2156 {
2157 ssl->session_negotiate->length = n = 0;
2158 memset( ssl->session_negotiate->id, 0, 32 );
2159 }
2160 else
2161#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002162 {
2163 ssl->session_negotiate->length = n = 32;
2164 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002165 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002166 return( ret );
2167 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002168 }
2169 else
2170 {
2171 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002172 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002173 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02002174 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002175 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002176
2177 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2178 {
2179 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2180 return( ret );
2181 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002182 }
2183
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002184 /*
2185 * 38 . 38 session id length
2186 * 39 . 38+n session id
2187 * 39+n . 40+n chosen ciphersuite
2188 * 41+n . 41+n chosen compression alg.
2189 * 42+n . 43+n extensions length
2190 * 44+n . 43+n+m extensions
2191 */
2192 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00002193 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
2194 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002195
2196 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2197 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2198 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002199 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002200
Paul Bakker48916f92012-09-16 19:57:18 +00002201 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2202 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2203 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002204
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02002205 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2206 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02002207 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002208 ssl->session_negotiate->compression ) );
2209
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002210 /*
2211 * First write extensions, then the total length
2212 */
2213 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2214 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002215
Paul Bakker05decb22013-08-15 13:33:48 +02002216#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002217 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2218 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002219#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002220
Paul Bakker1f2bc622013-08-15 13:45:55 +02002221#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002222 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2223 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002224#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002225
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002226#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2227 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2228 ext_len += olen;
2229#endif
2230
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002231#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2232 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2233 ext_len += olen;
2234#endif
2235
Paul Bakkera503a632013-08-14 13:48:06 +02002236#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002237 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2238 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002239#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002240
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002241#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002242 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2243 ext_len += olen;
2244#endif
2245
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002246#if defined(POLARSSL_SSL_ALPN)
2247 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2248 ext_len += olen;
2249#endif
2250
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002251 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002252
Paul Bakkera7036632014-04-30 10:15:38 +02002253 if( ext_len > 0 )
2254 {
2255 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2256 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2257 p += ext_len;
2258 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002259
2260 ssl->out_msglen = p - buf;
2261 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2262 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
2263
2264 ret = ssl_write_record( ssl );
2265
2266 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2267
2268 return( ret );
2269}
2270
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002271#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2272 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002273 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2274 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002275static int ssl_write_certificate_request( ssl_context *ssl )
2276{
Paul Bakkered27a042013-04-18 22:46:23 +02002277 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002278
2279 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2280
2281 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002282 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002283 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2284 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002285 {
2286 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2287 ssl->state++;
2288 return( 0 );
2289 }
2290
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002291 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2292 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002293}
2294#else
2295static int ssl_write_certificate_request( ssl_context *ssl )
2296{
2297 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2298 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002299 size_t dn_size, total_dn_size; /* excluding length bytes */
2300 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002301 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002302 const x509_crt *crt;
Manuel Pégourié-Gonnardde9c8a52015-10-02 11:16:47 +02002303 const unsigned char * const end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00002304
2305 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2306
2307 ssl->state++;
2308
Paul Bakkerfbb17802013-04-17 19:10:21 +02002309 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002310 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002311 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002312 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002313 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002314 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002315 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002316 return( 0 );
2317 }
2318
2319 /*
2320 * 0 . 0 handshake type
2321 * 1 . 3 handshake length
2322 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002323 * 5 .. m-1 cert types
2324 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002325 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002326 * n .. n+1 length of all DNs
2327 * n+2 .. n+3 length of DN 1
2328 * n+4 .. ... Distinguished Name #1
2329 * ... .. ... length of DN 2, etc.
2330 */
2331 buf = ssl->out_msg;
2332 p = buf + 4;
2333
2334 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002335 * Supported certificate types
2336 *
2337 * ClientCertificateType certificate_types<1..2^8-1>;
2338 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002339 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002340 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002341
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002342#if defined(POLARSSL_RSA_C)
2343 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2344#endif
2345#if defined(POLARSSL_ECDSA_C)
2346 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2347#endif
2348
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002349 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002350 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002351
Paul Bakker577e0062013-08-28 11:57:20 +02002352 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002353#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002354 /*
2355 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002356 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002357 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2358 *
2359 * struct {
2360 * HashAlgorithm hash;
2361 * SignatureAlgorithm signature;
2362 * } SignatureAndHashAlgorithm;
2363 *
2364 * enum { (255) } HashAlgorithm;
2365 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002366 */
Paul Bakker21dca692013-01-03 11:41:08 +01002367 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002368 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002369 /*
2370 * Only use current running hash algorithm that is already required
2371 * for requested ciphersuite.
2372 */
Paul Bakker926af752012-11-23 13:38:07 +01002373 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2374
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002375 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2376 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002377 {
2378 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2379 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002380
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002381 /*
2382 * Supported signature algorithms
2383 */
2384#if defined(POLARSSL_RSA_C)
2385 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2386 p[2 + sa_len++] = SSL_SIG_RSA;
2387#endif
2388#if defined(POLARSSL_ECDSA_C)
2389 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2390 p[2 + sa_len++] = SSL_SIG_ECDSA;
2391#endif
Paul Bakker926af752012-11-23 13:38:07 +01002392
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002393 p[0] = (unsigned char)( sa_len >> 8 );
2394 p[1] = (unsigned char)( sa_len );
2395 sa_len += 2;
2396 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002397 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002398#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002399
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002400 /*
2401 * DistinguishedName certificate_authorities<0..2^16-1>;
2402 * opaque DistinguishedName<1..2^16-1>;
2403 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002404 p += 2;
2405 crt = ssl->ca_chain;
2406
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002407 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002408 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002409 {
Paul Bakker926af752012-11-23 13:38:07 +01002410 dn_size = crt->subject_raw.len;
Manuel Pégourié-Gonnardde9c8a52015-10-02 11:16:47 +02002411
2412 if( end < p || (size_t)( end - p ) < 2 + dn_size )
2413 {
2414 SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) );
2415 break;
2416 }
2417
Paul Bakker926af752012-11-23 13:38:07 +01002418 *p++ = (unsigned char)( dn_size >> 8 );
2419 *p++ = (unsigned char)( dn_size );
2420 memcpy( p, crt->subject_raw.p, dn_size );
2421 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002422
Paul Bakker926af752012-11-23 13:38:07 +01002423 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2424
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002425 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002426 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002427 }
2428
Paul Bakker926af752012-11-23 13:38:07 +01002429 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002430 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2431 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002432 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2433 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002434
2435 ret = ssl_write_record( ssl );
2436
2437 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2438
2439 return( ret );
2440}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002441#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2442 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002443 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2444 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002445
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002446#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2447 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2448static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2449{
2450 int ret;
2451
2452 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2453 {
2454 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2455 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2456 }
2457
2458 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2459 pk_ec( *ssl_own_key( ssl ) ),
2460 POLARSSL_ECDH_OURS ) ) != 0 )
2461 {
2462 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2463 return( ret );
2464 }
2465
2466 return( 0 );
2467}
2468#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2469 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2470
Paul Bakker41c83d32013-03-20 14:39:14 +01002471static int ssl_write_server_key_exchange( ssl_context *ssl )
2472{
Paul Bakker23986e52011-04-24 08:57:21 +00002473 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002474 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002475 const ssl_ciphersuite_t *ciphersuite_info =
2476 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002477
2478#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2479 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2480 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002481 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002482 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002483 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002484 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002485 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002486 ((void) dig_signed);
2487 ((void) dig_signed_len);
2488#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002489
Paul Bakker5121ce52009-01-03 21:22:43 +00002490 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2491
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002492#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2493 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2494 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002495 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2496 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2497 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002498 {
2499 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2500 ssl->state++;
2501 return( 0 );
2502 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002503#endif
2504
2505#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2506 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2507 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2508 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2509 {
2510 ssl_get_ecdh_params_from_cert( ssl );
2511
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002512 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002513 ssl->state++;
2514 return( 0 );
2515 }
2516#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002517
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002518#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2519 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2520 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2521 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002522 {
2523 /* TODO: Support identity hints */
2524 *(p++) = 0x00;
2525 *(p++) = 0x00;
2526
2527 n += 2;
2528 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002529#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2530 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002531
2532#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2533 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2534 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2535 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002536 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002537 /*
2538 * Ephemeral DH parameters:
2539 *
2540 * struct {
2541 * opaque dh_p<1..2^16-1>;
2542 * opaque dh_g<1..2^16-1>;
2543 * opaque dh_Ys<1..2^16-1>;
2544 * } ServerDHParams;
2545 */
2546 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2547 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2548 {
2549 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2550 return( ret );
2551 }
Paul Bakker48916f92012-09-16 19:57:18 +00002552
Paul Bakker41c83d32013-03-20 14:39:14 +01002553 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002554 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2555 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002556 {
2557 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2558 return( ret );
2559 }
2560
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002561 dig_signed = p;
2562 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002563
2564 p += len;
2565 n += len;
2566
Paul Bakker41c83d32013-03-20 14:39:14 +01002567 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2568 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2569 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2570 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2571 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002572#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2573 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002574
Gergely Budai987bfb52014-01-19 21:48:42 +01002575#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002576 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002577 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2578 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002579 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002580 /*
2581 * Ephemeral ECDH parameters:
2582 *
2583 * struct {
2584 * ECParameters curve_params;
2585 * ECPoint public;
2586 * } ServerECDHParams;
2587 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002588 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002589#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002590 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002591
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002592 /* Match our preference list against the offered curves */
2593 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2594 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2595 if( (*curve)->grp_id == *gid )
2596 goto curve_matching_done;
2597
2598curve_matching_done:
2599#else
2600 curve = ssl->handshake->curves;
2601#endif
2602
Manuel Pégourié-Gonnard8e8ae3d2015-06-23 18:57:28 +02002603 if( curve == NULL || *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002604 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002605 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2606 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002607 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002608
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002609 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002610
Paul Bakker41c83d32013-03-20 14:39:14 +01002611 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002612 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002613 {
2614 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2615 return( ret );
2616 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002617
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002618 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2619 p, SSL_MAX_CONTENT_LEN - n,
2620 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002621 {
2622 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2623 return( ret );
2624 }
2625
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002626 dig_signed = p;
2627 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002628
2629 p += len;
2630 n += len;
2631
Paul Bakker41c83d32013-03-20 14:39:14 +01002632 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2633 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002634#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002635
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002636#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002637 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2638 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002639 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002640 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2641 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002642 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002643 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002644 unsigned int hashlen = 0;
2645 unsigned char hash[64];
2646 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002647
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002648 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002649 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2650 */
Paul Bakker577e0062013-08-28 11:57:20 +02002651#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002652 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2653 {
2654 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2655
2656 if( md_alg == POLARSSL_MD_NONE )
2657 {
2658 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002659 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002660 }
2661 }
Paul Bakker577e0062013-08-28 11:57:20 +02002662 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002663#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002664#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2665 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002666 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002667 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2668 {
2669 md_alg = POLARSSL_MD_SHA1;
2670 }
2671 else
Paul Bakker577e0062013-08-28 11:57:20 +02002672#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002673 {
2674 md_alg = POLARSSL_MD_NONE;
2675 }
2676
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002677 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002678 * Compute the hash to be signed
2679 */
Paul Bakker577e0062013-08-28 11:57:20 +02002680#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2681 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002682 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002683 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002684 md5_context md5;
2685 sha1_context sha1;
2686
Paul Bakker5b4af392014-06-26 12:09:34 +02002687 md5_init( &md5 );
2688 sha1_init( &sha1 );
2689
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002690 /*
2691 * digitally-signed struct {
2692 * opaque md5_hash[16];
2693 * opaque sha_hash[20];
2694 * };
2695 *
2696 * md5_hash
2697 * MD5(ClientHello.random + ServerHello.random
2698 * + ServerParams);
2699 * sha_hash
2700 * SHA(ClientHello.random + ServerHello.random
2701 * + ServerParams);
2702 */
2703 md5_starts( &md5 );
2704 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002705 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002706 md5_finish( &md5, hash );
2707
2708 sha1_starts( &sha1 );
2709 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002710 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002711 sha1_finish( &sha1, hash + 16 );
2712
2713 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02002714
2715 md5_free( &md5 );
2716 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002717 }
2718 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002719#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2720 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002721#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2722 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002723 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002724 {
2725 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02002726 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002727
Paul Bakker84bbeb52014-07-01 14:53:22 +02002728 md_init( &ctx );
2729
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002730 /* Info from md_alg will be used instead */
2731 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002732
2733 /*
2734 * digitally-signed struct {
2735 * opaque client_random[32];
2736 * opaque server_random[32];
2737 * ServerDHParams params;
2738 * };
2739 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002740 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002741 {
2742 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2743 return( ret );
2744 }
2745
2746 md_starts( &ctx );
2747 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002748 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002749 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002750 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00002751 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002752 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002753#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2754 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002755 {
2756 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002757 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002758 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002759
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002760 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2761 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002762
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002763 /*
2764 * Make the signature
2765 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002766 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002767 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002768 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2769 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002770 }
Paul Bakker23f36802012-09-28 14:15:14 +00002771
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002772#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002773 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2774 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002775 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002776 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002777
2778 n += 2;
2779 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002780#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002781
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002782 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002783 p + 2 , &signature_len,
2784 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002785 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002786 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002787 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002788 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002789
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002790 *(p++) = (unsigned char)( signature_len >> 8 );
2791 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002792 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002793
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002794 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002795
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002796 p += signature_len;
2797 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002798 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002799#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002800 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2801 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002802
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002803 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002804 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2805 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2806
2807 ssl->state++;
2808
2809 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2810 {
2811 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2812 return( ret );
2813 }
2814
2815 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2816
2817 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002818}
2819
2820static int ssl_write_server_hello_done( ssl_context *ssl )
2821{
2822 int ret;
2823
2824 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2825
2826 ssl->out_msglen = 4;
2827 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2828 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2829
2830 ssl->state++;
2831
2832 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2833 {
2834 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2835 return( ret );
2836 }
2837
2838 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2839
2840 return( 0 );
2841}
2842
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002843#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2844 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2845static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2846 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002847{
2848 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002849 size_t n;
2850
2851 /*
2852 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2853 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002854 if( *p + 2 > end )
2855 {
2856 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2857 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2858 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002859
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002860 n = ( (*p)[0] << 8 ) | (*p)[1];
2861 *p += 2;
2862
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002863 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002864 {
2865 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2866 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2867 }
2868
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002869 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002870 {
2871 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2872 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2873 }
2874
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002875 *p += n;
2876
Paul Bakker70df2fb2013-04-17 17:19:09 +02002877 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2878
Paul Bakker70df2fb2013-04-17 17:19:09 +02002879 return( ret );
2880}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002881#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2882 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002883
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002884#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2885 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002886static int ssl_parse_encrypted_pms( ssl_context *ssl,
2887 const unsigned char *p,
2888 const unsigned char *end,
2889 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002890{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002891 int ret;
2892 size_t len = pk_get_len( ssl_own_key( ssl ) );
2893 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002894 unsigned char fake_pms[48], peer_pms[48];
2895 unsigned char mask;
Manuel Pégourié-Gonnardb26b75e2015-06-23 18:52:09 +02002896 size_t i, peer_pmslen;
2897 unsigned int diff;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002898
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002899 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002900 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002901 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002902 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2903 }
2904
2905 /*
2906 * Decrypt the premaster using own private RSA key
2907 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002908#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2909 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002910 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2911 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002912 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2913 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002914 {
2915 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2916 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2917 }
2918 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002919#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002920
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002921 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002922 {
2923 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2924 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2925 }
2926
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002927 /*
2928 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
2929 * must not cause the connection to end immediately; instead, send a
2930 * bad_record_mac later in the handshake.
2931 * Also, avoid data-dependant branches here to protect against
2932 * timing-based variants.
2933 */
2934 ret = ssl->f_rng( ssl->p_rng, fake_pms, sizeof( fake_pms ) );
2935 if( ret != 0 )
2936 return( ret );
2937
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002938 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02002939 peer_pms, &peer_pmslen,
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002940 sizeof( peer_pms ),
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002941 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002942
Manuel Pégourié-Gonnardb26b75e2015-06-23 18:52:09 +02002943 diff = (unsigned int) ret;
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02002944 diff |= peer_pmslen ^ 48;
2945 diff |= peer_pms[0] ^ ssl->handshake->max_major_ver;
2946 diff |= peer_pms[1] ^ ssl->handshake->max_minor_ver;
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002947
2948#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02002949 if( diff != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002950 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002951#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002952
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002953 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
2954 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
2955 {
2956 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2957 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002958 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002959 ssl->handshake->pmslen = 48;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002960
Manuel Pégourié-Gonnardb26b75e2015-06-23 18:52:09 +02002961 /* mask = diff ? 0xff : 0x00 */
2962 mask = - ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00002963 for( i = 0; i < ssl->handshake->pmslen; i++ )
2964 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
2965
2966 return( 0 );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002967}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002968#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2969 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002970
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002971#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002972static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2973 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002974{
Paul Bakker6db455e2013-09-18 17:29:31 +02002975 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002976 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002977
Paul Bakker6db455e2013-09-18 17:29:31 +02002978 if( ssl->f_psk == NULL &&
2979 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2980 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002981 {
2982 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2983 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2984 }
2985
2986 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002987 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002988 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002989 if( *p + 2 > end )
2990 {
2991 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2992 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2993 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002994
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002995 n = ( (*p)[0] << 8 ) | (*p)[1];
2996 *p += 2;
2997
2998 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002999 {
3000 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3001 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3002 }
3003
Paul Bakker6db455e2013-09-18 17:29:31 +02003004 if( ssl->f_psk != NULL )
3005 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003006 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003007 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3008 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003009 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003010 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003011 /* Identity is not a big secret since clients send it in the clear,
3012 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02003013 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003014 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003015 {
3016 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3017 }
3018 }
3019
3020 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003021 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003022 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02003023 if( ( ret = ssl_send_alert_message( ssl,
3024 SSL_ALERT_LEVEL_FATAL,
3025 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
3026 {
3027 return( ret );
3028 }
3029
3030 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003031 }
3032
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003033 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003034
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003035 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003036}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003037#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003038
Paul Bakker5121ce52009-01-03 21:22:43 +00003039static int ssl_parse_client_key_exchange( ssl_context *ssl )
3040{
Paul Bakker23986e52011-04-24 08:57:21 +00003041 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003042 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003043
Paul Bakker41c83d32013-03-20 14:39:14 +01003044 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003045
3046 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
3047
3048 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3049 {
3050 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3051 return( ret );
3052 }
3053
3054 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3055 {
3056 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003057 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003058 }
3059
3060 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
3061 {
3062 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003063 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003064 }
3065
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003066#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01003067 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003068 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003069 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003070 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003071
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003072 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003073 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02003074 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3075 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003076 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003077
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003078 if( p != end )
3079 {
3080 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3081 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3082 }
3083
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02003084 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003085
3086 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
3087 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003088 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02003089 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003090 {
3091 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
3092 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3093 }
3094
3095 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003096 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003097 else
3098#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003099#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003100 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3101 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3102 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003103 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003104 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
3105 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
3106 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003107 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003108 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003109 ssl->in_msg + 4, ssl->in_hslen - 4 ) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003110 {
3111 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3112 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3113 }
3114
3115 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3116
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003117 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
3118 &ssl->handshake->pmslen,
3119 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02003120 POLARSSL_MPI_MAX_SIZE,
3121 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003122 {
3123 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
3124 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3125 }
3126
3127 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003128 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003129 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003130#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003131 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3132 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3133 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003134#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
3135 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003136 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003137 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003138 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003139
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003140 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003141 {
3142 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3143 return( ret );
3144 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003145
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003146 if( p != end )
3147 {
3148 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3149 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3150 }
3151
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003152 if( ( ret = ssl_psk_derive_premaster( ssl,
3153 ciphersuite_info->key_exchange ) ) != 0 )
3154 {
3155 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3156 return( ret );
3157 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003158 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003159 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003160#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003161#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
3162 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
3163 {
3164 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003165 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003166
3167 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3168 {
3169 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3170 return( ret );
3171 }
3172
3173 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3174 {
3175 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
3176 return( ret );
3177 }
3178
3179 if( ( ret = ssl_psk_derive_premaster( ssl,
3180 ciphersuite_info->key_exchange ) ) != 0 )
3181 {
3182 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3183 return( ret );
3184 }
3185 }
3186 else
3187#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003188#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3189 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3190 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003191 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003192 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003193
3194 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3195 {
3196 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3197 return( ret );
3198 }
3199 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3200 {
3201 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3202 return( ret );
3203 }
3204
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003205 if( p != end )
3206 {
3207 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3208 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3209 }
3210
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003211 if( ( ret = ssl_psk_derive_premaster( ssl,
3212 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003213 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003214 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3215 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003216 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003217 }
3218 else
3219#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003220#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3221 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
3222 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003223 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003224 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003225
3226 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3227 {
3228 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3229 return( ret );
3230 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003231
3232 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
3233 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003234 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003235 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3236 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003237 }
3238
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003239 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3240
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003241 if( ( ret = ssl_psk_derive_premaster( ssl,
3242 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003243 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003244 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003245 return( ret );
3246 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003247 }
3248 else
3249#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003250#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
3251 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003252 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003253 if( ( ret = ssl_parse_encrypted_pms( ssl,
3254 ssl->in_msg + 4,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003255 ssl->in_msg + ssl->in_hslen,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003256 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003257 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003258 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003259 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003260 }
3261 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003262 else
3263#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
3264 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003265 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003266 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003267 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003268
Paul Bakkerff60ee62010-03-16 21:09:09 +00003269 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
3270 {
3271 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
3272 return( ret );
3273 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003274
Paul Bakker5121ce52009-01-03 21:22:43 +00003275 ssl->state++;
3276
3277 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3278
3279 return( 0 );
3280}
3281
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003282#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3283 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003284 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3285 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003286static int ssl_parse_certificate_verify( ssl_context *ssl )
3287{
Paul Bakkerfbb17802013-04-17 19:10:21 +02003288 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003289
3290 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3291
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003292 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003293 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003294 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003295 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02003296 {
3297 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3298 ssl->state++;
3299 return( 0 );
3300 }
3301
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003302 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3303 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003304}
3305#else
3306static int ssl_parse_certificate_verify( ssl_context *ssl )
3307{
3308 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003309 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003310 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003311 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003312 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003313#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003314 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003315#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003316 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003317 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3318
3319 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3320
3321 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003322 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003323 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003324 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3325 {
3326 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3327 ssl->state++;
3328 return( 0 );
3329 }
3330
Paul Bakkered27a042013-04-18 22:46:23 +02003331 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003332 {
3333 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3334 ssl->state++;
3335 return( 0 );
3336 }
3337
Paul Bakker48916f92012-09-16 19:57:18 +00003338 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003339
3340 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3341 {
3342 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3343 return( ret );
3344 }
3345
3346 ssl->state++;
3347
3348 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3349 {
3350 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003351 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003352 }
3353
3354 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
3355 {
3356 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003357 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003358 }
3359
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003360 /*
3361 * 0 . 0 handshake type
3362 * 1 . 3 handshake length
3363 * 4 . 5 sig alg (TLS 1.2 only)
3364 * 4+n . 5+n signature length (n = sa_len)
3365 * 6+n . 6+n+m signature (m = sig_len)
3366 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003367
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003368#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3369 defined(POLARSSL_SSL_PROTO_TLS1_1)
3370 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003371 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003372 sa_len = 0;
3373
Paul Bakkerc70b9822013-04-07 22:00:46 +02003374 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003375 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003376
3377 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3378 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3379 POLARSSL_PK_ECDSA ) )
3380 {
3381 hash_start += 16;
3382 hashlen -= 16;
3383 md_alg = POLARSSL_MD_SHA1;
3384 }
Paul Bakker926af752012-11-23 13:38:07 +01003385 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003386 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003387#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3388 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003389#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3390 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003391 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003392 sa_len = 2;
3393
Paul Bakker5121ce52009-01-03 21:22:43 +00003394 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003395 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003396 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003397 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003398 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003399 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3400 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003401 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3402 }
3403
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003404 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003405
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003406 /* Info from md_alg will be used instead */
3407 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003408
3409 /*
3410 * Signature
3411 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003412 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
3413 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003414 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003415 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3416 " for verify message" ) );
3417 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003418 }
3419
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003420 /*
3421 * Check the certificate's key type matches the signature alg
3422 */
3423 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3424 {
3425 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3426 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3427 }
Paul Bakker577e0062013-08-28 11:57:20 +02003428 }
3429 else
3430#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3431 {
3432 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003433 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003434 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003435
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003436 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01003437
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003438 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003439 {
3440 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003441 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003442 }
3443
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003444 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003445 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003446 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003447 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003448 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003449 return( ret );
3450 }
3451
3452 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3453
Paul Bakkered27a042013-04-18 22:46:23 +02003454 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003455}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003456#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3457 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3458 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003459
Paul Bakkera503a632013-08-14 13:48:06 +02003460#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003461static int ssl_write_new_session_ticket( ssl_context *ssl )
3462{
3463 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003464 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003465 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003466
3467 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3468
3469 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3470 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3471
3472 /*
3473 * struct {
3474 * uint32 ticket_lifetime_hint;
3475 * opaque ticket<0..2^16-1>;
3476 * } NewSessionTicket;
3477 *
3478 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3479 * 8 . 9 ticket_len (n)
3480 * 10 . 9+n ticket content
3481 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003482
3483 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3484 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3485 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3486 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003487
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003488 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3489 {
3490 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3491 tlen = 0;
3492 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003493
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003494 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3495 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003496
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003497 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003498
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003499 /*
3500 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3501 * ChangeCipherSpec share the same state.
3502 */
3503 ssl->handshake->new_session_ticket = 0;
3504
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003505 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3506 {
3507 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3508 return( ret );
3509 }
3510
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003511 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3512
3513 return( 0 );
3514}
Paul Bakkera503a632013-08-14 13:48:06 +02003515#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003516
Paul Bakker5121ce52009-01-03 21:22:43 +00003517/*
Paul Bakker1961b702013-01-25 14:49:24 +01003518 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003519 */
Paul Bakker1961b702013-01-25 14:49:24 +01003520int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003521{
3522 int ret = 0;
3523
Paul Bakker1961b702013-01-25 14:49:24 +01003524 if( ssl->state == SSL_HANDSHAKE_OVER )
3525 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003526
Paul Bakker1961b702013-01-25 14:49:24 +01003527 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3528
3529 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3530 return( ret );
3531
3532 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003533 {
Paul Bakker1961b702013-01-25 14:49:24 +01003534 case SSL_HELLO_REQUEST:
3535 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003536 break;
3537
Paul Bakker1961b702013-01-25 14:49:24 +01003538 /*
3539 * <== ClientHello
3540 */
3541 case SSL_CLIENT_HELLO:
3542 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003543 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003544
3545 /*
3546 * ==> ServerHello
3547 * Certificate
3548 * ( ServerKeyExchange )
3549 * ( CertificateRequest )
3550 * ServerHelloDone
3551 */
3552 case SSL_SERVER_HELLO:
3553 ret = ssl_write_server_hello( ssl );
3554 break;
3555
3556 case SSL_SERVER_CERTIFICATE:
3557 ret = ssl_write_certificate( ssl );
3558 break;
3559
3560 case SSL_SERVER_KEY_EXCHANGE:
3561 ret = ssl_write_server_key_exchange( ssl );
3562 break;
3563
3564 case SSL_CERTIFICATE_REQUEST:
3565 ret = ssl_write_certificate_request( ssl );
3566 break;
3567
3568 case SSL_SERVER_HELLO_DONE:
3569 ret = ssl_write_server_hello_done( ssl );
3570 break;
3571
3572 /*
3573 * <== ( Certificate/Alert )
3574 * ClientKeyExchange
3575 * ( CertificateVerify )
3576 * ChangeCipherSpec
3577 * Finished
3578 */
3579 case SSL_CLIENT_CERTIFICATE:
3580 ret = ssl_parse_certificate( ssl );
3581 break;
3582
3583 case SSL_CLIENT_KEY_EXCHANGE:
3584 ret = ssl_parse_client_key_exchange( ssl );
3585 break;
3586
3587 case SSL_CERTIFICATE_VERIFY:
3588 ret = ssl_parse_certificate_verify( ssl );
3589 break;
3590
3591 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3592 ret = ssl_parse_change_cipher_spec( ssl );
3593 break;
3594
3595 case SSL_CLIENT_FINISHED:
3596 ret = ssl_parse_finished( ssl );
3597 break;
3598
3599 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003600 * ==> ( NewSessionTicket )
3601 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003602 * Finished
3603 */
3604 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003605#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003606 if( ssl->handshake->new_session_ticket != 0 )
3607 ret = ssl_write_new_session_ticket( ssl );
3608 else
Paul Bakkera503a632013-08-14 13:48:06 +02003609#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003610 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003611 break;
3612
3613 case SSL_SERVER_FINISHED:
3614 ret = ssl_write_finished( ssl );
3615 break;
3616
3617 case SSL_FLUSH_BUFFERS:
3618 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3619 ssl->state = SSL_HANDSHAKE_WRAPUP;
3620 break;
3621
3622 case SSL_HANDSHAKE_WRAPUP:
3623 ssl_handshake_wrapup( ssl );
3624 break;
3625
3626 default:
3627 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3628 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003629 }
3630
Paul Bakker5121ce52009-01-03 21:22:43 +00003631 return( ret );
3632}
Paul Bakker9af723c2014-05-01 13:03:14 +02003633#endif /* POLARSSL_SSL_SRV_C */