blob: 53b317975048b3005b8b6ac0700ff997ee289344 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 server-side functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (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 Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakker40e46942009-01-03 21:51:57 +000032#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/debug.h"
35#include "polarssl/ssl.h"
Paul Bakker41c83d32013-03-20 14:39:14 +010036#if defined(POLARSSL_ECP_C)
37#include "polarssl/ecp.h"
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker7dc4c442014-02-01 22:50:26 +010040#if defined(POLARSSL_PLATFORM_C)
41#include "polarssl/platform.h"
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020042#else
43#define polarssl_malloc malloc
44#define polarssl_free free
45#endif
46
Paul Bakker5121ce52009-01-03 21:22:43 +000047#include <stdlib.h>
48#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020049
50#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000051#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020052#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Paul Bakkera503a632013-08-14 13:48:06 +020054#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020055/*
56 * Serialize a session in the following format:
57 * 0 . n-1 session structure, n = sizeof(ssl_session)
58 * n . n+2 peer_cert length = m (0 if no certificate)
59 * n+3 . n+2+m peer cert ASN.1
60 *
61 * Assumes ticket is NULL (always true on server side).
62 */
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020063static int ssl_save_session( const ssl_session *session,
64 unsigned char *buf, size_t buf_len,
65 size_t *olen )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020066{
67 unsigned char *p = buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020068 size_t left = buf_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020069#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020070 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020071#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020072
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020073 if( left < sizeof( ssl_session ) )
74 return( -1 );
75
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020076 memcpy( p, session, sizeof( ssl_session ) );
77 p += sizeof( ssl_session );
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020078 left -= sizeof( ssl_session );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020079
Paul Bakker7c6b2c32013-09-16 13:49:26 +020080#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020081 if( session->peer_cert == NULL )
82 cert_len = 0;
83 else
84 cert_len = session->peer_cert->raw.len;
85
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020086 if( left < 3 + cert_len )
87 return( -1 );
88
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020089 *p++ = (unsigned char)( cert_len >> 16 & 0xFF );
90 *p++ = (unsigned char)( cert_len >> 8 & 0xFF );
91 *p++ = (unsigned char)( cert_len & 0xFF );
92
93 if( session->peer_cert != NULL )
94 memcpy( p, session->peer_cert->raw.p, cert_len );
95
96 p += cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020097#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020098
99 *olen = p - buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200100
101 return( 0 );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200102}
103
104/*
105 * Unserialise session, see ssl_save_session()
106 */
107static int ssl_load_session( ssl_session *session,
108 const unsigned char *buf, size_t len )
109{
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200110 const unsigned char *p = buf;
111 const unsigned char * const end = buf + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200112#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200113 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200114#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200115
116 if( p + sizeof( ssl_session ) > end )
117 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
118
119 memcpy( session, p, sizeof( ssl_session ) );
120 p += sizeof( ssl_session );
121
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200122#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200123 if( p + 3 > end )
124 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
125
126 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
127 p += 3;
128
129 if( cert_len == 0 )
130 {
131 session->peer_cert = NULL;
132 }
133 else
134 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200135 int ret;
136
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200137 if( p + cert_len > end )
138 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
139
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200140 session->peer_cert = polarssl_malloc( sizeof( x509_crt ) );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200141
142 if( session->peer_cert == NULL )
143 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
144
Paul Bakkerb6b09562013-09-18 14:17:41 +0200145 x509_crt_init( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200146
Paul Bakkerddf26b42013-09-18 13:46:23 +0200147 if( ( ret = x509_crt_parse( session->peer_cert, p, cert_len ) ) != 0 )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200148 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200149 x509_crt_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200150 polarssl_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200151 session->peer_cert = NULL;
152 return( ret );
153 }
154
155 p += cert_len;
156 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200157#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200158
159 if( p != end )
160 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
161
162 return( 0 );
163}
164
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200165/*
166 * Create session ticket, secured as recommended in RFC 5077 section 4:
167 *
168 * struct {
169 * opaque key_name[16];
170 * opaque iv[16];
171 * opaque encrypted_state<0..2^16-1>;
172 * opaque mac[32];
173 * } ticket;
174 *
175 * (the internal state structure differs, however).
176 */
177static int ssl_write_ticket( ssl_context *ssl, size_t *tlen )
178{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200179 int ret;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200180 unsigned char * const start = ssl->out_msg + 10;
181 unsigned char *p = start;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200182 unsigned char *state;
183 unsigned char iv[16];
184 size_t clear_len, enc_len, pad_len, i;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200185
Manuel Pégourié-Gonnard0a201712013-08-23 16:25:16 +0200186 *tlen = 0;
187
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200188 if( ssl->ticket_keys == NULL )
189 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
190
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200191 /* Write key name */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200192 memcpy( p, ssl->ticket_keys->key_name, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200193 p += 16;
194
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200195 /* Generate and write IV (with a copy for aes_crypt) */
196 if( ( ret = ssl->f_rng( ssl->p_rng, p, 16 ) ) != 0 )
197 return( ret );
198 memcpy( iv, p, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200199 p += 16;
200
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200201 /*
202 * Dump session state
203 *
204 * After the session state itself, we still need room for 16 bytes of
205 * padding and 32 bytes of MAC, so there's only so much room left
206 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200207 state = p + 2;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200208 if( ssl_save_session( ssl->session_negotiate, state,
209 SSL_MAX_CONTENT_LEN - (state - ssl->out_ctr) - 48,
210 &clear_len ) != 0 )
211 {
212 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
213 }
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200214 SSL_DEBUG_BUF( 3, "session ticket cleartext", state, clear_len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200215
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200216 /* Apply PKCS padding */
217 pad_len = 16 - clear_len % 16;
218 enc_len = clear_len + pad_len;
219 for( i = clear_len; i < enc_len; i++ )
220 state[i] = (unsigned char) pad_len;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200221
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200222 /* Encrypt */
223 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->enc, AES_ENCRYPT,
224 enc_len, iv, state, state ) ) != 0 )
225 {
226 return( ret );
227 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200228
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200229 /* Write length */
230 *p++ = (unsigned char)( ( enc_len >> 8 ) & 0xFF );
231 *p++ = (unsigned char)( ( enc_len ) & 0xFF );
232 p = state + enc_len;
233
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200234 /* Compute and write MAC( key_name + iv + enc_state_len + enc_state ) */
235 sha256_hmac( ssl->ticket_keys->mac_key, 16, start, p - start, p, 0 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200236 p += 32;
237
238 *tlen = p - start;
239
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200240 SSL_DEBUG_BUF( 3, "session ticket structure", start, *tlen );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200241
242 return( 0 );
243}
244
245/*
246 * Load session ticket (see ssl_write_ticket for structure)
247 */
248static int ssl_parse_ticket( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200249 unsigned char *buf,
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200250 size_t len )
251{
252 int ret;
253 ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200254 unsigned char *key_name = buf;
255 unsigned char *iv = buf + 16;
256 unsigned char *enc_len_p = iv + 16;
257 unsigned char *ticket = enc_len_p + 2;
258 unsigned char *mac;
Manuel Pégourié-Gonnard34ced2d2013-09-20 11:37:39 +0200259 unsigned char computed_mac[32];
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200260 size_t enc_len, clear_len, i;
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100261 unsigned char pad_len, diff;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200262
263 SSL_DEBUG_BUF( 3, "session ticket structure", buf, len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200264
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200265 if( len < 34 || ssl->ticket_keys == NULL )
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200266 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
267
268 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
269 mac = ticket + enc_len;
270
271 if( len != enc_len + 66 )
272 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
273
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100274 /* Check name, in constant time though it's not a big secret */
275 diff = 0;
276 for( i = 0; i < 16; i++ )
277 diff |= key_name[i] ^ ssl->ticket_keys->key_name[i];
278 /* don't return yet, check the MAC anyway */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200279
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100280 /* Check mac, with constant-time buffer comparison */
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200281 sha256_hmac( ssl->ticket_keys->mac_key, 16, buf, len - 32,
282 computed_mac, 0 );
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100283
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200284 for( i = 0; i < 32; i++ )
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100285 diff |= mac[i] ^ computed_mac[i];
286
287 /* Now return if ticket is not authentic, since we want to avoid
288 * decrypting arbitrary attacker-chosen data */
289 if( diff != 0 )
290 return( POLARSSL_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200291
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200292 /* Decrypt */
293 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->dec, AES_DECRYPT,
294 enc_len, iv, ticket, ticket ) ) != 0 )
295 {
296 return( ret );
297 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200298
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200299 /* Check PKCS padding */
300 pad_len = ticket[enc_len - 1];
301
302 ret = 0;
303 for( i = 2; i < pad_len; i++ )
304 if( ticket[enc_len - i] != pad_len )
305 ret = POLARSSL_ERR_SSL_BAD_INPUT_DATA;
306 if( ret != 0 )
307 return( ret );
308
309 clear_len = enc_len - pad_len;
310
311 SSL_DEBUG_BUF( 3, "session ticket cleartext", ticket, clear_len );
312
313 /* Actually load session */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200314 if( ( ret = ssl_load_session( &session, ticket, clear_len ) ) != 0 )
315 {
316 SSL_DEBUG_MSG( 1, ( "failed to parse ticket content" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100317 ssl_session_free( &session );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200318 return( ret );
319 }
320
Paul Bakker606b4ba2013-08-14 16:52:14 +0200321#if defined(POLARSSL_HAVE_TIME)
322 /* Check if still valid */
323 if( (int) ( time( NULL) - session.start ) > ssl->ticket_lifetime )
324 {
325 SSL_DEBUG_MSG( 1, ( "session ticket expired" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100326 ssl_session_free( &session );
Paul Bakker606b4ba2013-08-14 16:52:14 +0200327 return( POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED );
328 }
329#endif
330
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200331 /*
332 * Keep the session ID sent by the client, since we MUST send it back to
333 * inform him we're accepting the ticket (RFC 5077 section 3.4)
334 */
335 session.length = ssl->session_negotiate->length;
336 memcpy( &session.id, ssl->session_negotiate->id, session.length );
337
338 ssl_session_free( ssl->session_negotiate );
339 memcpy( ssl->session_negotiate, &session, sizeof( ssl_session ) );
340 memset( &session, 0, sizeof( ssl_session ) );
341
342 return( 0 );
343}
Paul Bakkera503a632013-08-14 13:48:06 +0200344#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200345
Paul Bakker0be444a2013-08-27 21:55:01 +0200346#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200347/*
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200348 * Wrapper around f_sni, allowing use of ssl_set_own_cert() but
349 * making it act on ssl->hanshake->sni_key_cert instead.
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200350 */
351static int ssl_sni_wrapper( ssl_context *ssl,
352 const unsigned char* name, size_t len )
353{
354 int ret;
355 ssl_key_cert *key_cert_ori = ssl->key_cert;
356
357 ssl->key_cert = NULL;
358 ret = ssl->f_sni( ssl->p_sni, ssl, name, len );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200359 ssl->handshake->sni_key_cert = ssl->key_cert;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200360
361 ssl->key_cert = key_cert_ori;
362
363 return( ret );
364}
365
Paul Bakker5701cdc2012-09-27 21:49:42 +0000366static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000367 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000368 size_t len )
369{
370 int ret;
371 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000372 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000373
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100374 SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
375
Paul Bakker5701cdc2012-09-27 21:49:42 +0000376 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
377 if( servername_list_size + 2 != len )
378 {
379 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
380 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
381 }
382
383 p = buf + 2;
384 while( servername_list_size > 0 )
385 {
386 hostname_len = ( ( p[1] << 8 ) | p[2] );
387 if( hostname_len + 3 > servername_list_size )
388 {
389 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
390 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
391 }
392
393 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
394 {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200395 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000396 if( ret != 0 )
397 {
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100398 SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000399 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
400 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
401 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
402 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000403 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000404 }
405
406 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000407 p += hostname_len + 3;
408 }
409
410 if( servername_list_size != 0 )
411 {
412 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
413 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000414 }
415
416 return( 0 );
417}
Paul Bakker0be444a2013-08-27 21:55:01 +0200418#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000419
Paul Bakker48916f92012-09-16 19:57:18 +0000420static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000421 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000422 size_t len )
423{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000424 int ret;
425
Paul Bakker48916f92012-09-16 19:57:18 +0000426 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
427 {
428 if( len != 1 || buf[0] != 0x0 )
429 {
430 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000431
432 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
433 return( ret );
434
Paul Bakker48916f92012-09-16 19:57:18 +0000435 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
436 }
437
438 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
439 }
440 else
441 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100442 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000443 if( len != 1 + ssl->verify_data_len ||
444 buf[0] != ssl->verify_data_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100445 safer_memcmp( buf + 1, ssl->peer_verify_data,
446 ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000447 {
448 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000449
450 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
451 return( ret );
452
Paul Bakker48916f92012-09-16 19:57:18 +0000453 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
454 }
455 }
456
457 return( 0 );
458}
459
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200460#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +0000461static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
462 const unsigned char *buf,
463 size_t len )
464{
465 size_t sig_alg_list_size;
466 const unsigned char *p;
467
468 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
469 if( sig_alg_list_size + 2 != len ||
470 sig_alg_list_size %2 != 0 )
471 {
472 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
473 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
474 }
475
476 p = buf + 2;
477 while( sig_alg_list_size > 0 )
478 {
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200479 /*
480 * For now, just ignore signature algorithm and rely on offered
481 * ciphersuites only. To be fixed later.
482 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200483#if defined(POLARSSL_SHA512_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000484 if( p[0] == SSL_HASH_SHA512 )
485 {
486 ssl->handshake->sig_alg = SSL_HASH_SHA512;
487 break;
488 }
489 if( p[0] == SSL_HASH_SHA384 )
490 {
491 ssl->handshake->sig_alg = SSL_HASH_SHA384;
492 break;
493 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200494#endif /* POLARSSL_SHA512_C */
Paul Bakker9e36f042013-06-30 14:34:05 +0200495#if defined(POLARSSL_SHA256_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000496 if( p[0] == SSL_HASH_SHA256 )
497 {
498 ssl->handshake->sig_alg = SSL_HASH_SHA256;
499 break;
500 }
501 if( p[0] == SSL_HASH_SHA224 )
502 {
503 ssl->handshake->sig_alg = SSL_HASH_SHA224;
504 break;
505 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200506#endif /* POLARSSL_SHA256_C */
Paul Bakker23f36802012-09-28 14:15:14 +0000507 if( p[0] == SSL_HASH_SHA1 )
508 {
509 ssl->handshake->sig_alg = SSL_HASH_SHA1;
510 break;
511 }
512 if( p[0] == SSL_HASH_MD5 )
513 {
514 ssl->handshake->sig_alg = SSL_HASH_MD5;
515 break;
516 }
517
518 sig_alg_list_size -= 2;
519 p += 2;
520 }
521
522 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
523 ssl->handshake->sig_alg ) );
524
525 return( 0 );
526}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200527#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker23f36802012-09-28 14:15:14 +0000528
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200529#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200530static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
531 const unsigned char *buf,
532 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100533{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200534 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100535 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200536 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100537
538 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
539 if( list_size + 2 != len ||
540 list_size % 2 != 0 )
541 {
542 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
543 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
544 }
545
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +0100546 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200547 * and leave room for a final 0 */
548 our_size = list_size / 2 + 1;
549 if( our_size > POLARSSL_ECP_DP_MAX )
550 our_size = POLARSSL_ECP_DP_MAX;
551
552 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
553 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
554
Paul Bakker9af723c2014-05-01 13:03:14 +0200555 /* explicit void pointer cast for buggy MS compiler */
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200556 memset( (void *) curves, 0, our_size * sizeof( *curves ) );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200557 ssl->handshake->curves = curves;
558
Paul Bakker41c83d32013-03-20 14:39:14 +0100559 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200560 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100561 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200562 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200563
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200564 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100565 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200566 *curves++ = curve_info;
567 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100568 }
569
570 list_size -= 2;
571 p += 2;
572 }
573
574 return( 0 );
575}
576
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200577static int ssl_parse_supported_point_formats( ssl_context *ssl,
578 const unsigned char *buf,
579 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100580{
581 size_t list_size;
582 const unsigned char *p;
583
584 list_size = buf[0];
585 if( list_size + 1 != len )
586 {
587 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
588 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
589 }
590
591 p = buf + 2;
592 while( list_size > 0 )
593 {
594 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
595 p[0] == POLARSSL_ECP_PF_COMPRESSED )
596 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200597 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200598 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100599 return( 0 );
600 }
601
602 list_size--;
603 p++;
604 }
605
606 return( 0 );
607}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200608#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100609
Paul Bakker05decb22013-08-15 13:33:48 +0200610#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200611static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
612 const unsigned char *buf,
613 size_t len )
614{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200615 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200616 {
617 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
618 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
619 }
620
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200621 ssl->session_negotiate->mfl_code = buf[0];
622
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200623 return( 0 );
624}
Paul Bakker05decb22013-08-15 13:33:48 +0200625#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200626
Paul Bakker1f2bc622013-08-15 13:45:55 +0200627#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200628static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
629 const unsigned char *buf,
630 size_t len )
631{
632 if( len != 0 )
633 {
634 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
635 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
636 }
637
638 ((void) buf);
639
640 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
641
642 return( 0 );
643}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200644#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200645
Paul Bakkera503a632013-08-14 13:48:06 +0200646#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200647static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200648 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200649 size_t len )
650{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200651 int ret;
652
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200653 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
654 return( 0 );
655
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200656 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200657 ssl->handshake->new_session_ticket = 1;
658
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200659 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
660
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200661 if( len == 0 )
662 return( 0 );
663
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200664 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
665 {
666 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
667 return( 0 );
668 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200669
670 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200671 * Failures are ok: just ignore the ticket and proceed.
672 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200673 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
674 {
675 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200676 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200677 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200678
679 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
680
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200681 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200682
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200683 /* Don't send a new ticket after all, this one is OK */
684 ssl->handshake->new_session_ticket = 0;
685
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200686 return( 0 );
687}
Paul Bakkera503a632013-08-14 13:48:06 +0200688#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200689
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200690#if defined(POLARSSL_SSL_ALPN)
691static int ssl_parse_alpn_ext( ssl_context *ssl,
692 unsigned char *buf, size_t len )
693{
Paul Bakker14b16c62014-05-28 11:33:54 +0200694 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200695 const unsigned char *theirs, *start, *end;
696 const char **ours;
697
698 /* If ALPN not configured, just ignore the extension */
699 if( ssl->alpn_list == NULL )
700 return( 0 );
701
702 /*
703 * opaque ProtocolName<1..2^8-1>;
704 *
705 * struct {
706 * ProtocolName protocol_name_list<2..2^16-1>
707 * } ProtocolNameList;
708 */
709
710 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
711 if( len < 4 )
712 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
713
714 list_len = ( buf[0] << 8 ) | buf[1];
715 if( list_len != len - 2 )
716 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
717
718 /*
719 * Use our order of preference
720 */
721 start = buf + 2;
722 end = buf + len;
723 for( ours = ssl->alpn_list; *ours != NULL; ours++ )
724 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200725 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200726 for( theirs = start; theirs != end; theirs += cur_len )
727 {
728 /* If the list is well formed, we should get equality first */
729 if( theirs > end )
730 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
731
732 cur_len = *theirs++;
733
734 /* Empty strings MUST NOT be included */
735 if( cur_len == 0 )
736 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
737
Paul Bakker14b16c62014-05-28 11:33:54 +0200738 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200739 memcmp( theirs, *ours, cur_len ) == 0 )
740 {
741 ssl->alpn_chosen = *ours;
742 return( 0 );
743 }
744 }
745 }
746
747 /* If we get there, no match was found */
748 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
749 SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
750 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
751}
752#endif /* POLARSSL_SSL_ALPN */
753
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100754/*
755 * Auxiliary functions for ServerHello parsing and related actions
756 */
757
758#if defined(POLARSSL_X509_CRT_PARSE_C)
759/*
760 * Return 1 if the given EC key uses the given curve, 0 otherwise
761 */
762#if defined(POLARSSL_ECDSA_C)
763static int ssl_key_matches_curves( pk_context *pk,
764 const ecp_curve_info **curves )
765{
766 const ecp_curve_info **crv = curves;
767 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
768
769 while( *crv != NULL )
770 {
771 if( (*crv)->grp_id == grp_id )
772 return( 1 );
773 crv++;
774 }
775
776 return( 0 );
777}
778#endif /* POLARSSL_ECDSA_C */
779
780/*
781 * Try picking a certificate for this ciphersuite,
782 * return 0 on success and -1 on failure.
783 */
784static int ssl_pick_cert( ssl_context *ssl,
785 const ssl_ciphersuite_t * ciphersuite_info )
786{
787 ssl_key_cert *cur, *list;
788 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
789
790#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
791 if( ssl->handshake->sni_key_cert != NULL )
792 list = ssl->handshake->sni_key_cert;
793 else
794#endif
795 list = ssl->handshake->key_cert;
796
797 if( pk_alg == POLARSSL_PK_NONE )
798 return( 0 );
799
800 for( cur = list; cur != NULL; cur = cur->next )
801 {
802 if( ! pk_can_do( cur->key, pk_alg ) )
803 continue;
804
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200805 /*
806 * This avoids sending the client a cert it'll reject based on
807 * keyUsage or other extensions.
808 *
809 * It also allows the user to provision different certificates for
810 * different uses based on keyUsage, eg if they want to avoid signing
811 * and decrypting with the same RSA key.
812 */
813 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
814 SSL_IS_SERVER ) != 0 )
815 {
816 continue;
817 }
818
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100819#if defined(POLARSSL_ECDSA_C)
820 if( pk_alg == POLARSSL_PK_ECDSA )
821 {
822 if( ssl_key_matches_curves( cur->key, ssl->handshake->curves ) )
823 break;
824 }
825 else
826#endif
827 break;
828 }
829
830 if( cur == NULL )
831 return( -1 );
832
833 ssl->handshake->key_cert = cur;
834 return( 0 );
835}
836#endif /* POLARSSL_X509_CRT_PARSE_C */
837
838/*
839 * Check if a given ciphersuite is suitable for use with our config/keys/etc
840 * Sets ciphersuite_info only if the suite matches.
841 */
842static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
843 const ssl_ciphersuite_t **ciphersuite_info )
844{
845 const ssl_ciphersuite_t *suite_info;
846
847 suite_info = ssl_ciphersuite_from_id( suite_id );
848 if( suite_info == NULL )
849 {
850 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", suite_id ) );
851 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
852 }
853
854 if( suite_info->min_minor_ver > ssl->minor_ver ||
855 suite_info->max_minor_ver < ssl->minor_ver )
856 return( 0 );
857
858#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
859 if( ssl_ciphersuite_uses_ec( suite_info ) &&
860 ( ssl->handshake->curves == NULL ||
861 ssl->handshake->curves[0] == NULL ) )
862 return( 0 );
863#endif
864
865#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
866 /* If the ciphersuite requires a pre-shared key and we don't
867 * have one, skip it now rather than failing later */
868 if( ssl_ciphersuite_uses_psk( suite_info ) &&
869 ssl->f_psk == NULL &&
870 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
871 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
872 return( 0 );
873#endif
874
875#if defined(POLARSSL_X509_CRT_PARSE_C)
876 /*
877 * Final check: if ciphersuite requires us to have a
878 * certificate/key of a particular type:
879 * - select the appropriate certificate if we have one, or
880 * - try the next ciphersuite if we don't
881 * This must be done last since we modify the key_cert list.
882 */
883 if( ssl_pick_cert( ssl, suite_info ) != 0 )
884 return( 0 );
885#endif
886
887 *ciphersuite_info = suite_info;
888 return( 0 );
889}
890
Paul Bakker78a8c712013-03-06 17:01:52 +0100891#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
892static int ssl_parse_client_hello_v2( ssl_context *ssl )
893{
894 int ret;
895 unsigned int i, j;
896 size_t n;
897 unsigned int ciph_len, sess_len, chal_len;
898 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200899 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200900 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100901
902 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
903
904 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
905 {
906 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
907
908 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
909 return( ret );
910
911 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
912 }
913
914 buf = ssl->in_hdr;
915
916 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
917
918 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
919 buf[2] ) );
920 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
921 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
922 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
923 buf[3], buf[4] ) );
924
925 /*
926 * SSLv2 Client Hello
927 *
928 * Record layer:
929 * 0 . 1 message length
930 *
931 * SSL layer:
932 * 2 . 2 message type
933 * 3 . 4 protocol version
934 */
935 if( buf[2] != SSL_HS_CLIENT_HELLO ||
936 buf[3] != SSL_MAJOR_VERSION_3 )
937 {
938 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
939 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
940 }
941
942 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
943
944 if( n < 17 || n > 512 )
945 {
946 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
947 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
948 }
949
950 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200951 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
952 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100953
954 if( ssl->minor_ver < ssl->min_minor_ver )
955 {
956 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200957 " [%d:%d] < [%d:%d]",
958 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +0100959 ssl->min_major_ver, ssl->min_minor_ver ) );
960
961 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
962 SSL_ALERT_MSG_PROTOCOL_VERSION );
963 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
964 }
965
Paul Bakker2fbefde2013-06-29 16:01:15 +0200966 ssl->handshake->max_major_ver = buf[3];
967 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100968
969 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
970 {
971 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
972 return( ret );
973 }
974
975 ssl->handshake->update_checksum( ssl, buf + 2, n );
976
977 buf = ssl->in_msg;
978 n = ssl->in_left - 5;
979
980 /*
981 * 0 . 1 ciphersuitelist length
982 * 2 . 3 session id length
983 * 4 . 5 challenge length
984 * 6 . .. ciphersuitelist
985 * .. . .. session id
986 * .. . .. challenge
987 */
988 SSL_DEBUG_BUF( 4, "record contents", buf, n );
989
990 ciph_len = ( buf[0] << 8 ) | buf[1];
991 sess_len = ( buf[2] << 8 ) | buf[3];
992 chal_len = ( buf[4] << 8 ) | buf[5];
993
994 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
995 ciph_len, sess_len, chal_len ) );
996
997 /*
998 * Make sure each parameter length is valid
999 */
1000 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1001 {
1002 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1003 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1004 }
1005
1006 if( sess_len > 32 )
1007 {
1008 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1009 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1010 }
1011
1012 if( chal_len < 8 || chal_len > 32 )
1013 {
1014 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1015 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1016 }
1017
1018 if( n != 6 + ciph_len + sess_len + chal_len )
1019 {
1020 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1021 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1022 }
1023
1024 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1025 buf + 6, ciph_len );
1026 SSL_DEBUG_BUF( 3, "client hello, session id",
1027 buf + 6 + ciph_len, sess_len );
1028 SSL_DEBUG_BUF( 3, "client hello, challenge",
1029 buf + 6 + ciph_len + sess_len, chal_len );
1030
1031 p = buf + 6 + ciph_len;
1032 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001033 memset( ssl->session_negotiate->id, 0,
1034 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001035 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1036
1037 p += sess_len;
1038 memset( ssl->handshake->randbytes, 0, 64 );
1039 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1040
1041 /*
1042 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1043 */
1044 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1045 {
1046 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1047 {
1048 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1049 if( ssl->renegotiation == SSL_RENEGOTIATION )
1050 {
1051 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1052
1053 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1054 return( ret );
1055
1056 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1057 }
1058 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1059 break;
1060 }
1061 }
1062
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001063 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001064 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001065#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1066 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1067 {
1068 for( i = 0; ciphersuites[i] != 0; i++ )
1069#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001070 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001071 {
1072 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001073#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001074 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001075 if( p[0] != 0 ||
1076 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1077 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1078 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001079
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001080 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1081 &ciphersuite_info ) ) != 0 )
1082 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001083
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001084 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001085 goto have_ciphersuite_v2;
1086 }
1087 }
1088
1089 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1090
1091 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1092
1093have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001094 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001095 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001096 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001097
1098 /*
1099 * SSLv2 Client Hello relevant renegotiation security checks
1100 */
1101 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1102 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1103 {
1104 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1105
1106 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1107 return( ret );
1108
1109 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1110 }
1111
1112 ssl->in_left = 0;
1113 ssl->state++;
1114
1115 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1116
1117 return( 0 );
1118}
1119#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1120
Paul Bakker5121ce52009-01-03 21:22:43 +00001121static int ssl_parse_client_hello( ssl_context *ssl )
1122{
Paul Bakker23986e52011-04-24 08:57:21 +00001123 int ret;
1124 unsigned int i, j;
1125 size_t n;
1126 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001127 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001128 unsigned int ext_len = 0;
1129 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001130 int renegotiation_info_seen = 0;
1131 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001132 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001133 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001134
1135 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1136
Paul Bakker48916f92012-09-16 19:57:18 +00001137 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1138 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001139 {
1140 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1141 return( ret );
1142 }
1143
1144 buf = ssl->in_hdr;
1145
Paul Bakker78a8c712013-03-06 17:01:52 +01001146#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1147 if( ( buf[0] & 0x80 ) != 0 )
1148 return ssl_parse_client_hello_v2( ssl );
1149#endif
1150
Paul Bakkerec636f32012-09-09 19:17:02 +00001151 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1152
1153 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1154 buf[0] ) );
1155 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1156 ( buf[3] << 8 ) | buf[4] ) );
1157 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
1158 buf[1], buf[2] ) );
1159
1160 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001161 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001162 *
1163 * Record layer:
1164 * 0 . 0 message type
1165 * 1 . 2 protocol version
1166 * 3 . 4 message length
1167 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001168
1169 /* According to RFC 5246 Appendix E.1, the version here is typically
1170 * "{03,00}, the lowest version number supported by the client, [or] the
1171 * value of ClientHello.client_version", so the only meaningful check here
1172 * is the major version shouldn't be less than 3 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001173 if( buf[0] != SSL_MSG_HANDSHAKE ||
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001174 buf[1] < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001175 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001176 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1177 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1178 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001179
Paul Bakkerec636f32012-09-09 19:17:02 +00001180 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +00001181
Paul Bakker4f42c112014-04-17 14:48:23 +02001182 if( n < 45 || n > SSL_MAX_CONTENT_LEN )
Paul Bakkerec636f32012-09-09 19:17:02 +00001183 {
1184 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1185 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1186 }
1187
Paul Bakker48916f92012-09-16 19:57:18 +00001188 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1189 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001190 {
1191 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1192 return( ret );
1193 }
1194
1195 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +00001196 if( !ssl->renegotiation )
1197 n = ssl->in_left - 5;
1198 else
1199 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +00001200
Paul Bakker48916f92012-09-16 19:57:18 +00001201 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +00001202
1203 /*
1204 * SSL layer:
1205 * 0 . 0 handshake type
1206 * 1 . 3 handshake length
1207 * 4 . 5 protocol version
1208 * 6 . 9 UNIX time()
1209 * 10 . 37 random bytes
1210 * 38 . 38 session id length
1211 * 39 . 38+x session id
1212 * 39+x . 40+x ciphersuitelist length
Paul Bakker0f651c72014-05-22 15:12:19 +02001213 * 41+x . 40+y ciphersuitelist
1214 * 41+y . 41+y compression alg length
1215 * 42+y . 41+z compression algs
Paul Bakkerec636f32012-09-09 19:17:02 +00001216 * .. . .. extensions
1217 */
1218 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1219
1220 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1221 buf[0] ) );
1222 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1223 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1224 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1225 buf[4], buf[5] ) );
1226
1227 /*
1228 * Check the handshake type and protocol version
1229 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001230 if( buf[0] != SSL_HS_CLIENT_HELLO )
Paul Bakkerec636f32012-09-09 19:17:02 +00001231 {
1232 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1233 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1234 }
1235
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001236 ssl->major_ver = buf[4];
1237 ssl->minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001238
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001239 ssl->handshake->max_major_ver = ssl->major_ver;
1240 ssl->handshake->max_minor_ver = ssl->minor_ver;
1241
1242 if( ssl->major_ver < ssl->min_major_ver ||
1243 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001244 {
1245 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001246 " [%d:%d] < [%d:%d]",
1247 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001248 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001249
1250 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1251 SSL_ALERT_MSG_PROTOCOL_VERSION );
1252
1253 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1254 }
1255
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001256 if( ssl->major_ver > ssl->max_major_ver )
1257 {
1258 ssl->major_ver = ssl->max_major_ver;
1259 ssl->minor_ver = ssl->max_minor_ver;
1260 }
1261 else if( ssl->minor_ver > ssl->max_minor_ver )
1262 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001263
Paul Bakker48916f92012-09-16 19:57:18 +00001264 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001265
1266 /*
1267 * Check the handshake message length
1268 */
1269 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1270 {
1271 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1272 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1273 }
1274
1275 /*
1276 * Check the session length
1277 */
1278 sess_len = buf[38];
1279
Paul Bakker0f651c72014-05-22 15:12:19 +02001280 if( sess_len > 32 || sess_len > n - 42 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001281 {
1282 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1283 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1284 }
1285
Paul Bakker48916f92012-09-16 19:57:18 +00001286 ssl->session_negotiate->length = sess_len;
1287 memset( ssl->session_negotiate->id, 0,
1288 sizeof( ssl->session_negotiate->id ) );
1289 memcpy( ssl->session_negotiate->id, buf + 39,
1290 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001291
1292 /*
1293 * Check the ciphersuitelist length
1294 */
1295 ciph_len = ( buf[39 + sess_len] << 8 )
1296 | ( buf[40 + sess_len] );
1297
Paul Bakker0f651c72014-05-22 15:12:19 +02001298 if( ciph_len < 2 || ( ciph_len % 2 ) != 0 || ciph_len > n - 42 - sess_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001299 {
1300 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1301 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1302 }
1303
1304 /*
1305 * Check the compression algorithms length
1306 */
1307 comp_len = buf[41 + sess_len + ciph_len];
1308
Paul Bakker0f651c72014-05-22 15:12:19 +02001309 if( comp_len < 1 || comp_len > 16 ||
1310 comp_len > n - 42 - sess_len - ciph_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001311 {
1312 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1313 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1314 }
1315
Paul Bakker48916f92012-09-16 19:57:18 +00001316 /*
1317 * Check the extension length
1318 */
1319 if( n > 42 + sess_len + ciph_len + comp_len )
1320 {
1321 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1322 | ( buf[43 + sess_len + ciph_len + comp_len] );
1323
1324 if( ( ext_len > 0 && ext_len < 4 ) ||
1325 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1326 {
1327 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1328 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1329 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1330 }
1331 }
1332
1333 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001334#if defined(POLARSSL_ZLIB_SUPPORT)
1335 for( i = 0; i < comp_len; ++i )
1336 {
Paul Bakker48916f92012-09-16 19:57:18 +00001337 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001338 {
Paul Bakker48916f92012-09-16 19:57:18 +00001339 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001340 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001341 }
1342 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001343#endif
1344
Paul Bakkerec636f32012-09-09 19:17:02 +00001345 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1346 buf + 6, 32 );
1347 SSL_DEBUG_BUF( 3, "client hello, session id",
1348 buf + 38, sess_len );
1349 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1350 buf + 41 + sess_len, ciph_len );
1351 SSL_DEBUG_BUF( 3, "client hello, compression",
1352 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001353
Paul Bakkerec636f32012-09-09 19:17:02 +00001354 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001355 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1356 */
1357 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1358 {
1359 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1360 {
1361 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1362 if( ssl->renegotiation == SSL_RENEGOTIATION )
1363 {
1364 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001365
1366 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1367 return( ret );
1368
Paul Bakker48916f92012-09-16 19:57:18 +00001369 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1370 }
1371 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1372 break;
1373 }
1374 }
1375
Paul Bakker48916f92012-09-16 19:57:18 +00001376 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001377
1378 while( ext_len )
1379 {
1380 unsigned int ext_id = ( ( ext[0] << 8 )
1381 | ( ext[1] ) );
1382 unsigned int ext_size = ( ( ext[2] << 8 )
1383 | ( ext[3] ) );
1384
1385 if( ext_size + 4 > ext_len )
1386 {
1387 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1388 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1389 }
1390 switch( ext_id )
1391 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001392#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001393 case TLS_EXT_SERVERNAME:
1394 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1395 if( ssl->f_sni == NULL )
1396 break;
1397
1398 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1399 if( ret != 0 )
1400 return( ret );
1401 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001402#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001403
Paul Bakker48916f92012-09-16 19:57:18 +00001404 case TLS_EXT_RENEGOTIATION_INFO:
1405 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1406 renegotiation_info_seen = 1;
1407
Paul Bakker23f36802012-09-28 14:15:14 +00001408 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1409 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001410 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001411 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001412
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001413#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001414 case TLS_EXT_SIG_ALG:
1415 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1416 if( ssl->renegotiation == SSL_RENEGOTIATION )
1417 break;
1418
1419 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1420 if( ret != 0 )
1421 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001422 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001423#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001424
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001425#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001426 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1427 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1428
1429 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1430 if( ret != 0 )
1431 return( ret );
1432 break;
1433
1434 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1435 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001436 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001437
1438 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1439 if( ret != 0 )
1440 return( ret );
1441 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001442#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001443
Paul Bakker05decb22013-08-15 13:33:48 +02001444#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001445 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1446 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1447
1448 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1449 if( ret != 0 )
1450 return( ret );
1451 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001452#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001453
Paul Bakker1f2bc622013-08-15 13:45:55 +02001454#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001455 case TLS_EXT_TRUNCATED_HMAC:
1456 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1457
1458 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1459 if( ret != 0 )
1460 return( ret );
1461 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001462#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001463
Paul Bakkera503a632013-08-14 13:48:06 +02001464#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001465 case TLS_EXT_SESSION_TICKET:
1466 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1467
1468 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1469 if( ret != 0 )
1470 return( ret );
1471 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001472#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001473
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001474#if defined(POLARSSL_SSL_ALPN)
1475 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001476 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001477
1478 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1479 if( ret != 0 )
1480 return( ret );
1481 break;
1482#endif /* POLARSSL_SSL_SESSION_TICKETS */
1483
Paul Bakker48916f92012-09-16 19:57:18 +00001484 default:
1485 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1486 ext_id ) );
1487 }
1488
1489 ext_len -= 4 + ext_size;
1490 ext += 4 + ext_size;
1491
1492 if( ext_len > 0 && ext_len < 4 )
1493 {
1494 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1495 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1496 }
1497 }
1498
1499 /*
1500 * Renegotiation security checks
1501 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001502 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1503 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1504 {
1505 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1506 handshake_failure = 1;
1507 }
1508 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1509 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1510 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001511 {
1512 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001513 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001514 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001515 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1516 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1517 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001518 {
1519 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001520 handshake_failure = 1;
1521 }
1522 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1523 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1524 renegotiation_info_seen == 1 )
1525 {
1526 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1527 handshake_failure = 1;
1528 }
1529
1530 if( handshake_failure == 1 )
1531 {
1532 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1533 return( ret );
1534
Paul Bakker48916f92012-09-16 19:57:18 +00001535 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1536 }
Paul Bakker380da532012-04-18 16:10:25 +00001537
Paul Bakker41c83d32013-03-20 14:39:14 +01001538 /*
1539 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001540 * (At the end because we need information from the EC-based extensions
1541 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001542 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001543 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001544 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001545#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1546 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
1547 {
1548 for( i = 0; ciphersuites[i] != 0; i++ )
1549#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001550 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001551 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001552 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001553#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001554 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001555 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1556 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1557 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001558
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001559 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1560 &ciphersuite_info ) ) != 0 )
1561 return( ret );
1562
1563 if( ciphersuite_info != NULL )
1564 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001565 }
1566 }
1567
1568 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1569
1570 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1571 return( ret );
1572
1573 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1574
1575have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001576 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001577 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1578 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1579
Paul Bakker5121ce52009-01-03 21:22:43 +00001580 ssl->in_left = 0;
1581 ssl->state++;
1582
1583 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1584
1585 return( 0 );
1586}
1587
Paul Bakker1f2bc622013-08-15 13:45:55 +02001588#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001589static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1590 unsigned char *buf,
1591 size_t *olen )
1592{
1593 unsigned char *p = buf;
1594
1595 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1596 {
1597 *olen = 0;
1598 return;
1599 }
1600
1601 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1602
1603 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1604 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1605
1606 *p++ = 0x00;
1607 *p++ = 0x00;
1608
1609 *olen = 4;
1610}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001611#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001612
Paul Bakkera503a632013-08-14 13:48:06 +02001613#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001614static void ssl_write_session_ticket_ext( ssl_context *ssl,
1615 unsigned char *buf,
1616 size_t *olen )
1617{
1618 unsigned char *p = buf;
1619
1620 if( ssl->handshake->new_session_ticket == 0 )
1621 {
1622 *olen = 0;
1623 return;
1624 }
1625
1626 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1627
1628 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1629 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1630
1631 *p++ = 0x00;
1632 *p++ = 0x00;
1633
1634 *olen = 4;
1635}
Paul Bakkera503a632013-08-14 13:48:06 +02001636#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001637
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001638static void ssl_write_renegotiation_ext( ssl_context *ssl,
1639 unsigned char *buf,
1640 size_t *olen )
1641{
1642 unsigned char *p = buf;
1643
1644 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1645 {
1646 *olen = 0;
1647 return;
1648 }
1649
1650 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1651
1652 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1653 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1654
1655 *p++ = 0x00;
1656 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1657 *p++ = ssl->verify_data_len * 2 & 0xFF;
1658
1659 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1660 p += ssl->verify_data_len;
1661 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1662 p += ssl->verify_data_len;
1663
1664 *olen = 5 + ssl->verify_data_len * 2;
1665}
1666
Paul Bakker05decb22013-08-15 13:33:48 +02001667#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001668static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1669 unsigned char *buf,
1670 size_t *olen )
1671{
1672 unsigned char *p = buf;
1673
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001674 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1675 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001676 *olen = 0;
1677 return;
1678 }
1679
1680 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1681
1682 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1683 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1684
1685 *p++ = 0x00;
1686 *p++ = 1;
1687
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001688 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001689
1690 *olen = 5;
1691}
Paul Bakker05decb22013-08-15 13:33:48 +02001692#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001693
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001694#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001695static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1696 unsigned char *buf,
1697 size_t *olen )
1698{
1699 unsigned char *p = buf;
1700 ((void) ssl);
1701
Paul Bakker677377f2013-10-28 12:54:26 +01001702 if( ( ssl->handshake->cli_exts &
1703 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
1704 {
1705 *olen = 0;
1706 return;
1707 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001708
1709 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1710
1711 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1712 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1713
1714 *p++ = 0x00;
1715 *p++ = 2;
1716
1717 *p++ = 1;
1718 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1719
1720 *olen = 6;
1721}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001722#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001723
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001724#if defined(POLARSSL_SSL_ALPN )
1725static void ssl_write_alpn_ext( ssl_context *ssl,
1726 unsigned char *buf, size_t *olen )
1727{
1728 if( ssl->alpn_chosen == NULL )
1729 {
1730 *olen = 0;
1731 return;
1732 }
1733
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001734 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001735
1736 /*
1737 * 0 . 1 ext identifier
1738 * 2 . 3 ext length
1739 * 4 . 5 protocol list length
1740 * 6 . 6 protocol name length
1741 * 7 . 7+n protocol name
1742 */
1743 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
1744 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
1745
1746 *olen = 7 + strlen( ssl->alpn_chosen );
1747
1748 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
1749 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
1750
1751 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
1752 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
1753
1754 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
1755
1756 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
1757}
1758#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
1759
Paul Bakker5121ce52009-01-03 21:22:43 +00001760static int ssl_write_server_hello( ssl_context *ssl )
1761{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001762#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001763 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001764#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001765 int ret;
1766 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001767 unsigned char *buf, *p;
1768
1769 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1770
Paul Bakkera9a028e2013-11-21 17:31:06 +01001771 if( ssl->f_rng == NULL )
1772 {
1773 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
1774 return( POLARSSL_ERR_SSL_NO_RNG );
1775 }
1776
Paul Bakker5121ce52009-01-03 21:22:43 +00001777 /*
1778 * 0 . 0 handshake type
1779 * 1 . 3 handshake length
1780 * 4 . 5 protocol version
1781 * 6 . 9 UNIX time()
1782 * 10 . 37 random bytes
1783 */
1784 buf = ssl->out_msg;
1785 p = buf + 4;
1786
1787 *p++ = (unsigned char) ssl->major_ver;
1788 *p++ = (unsigned char) ssl->minor_ver;
1789
1790 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1791 buf[4], buf[5] ) );
1792
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001793#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001794 t = time( NULL );
1795 *p++ = (unsigned char)( t >> 24 );
1796 *p++ = (unsigned char)( t >> 16 );
1797 *p++ = (unsigned char)( t >> 8 );
1798 *p++ = (unsigned char)( t );
1799
1800 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001801#else
1802 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1803 return( ret );
1804
1805 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02001806#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001807
Paul Bakkera3d195c2011-11-27 21:07:34 +00001808 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1809 return( ret );
1810
1811 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001812
Paul Bakker48916f92012-09-16 19:57:18 +00001813 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001814
1815 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1816
1817 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001818 * Resume is 0 by default, see ssl_handshake_init().
1819 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1820 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001821 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001822 if( ssl->handshake->resume == 0 &&
1823 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001824 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001825 ssl->f_get_cache != NULL &&
1826 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1827 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01001828 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001829 ssl->handshake->resume = 1;
1830 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001831
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001832 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001833 {
1834 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001835 * New session, create a new session id,
1836 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001837 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001838 ssl->state++;
1839
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001840#if defined(POLARSSL_HAVE_TIME)
1841 ssl->session_negotiate->start = time( NULL );
1842#endif
1843
Paul Bakkera503a632013-08-14 13:48:06 +02001844#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001845 if( ssl->handshake->new_session_ticket != 0 )
1846 {
1847 ssl->session_negotiate->length = n = 0;
1848 memset( ssl->session_negotiate->id, 0, 32 );
1849 }
1850 else
1851#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001852 {
1853 ssl->session_negotiate->length = n = 32;
1854 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001855 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001856 return( ret );
1857 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001858 }
1859 else
1860 {
1861 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001862 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001863 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001864 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001865 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001866
1867 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1868 {
1869 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1870 return( ret );
1871 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001872 }
1873
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001874 /*
1875 * 38 . 38 session id length
1876 * 39 . 38+n session id
1877 * 39+n . 40+n chosen ciphersuite
1878 * 41+n . 41+n chosen compression alg.
1879 * 42+n . 43+n extensions length
1880 * 44+n . 43+n+m extensions
1881 */
1882 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001883 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1884 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001885
1886 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1887 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1888 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001889 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001890
Paul Bakker48916f92012-09-16 19:57:18 +00001891 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1892 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1893 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001894
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001895 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
1896 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001897 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001898 ssl->session_negotiate->compression ) );
1899
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001900 /*
1901 * First write extensions, then the total length
1902 */
1903 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1904 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001905
Paul Bakker05decb22013-08-15 13:33:48 +02001906#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001907 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1908 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001909#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001910
Paul Bakker1f2bc622013-08-15 13:45:55 +02001911#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001912 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1913 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001914#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001915
Paul Bakkera503a632013-08-14 13:48:06 +02001916#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001917 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1918 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001919#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001920
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001921#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001922 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1923 ext_len += olen;
1924#endif
1925
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001926#if defined(POLARSSL_SSL_ALPN)
1927 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
1928 ext_len += olen;
1929#endif
1930
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001931 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001932
Paul Bakkera7036632014-04-30 10:15:38 +02001933 if( ext_len > 0 )
1934 {
1935 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1936 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1937 p += ext_len;
1938 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001939
1940 ssl->out_msglen = p - buf;
1941 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1942 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1943
1944 ret = ssl_write_record( ssl );
1945
1946 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1947
1948 return( ret );
1949}
1950
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001951#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1952 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001953 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1954 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001955static int ssl_write_certificate_request( ssl_context *ssl )
1956{
Paul Bakkered27a042013-04-18 22:46:23 +02001957 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001958
1959 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1960
1961 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01001962 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001963 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1964 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001965 {
1966 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1967 ssl->state++;
1968 return( 0 );
1969 }
1970
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001971 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1972 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001973}
1974#else
1975static int ssl_write_certificate_request( ssl_context *ssl )
1976{
1977 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1978 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001979 size_t dn_size, total_dn_size; /* excluding length bytes */
1980 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00001981 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001982 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001983
1984 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1985
1986 ssl->state++;
1987
Paul Bakkerfbb17802013-04-17 19:10:21 +02001988 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01001989 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001990 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001991 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001992 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001993 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001994 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001995 return( 0 );
1996 }
1997
1998 /*
1999 * 0 . 0 handshake type
2000 * 1 . 3 handshake length
2001 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002002 * 5 .. m-1 cert types
2003 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002004 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002005 * n .. n+1 length of all DNs
2006 * n+2 .. n+3 length of DN 1
2007 * n+4 .. ... Distinguished Name #1
2008 * ... .. ... length of DN 2, etc.
2009 */
2010 buf = ssl->out_msg;
2011 p = buf + 4;
2012
2013 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002014 * Supported certificate types
2015 *
2016 * ClientCertificateType certificate_types<1..2^8-1>;
2017 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002018 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002019 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002020
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002021#if defined(POLARSSL_RSA_C)
2022 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2023#endif
2024#if defined(POLARSSL_ECDSA_C)
2025 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2026#endif
2027
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002028 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002029 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002030
Paul Bakker577e0062013-08-28 11:57:20 +02002031 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002032#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002033 /*
2034 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002035 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002036 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2037 *
2038 * struct {
2039 * HashAlgorithm hash;
2040 * SignatureAlgorithm signature;
2041 * } SignatureAndHashAlgorithm;
2042 *
2043 * enum { (255) } HashAlgorithm;
2044 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002045 */
Paul Bakker21dca692013-01-03 11:41:08 +01002046 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002047 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002048 /*
2049 * Only use current running hash algorithm that is already required
2050 * for requested ciphersuite.
2051 */
Paul Bakker926af752012-11-23 13:38:07 +01002052 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2053
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002054 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2055 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002056 {
2057 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2058 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002059
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002060 /*
2061 * Supported signature algorithms
2062 */
2063#if defined(POLARSSL_RSA_C)
2064 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2065 p[2 + sa_len++] = SSL_SIG_RSA;
2066#endif
2067#if defined(POLARSSL_ECDSA_C)
2068 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2069 p[2 + sa_len++] = SSL_SIG_ECDSA;
2070#endif
Paul Bakker926af752012-11-23 13:38:07 +01002071
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002072 p[0] = (unsigned char)( sa_len >> 8 );
2073 p[1] = (unsigned char)( sa_len );
2074 sa_len += 2;
2075 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002076 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002077#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002078
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002079 /*
2080 * DistinguishedName certificate_authorities<0..2^16-1>;
2081 * opaque DistinguishedName<1..2^16-1>;
2082 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002083 p += 2;
2084 crt = ssl->ca_chain;
2085
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002086 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002087 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002088 {
2089 if( p - buf > 4096 )
2090 break;
2091
Paul Bakker926af752012-11-23 13:38:07 +01002092 dn_size = crt->subject_raw.len;
2093 *p++ = (unsigned char)( dn_size >> 8 );
2094 *p++ = (unsigned char)( dn_size );
2095 memcpy( p, crt->subject_raw.p, dn_size );
2096 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002097
Paul Bakker926af752012-11-23 13:38:07 +01002098 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2099
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002100 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002101 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002102 }
2103
Paul Bakker926af752012-11-23 13:38:07 +01002104 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2106 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002107 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2108 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002109
2110 ret = ssl_write_record( ssl );
2111
2112 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2113
2114 return( ret );
2115}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002116#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2117 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002118 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2119 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002120
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002121#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2122 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2123static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2124{
2125 int ret;
2126
2127 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2128 {
2129 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2130 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2131 }
2132
2133 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2134 pk_ec( *ssl_own_key( ssl ) ),
2135 POLARSSL_ECDH_OURS ) ) != 0 )
2136 {
2137 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2138 return( ret );
2139 }
2140
2141 return( 0 );
2142}
2143#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2144 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2145
Paul Bakker41c83d32013-03-20 14:39:14 +01002146static int ssl_write_server_key_exchange( ssl_context *ssl )
2147{
Paul Bakker23986e52011-04-24 08:57:21 +00002148 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002149 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002150 const ssl_ciphersuite_t *ciphersuite_info =
2151 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002152
2153#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2154 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2155 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002156 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002157 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002158 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002159 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002160 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002161 ((void) dig_signed);
2162 ((void) dig_signed_len);
2163#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002164
Paul Bakker5121ce52009-01-03 21:22:43 +00002165 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2166
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002167#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2168 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2169 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002170 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2171 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2172 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002173 {
2174 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2175 ssl->state++;
2176 return( 0 );
2177 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002178#endif
2179
2180#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2181 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2182 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2183 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2184 {
2185 ssl_get_ecdh_params_from_cert( ssl );
2186
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002187 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002188 ssl->state++;
2189 return( 0 );
2190 }
2191#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002192
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002193#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2194 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2195 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2196 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002197 {
2198 /* TODO: Support identity hints */
2199 *(p++) = 0x00;
2200 *(p++) = 0x00;
2201
2202 n += 2;
2203 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002204#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2205 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002206
2207#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2208 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2209 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2210 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002211 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002212 /*
2213 * Ephemeral DH parameters:
2214 *
2215 * struct {
2216 * opaque dh_p<1..2^16-1>;
2217 * opaque dh_g<1..2^16-1>;
2218 * opaque dh_Ys<1..2^16-1>;
2219 * } ServerDHParams;
2220 */
2221 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2222 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2223 {
2224 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2225 return( ret );
2226 }
Paul Bakker48916f92012-09-16 19:57:18 +00002227
Paul Bakker41c83d32013-03-20 14:39:14 +01002228 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002229 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2230 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002231 {
2232 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2233 return( ret );
2234 }
2235
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002236 dig_signed = p;
2237 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002238
2239 p += len;
2240 n += len;
2241
Paul Bakker41c83d32013-03-20 14:39:14 +01002242 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2243 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2244 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2245 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2246 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002247#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2248 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002249
Gergely Budai987bfb52014-01-19 21:48:42 +01002250#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002251 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002252 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2253 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002254 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002255 /*
2256 * Ephemeral ECDH parameters:
2257 *
2258 * struct {
2259 * ECParameters curve_params;
2260 * ECPoint public;
2261 * } ServerECDHParams;
2262 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002263 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002264#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002265 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002266
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002267 /* Match our preference list against the offered curves */
2268 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2269 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2270 if( (*curve)->grp_id == *gid )
2271 goto curve_matching_done;
2272
2273curve_matching_done:
2274#else
2275 curve = ssl->handshake->curves;
2276#endif
2277
2278 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002279 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002280 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2281 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002282 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002283
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002284 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002285
Paul Bakker41c83d32013-03-20 14:39:14 +01002286 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002287 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002288 {
2289 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2290 return( ret );
2291 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002292
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002293 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2294 p, SSL_MAX_CONTENT_LEN - n,
2295 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002296 {
2297 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2298 return( ret );
2299 }
2300
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002301 dig_signed = p;
2302 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002303
2304 p += len;
2305 n += len;
2306
Paul Bakker41c83d32013-03-20 14:39:14 +01002307 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2308 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002309#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002310
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002311#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002312 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2313 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002314 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002315 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2316 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002317 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002318 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002319 unsigned int hashlen = 0;
2320 unsigned char hash[64];
2321 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002322
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002323 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002324 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2325 */
Paul Bakker577e0062013-08-28 11:57:20 +02002326#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002327 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2328 {
2329 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2330
2331 if( md_alg == POLARSSL_MD_NONE )
2332 {
2333 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002334 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002335 }
2336 }
Paul Bakker577e0062013-08-28 11:57:20 +02002337 else
2338#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002339#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2340 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker577e0062013-08-28 11:57:20 +02002341 if ( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002342 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2343 {
2344 md_alg = POLARSSL_MD_SHA1;
2345 }
2346 else
Paul Bakker577e0062013-08-28 11:57:20 +02002347#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002348 {
2349 md_alg = POLARSSL_MD_NONE;
2350 }
2351
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002352 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002353 * Compute the hash to be signed
2354 */
Paul Bakker577e0062013-08-28 11:57:20 +02002355#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2356 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002357 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002358 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002359 md5_context md5;
2360 sha1_context sha1;
2361
2362 /*
2363 * digitally-signed struct {
2364 * opaque md5_hash[16];
2365 * opaque sha_hash[20];
2366 * };
2367 *
2368 * md5_hash
2369 * MD5(ClientHello.random + ServerHello.random
2370 * + ServerParams);
2371 * sha_hash
2372 * SHA(ClientHello.random + ServerHello.random
2373 * + ServerParams);
2374 */
2375 md5_starts( &md5 );
2376 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002377 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002378 md5_finish( &md5, hash );
2379
2380 sha1_starts( &sha1 );
2381 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002382 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002383 sha1_finish( &sha1, hash + 16 );
2384
2385 hashlen = 36;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002386 }
2387 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002388#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2389 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002390#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2391 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002392 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002393 {
2394 md_context_t ctx;
2395
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002396 /* Info from md_alg will be used instead */
2397 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002398
2399 /*
2400 * digitally-signed struct {
2401 * opaque client_random[32];
2402 * opaque server_random[32];
2403 * ServerDHParams params;
2404 * };
2405 */
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002406 if( ( ret = md_init_ctx( &ctx, md_info_from_type(md_alg) ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002407 {
2408 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2409 return( ret );
2410 }
2411
2412 md_starts( &ctx );
2413 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002414 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002415 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02002416
2417 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
2418 {
2419 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
2420 return( ret );
2421 }
2422
Paul Bakker23f36802012-09-28 14:15:14 +00002423 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002424 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002425#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2426 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002427 {
2428 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002429 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002430 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002431
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002432 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2433 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002434
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002435 /*
2436 * Make the signature
2437 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002438 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002439 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002440 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2441 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002442 }
Paul Bakker23f36802012-09-28 14:15:14 +00002443
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002444#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002445 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2446 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002447 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002448 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002449
2450 n += 2;
2451 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002452#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002453
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002454 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002455 p + 2 , &signature_len,
2456 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002457 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002458 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002459 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002460 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002461
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002462 *(p++) = (unsigned char)( signature_len >> 8 );
2463 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002464 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002465
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002466 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002467
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002468 p += signature_len;
2469 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002470 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002471#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002472 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2473 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002474
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002475 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002476 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2477 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2478
2479 ssl->state++;
2480
2481 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2482 {
2483 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2484 return( ret );
2485 }
2486
2487 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2488
2489 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002490}
2491
2492static int ssl_write_server_hello_done( ssl_context *ssl )
2493{
2494 int ret;
2495
2496 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2497
2498 ssl->out_msglen = 4;
2499 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2500 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2501
2502 ssl->state++;
2503
2504 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2505 {
2506 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2507 return( ret );
2508 }
2509
2510 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2511
2512 return( 0 );
2513}
2514
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002515#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2516 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2517static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2518 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002519{
2520 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002521 size_t n;
2522
2523 /*
2524 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2525 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002526 if( *p + 2 > end )
2527 {
2528 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2529 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2530 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002531
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002532 n = ( (*p)[0] << 8 ) | (*p)[1];
2533 *p += 2;
2534
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002535 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002536 {
2537 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2538 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2539 }
2540
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002541 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002542 {
2543 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2544 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2545 }
2546
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002547 *p += n;
2548
Paul Bakker70df2fb2013-04-17 17:19:09 +02002549 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2550
Paul Bakker70df2fb2013-04-17 17:19:09 +02002551 return( ret );
2552}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002553#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2554 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002555
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002556#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2557 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002558static int ssl_parse_encrypted_pms( ssl_context *ssl,
2559 const unsigned char *p,
2560 const unsigned char *end,
2561 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002562{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002563 int ret;
2564 size_t len = pk_get_len( ssl_own_key( ssl ) );
2565 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002566
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002567 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002568 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002569 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002570 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2571 }
2572
2573 /*
2574 * Decrypt the premaster using own private RSA key
2575 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002576#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2577 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002578 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2579 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002580 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2581 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002582 {
2583 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2584 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2585 }
2586 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002587#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002588
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002589 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002590 {
2591 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2592 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2593 }
2594
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002595 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2596 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002597 sizeof( ssl->handshake->premaster ) - pms_offset,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002598 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002599
2600 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002601 pms[0] != ssl->handshake->max_major_ver ||
2602 pms[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002603 {
2604 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2605
2606 /*
2607 * Protection against Bleichenbacher's attack:
2608 * invalid PKCS#1 v1.5 padding must not cause
2609 * the connection to end immediately; instead,
2610 * send a bad_record_mac later in the handshake.
2611 */
2612 ssl->handshake->pmslen = 48;
2613
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002614 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002615 if( ret != 0 )
2616 return( ret );
2617 }
2618
2619 return( ret );
2620}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002621#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2622 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002623
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002624#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002625static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2626 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002627{
Paul Bakker6db455e2013-09-18 17:29:31 +02002628 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002629 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002630
Paul Bakker6db455e2013-09-18 17:29:31 +02002631 if( ssl->f_psk == NULL &&
2632 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2633 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002634 {
2635 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2636 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2637 }
2638
2639 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002640 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002641 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002642 if( *p + 2 > end )
2643 {
2644 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2645 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2646 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002647
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002648 n = ( (*p)[0] << 8 ) | (*p)[1];
2649 *p += 2;
2650
2651 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002652 {
2653 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2654 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2655 }
2656
Paul Bakker6db455e2013-09-18 17:29:31 +02002657 if( ssl->f_psk != NULL )
2658 {
2659 if( ( ret != ssl->f_psk( ssl->p_psk, ssl, *p, n ) ) != 0 )
2660 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2661 }
2662
2663 if( ret == 0 )
2664 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002665 /* Identity is not a big secret since clients send it in the clear,
2666 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02002667 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002668 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002669 {
2670 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2671 }
2672 }
2673
2674 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002675 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002676 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002677 if( ( ret = ssl_send_alert_message( ssl,
2678 SSL_ALERT_LEVEL_FATAL,
2679 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2680 {
2681 return( ret );
2682 }
2683
2684 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002685 }
2686
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002687 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002688 ret = 0;
2689
Paul Bakkerfbb17802013-04-17 19:10:21 +02002690 return( ret );
2691}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002692#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002693
Paul Bakker5121ce52009-01-03 21:22:43 +00002694static int ssl_parse_client_key_exchange( ssl_context *ssl )
2695{
Paul Bakker23986e52011-04-24 08:57:21 +00002696 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002697 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002698
Paul Bakker41c83d32013-03-20 14:39:14 +01002699 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002700
2701 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2702
2703 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2704 {
2705 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2706 return( ret );
2707 }
2708
2709 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2710 {
2711 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002712 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002713 }
2714
2715 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2716 {
2717 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002718 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002719 }
2720
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002721#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002722 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002723 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002724 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002725 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002726
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002727 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002728 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002729 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2730 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002731 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002732
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002733 if( p != end )
2734 {
2735 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2736 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2737 }
2738
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002739 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
2740
2741 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2742 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002743 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002744 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002745 {
2746 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2747 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2748 }
2749
2750 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002751 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002752 else
2753#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002754#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002755 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2756 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2757 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002758 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002759 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2760 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2761 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002762 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002763 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002764 ssl->in_msg + 4, ssl->in_hslen - 4 ) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002765 {
2766 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2767 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2768 }
2769
2770 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2771
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002772 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2773 &ssl->handshake->pmslen,
2774 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002775 POLARSSL_MPI_MAX_SIZE,
2776 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002777 {
2778 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2779 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2780 }
2781
2782 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002783 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002784 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002785#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002786 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2787 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2788 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002789#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2790 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002791 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002792 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002793 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002794
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002795 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002796 {
2797 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2798 return( ret );
2799 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002800
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002801 if( p != end )
2802 {
2803 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2804 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2805 }
2806
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002807 if( ( ret = ssl_psk_derive_premaster( ssl,
2808 ciphersuite_info->key_exchange ) ) != 0 )
2809 {
2810 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2811 return( ret );
2812 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002813 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002814 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002815#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002816#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2817 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2818 {
2819 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002820 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002821
2822 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2823 {
2824 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2825 return( ret );
2826 }
2827
2828 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
2829 {
2830 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
2831 return( ret );
2832 }
2833
2834 if( ( ret = ssl_psk_derive_premaster( ssl,
2835 ciphersuite_info->key_exchange ) ) != 0 )
2836 {
2837 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2838 return( ret );
2839 }
2840 }
2841 else
2842#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002843#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2844 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2845 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002846 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002847 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002848
2849 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2850 {
2851 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2852 return( ret );
2853 }
2854 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2855 {
2856 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2857 return( ret );
2858 }
2859
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002860 if( p != end )
2861 {
2862 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2863 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2864 }
2865
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002866 if( ( ret = ssl_psk_derive_premaster( ssl,
2867 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002868 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002869 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2870 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002871 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002872 }
2873 else
2874#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002875#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2876 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2877 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002878 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002879 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002880
2881 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2882 {
2883 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2884 return( ret );
2885 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002886
2887 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2888 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002889 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002890 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2891 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002892 }
2893
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002894 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2895
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002896 if( ( ret = ssl_psk_derive_premaster( ssl,
2897 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002898 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002899 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002900 return( ret );
2901 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002902 }
2903 else
2904#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002905#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2906 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002907 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002908 if( ( ret = ssl_parse_encrypted_pms( ssl,
2909 ssl->in_msg + 4,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002910 ssl->in_msg + ssl->in_hslen,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002911 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002912 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002913 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002914 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002915 }
2916 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002917 else
2918#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2919 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002920 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002921 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002922 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002923
Paul Bakkerff60ee62010-03-16 21:09:09 +00002924 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2925 {
2926 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2927 return( ret );
2928 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002929
Paul Bakker5121ce52009-01-03 21:22:43 +00002930 ssl->state++;
2931
2932 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2933
2934 return( 0 );
2935}
2936
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002937#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2938 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002939 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2940 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002941static int ssl_parse_certificate_verify( ssl_context *ssl )
2942{
Paul Bakkered27a042013-04-18 22:46:23 +02002943 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002944 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002945
2946 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2947
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002948 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002949 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002950 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002951 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002952 {
2953 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2954 ssl->state++;
2955 return( 0 );
2956 }
2957
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002958 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2959 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002960}
2961#else
2962static int ssl_parse_certificate_verify( ssl_context *ssl )
2963{
2964 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002965 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002966 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002967 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002968 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02002969#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002970 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02002971#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002972 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002973 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2974
2975 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2976
2977 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002978 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002979 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002980 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2981 {
2982 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2983 ssl->state++;
2984 return( 0 );
2985 }
2986
Paul Bakkered27a042013-04-18 22:46:23 +02002987 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002988 {
2989 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2990 ssl->state++;
2991 return( 0 );
2992 }
2993
Paul Bakker48916f92012-09-16 19:57:18 +00002994 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002995
2996 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2997 {
2998 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2999 return( ret );
3000 }
3001
3002 ssl->state++;
3003
3004 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3005 {
3006 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003007 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003008 }
3009
3010 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
3011 {
3012 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003013 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003014 }
3015
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003016 /*
3017 * 0 . 0 handshake type
3018 * 1 . 3 handshake length
3019 * 4 . 5 sig alg (TLS 1.2 only)
3020 * 4+n . 5+n signature length (n = sa_len)
3021 * 6+n . 6+n+m signature (m = sig_len)
3022 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003023
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003024#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3025 defined(POLARSSL_SSL_PROTO_TLS1_1)
3026 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003027 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003028 sa_len = 0;
3029
Paul Bakkerc70b9822013-04-07 22:00:46 +02003030 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003031 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003032
3033 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3034 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3035 POLARSSL_PK_ECDSA ) )
3036 {
3037 hash_start += 16;
3038 hashlen -= 16;
3039 md_alg = POLARSSL_MD_SHA1;
3040 }
Paul Bakker926af752012-11-23 13:38:07 +01003041 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003042 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003043#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3044 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003045#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3046 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003047 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003048 sa_len = 2;
3049
Paul Bakker5121ce52009-01-03 21:22:43 +00003050 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003051 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003052 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003053 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003054 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003055 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3056 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003057 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3058 }
3059
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003060 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003061
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003062 /* Info from md_alg will be used instead */
3063 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003064
3065 /*
3066 * Signature
3067 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003068 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
3069 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003070 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003071 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3072 " for verify message" ) );
3073 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003074 }
3075
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003076 /*
3077 * Check the certificate's key type matches the signature alg
3078 */
3079 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3080 {
3081 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3082 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3083 }
Paul Bakker577e0062013-08-28 11:57:20 +02003084 }
3085 else
3086#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3087 {
3088 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003089 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003090 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003091
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003092 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01003093
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003094 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003095 {
3096 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003097 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003098 }
3099
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003100 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003101 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003102 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003103 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003104 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003105 return( ret );
3106 }
3107
3108 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3109
Paul Bakkered27a042013-04-18 22:46:23 +02003110 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003111}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003112#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3113 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3114 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003115
Paul Bakkera503a632013-08-14 13:48:06 +02003116#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003117static int ssl_write_new_session_ticket( ssl_context *ssl )
3118{
3119 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003120 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003121 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003122
3123 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3124
3125 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3126 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3127
3128 /*
3129 * struct {
3130 * uint32 ticket_lifetime_hint;
3131 * opaque ticket<0..2^16-1>;
3132 * } NewSessionTicket;
3133 *
3134 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3135 * 8 . 9 ticket_len (n)
3136 * 10 . 9+n ticket content
3137 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003138
3139 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3140 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3141 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3142 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003143
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003144 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3145 {
3146 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3147 tlen = 0;
3148 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003149
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003150 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3151 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003152
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003153 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003154
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003155 /*
3156 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3157 * ChangeCipherSpec share the same state.
3158 */
3159 ssl->handshake->new_session_ticket = 0;
3160
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003161 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3162 {
3163 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3164 return( ret );
3165 }
3166
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003167 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3168
3169 return( 0 );
3170}
Paul Bakkera503a632013-08-14 13:48:06 +02003171#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003172
Paul Bakker5121ce52009-01-03 21:22:43 +00003173/*
Paul Bakker1961b702013-01-25 14:49:24 +01003174 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003175 */
Paul Bakker1961b702013-01-25 14:49:24 +01003176int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003177{
3178 int ret = 0;
3179
Paul Bakker1961b702013-01-25 14:49:24 +01003180 if( ssl->state == SSL_HANDSHAKE_OVER )
3181 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003182
Paul Bakker1961b702013-01-25 14:49:24 +01003183 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3184
3185 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3186 return( ret );
3187
3188 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003189 {
Paul Bakker1961b702013-01-25 14:49:24 +01003190 case SSL_HELLO_REQUEST:
3191 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003192 break;
3193
Paul Bakker1961b702013-01-25 14:49:24 +01003194 /*
3195 * <== ClientHello
3196 */
3197 case SSL_CLIENT_HELLO:
3198 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003199 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003200
3201 /*
3202 * ==> ServerHello
3203 * Certificate
3204 * ( ServerKeyExchange )
3205 * ( CertificateRequest )
3206 * ServerHelloDone
3207 */
3208 case SSL_SERVER_HELLO:
3209 ret = ssl_write_server_hello( ssl );
3210 break;
3211
3212 case SSL_SERVER_CERTIFICATE:
3213 ret = ssl_write_certificate( ssl );
3214 break;
3215
3216 case SSL_SERVER_KEY_EXCHANGE:
3217 ret = ssl_write_server_key_exchange( ssl );
3218 break;
3219
3220 case SSL_CERTIFICATE_REQUEST:
3221 ret = ssl_write_certificate_request( ssl );
3222 break;
3223
3224 case SSL_SERVER_HELLO_DONE:
3225 ret = ssl_write_server_hello_done( ssl );
3226 break;
3227
3228 /*
3229 * <== ( Certificate/Alert )
3230 * ClientKeyExchange
3231 * ( CertificateVerify )
3232 * ChangeCipherSpec
3233 * Finished
3234 */
3235 case SSL_CLIENT_CERTIFICATE:
3236 ret = ssl_parse_certificate( ssl );
3237 break;
3238
3239 case SSL_CLIENT_KEY_EXCHANGE:
3240 ret = ssl_parse_client_key_exchange( ssl );
3241 break;
3242
3243 case SSL_CERTIFICATE_VERIFY:
3244 ret = ssl_parse_certificate_verify( ssl );
3245 break;
3246
3247 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3248 ret = ssl_parse_change_cipher_spec( ssl );
3249 break;
3250
3251 case SSL_CLIENT_FINISHED:
3252 ret = ssl_parse_finished( ssl );
3253 break;
3254
3255 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003256 * ==> ( NewSessionTicket )
3257 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003258 * Finished
3259 */
3260 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003261#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003262 if( ssl->handshake->new_session_ticket != 0 )
3263 ret = ssl_write_new_session_ticket( ssl );
3264 else
Paul Bakkera503a632013-08-14 13:48:06 +02003265#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003266 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003267 break;
3268
3269 case SSL_SERVER_FINISHED:
3270 ret = ssl_write_finished( ssl );
3271 break;
3272
3273 case SSL_FLUSH_BUFFERS:
3274 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3275 ssl->state = SSL_HANDSHAKE_WRAPUP;
3276 break;
3277
3278 case SSL_HANDSHAKE_WRAPUP:
3279 ssl_handshake_wrapup( ssl );
3280 break;
3281
3282 default:
3283 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3284 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003285 }
3286
Paul Bakker5121ce52009-01-03 21:22:43 +00003287 return( ret );
3288}
Paul Bakker9af723c2014-05-01 13:03:14 +02003289#endif /* POLARSSL_SSL_SRV_C */