blob: 0f6d5b49449ef553daff7894cda550f1535b25b9 [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é-Gonnard967a2a52015-01-22 14:28:16 +00006 * This file is part of mbed TLS (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker5121ce52009-01-03 21:22:43 +00009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000025#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#else
27#include POLARSSL_CONFIG_FILE
28#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakker40e46942009-01-03 21:51:57 +000030#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/debug.h"
33#include "polarssl/ssl.h"
Paul Bakker41c83d32013-03-20 14:39:14 +010034#if defined(POLARSSL_ECP_C)
35#include "polarssl/ecp.h"
36#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Paul Bakker7dc4c442014-02-01 22:50:26 +010038#if defined(POLARSSL_PLATFORM_C)
39#include "polarssl/platform.h"
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020040#else
41#define polarssl_malloc malloc
42#define polarssl_free free
43#endif
44
Paul Bakker5121ce52009-01-03 21:22:43 +000045#include <stdlib.h>
46#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020047
48#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
585 p = buf + 2;
586 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 );
832
833#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
834 if( ssl->handshake->sni_key_cert != NULL )
835 list = ssl->handshake->sni_key_cert;
836 else
837#endif
838 list = ssl->handshake->key_cert;
839
840 if( pk_alg == POLARSSL_PK_NONE )
841 return( 0 );
842
843 for( cur = list; cur != NULL; cur = cur->next )
844 {
845 if( ! pk_can_do( cur->key, pk_alg ) )
846 continue;
847
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200848 /*
849 * This avoids sending the client a cert it'll reject based on
850 * keyUsage or other extensions.
851 *
852 * It also allows the user to provision different certificates for
853 * different uses based on keyUsage, eg if they want to avoid signing
854 * and decrypting with the same RSA key.
855 */
856 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
857 SSL_IS_SERVER ) != 0 )
858 {
859 continue;
860 }
861
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100862#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100863 if( pk_alg == POLARSSL_PK_ECDSA &&
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100864 ssl_check_key_curve( cur->key, ssl->handshake->curves ) != 0 )
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100865 continue;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100866#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100867
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100868 /*
869 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
870 * present them a SHA-higher cert rather than failing if it's the only
871 * one we got that satisfies the other conditions.
872 */
873 if( ssl->minor_ver < SSL_MINOR_VERSION_3 &&
874 cur->cert->sig_md != POLARSSL_MD_SHA1 )
875 {
876 if( fallback == NULL )
877 fallback = cur;
878 continue;
879 }
880
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100881 /* If we get there, we got a winner */
882 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100883 }
884
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100885 if( cur != NULL )
886 {
887 ssl->handshake->key_cert = cur;
888 return( 0 );
889 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100890
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100891 if( fallback != NULL )
892 {
893 ssl->handshake->key_cert = fallback;
894 return( 0 );
895 }
896
897 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100898}
899#endif /* POLARSSL_X509_CRT_PARSE_C */
900
901/*
902 * Check if a given ciphersuite is suitable for use with our config/keys/etc
903 * Sets ciphersuite_info only if the suite matches.
904 */
905static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
906 const ssl_ciphersuite_t **ciphersuite_info )
907{
908 const ssl_ciphersuite_t *suite_info;
909
910 suite_info = ssl_ciphersuite_from_id( suite_id );
911 if( suite_info == NULL )
912 {
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100913 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
914 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100915 }
916
917 if( suite_info->min_minor_ver > ssl->minor_ver ||
918 suite_info->max_minor_ver < ssl->minor_ver )
919 return( 0 );
920
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100921 if( ssl->arc4_disabled == SSL_ARC4_DISABLED &&
922 suite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
923 return( 0 );
924
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100925#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
926 if( ssl_ciphersuite_uses_ec( suite_info ) &&
927 ( ssl->handshake->curves == NULL ||
928 ssl->handshake->curves[0] == NULL ) )
929 return( 0 );
930#endif
931
932#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
933 /* If the ciphersuite requires a pre-shared key and we don't
934 * have one, skip it now rather than failing later */
935 if( ssl_ciphersuite_uses_psk( suite_info ) &&
936 ssl->f_psk == NULL &&
937 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
938 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
939 return( 0 );
940#endif
941
942#if defined(POLARSSL_X509_CRT_PARSE_C)
943 /*
944 * Final check: if ciphersuite requires us to have a
945 * certificate/key of a particular type:
946 * - select the appropriate certificate if we have one, or
947 * - try the next ciphersuite if we don't
948 * This must be done last since we modify the key_cert list.
949 */
950 if( ssl_pick_cert( ssl, suite_info ) != 0 )
951 return( 0 );
952#endif
953
954 *ciphersuite_info = suite_info;
955 return( 0 );
956}
957
Paul Bakker78a8c712013-03-06 17:01:52 +0100958#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
959static int ssl_parse_client_hello_v2( ssl_context *ssl )
960{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +0100961 int ret, got_common_suite;
Paul Bakker78a8c712013-03-06 17:01:52 +0100962 unsigned int i, j;
963 size_t n;
964 unsigned int ciph_len, sess_len, chal_len;
965 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200966 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200967 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100968
969 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
970
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100971#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker78a8c712013-03-06 17:01:52 +0100972 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
973 {
974 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
975
976 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
977 return( ret );
978
979 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
980 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100981#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +0100982
983 buf = ssl->in_hdr;
984
985 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
986
987 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
988 buf[2] ) );
989 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
990 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
991 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
992 buf[3], buf[4] ) );
993
994 /*
995 * SSLv2 Client Hello
996 *
997 * Record layer:
998 * 0 . 1 message length
999 *
1000 * SSL layer:
1001 * 2 . 2 message type
1002 * 3 . 4 protocol version
1003 */
1004 if( buf[2] != SSL_HS_CLIENT_HELLO ||
1005 buf[3] != SSL_MAJOR_VERSION_3 )
1006 {
1007 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1008 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1009 }
1010
1011 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
1012
1013 if( n < 17 || n > 512 )
1014 {
1015 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1016 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1017 }
1018
1019 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +02001020 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
1021 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +01001022
1023 if( ssl->minor_ver < ssl->min_minor_ver )
1024 {
1025 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001026 " [%d:%d] < [%d:%d]",
1027 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +01001028 ssl->min_major_ver, ssl->min_minor_ver ) );
1029
1030 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1031 SSL_ALERT_MSG_PROTOCOL_VERSION );
1032 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1033 }
1034
Paul Bakker2fbefde2013-06-29 16:01:15 +02001035 ssl->handshake->max_major_ver = buf[3];
1036 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +01001037
1038 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
1039 {
1040 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1041 return( ret );
1042 }
1043
1044 ssl->handshake->update_checksum( ssl, buf + 2, n );
1045
1046 buf = ssl->in_msg;
1047 n = ssl->in_left - 5;
1048
1049 /*
1050 * 0 . 1 ciphersuitelist length
1051 * 2 . 3 session id length
1052 * 4 . 5 challenge length
1053 * 6 . .. ciphersuitelist
1054 * .. . .. session id
1055 * .. . .. challenge
1056 */
1057 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1058
1059 ciph_len = ( buf[0] << 8 ) | buf[1];
1060 sess_len = ( buf[2] << 8 ) | buf[3];
1061 chal_len = ( buf[4] << 8 ) | buf[5];
1062
1063 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
1064 ciph_len, sess_len, chal_len ) );
1065
1066 /*
1067 * Make sure each parameter length is valid
1068 */
1069 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1070 {
1071 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1072 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1073 }
1074
1075 if( sess_len > 32 )
1076 {
1077 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1078 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1079 }
1080
1081 if( chal_len < 8 || chal_len > 32 )
1082 {
1083 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1084 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1085 }
1086
1087 if( n != 6 + ciph_len + sess_len + chal_len )
1088 {
1089 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1090 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1091 }
1092
1093 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1094 buf + 6, ciph_len );
1095 SSL_DEBUG_BUF( 3, "client hello, session id",
1096 buf + 6 + ciph_len, sess_len );
1097 SSL_DEBUG_BUF( 3, "client hello, challenge",
1098 buf + 6 + ciph_len + sess_len, chal_len );
1099
1100 p = buf + 6 + ciph_len;
1101 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001102 memset( ssl->session_negotiate->id, 0,
1103 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001104 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1105
1106 p += sess_len;
1107 memset( ssl->handshake->randbytes, 0, 64 );
1108 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1109
1110 /*
1111 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1112 */
1113 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1114 {
1115 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1116 {
1117 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001118#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker78a8c712013-03-06 17:01:52 +01001119 if( ssl->renegotiation == SSL_RENEGOTIATION )
1120 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001121 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
1122 "during renegotiation" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001123
1124 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1125 return( ret );
1126
1127 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1128 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001129#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001130 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1131 break;
1132 }
1133 }
1134
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001135#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1136 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1137 {
1138 if( p[0] == 0 &&
1139 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1140 p[2] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1141 {
1142 SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
1143
1144 if( ssl->minor_ver < ssl->max_minor_ver )
1145 {
1146 SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
1147
1148 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1149 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1150
1151 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1152 }
1153
1154 break;
1155 }
1156 }
1157#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1158
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001159 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001160 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001161 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001162#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1163 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1164 {
1165 for( i = 0; ciphersuites[i] != 0; i++ )
1166#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001167 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001168 {
1169 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001170#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001171 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001172 if( p[0] != 0 ||
1173 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1174 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1175 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001176
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001177 got_common_suite = 1;
1178
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001179 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1180 &ciphersuite_info ) ) != 0 )
1181 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001182
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001183 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001184 goto have_ciphersuite_v2;
1185 }
1186 }
1187
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001188 if( got_common_suite )
1189 {
1190 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1191 "but none of them usable" ) );
1192 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
1193 }
1194 else
1195 {
1196 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1197 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1198 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001199
1200have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001201 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001202 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001203 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001204
1205 /*
1206 * SSLv2 Client Hello relevant renegotiation security checks
1207 */
1208 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1209 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1210 {
1211 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1212
1213 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1214 return( ret );
1215
1216 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1217 }
1218
1219 ssl->in_left = 0;
1220 ssl->state++;
1221
1222 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1223
1224 return( 0 );
1225}
1226#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1227
Paul Bakker5121ce52009-01-03 21:22:43 +00001228static int ssl_parse_client_hello( ssl_context *ssl )
1229{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001230 int ret, got_common_suite;
Paul Bakker23986e52011-04-24 08:57:21 +00001231 unsigned int i, j;
1232 size_t n;
1233 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001234 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001235 unsigned int ext_len = 0;
1236 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001237#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001238 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001239#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001240 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001241 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001242 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001243
1244 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1245
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001246#if defined(POLARSSL_SSL_RENEGOTIATION)
1247 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1248#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001249 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001250 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
1251 {
1252 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1253 return( ret );
1254 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001255 }
1256
1257 buf = ssl->in_hdr;
1258
Paul Bakker78a8c712013-03-06 17:01:52 +01001259#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1260 if( ( buf[0] & 0x80 ) != 0 )
1261 return ssl_parse_client_hello_v2( ssl );
1262#endif
1263
Paul Bakkerec636f32012-09-09 19:17:02 +00001264 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1265
1266 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1267 buf[0] ) );
1268 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1269 ( buf[3] << 8 ) | buf[4] ) );
1270 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
1271 buf[1], buf[2] ) );
1272
1273 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001274 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001275 *
1276 * Record layer:
1277 * 0 . 0 message type
1278 * 1 . 2 protocol version
1279 * 3 . 4 message length
1280 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001281
1282 /* According to RFC 5246 Appendix E.1, the version here is typically
1283 * "{03,00}, the lowest version number supported by the client, [or] the
1284 * value of ClientHello.client_version", so the only meaningful check here
1285 * is the major version shouldn't be less than 3 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001286 if( buf[0] != SSL_MSG_HANDSHAKE ||
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001287 buf[1] < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001288 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001289 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1290 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1291 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001292
Paul Bakkerec636f32012-09-09 19:17:02 +00001293 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +00001294
Paul Bakker4f42c112014-04-17 14:48:23 +02001295 if( n < 45 || n > SSL_MAX_CONTENT_LEN )
Paul Bakkerec636f32012-09-09 19:17:02 +00001296 {
1297 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1298 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1299 }
1300
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001301#if defined(POLARSSL_SSL_RENEGOTIATION)
1302 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1303#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001304 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001305 if( ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
1306 {
1307 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1308 return( ret );
1309 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001310 }
1311
1312 buf = ssl->in_msg;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001313#if defined(POLARSSL_SSL_RENEGOTIATION)
1314 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001315 n = ssl->in_msglen;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001316 else
1317#endif
1318 n = ssl->in_left - 5;
Paul Bakkerec636f32012-09-09 19:17:02 +00001319
Paul Bakker48916f92012-09-16 19:57:18 +00001320 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +00001321
1322 /*
1323 * SSL layer:
1324 * 0 . 0 handshake type
1325 * 1 . 3 handshake length
1326 * 4 . 5 protocol version
1327 * 6 . 9 UNIX time()
1328 * 10 . 37 random bytes
1329 * 38 . 38 session id length
1330 * 39 . 38+x session id
1331 * 39+x . 40+x ciphersuitelist length
Paul Bakker0f651c72014-05-22 15:12:19 +02001332 * 41+x . 40+y ciphersuitelist
1333 * 41+y . 41+y compression alg length
1334 * 42+y . 41+z compression algs
Paul Bakkerec636f32012-09-09 19:17:02 +00001335 * .. . .. extensions
1336 */
1337 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1338
1339 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1340 buf[0] ) );
1341 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1342 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1343 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1344 buf[4], buf[5] ) );
1345
1346 /*
1347 * Check the handshake type and protocol version
1348 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001349 if( buf[0] != SSL_HS_CLIENT_HELLO )
Paul Bakkerec636f32012-09-09 19:17:02 +00001350 {
1351 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1352 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1353 }
1354
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001355 ssl->major_ver = buf[4];
1356 ssl->minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001357
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001358 ssl->handshake->max_major_ver = ssl->major_ver;
1359 ssl->handshake->max_minor_ver = ssl->minor_ver;
1360
1361 if( ssl->major_ver < ssl->min_major_ver ||
1362 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001363 {
1364 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001365 " [%d:%d] < [%d:%d]",
1366 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001367 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001368
1369 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1370 SSL_ALERT_MSG_PROTOCOL_VERSION );
1371
1372 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1373 }
1374
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001375 if( ssl->major_ver > ssl->max_major_ver )
1376 {
1377 ssl->major_ver = ssl->max_major_ver;
1378 ssl->minor_ver = ssl->max_minor_ver;
1379 }
1380 else if( ssl->minor_ver > ssl->max_minor_ver )
1381 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001382
Paul Bakker48916f92012-09-16 19:57:18 +00001383 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001384
1385 /*
1386 * Check the handshake message length
1387 */
1388 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1389 {
1390 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1391 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1392 }
1393
1394 /*
1395 * Check the session length
1396 */
1397 sess_len = buf[38];
1398
Paul Bakker0f651c72014-05-22 15:12:19 +02001399 if( sess_len > 32 || sess_len > n - 42 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001400 {
1401 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1402 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1403 }
1404
Paul Bakker48916f92012-09-16 19:57:18 +00001405 ssl->session_negotiate->length = sess_len;
1406 memset( ssl->session_negotiate->id, 0,
1407 sizeof( ssl->session_negotiate->id ) );
1408 memcpy( ssl->session_negotiate->id, buf + 39,
1409 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001410
1411 /*
1412 * Check the ciphersuitelist length
1413 */
1414 ciph_len = ( buf[39 + sess_len] << 8 )
1415 | ( buf[40 + sess_len] );
1416
Paul Bakker0f651c72014-05-22 15:12:19 +02001417 if( ciph_len < 2 || ( ciph_len % 2 ) != 0 || ciph_len > n - 42 - sess_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001418 {
1419 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1420 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1421 }
1422
1423 /*
1424 * Check the compression algorithms length
1425 */
1426 comp_len = buf[41 + sess_len + ciph_len];
1427
Paul Bakker0f651c72014-05-22 15:12:19 +02001428 if( comp_len < 1 || comp_len > 16 ||
1429 comp_len > n - 42 - sess_len - ciph_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001430 {
1431 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1432 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1433 }
1434
Paul Bakker48916f92012-09-16 19:57:18 +00001435 /*
1436 * Check the extension length
1437 */
1438 if( n > 42 + sess_len + ciph_len + comp_len )
1439 {
1440 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1441 | ( buf[43 + sess_len + ciph_len + comp_len] );
1442
1443 if( ( ext_len > 0 && ext_len < 4 ) ||
1444 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1445 {
1446 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1447 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1448 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1449 }
1450 }
1451
1452 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001453#if defined(POLARSSL_ZLIB_SUPPORT)
1454 for( i = 0; i < comp_len; ++i )
1455 {
Paul Bakker48916f92012-09-16 19:57:18 +00001456 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001457 {
Paul Bakker48916f92012-09-16 19:57:18 +00001458 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001459 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001460 }
1461 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001462#endif
1463
Paul Bakkerec636f32012-09-09 19:17:02 +00001464 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1465 buf + 6, 32 );
1466 SSL_DEBUG_BUF( 3, "client hello, session id",
1467 buf + 38, sess_len );
1468 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1469 buf + 41 + sess_len, ciph_len );
1470 SSL_DEBUG_BUF( 3, "client hello, compression",
1471 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001472
Paul Bakkerec636f32012-09-09 19:17:02 +00001473 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001474 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1475 */
1476 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1477 {
1478 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1479 {
1480 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001481#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001482 if( ssl->renegotiation == SSL_RENEGOTIATION )
1483 {
1484 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001485
1486 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1487 return( ret );
1488
Paul Bakker48916f92012-09-16 19:57:18 +00001489 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1490 }
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001491 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001492#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00001493 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1494 break;
1495 }
1496 }
1497
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001498#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1499 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1500 {
1501 if( p[0] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1502 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1503 {
1504 SSL_DEBUG_MSG( 0, ( "received FALLBACK_SCSV" ) );
1505
1506 if( ssl->minor_ver < ssl->max_minor_ver )
1507 {
1508 SSL_DEBUG_MSG( 0, ( "inapropriate fallback" ) );
1509
1510 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1511 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1512
1513 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1514 }
1515
1516 break;
1517 }
1518 }
1519#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1520
Paul Bakker48916f92012-09-16 19:57:18 +00001521 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001522
1523 while( ext_len )
1524 {
1525 unsigned int ext_id = ( ( ext[0] << 8 )
1526 | ( ext[1] ) );
1527 unsigned int ext_size = ( ( ext[2] << 8 )
1528 | ( ext[3] ) );
1529
1530 if( ext_size + 4 > ext_len )
1531 {
1532 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1533 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1534 }
1535 switch( ext_id )
1536 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001537#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001538 case TLS_EXT_SERVERNAME:
1539 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1540 if( ssl->f_sni == NULL )
1541 break;
1542
1543 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1544 if( ret != 0 )
1545 return( ret );
1546 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001547#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001548
Paul Bakker48916f92012-09-16 19:57:18 +00001549 case TLS_EXT_RENEGOTIATION_INFO:
1550 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001551#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001552 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001553#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001554
Paul Bakker23f36802012-09-28 14:15:14 +00001555 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1556 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001557 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001558 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001559
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001560#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1561 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakker23f36802012-09-28 14:15:14 +00001562 case TLS_EXT_SIG_ALG:
1563 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001564#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker23f36802012-09-28 14:15:14 +00001565 if( ssl->renegotiation == SSL_RENEGOTIATION )
1566 break;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001567#endif
Paul Bakker23f36802012-09-28 14:15:14 +00001568
1569 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1570 if( ret != 0 )
1571 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001572 break;
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001573#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
1574 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001575
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001576#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001577 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1578 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1579
1580 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1581 if( ret != 0 )
1582 return( ret );
1583 break;
1584
1585 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1586 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001587 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001588
1589 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1590 if( ret != 0 )
1591 return( ret );
1592 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001593#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001594
Paul Bakker05decb22013-08-15 13:33:48 +02001595#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001596 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1597 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1598
1599 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1600 if( ret != 0 )
1601 return( ret );
1602 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001603#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001604
Paul Bakker1f2bc622013-08-15 13:45:55 +02001605#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001606 case TLS_EXT_TRUNCATED_HMAC:
1607 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1608
1609 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1610 if( ret != 0 )
1611 return( ret );
1612 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001613#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001614
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001615#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1616 case TLS_EXT_ENCRYPT_THEN_MAC:
1617 SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
1618
1619 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1620 if( ret != 0 )
1621 return( ret );
1622 break;
1623#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1624
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001625#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1626 case TLS_EXT_EXTENDED_MASTER_SECRET:
1627 SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
1628
1629 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1630 if( ret != 0 )
1631 return( ret );
1632 break;
1633#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1634
Paul Bakkera503a632013-08-14 13:48:06 +02001635#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001636 case TLS_EXT_SESSION_TICKET:
1637 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1638
1639 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1640 if( ret != 0 )
1641 return( ret );
1642 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001643#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001644
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001645#if defined(POLARSSL_SSL_ALPN)
1646 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001647 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001648
1649 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1650 if( ret != 0 )
1651 return( ret );
1652 break;
1653#endif /* POLARSSL_SSL_SESSION_TICKETS */
1654
Paul Bakker48916f92012-09-16 19:57:18 +00001655 default:
1656 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1657 ext_id ) );
1658 }
1659
1660 ext_len -= 4 + ext_size;
1661 ext += 4 + ext_size;
1662
1663 if( ext_len > 0 && ext_len < 4 )
1664 {
1665 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1666 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1667 }
1668 }
1669
1670 /*
1671 * Renegotiation security checks
1672 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001673 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001674 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1675 {
1676 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1677 handshake_failure = 1;
1678 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001679#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001680 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1681 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1682 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001683 {
1684 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001685 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001686 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001687 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1688 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1689 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001690 {
1691 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001692 handshake_failure = 1;
1693 }
1694 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1695 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1696 renegotiation_info_seen == 1 )
1697 {
1698 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1699 handshake_failure = 1;
1700 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001701#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001702
1703 if( handshake_failure == 1 )
1704 {
1705 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1706 return( ret );
1707
Paul Bakker48916f92012-09-16 19:57:18 +00001708 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1709 }
Paul Bakker380da532012-04-18 16:10:25 +00001710
Paul Bakker41c83d32013-03-20 14:39:14 +01001711 /*
1712 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001713 * (At the end because we need information from the EC-based extensions
1714 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001715 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001716 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001717 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001718 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001719#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1720 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
1721 {
1722 for( i = 0; ciphersuites[i] != 0; i++ )
1723#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001724 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001725 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001726 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001727#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001728 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001729 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1730 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1731 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001732
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001733 got_common_suite = 1;
1734
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001735 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1736 &ciphersuite_info ) ) != 0 )
1737 return( ret );
1738
1739 if( ciphersuite_info != NULL )
1740 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001741 }
1742 }
1743
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001744 if( got_common_suite )
1745 {
1746 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1747 "but none of them usable" ) );
1748 ssl_send_fatal_handshake_failure( ssl );
1749 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
1750 }
1751 else
1752 {
1753 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1754 ssl_send_fatal_handshake_failure( ssl );
1755 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1756 }
Paul Bakker41c83d32013-03-20 14:39:14 +01001757
1758have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001759 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001760 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1761 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1762
Paul Bakker5121ce52009-01-03 21:22:43 +00001763 ssl->in_left = 0;
1764 ssl->state++;
1765
1766 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1767
1768 return( 0 );
1769}
1770
Paul Bakker1f2bc622013-08-15 13:45:55 +02001771#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001772static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1773 unsigned char *buf,
1774 size_t *olen )
1775{
1776 unsigned char *p = buf;
1777
1778 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1779 {
1780 *olen = 0;
1781 return;
1782 }
1783
1784 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1785
1786 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1787 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1788
1789 *p++ = 0x00;
1790 *p++ = 0x00;
1791
1792 *olen = 4;
1793}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001794#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001795
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001796#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1797static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
1798 unsigned char *buf,
1799 size_t *olen )
1800{
1801 unsigned char *p = buf;
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01001802 const ssl_ciphersuite_t *suite = NULL;
1803 const cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001804
1805 if( ssl->session_negotiate->encrypt_then_mac == SSL_EXTENDED_MS_DISABLED ||
1806 ssl->minor_ver == SSL_MINOR_VERSION_0 )
1807 {
1808 *olen = 0;
1809 return;
1810 }
1811
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01001812 /*
1813 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
1814 * from a client and then selects a stream or Authenticated Encryption
1815 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
1816 * encrypt-then-MAC response extension back to the client."
1817 */
1818 if( ( suite = ssl_ciphersuite_from_id(
1819 ssl->session_negotiate->ciphersuite ) ) == NULL ||
1820 ( cipher = cipher_info_from_type( suite->cipher ) ) == NULL ||
1821 cipher->mode != POLARSSL_MODE_CBC )
1822 {
1823 *olen = 0;
1824 return;
1825 }
1826
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001827 SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
1828
1829 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
1830 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
1831
1832 *p++ = 0x00;
1833 *p++ = 0x00;
1834
1835 *olen = 4;
1836}
1837#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1838
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001839#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1840static void ssl_write_extended_ms_ext( ssl_context *ssl,
1841 unsigned char *buf,
1842 size_t *olen )
1843{
1844 unsigned char *p = buf;
1845
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +02001846 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_DISABLED ||
1847 ssl->minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001848 {
1849 *olen = 0;
1850 return;
1851 }
1852
1853 SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
1854 "extension" ) );
1855
1856 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
1857 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
1858
1859 *p++ = 0x00;
1860 *p++ = 0x00;
1861
1862 *olen = 4;
1863}
1864#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1865
Paul Bakkera503a632013-08-14 13:48:06 +02001866#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001867static void ssl_write_session_ticket_ext( ssl_context *ssl,
1868 unsigned char *buf,
1869 size_t *olen )
1870{
1871 unsigned char *p = buf;
1872
1873 if( ssl->handshake->new_session_ticket == 0 )
1874 {
1875 *olen = 0;
1876 return;
1877 }
1878
1879 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1880
1881 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1882 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1883
1884 *p++ = 0x00;
1885 *p++ = 0x00;
1886
1887 *olen = 4;
1888}
Paul Bakkera503a632013-08-14 13:48:06 +02001889#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001890
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001891static void ssl_write_renegotiation_ext( ssl_context *ssl,
1892 unsigned char *buf,
1893 size_t *olen )
1894{
1895 unsigned char *p = buf;
1896
1897 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1898 {
1899 *olen = 0;
1900 return;
1901 }
1902
1903 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1904
1905 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1906 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1907
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001908#if defined(POLARSSL_SSL_RENEGOTIATION)
1909 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
1910 {
1911 *p++ = 0x00;
1912 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1913 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001914
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001915 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1916 p += ssl->verify_data_len;
1917 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1918 p += ssl->verify_data_len;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001919
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001920 *olen = 5 + ssl->verify_data_len * 2;
1921 }
1922 else
1923#endif /* POLARSSL_SSL_RENEGOTIATION */
1924 {
1925 *p++ = 0x00;
1926 *p++ = 0x01;
1927 *p++ = 0x00;
1928
1929 *olen = 5;
1930 }
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001931}
1932
Paul Bakker05decb22013-08-15 13:33:48 +02001933#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001934static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1935 unsigned char *buf,
1936 size_t *olen )
1937{
1938 unsigned char *p = buf;
1939
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001940 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1941 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001942 *olen = 0;
1943 return;
1944 }
1945
1946 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1947
1948 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1949 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1950
1951 *p++ = 0x00;
1952 *p++ = 1;
1953
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001954 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001955
1956 *olen = 5;
1957}
Paul Bakker05decb22013-08-15 13:33:48 +02001958#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001959
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001960#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001961static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1962 unsigned char *buf,
1963 size_t *olen )
1964{
1965 unsigned char *p = buf;
1966 ((void) ssl);
1967
Paul Bakker677377f2013-10-28 12:54:26 +01001968 if( ( ssl->handshake->cli_exts &
1969 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
1970 {
1971 *olen = 0;
1972 return;
1973 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001974
1975 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1976
1977 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1978 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1979
1980 *p++ = 0x00;
1981 *p++ = 2;
1982
1983 *p++ = 1;
1984 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1985
1986 *olen = 6;
1987}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001988#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001989
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001990#if defined(POLARSSL_SSL_ALPN )
1991static void ssl_write_alpn_ext( ssl_context *ssl,
1992 unsigned char *buf, size_t *olen )
1993{
1994 if( ssl->alpn_chosen == NULL )
1995 {
1996 *olen = 0;
1997 return;
1998 }
1999
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02002000 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002001
2002 /*
2003 * 0 . 1 ext identifier
2004 * 2 . 3 ext length
2005 * 4 . 5 protocol list length
2006 * 6 . 6 protocol name length
2007 * 7 . 7+n protocol name
2008 */
2009 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
2010 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
2011
2012 *olen = 7 + strlen( ssl->alpn_chosen );
2013
2014 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2015 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2016
2017 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2018 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2019
2020 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2021
2022 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2023}
2024#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
2025
Paul Bakker5121ce52009-01-03 21:22:43 +00002026static int ssl_write_server_hello( ssl_context *ssl )
2027{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002028#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002029 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002030#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002031 int ret;
2032 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002033 unsigned char *buf, *p;
2034
2035 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
2036
Paul Bakkera9a028e2013-11-21 17:31:06 +01002037 if( ssl->f_rng == NULL )
2038 {
2039 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2040 return( POLARSSL_ERR_SSL_NO_RNG );
2041 }
2042
Paul Bakker5121ce52009-01-03 21:22:43 +00002043 /*
2044 * 0 . 0 handshake type
2045 * 1 . 3 handshake length
2046 * 4 . 5 protocol version
2047 * 6 . 9 UNIX time()
2048 * 10 . 37 random bytes
2049 */
2050 buf = ssl->out_msg;
2051 p = buf + 4;
2052
2053 *p++ = (unsigned char) ssl->major_ver;
2054 *p++ = (unsigned char) ssl->minor_ver;
2055
2056 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
2057 buf[4], buf[5] ) );
2058
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002059#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002060 t = time( NULL );
2061 *p++ = (unsigned char)( t >> 24 );
2062 *p++ = (unsigned char)( t >> 16 );
2063 *p++ = (unsigned char)( t >> 8 );
2064 *p++ = (unsigned char)( t );
2065
2066 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002067#else
2068 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
2069 return( ret );
2070
2071 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02002072#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002073
Paul Bakkera3d195c2011-11-27 21:07:34 +00002074 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
2075 return( ret );
2076
2077 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002078
Paul Bakker48916f92012-09-16 19:57:18 +00002079 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002080
2081 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
2082
2083 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002084 * Resume is 0 by default, see ssl_handshake_init().
2085 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2086 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002087 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002088 if( ssl->handshake->resume == 0 &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002089#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002090 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002091#endif
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002092 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002093 ssl->f_get_cache != NULL &&
2094 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
2095 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01002096 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002097 ssl->handshake->resume = 1;
2098 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002099
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002100 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 {
2102 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002103 * New session, create a new session id,
2104 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002106 ssl->state++;
2107
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002108#if defined(POLARSSL_HAVE_TIME)
2109 ssl->session_negotiate->start = time( NULL );
2110#endif
2111
Paul Bakkera503a632013-08-14 13:48:06 +02002112#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002113 if( ssl->handshake->new_session_ticket != 0 )
2114 {
2115 ssl->session_negotiate->length = n = 0;
2116 memset( ssl->session_negotiate->id, 0, 32 );
2117 }
2118 else
2119#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002120 {
2121 ssl->session_negotiate->length = n = 32;
2122 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002123 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002124 return( ret );
2125 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002126 }
2127 else
2128 {
2129 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002130 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002131 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02002132 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002133 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002134
2135 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2136 {
2137 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2138 return( ret );
2139 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002140 }
2141
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002142 /*
2143 * 38 . 38 session id length
2144 * 39 . 38+n session id
2145 * 39+n . 40+n chosen ciphersuite
2146 * 41+n . 41+n chosen compression alg.
2147 * 42+n . 43+n extensions length
2148 * 44+n . 43+n+m extensions
2149 */
2150 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00002151 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
2152 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002153
2154 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2155 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2156 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002157 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002158
Paul Bakker48916f92012-09-16 19:57:18 +00002159 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2160 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2161 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002162
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02002163 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2164 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02002165 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002166 ssl->session_negotiate->compression ) );
2167
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002168 /*
2169 * First write extensions, then the total length
2170 */
2171 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2172 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002173
Paul Bakker05decb22013-08-15 13:33:48 +02002174#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002175 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2176 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002177#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002178
Paul Bakker1f2bc622013-08-15 13:45:55 +02002179#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002180 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2181 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002182#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002183
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002184#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2185 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2186 ext_len += olen;
2187#endif
2188
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002189#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2190 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2191 ext_len += olen;
2192#endif
2193
Paul Bakkera503a632013-08-14 13:48:06 +02002194#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002195 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2196 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002197#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002198
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002199#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002200 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2201 ext_len += olen;
2202#endif
2203
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002204#if defined(POLARSSL_SSL_ALPN)
2205 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2206 ext_len += olen;
2207#endif
2208
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002209 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002210
Paul Bakkera7036632014-04-30 10:15:38 +02002211 if( ext_len > 0 )
2212 {
2213 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2214 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2215 p += ext_len;
2216 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002217
2218 ssl->out_msglen = p - buf;
2219 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2220 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
2221
2222 ret = ssl_write_record( ssl );
2223
2224 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2225
2226 return( ret );
2227}
2228
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002229#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2230 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002231 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2232 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002233static int ssl_write_certificate_request( ssl_context *ssl )
2234{
Paul Bakkered27a042013-04-18 22:46:23 +02002235 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002236
2237 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2238
2239 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002240 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002241 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2242 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002243 {
2244 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2245 ssl->state++;
2246 return( 0 );
2247 }
2248
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002249 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2250 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002251}
2252#else
2253static int ssl_write_certificate_request( ssl_context *ssl )
2254{
2255 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2256 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002257 size_t dn_size, total_dn_size; /* excluding length bytes */
2258 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002259 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002260 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00002261
2262 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2263
2264 ssl->state++;
2265
Paul Bakkerfbb17802013-04-17 19:10:21 +02002266 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002267 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002268 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002269 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002270 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002271 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002272 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002273 return( 0 );
2274 }
2275
2276 /*
2277 * 0 . 0 handshake type
2278 * 1 . 3 handshake length
2279 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002280 * 5 .. m-1 cert types
2281 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002282 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002283 * n .. n+1 length of all DNs
2284 * n+2 .. n+3 length of DN 1
2285 * n+4 .. ... Distinguished Name #1
2286 * ... .. ... length of DN 2, etc.
2287 */
2288 buf = ssl->out_msg;
2289 p = buf + 4;
2290
2291 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002292 * Supported certificate types
2293 *
2294 * ClientCertificateType certificate_types<1..2^8-1>;
2295 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002296 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002297 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002298
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002299#if defined(POLARSSL_RSA_C)
2300 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2301#endif
2302#if defined(POLARSSL_ECDSA_C)
2303 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2304#endif
2305
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002306 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002307 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002308
Paul Bakker577e0062013-08-28 11:57:20 +02002309 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002310#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002311 /*
2312 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002313 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002314 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2315 *
2316 * struct {
2317 * HashAlgorithm hash;
2318 * SignatureAlgorithm signature;
2319 * } SignatureAndHashAlgorithm;
2320 *
2321 * enum { (255) } HashAlgorithm;
2322 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002323 */
Paul Bakker21dca692013-01-03 11:41:08 +01002324 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002325 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002326 /*
2327 * Only use current running hash algorithm that is already required
2328 * for requested ciphersuite.
2329 */
Paul Bakker926af752012-11-23 13:38:07 +01002330 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2331
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002332 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2333 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002334 {
2335 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2336 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002337
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002338 /*
2339 * Supported signature algorithms
2340 */
2341#if defined(POLARSSL_RSA_C)
2342 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2343 p[2 + sa_len++] = SSL_SIG_RSA;
2344#endif
2345#if defined(POLARSSL_ECDSA_C)
2346 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2347 p[2 + sa_len++] = SSL_SIG_ECDSA;
2348#endif
Paul Bakker926af752012-11-23 13:38:07 +01002349
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002350 p[0] = (unsigned char)( sa_len >> 8 );
2351 p[1] = (unsigned char)( sa_len );
2352 sa_len += 2;
2353 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002354 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002355#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002356
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002357 /*
2358 * DistinguishedName certificate_authorities<0..2^16-1>;
2359 * opaque DistinguishedName<1..2^16-1>;
2360 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002361 p += 2;
2362 crt = ssl->ca_chain;
2363
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002364 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002365 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002366 {
2367 if( p - buf > 4096 )
2368 break;
2369
Paul Bakker926af752012-11-23 13:38:07 +01002370 dn_size = crt->subject_raw.len;
2371 *p++ = (unsigned char)( dn_size >> 8 );
2372 *p++ = (unsigned char)( dn_size );
2373 memcpy( p, crt->subject_raw.p, dn_size );
2374 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002375
Paul Bakker926af752012-11-23 13:38:07 +01002376 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2377
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002378 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002379 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002380 }
2381
Paul Bakker926af752012-11-23 13:38:07 +01002382 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002383 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2384 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002385 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2386 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002387
2388 ret = ssl_write_record( ssl );
2389
2390 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2391
2392 return( ret );
2393}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002394#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2395 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002396 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2397 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002398
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002399#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2400 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2401static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2402{
2403 int ret;
2404
2405 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2406 {
2407 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2408 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2409 }
2410
2411 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2412 pk_ec( *ssl_own_key( ssl ) ),
2413 POLARSSL_ECDH_OURS ) ) != 0 )
2414 {
2415 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2416 return( ret );
2417 }
2418
2419 return( 0 );
2420}
2421#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2422 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2423
Paul Bakker41c83d32013-03-20 14:39:14 +01002424static int ssl_write_server_key_exchange( ssl_context *ssl )
2425{
Paul Bakker23986e52011-04-24 08:57:21 +00002426 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002427 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002428 const ssl_ciphersuite_t *ciphersuite_info =
2429 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002430
2431#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2432 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2433 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002434 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002435 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002436 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002437 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002438 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002439 ((void) dig_signed);
2440 ((void) dig_signed_len);
2441#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002442
Paul Bakker5121ce52009-01-03 21:22:43 +00002443 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2444
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002445#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2446 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2447 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002448 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2449 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2450 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002451 {
2452 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2453 ssl->state++;
2454 return( 0 );
2455 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002456#endif
2457
2458#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2459 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2460 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2461 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2462 {
2463 ssl_get_ecdh_params_from_cert( ssl );
2464
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002465 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002466 ssl->state++;
2467 return( 0 );
2468 }
2469#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002470
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002471#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2472 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2473 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2474 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002475 {
2476 /* TODO: Support identity hints */
2477 *(p++) = 0x00;
2478 *(p++) = 0x00;
2479
2480 n += 2;
2481 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002482#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2483 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002484
2485#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2486 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2487 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2488 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002489 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002490 /*
2491 * Ephemeral DH parameters:
2492 *
2493 * struct {
2494 * opaque dh_p<1..2^16-1>;
2495 * opaque dh_g<1..2^16-1>;
2496 * opaque dh_Ys<1..2^16-1>;
2497 * } ServerDHParams;
2498 */
2499 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2500 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2501 {
2502 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2503 return( ret );
2504 }
Paul Bakker48916f92012-09-16 19:57:18 +00002505
Paul Bakker41c83d32013-03-20 14:39:14 +01002506 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002507 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2508 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002509 {
2510 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2511 return( ret );
2512 }
2513
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002514 dig_signed = p;
2515 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002516
2517 p += len;
2518 n += len;
2519
Paul Bakker41c83d32013-03-20 14:39:14 +01002520 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2521 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2522 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2523 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2524 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002525#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2526 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002527
Gergely Budai987bfb52014-01-19 21:48:42 +01002528#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002529 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002530 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2531 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002532 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002533 /*
2534 * Ephemeral ECDH parameters:
2535 *
2536 * struct {
2537 * ECParameters curve_params;
2538 * ECPoint public;
2539 * } ServerECDHParams;
2540 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002541 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002542#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002543 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002544
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002545 /* Match our preference list against the offered curves */
2546 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2547 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2548 if( (*curve)->grp_id == *gid )
2549 goto curve_matching_done;
2550
2551curve_matching_done:
2552#else
2553 curve = ssl->handshake->curves;
2554#endif
2555
2556 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002557 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002558 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2559 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002560 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002561
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002562 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002563
Paul Bakker41c83d32013-03-20 14:39:14 +01002564 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002565 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002566 {
2567 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2568 return( ret );
2569 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002570
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002571 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2572 p, SSL_MAX_CONTENT_LEN - n,
2573 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002574 {
2575 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2576 return( ret );
2577 }
2578
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002579 dig_signed = p;
2580 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002581
2582 p += len;
2583 n += len;
2584
Paul Bakker41c83d32013-03-20 14:39:14 +01002585 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2586 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002587#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002588
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002589#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002590 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2591 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002592 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002593 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2594 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002595 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002596 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002597 unsigned int hashlen = 0;
2598 unsigned char hash[64];
2599 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002600
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002601 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002602 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2603 */
Paul Bakker577e0062013-08-28 11:57:20 +02002604#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002605 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2606 {
2607 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2608
2609 if( md_alg == POLARSSL_MD_NONE )
2610 {
2611 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002612 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002613 }
2614 }
Paul Bakker577e0062013-08-28 11:57:20 +02002615 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002616#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002617#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2618 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002619 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002620 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2621 {
2622 md_alg = POLARSSL_MD_SHA1;
2623 }
2624 else
Paul Bakker577e0062013-08-28 11:57:20 +02002625#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002626 {
2627 md_alg = POLARSSL_MD_NONE;
2628 }
2629
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002630 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002631 * Compute the hash to be signed
2632 */
Paul Bakker577e0062013-08-28 11:57:20 +02002633#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2634 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002635 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002636 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002637 md5_context md5;
2638 sha1_context sha1;
2639
Paul Bakker5b4af392014-06-26 12:09:34 +02002640 md5_init( &md5 );
2641 sha1_init( &sha1 );
2642
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002643 /*
2644 * digitally-signed struct {
2645 * opaque md5_hash[16];
2646 * opaque sha_hash[20];
2647 * };
2648 *
2649 * md5_hash
2650 * MD5(ClientHello.random + ServerHello.random
2651 * + ServerParams);
2652 * sha_hash
2653 * SHA(ClientHello.random + ServerHello.random
2654 * + ServerParams);
2655 */
2656 md5_starts( &md5 );
2657 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002658 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002659 md5_finish( &md5, hash );
2660
2661 sha1_starts( &sha1 );
2662 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002663 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002664 sha1_finish( &sha1, hash + 16 );
2665
2666 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02002667
2668 md5_free( &md5 );
2669 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002670 }
2671 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002672#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2673 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002674#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2675 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002676 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002677 {
2678 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02002679 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002680
Paul Bakker84bbeb52014-07-01 14:53:22 +02002681 md_init( &ctx );
2682
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002683 /* Info from md_alg will be used instead */
2684 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002685
2686 /*
2687 * digitally-signed struct {
2688 * opaque client_random[32];
2689 * opaque server_random[32];
2690 * ServerDHParams params;
2691 * };
2692 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002693 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002694 {
2695 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2696 return( ret );
2697 }
2698
2699 md_starts( &ctx );
2700 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002701 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002702 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002703 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00002704 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002705 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002706#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2707 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002708 {
2709 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002710 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002711 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002712
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002713 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2714 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002715
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002716 /*
2717 * Make the signature
2718 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002719 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002720 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002721 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2722 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002723 }
Paul Bakker23f36802012-09-28 14:15:14 +00002724
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002725#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002726 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2727 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002728 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002729 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002730
2731 n += 2;
2732 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002733#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002734
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002735 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002736 p + 2 , &signature_len,
2737 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002738 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002739 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002740 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002741 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002742
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002743 *(p++) = (unsigned char)( signature_len >> 8 );
2744 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002745 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002746
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002747 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002748
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002749 p += signature_len;
2750 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002751 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002752#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002753 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2754 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002755
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002756 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002757 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2758 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2759
2760 ssl->state++;
2761
2762 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2763 {
2764 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2765 return( ret );
2766 }
2767
2768 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2769
2770 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002771}
2772
2773static int ssl_write_server_hello_done( ssl_context *ssl )
2774{
2775 int ret;
2776
2777 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2778
2779 ssl->out_msglen = 4;
2780 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2781 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2782
2783 ssl->state++;
2784
2785 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2786 {
2787 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2788 return( ret );
2789 }
2790
2791 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2792
2793 return( 0 );
2794}
2795
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002796#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2797 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2798static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2799 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002800{
2801 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002802 size_t n;
2803
2804 /*
2805 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2806 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002807 if( *p + 2 > end )
2808 {
2809 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2810 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2811 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002812
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002813 n = ( (*p)[0] << 8 ) | (*p)[1];
2814 *p += 2;
2815
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002816 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002817 {
2818 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2819 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2820 }
2821
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002822 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002823 {
2824 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2825 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2826 }
2827
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002828 *p += n;
2829
Paul Bakker70df2fb2013-04-17 17:19:09 +02002830 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2831
Paul Bakker70df2fb2013-04-17 17:19:09 +02002832 return( ret );
2833}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002834#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2835 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002836
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002837#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2838 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002839static int ssl_parse_encrypted_pms( ssl_context *ssl,
2840 const unsigned char *p,
2841 const unsigned char *end,
2842 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002843{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002844 int ret;
2845 size_t len = pk_get_len( ssl_own_key( ssl ) );
2846 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002847
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002848 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002849 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002850 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002851 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2852 }
2853
2854 /*
2855 * Decrypt the premaster using own private RSA key
2856 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002857#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2858 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002859 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2860 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002861 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2862 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002863 {
2864 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2865 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2866 }
2867 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002868#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002869
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002870 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002871 {
2872 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2873 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2874 }
2875
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002876 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2877 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002878 sizeof( ssl->handshake->premaster ) - pms_offset,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002879 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002880
2881 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002882 pms[0] != ssl->handshake->max_major_ver ||
2883 pms[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002884 {
2885 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2886
2887 /*
2888 * Protection against Bleichenbacher's attack:
2889 * invalid PKCS#1 v1.5 padding must not cause
2890 * the connection to end immediately; instead,
2891 * send a bad_record_mac later in the handshake.
2892 */
2893 ssl->handshake->pmslen = 48;
2894
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002895 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002896 if( ret != 0 )
2897 return( ret );
2898 }
2899
2900 return( ret );
2901}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002902#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2903 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002904
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002905#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002906static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2907 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002908{
Paul Bakker6db455e2013-09-18 17:29:31 +02002909 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002910 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002911
Paul Bakker6db455e2013-09-18 17:29:31 +02002912 if( ssl->f_psk == NULL &&
2913 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2914 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002915 {
2916 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2917 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2918 }
2919
2920 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002921 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002922 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002923 if( *p + 2 > end )
2924 {
2925 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2926 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2927 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002928
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002929 n = ( (*p)[0] << 8 ) | (*p)[1];
2930 *p += 2;
2931
2932 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002933 {
2934 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2935 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2936 }
2937
Paul Bakker6db455e2013-09-18 17:29:31 +02002938 if( ssl->f_psk != NULL )
2939 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002940 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002941 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2942 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002943 else
Paul Bakker6db455e2013-09-18 17:29:31 +02002944 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002945 /* Identity is not a big secret since clients send it in the clear,
2946 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02002947 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002948 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002949 {
2950 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2951 }
2952 }
2953
2954 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002955 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002956 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002957 if( ( ret = ssl_send_alert_message( ssl,
2958 SSL_ALERT_LEVEL_FATAL,
2959 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2960 {
2961 return( ret );
2962 }
2963
2964 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002965 }
2966
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002967 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002968
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002969 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002970}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002971#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002972
Paul Bakker5121ce52009-01-03 21:22:43 +00002973static int ssl_parse_client_key_exchange( ssl_context *ssl )
2974{
Paul Bakker23986e52011-04-24 08:57:21 +00002975 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002976 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002977
Paul Bakker41c83d32013-03-20 14:39:14 +01002978 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002979
2980 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2981
2982 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2983 {
2984 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2985 return( ret );
2986 }
2987
2988 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2989 {
2990 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002991 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002992 }
2993
2994 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2995 {
2996 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002997 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002998 }
2999
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003000#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01003001 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003002 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003003 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003004 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003005
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003006 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003007 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02003008 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3009 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003010 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003011
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003012 if( p != end )
3013 {
3014 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3015 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3016 }
3017
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02003018 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003019
3020 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
3021 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003022 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02003023 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003024 {
3025 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
3026 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3027 }
3028
3029 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003030 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003031 else
3032#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003033#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003034 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3035 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3036 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003037 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003038 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
3039 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
3040 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003041 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003042 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003043 ssl->in_msg + 4, ssl->in_hslen - 4 ) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003044 {
3045 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3046 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3047 }
3048
3049 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3050
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003051 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
3052 &ssl->handshake->pmslen,
3053 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02003054 POLARSSL_MPI_MAX_SIZE,
3055 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003056 {
3057 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
3058 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3059 }
3060
3061 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003062 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003063 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003064#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003065 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3066 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3067 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003068#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
3069 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003070 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003071 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003072 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003073
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003074 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003075 {
3076 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3077 return( ret );
3078 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003079
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003080 if( p != end )
3081 {
3082 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3083 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3084 }
3085
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003086 if( ( ret = ssl_psk_derive_premaster( ssl,
3087 ciphersuite_info->key_exchange ) ) != 0 )
3088 {
3089 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3090 return( ret );
3091 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003092 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003093 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003094#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003095#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
3096 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
3097 {
3098 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003099 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003100
3101 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3102 {
3103 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3104 return( ret );
3105 }
3106
3107 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3108 {
3109 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
3110 return( ret );
3111 }
3112
3113 if( ( ret = ssl_psk_derive_premaster( ssl,
3114 ciphersuite_info->key_exchange ) ) != 0 )
3115 {
3116 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3117 return( ret );
3118 }
3119 }
3120 else
3121#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003122#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3123 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3124 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003125 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003126 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003127
3128 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3129 {
3130 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3131 return( ret );
3132 }
3133 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3134 {
3135 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3136 return( ret );
3137 }
3138
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003139 if( p != end )
3140 {
3141 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3142 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3143 }
3144
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003145 if( ( ret = ssl_psk_derive_premaster( ssl,
3146 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003147 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003148 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3149 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003150 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003151 }
3152 else
3153#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003154#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3155 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
3156 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003157 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003158 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003159
3160 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3161 {
3162 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3163 return( ret );
3164 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003165
3166 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
3167 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003168 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003169 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3170 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003171 }
3172
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003173 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3174
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003175 if( ( ret = ssl_psk_derive_premaster( ssl,
3176 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003177 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003178 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003179 return( ret );
3180 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003181 }
3182 else
3183#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003184#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
3185 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003186 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003187 if( ( ret = ssl_parse_encrypted_pms( ssl,
3188 ssl->in_msg + 4,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003189 ssl->in_msg + ssl->in_hslen,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003190 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003191 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003192 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003193 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003194 }
3195 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003196 else
3197#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
3198 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003199 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003200 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003201 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003202
Paul Bakkerff60ee62010-03-16 21:09:09 +00003203 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
3204 {
3205 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
3206 return( ret );
3207 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003208
Paul Bakker5121ce52009-01-03 21:22:43 +00003209 ssl->state++;
3210
3211 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3212
3213 return( 0 );
3214}
3215
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003216#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3217 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003218 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3219 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003220static int ssl_parse_certificate_verify( ssl_context *ssl )
3221{
Paul Bakkerfbb17802013-04-17 19:10:21 +02003222 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003223
3224 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3225
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003226 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003227 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003228 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003229 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02003230 {
3231 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3232 ssl->state++;
3233 return( 0 );
3234 }
3235
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003236 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3237 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003238}
3239#else
3240static int ssl_parse_certificate_verify( ssl_context *ssl )
3241{
3242 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003243 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003244 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003245 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003246 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003247#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003248 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003249#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003250 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003251 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3252
3253 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3254
3255 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003256 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003257 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003258 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3259 {
3260 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3261 ssl->state++;
3262 return( 0 );
3263 }
3264
Paul Bakkered27a042013-04-18 22:46:23 +02003265 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003266 {
3267 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3268 ssl->state++;
3269 return( 0 );
3270 }
3271
Paul Bakker48916f92012-09-16 19:57:18 +00003272 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003273
3274 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3275 {
3276 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3277 return( ret );
3278 }
3279
3280 ssl->state++;
3281
3282 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3283 {
3284 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003285 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003286 }
3287
3288 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
3289 {
3290 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003291 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003292 }
3293
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003294 /*
3295 * 0 . 0 handshake type
3296 * 1 . 3 handshake length
3297 * 4 . 5 sig alg (TLS 1.2 only)
3298 * 4+n . 5+n signature length (n = sa_len)
3299 * 6+n . 6+n+m signature (m = sig_len)
3300 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003301
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003302#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3303 defined(POLARSSL_SSL_PROTO_TLS1_1)
3304 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003305 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003306 sa_len = 0;
3307
Paul Bakkerc70b9822013-04-07 22:00:46 +02003308 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003309 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003310
3311 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3312 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3313 POLARSSL_PK_ECDSA ) )
3314 {
3315 hash_start += 16;
3316 hashlen -= 16;
3317 md_alg = POLARSSL_MD_SHA1;
3318 }
Paul Bakker926af752012-11-23 13:38:07 +01003319 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003320 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003321#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3322 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003323#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3324 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003325 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003326 sa_len = 2;
3327
Paul Bakker5121ce52009-01-03 21:22:43 +00003328 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003329 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003330 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003331 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003332 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003333 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3334 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003335 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3336 }
3337
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003338 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003339
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003340 /* Info from md_alg will be used instead */
3341 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003342
3343 /*
3344 * Signature
3345 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003346 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
3347 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003348 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003349 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3350 " for verify message" ) );
3351 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003352 }
3353
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003354 /*
3355 * Check the certificate's key type matches the signature alg
3356 */
3357 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3358 {
3359 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3360 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3361 }
Paul Bakker577e0062013-08-28 11:57:20 +02003362 }
3363 else
3364#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3365 {
3366 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003367 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003368 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003369
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003370 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01003371
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003372 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003373 {
3374 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003375 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003376 }
3377
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003378 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003379 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003380 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003381 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003382 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003383 return( ret );
3384 }
3385
3386 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3387
Paul Bakkered27a042013-04-18 22:46:23 +02003388 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003389}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003390#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3391 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3392 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003393
Paul Bakkera503a632013-08-14 13:48:06 +02003394#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003395static int ssl_write_new_session_ticket( ssl_context *ssl )
3396{
3397 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003398 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003399 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003400
3401 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3402
3403 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3404 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3405
3406 /*
3407 * struct {
3408 * uint32 ticket_lifetime_hint;
3409 * opaque ticket<0..2^16-1>;
3410 * } NewSessionTicket;
3411 *
3412 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3413 * 8 . 9 ticket_len (n)
3414 * 10 . 9+n ticket content
3415 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003416
3417 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3418 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3419 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3420 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003421
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003422 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3423 {
3424 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3425 tlen = 0;
3426 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003427
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003428 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3429 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003430
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003431 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003432
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003433 /*
3434 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3435 * ChangeCipherSpec share the same state.
3436 */
3437 ssl->handshake->new_session_ticket = 0;
3438
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003439 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3440 {
3441 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3442 return( ret );
3443 }
3444
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003445 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3446
3447 return( 0 );
3448}
Paul Bakkera503a632013-08-14 13:48:06 +02003449#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003450
Paul Bakker5121ce52009-01-03 21:22:43 +00003451/*
Paul Bakker1961b702013-01-25 14:49:24 +01003452 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003453 */
Paul Bakker1961b702013-01-25 14:49:24 +01003454int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003455{
3456 int ret = 0;
3457
Paul Bakker1961b702013-01-25 14:49:24 +01003458 if( ssl->state == SSL_HANDSHAKE_OVER )
3459 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003460
Paul Bakker1961b702013-01-25 14:49:24 +01003461 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3462
3463 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3464 return( ret );
3465
3466 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003467 {
Paul Bakker1961b702013-01-25 14:49:24 +01003468 case SSL_HELLO_REQUEST:
3469 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003470 break;
3471
Paul Bakker1961b702013-01-25 14:49:24 +01003472 /*
3473 * <== ClientHello
3474 */
3475 case SSL_CLIENT_HELLO:
3476 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003477 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003478
3479 /*
3480 * ==> ServerHello
3481 * Certificate
3482 * ( ServerKeyExchange )
3483 * ( CertificateRequest )
3484 * ServerHelloDone
3485 */
3486 case SSL_SERVER_HELLO:
3487 ret = ssl_write_server_hello( ssl );
3488 break;
3489
3490 case SSL_SERVER_CERTIFICATE:
3491 ret = ssl_write_certificate( ssl );
3492 break;
3493
3494 case SSL_SERVER_KEY_EXCHANGE:
3495 ret = ssl_write_server_key_exchange( ssl );
3496 break;
3497
3498 case SSL_CERTIFICATE_REQUEST:
3499 ret = ssl_write_certificate_request( ssl );
3500 break;
3501
3502 case SSL_SERVER_HELLO_DONE:
3503 ret = ssl_write_server_hello_done( ssl );
3504 break;
3505
3506 /*
3507 * <== ( Certificate/Alert )
3508 * ClientKeyExchange
3509 * ( CertificateVerify )
3510 * ChangeCipherSpec
3511 * Finished
3512 */
3513 case SSL_CLIENT_CERTIFICATE:
3514 ret = ssl_parse_certificate( ssl );
3515 break;
3516
3517 case SSL_CLIENT_KEY_EXCHANGE:
3518 ret = ssl_parse_client_key_exchange( ssl );
3519 break;
3520
3521 case SSL_CERTIFICATE_VERIFY:
3522 ret = ssl_parse_certificate_verify( ssl );
3523 break;
3524
3525 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3526 ret = ssl_parse_change_cipher_spec( ssl );
3527 break;
3528
3529 case SSL_CLIENT_FINISHED:
3530 ret = ssl_parse_finished( ssl );
3531 break;
3532
3533 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003534 * ==> ( NewSessionTicket )
3535 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003536 * Finished
3537 */
3538 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003539#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003540 if( ssl->handshake->new_session_ticket != 0 )
3541 ret = ssl_write_new_session_ticket( ssl );
3542 else
Paul Bakkera503a632013-08-14 13:48:06 +02003543#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003544 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003545 break;
3546
3547 case SSL_SERVER_FINISHED:
3548 ret = ssl_write_finished( ssl );
3549 break;
3550
3551 case SSL_FLUSH_BUFFERS:
3552 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3553 ssl->state = SSL_HANDSHAKE_WRAPUP;
3554 break;
3555
3556 case SSL_HANDSHAKE_WRAPUP:
3557 ssl_handshake_wrapup( ssl );
3558 break;
3559
3560 default:
3561 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3562 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003563 }
3564
Paul Bakker5121ce52009-01-03 21:22:43 +00003565 return( ret );
3566}
Paul Bakker9af723c2014-05-01 13:03:14 +02003567#endif /* POLARSSL_SSL_SRV_C */