blob: 515289510d4a3c165e5f31c5d163a59e9bdbdcd4 [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{
694 size_t list_len, cur_len;
695 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 {
725 for( theirs = start; theirs != end; theirs += cur_len )
726 {
727 /* If the list is well formed, we should get equality first */
728 if( theirs > end )
729 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
730
731 cur_len = *theirs++;
732
733 /* Empty strings MUST NOT be included */
734 if( cur_len == 0 )
735 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
736
737 if( cur_len == strlen( *ours ) &&
738 memcmp( theirs, *ours, cur_len ) == 0 )
739 {
740 ssl->alpn_chosen = *ours;
741 return( 0 );
742 }
743 }
744 }
745
746 /* If we get there, no match was found */
747 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
748 SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
749 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
750}
751#endif /* POLARSSL_SSL_ALPN */
752
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100753/*
754 * Auxiliary functions for ServerHello parsing and related actions
755 */
756
757#if defined(POLARSSL_X509_CRT_PARSE_C)
758/*
759 * Return 1 if the given EC key uses the given curve, 0 otherwise
760 */
761#if defined(POLARSSL_ECDSA_C)
762static int ssl_key_matches_curves( pk_context *pk,
763 const ecp_curve_info **curves )
764{
765 const ecp_curve_info **crv = curves;
766 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
767
768 while( *crv != NULL )
769 {
770 if( (*crv)->grp_id == grp_id )
771 return( 1 );
772 crv++;
773 }
774
775 return( 0 );
776}
777#endif /* POLARSSL_ECDSA_C */
778
779/*
780 * Try picking a certificate for this ciphersuite,
781 * return 0 on success and -1 on failure.
782 */
783static int ssl_pick_cert( ssl_context *ssl,
784 const ssl_ciphersuite_t * ciphersuite_info )
785{
786 ssl_key_cert *cur, *list;
787 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
788
789#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
790 if( ssl->handshake->sni_key_cert != NULL )
791 list = ssl->handshake->sni_key_cert;
792 else
793#endif
794 list = ssl->handshake->key_cert;
795
796 if( pk_alg == POLARSSL_PK_NONE )
797 return( 0 );
798
799 for( cur = list; cur != NULL; cur = cur->next )
800 {
801 if( ! pk_can_do( cur->key, pk_alg ) )
802 continue;
803
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200804 /*
805 * This avoids sending the client a cert it'll reject based on
806 * keyUsage or other extensions.
807 *
808 * It also allows the user to provision different certificates for
809 * different uses based on keyUsage, eg if they want to avoid signing
810 * and decrypting with the same RSA key.
811 */
812 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
813 SSL_IS_SERVER ) != 0 )
814 {
815 continue;
816 }
817
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100818#if defined(POLARSSL_ECDSA_C)
819 if( pk_alg == POLARSSL_PK_ECDSA )
820 {
821 if( ssl_key_matches_curves( cur->key, ssl->handshake->curves ) )
822 break;
823 }
824 else
825#endif
826 break;
827 }
828
829 if( cur == NULL )
830 return( -1 );
831
832 ssl->handshake->key_cert = cur;
833 return( 0 );
834}
835#endif /* POLARSSL_X509_CRT_PARSE_C */
836
837/*
838 * Check if a given ciphersuite is suitable for use with our config/keys/etc
839 * Sets ciphersuite_info only if the suite matches.
840 */
841static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
842 const ssl_ciphersuite_t **ciphersuite_info )
843{
844 const ssl_ciphersuite_t *suite_info;
845
846 suite_info = ssl_ciphersuite_from_id( suite_id );
847 if( suite_info == NULL )
848 {
849 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", suite_id ) );
850 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
851 }
852
853 if( suite_info->min_minor_ver > ssl->minor_ver ||
854 suite_info->max_minor_ver < ssl->minor_ver )
855 return( 0 );
856
857#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
858 if( ssl_ciphersuite_uses_ec( suite_info ) &&
859 ( ssl->handshake->curves == NULL ||
860 ssl->handshake->curves[0] == NULL ) )
861 return( 0 );
862#endif
863
864#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
865 /* If the ciphersuite requires a pre-shared key and we don't
866 * have one, skip it now rather than failing later */
867 if( ssl_ciphersuite_uses_psk( suite_info ) &&
868 ssl->f_psk == NULL &&
869 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
870 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
871 return( 0 );
872#endif
873
874#if defined(POLARSSL_X509_CRT_PARSE_C)
875 /*
876 * Final check: if ciphersuite requires us to have a
877 * certificate/key of a particular type:
878 * - select the appropriate certificate if we have one, or
879 * - try the next ciphersuite if we don't
880 * This must be done last since we modify the key_cert list.
881 */
882 if( ssl_pick_cert( ssl, suite_info ) != 0 )
883 return( 0 );
884#endif
885
886 *ciphersuite_info = suite_info;
887 return( 0 );
888}
889
Paul Bakker78a8c712013-03-06 17:01:52 +0100890#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
891static int ssl_parse_client_hello_v2( ssl_context *ssl )
892{
893 int ret;
894 unsigned int i, j;
895 size_t n;
896 unsigned int ciph_len, sess_len, chal_len;
897 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200898 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200899 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100900
901 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
902
903 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
904 {
905 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
906
907 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
908 return( ret );
909
910 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
911 }
912
913 buf = ssl->in_hdr;
914
915 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
916
917 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
918 buf[2] ) );
919 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
920 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
921 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
922 buf[3], buf[4] ) );
923
924 /*
925 * SSLv2 Client Hello
926 *
927 * Record layer:
928 * 0 . 1 message length
929 *
930 * SSL layer:
931 * 2 . 2 message type
932 * 3 . 4 protocol version
933 */
934 if( buf[2] != SSL_HS_CLIENT_HELLO ||
935 buf[3] != SSL_MAJOR_VERSION_3 )
936 {
937 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
938 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
939 }
940
941 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
942
943 if( n < 17 || n > 512 )
944 {
945 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
946 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
947 }
948
949 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200950 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
951 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100952
953 if( ssl->minor_ver < ssl->min_minor_ver )
954 {
955 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200956 " [%d:%d] < [%d:%d]",
957 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +0100958 ssl->min_major_ver, ssl->min_minor_ver ) );
959
960 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
961 SSL_ALERT_MSG_PROTOCOL_VERSION );
962 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
963 }
964
Paul Bakker2fbefde2013-06-29 16:01:15 +0200965 ssl->handshake->max_major_ver = buf[3];
966 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100967
968 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
969 {
970 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
971 return( ret );
972 }
973
974 ssl->handshake->update_checksum( ssl, buf + 2, n );
975
976 buf = ssl->in_msg;
977 n = ssl->in_left - 5;
978
979 /*
980 * 0 . 1 ciphersuitelist length
981 * 2 . 3 session id length
982 * 4 . 5 challenge length
983 * 6 . .. ciphersuitelist
984 * .. . .. session id
985 * .. . .. challenge
986 */
987 SSL_DEBUG_BUF( 4, "record contents", buf, n );
988
989 ciph_len = ( buf[0] << 8 ) | buf[1];
990 sess_len = ( buf[2] << 8 ) | buf[3];
991 chal_len = ( buf[4] << 8 ) | buf[5];
992
993 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
994 ciph_len, sess_len, chal_len ) );
995
996 /*
997 * Make sure each parameter length is valid
998 */
999 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1000 {
1001 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1002 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1003 }
1004
1005 if( sess_len > 32 )
1006 {
1007 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1008 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1009 }
1010
1011 if( chal_len < 8 || chal_len > 32 )
1012 {
1013 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1014 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1015 }
1016
1017 if( n != 6 + ciph_len + sess_len + chal_len )
1018 {
1019 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1020 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1021 }
1022
1023 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1024 buf + 6, ciph_len );
1025 SSL_DEBUG_BUF( 3, "client hello, session id",
1026 buf + 6 + ciph_len, sess_len );
1027 SSL_DEBUG_BUF( 3, "client hello, challenge",
1028 buf + 6 + ciph_len + sess_len, chal_len );
1029
1030 p = buf + 6 + ciph_len;
1031 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001032 memset( ssl->session_negotiate->id, 0,
1033 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001034 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1035
1036 p += sess_len;
1037 memset( ssl->handshake->randbytes, 0, 64 );
1038 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1039
1040 /*
1041 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1042 */
1043 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1044 {
1045 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1046 {
1047 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1048 if( ssl->renegotiation == SSL_RENEGOTIATION )
1049 {
1050 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1051
1052 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1053 return( ret );
1054
1055 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1056 }
1057 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1058 break;
1059 }
1060 }
1061
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001062 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001063 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001064#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1065 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1066 {
1067 for( i = 0; ciphersuites[i] != 0; i++ )
1068#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001069 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001070 {
1071 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001072#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001073 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001074 if( p[0] != 0 ||
1075 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1076 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1077 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001078
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001079 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1080 &ciphersuite_info ) ) != 0 )
1081 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001082
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001083 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001084 goto have_ciphersuite_v2;
1085 }
1086 }
1087
1088 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1089
1090 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1091
1092have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001093 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001094 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001095 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001096
1097 /*
1098 * SSLv2 Client Hello relevant renegotiation security checks
1099 */
1100 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1101 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1102 {
1103 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1104
1105 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1106 return( ret );
1107
1108 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1109 }
1110
1111 ssl->in_left = 0;
1112 ssl->state++;
1113
1114 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1115
1116 return( 0 );
1117}
1118#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1119
Paul Bakker5121ce52009-01-03 21:22:43 +00001120static int ssl_parse_client_hello( ssl_context *ssl )
1121{
Paul Bakker23986e52011-04-24 08:57:21 +00001122 int ret;
1123 unsigned int i, j;
1124 size_t n;
1125 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001126 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001127 unsigned int ext_len = 0;
1128 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001129 int renegotiation_info_seen = 0;
1130 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001131 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001132 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001133
1134 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1135
Paul Bakker48916f92012-09-16 19:57:18 +00001136 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1137 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001138 {
1139 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1140 return( ret );
1141 }
1142
1143 buf = ssl->in_hdr;
1144
Paul Bakker78a8c712013-03-06 17:01:52 +01001145#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1146 if( ( buf[0] & 0x80 ) != 0 )
1147 return ssl_parse_client_hello_v2( ssl );
1148#endif
1149
Paul Bakkerec636f32012-09-09 19:17:02 +00001150 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1151
1152 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1153 buf[0] ) );
1154 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1155 ( buf[3] << 8 ) | buf[4] ) );
1156 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
1157 buf[1], buf[2] ) );
1158
1159 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001160 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001161 *
1162 * Record layer:
1163 * 0 . 0 message type
1164 * 1 . 2 protocol version
1165 * 3 . 4 message length
1166 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001167
1168 /* According to RFC 5246 Appendix E.1, the version here is typically
1169 * "{03,00}, the lowest version number supported by the client, [or] the
1170 * value of ClientHello.client_version", so the only meaningful check here
1171 * is the major version shouldn't be less than 3 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001172 if( buf[0] != SSL_MSG_HANDSHAKE ||
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001173 buf[1] < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001174 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001175 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1176 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1177 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001178
Paul Bakkerec636f32012-09-09 19:17:02 +00001179 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +00001180
Paul Bakker4f42c112014-04-17 14:48:23 +02001181 if( n < 45 || n > SSL_MAX_CONTENT_LEN )
Paul Bakkerec636f32012-09-09 19:17:02 +00001182 {
1183 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1184 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1185 }
1186
Paul Bakker48916f92012-09-16 19:57:18 +00001187 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1188 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001189 {
1190 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1191 return( ret );
1192 }
1193
1194 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +00001195 if( !ssl->renegotiation )
1196 n = ssl->in_left - 5;
1197 else
1198 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +00001199
Paul Bakker48916f92012-09-16 19:57:18 +00001200 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +00001201
1202 /*
1203 * SSL layer:
1204 * 0 . 0 handshake type
1205 * 1 . 3 handshake length
1206 * 4 . 5 protocol version
1207 * 6 . 9 UNIX time()
1208 * 10 . 37 random bytes
1209 * 38 . 38 session id length
1210 * 39 . 38+x session id
1211 * 39+x . 40+x ciphersuitelist length
Paul Bakker0f651c72014-05-22 15:12:19 +02001212 * 41+x . 40+y ciphersuitelist
1213 * 41+y . 41+y compression alg length
1214 * 42+y . 41+z compression algs
Paul Bakkerec636f32012-09-09 19:17:02 +00001215 * .. . .. extensions
1216 */
1217 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1218
1219 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1220 buf[0] ) );
1221 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1222 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1223 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1224 buf[4], buf[5] ) );
1225
1226 /*
1227 * Check the handshake type and protocol version
1228 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001229 if( buf[0] != SSL_HS_CLIENT_HELLO )
Paul Bakkerec636f32012-09-09 19:17:02 +00001230 {
1231 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1232 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1233 }
1234
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001235 ssl->major_ver = buf[4];
1236 ssl->minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001237
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001238 ssl->handshake->max_major_ver = ssl->major_ver;
1239 ssl->handshake->max_minor_ver = ssl->minor_ver;
1240
1241 if( ssl->major_ver < ssl->min_major_ver ||
1242 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001243 {
1244 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001245 " [%d:%d] < [%d:%d]",
1246 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001247 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001248
1249 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1250 SSL_ALERT_MSG_PROTOCOL_VERSION );
1251
1252 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1253 }
1254
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001255 if( ssl->major_ver > ssl->max_major_ver )
1256 {
1257 ssl->major_ver = ssl->max_major_ver;
1258 ssl->minor_ver = ssl->max_minor_ver;
1259 }
1260 else if( ssl->minor_ver > ssl->max_minor_ver )
1261 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001262
Paul Bakker48916f92012-09-16 19:57:18 +00001263 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001264
1265 /*
1266 * Check the handshake message length
1267 */
1268 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1269 {
1270 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1271 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1272 }
1273
1274 /*
1275 * Check the session length
1276 */
1277 sess_len = buf[38];
1278
Paul Bakker0f651c72014-05-22 15:12:19 +02001279 if( sess_len > 32 || sess_len > n - 42 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001280 {
1281 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1282 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1283 }
1284
Paul Bakker48916f92012-09-16 19:57:18 +00001285 ssl->session_negotiate->length = sess_len;
1286 memset( ssl->session_negotiate->id, 0,
1287 sizeof( ssl->session_negotiate->id ) );
1288 memcpy( ssl->session_negotiate->id, buf + 39,
1289 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001290
1291 /*
1292 * Check the ciphersuitelist length
1293 */
1294 ciph_len = ( buf[39 + sess_len] << 8 )
1295 | ( buf[40 + sess_len] );
1296
Paul Bakker0f651c72014-05-22 15:12:19 +02001297 if( ciph_len < 2 || ( ciph_len % 2 ) != 0 || ciph_len > n - 42 - sess_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001298 {
1299 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1300 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1301 }
1302
1303 /*
1304 * Check the compression algorithms length
1305 */
1306 comp_len = buf[41 + sess_len + ciph_len];
1307
Paul Bakker0f651c72014-05-22 15:12:19 +02001308 if( comp_len < 1 || comp_len > 16 ||
1309 comp_len > n - 42 - sess_len - ciph_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001310 {
1311 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1312 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1313 }
1314
Paul Bakker48916f92012-09-16 19:57:18 +00001315 /*
1316 * Check the extension length
1317 */
1318 if( n > 42 + sess_len + ciph_len + comp_len )
1319 {
1320 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1321 | ( buf[43 + sess_len + ciph_len + comp_len] );
1322
1323 if( ( ext_len > 0 && ext_len < 4 ) ||
1324 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1325 {
1326 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1327 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1328 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1329 }
1330 }
1331
1332 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001333#if defined(POLARSSL_ZLIB_SUPPORT)
1334 for( i = 0; i < comp_len; ++i )
1335 {
Paul Bakker48916f92012-09-16 19:57:18 +00001336 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001337 {
Paul Bakker48916f92012-09-16 19:57:18 +00001338 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001339 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001340 }
1341 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001342#endif
1343
Paul Bakkerec636f32012-09-09 19:17:02 +00001344 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1345 buf + 6, 32 );
1346 SSL_DEBUG_BUF( 3, "client hello, session id",
1347 buf + 38, sess_len );
1348 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1349 buf + 41 + sess_len, ciph_len );
1350 SSL_DEBUG_BUF( 3, "client hello, compression",
1351 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001352
Paul Bakkerec636f32012-09-09 19:17:02 +00001353 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001354 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1355 */
1356 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1357 {
1358 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1359 {
1360 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1361 if( ssl->renegotiation == SSL_RENEGOTIATION )
1362 {
1363 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001364
1365 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1366 return( ret );
1367
Paul Bakker48916f92012-09-16 19:57:18 +00001368 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1369 }
1370 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1371 break;
1372 }
1373 }
1374
Paul Bakker48916f92012-09-16 19:57:18 +00001375 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001376
1377 while( ext_len )
1378 {
1379 unsigned int ext_id = ( ( ext[0] << 8 )
1380 | ( ext[1] ) );
1381 unsigned int ext_size = ( ( ext[2] << 8 )
1382 | ( ext[3] ) );
1383
1384 if( ext_size + 4 > ext_len )
1385 {
1386 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1387 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1388 }
1389 switch( ext_id )
1390 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001391#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001392 case TLS_EXT_SERVERNAME:
1393 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1394 if( ssl->f_sni == NULL )
1395 break;
1396
1397 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1398 if( ret != 0 )
1399 return( ret );
1400 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001401#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001402
Paul Bakker48916f92012-09-16 19:57:18 +00001403 case TLS_EXT_RENEGOTIATION_INFO:
1404 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1405 renegotiation_info_seen = 1;
1406
Paul Bakker23f36802012-09-28 14:15:14 +00001407 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1408 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001409 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001410 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001411
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001412#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001413 case TLS_EXT_SIG_ALG:
1414 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1415 if( ssl->renegotiation == SSL_RENEGOTIATION )
1416 break;
1417
1418 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1419 if( ret != 0 )
1420 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001421 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001422#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001423
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001424#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001425 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1426 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1427
1428 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1429 if( ret != 0 )
1430 return( ret );
1431 break;
1432
1433 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1434 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001435 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001436
1437 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1438 if( ret != 0 )
1439 return( ret );
1440 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001441#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001442
Paul Bakker05decb22013-08-15 13:33:48 +02001443#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001444 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1445 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1446
1447 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1448 if( ret != 0 )
1449 return( ret );
1450 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001451#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001452
Paul Bakker1f2bc622013-08-15 13:45:55 +02001453#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001454 case TLS_EXT_TRUNCATED_HMAC:
1455 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1456
1457 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1458 if( ret != 0 )
1459 return( ret );
1460 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001461#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001462
Paul Bakkera503a632013-08-14 13:48:06 +02001463#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001464 case TLS_EXT_SESSION_TICKET:
1465 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1466
1467 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1468 if( ret != 0 )
1469 return( ret );
1470 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001471#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001472
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001473#if defined(POLARSSL_SSL_ALPN)
1474 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001475 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001476
1477 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1478 if( ret != 0 )
1479 return( ret );
1480 break;
1481#endif /* POLARSSL_SSL_SESSION_TICKETS */
1482
Paul Bakker48916f92012-09-16 19:57:18 +00001483 default:
1484 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1485 ext_id ) );
1486 }
1487
1488 ext_len -= 4 + ext_size;
1489 ext += 4 + ext_size;
1490
1491 if( ext_len > 0 && ext_len < 4 )
1492 {
1493 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1494 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1495 }
1496 }
1497
1498 /*
1499 * Renegotiation security checks
1500 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001501 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1502 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1503 {
1504 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1505 handshake_failure = 1;
1506 }
1507 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1508 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1509 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001510 {
1511 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001512 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001513 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001514 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1515 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1516 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001517 {
1518 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001519 handshake_failure = 1;
1520 }
1521 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1522 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1523 renegotiation_info_seen == 1 )
1524 {
1525 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1526 handshake_failure = 1;
1527 }
1528
1529 if( handshake_failure == 1 )
1530 {
1531 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1532 return( ret );
1533
Paul Bakker48916f92012-09-16 19:57:18 +00001534 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1535 }
Paul Bakker380da532012-04-18 16:10:25 +00001536
Paul Bakker41c83d32013-03-20 14:39:14 +01001537 /*
1538 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001539 * (At the end because we need information from the EC-based extensions
1540 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001541 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001542 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001543 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001544#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1545 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
1546 {
1547 for( i = 0; ciphersuites[i] != 0; i++ )
1548#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001549 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001550 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001551 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001552#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001553 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001554 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1555 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1556 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001557
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001558 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1559 &ciphersuite_info ) ) != 0 )
1560 return( ret );
1561
1562 if( ciphersuite_info != NULL )
1563 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001564 }
1565 }
1566
1567 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1568
1569 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1570 return( ret );
1571
1572 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1573
1574have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001575 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001576 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1577 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1578
Paul Bakker5121ce52009-01-03 21:22:43 +00001579 ssl->in_left = 0;
1580 ssl->state++;
1581
1582 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1583
1584 return( 0 );
1585}
1586
Paul Bakker1f2bc622013-08-15 13:45:55 +02001587#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001588static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1589 unsigned char *buf,
1590 size_t *olen )
1591{
1592 unsigned char *p = buf;
1593
1594 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1595 {
1596 *olen = 0;
1597 return;
1598 }
1599
1600 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1601
1602 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1603 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1604
1605 *p++ = 0x00;
1606 *p++ = 0x00;
1607
1608 *olen = 4;
1609}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001610#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001611
Paul Bakkera503a632013-08-14 13:48:06 +02001612#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001613static void ssl_write_session_ticket_ext( ssl_context *ssl,
1614 unsigned char *buf,
1615 size_t *olen )
1616{
1617 unsigned char *p = buf;
1618
1619 if( ssl->handshake->new_session_ticket == 0 )
1620 {
1621 *olen = 0;
1622 return;
1623 }
1624
1625 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1626
1627 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1628 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1629
1630 *p++ = 0x00;
1631 *p++ = 0x00;
1632
1633 *olen = 4;
1634}
Paul Bakkera503a632013-08-14 13:48:06 +02001635#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001636
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001637static void ssl_write_renegotiation_ext( ssl_context *ssl,
1638 unsigned char *buf,
1639 size_t *olen )
1640{
1641 unsigned char *p = buf;
1642
1643 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1644 {
1645 *olen = 0;
1646 return;
1647 }
1648
1649 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1650
1651 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1652 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1653
1654 *p++ = 0x00;
1655 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1656 *p++ = ssl->verify_data_len * 2 & 0xFF;
1657
1658 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1659 p += ssl->verify_data_len;
1660 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1661 p += ssl->verify_data_len;
1662
1663 *olen = 5 + ssl->verify_data_len * 2;
1664}
1665
Paul Bakker05decb22013-08-15 13:33:48 +02001666#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001667static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1668 unsigned char *buf,
1669 size_t *olen )
1670{
1671 unsigned char *p = buf;
1672
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001673 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1674 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001675 *olen = 0;
1676 return;
1677 }
1678
1679 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1680
1681 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1682 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1683
1684 *p++ = 0x00;
1685 *p++ = 1;
1686
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001687 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001688
1689 *olen = 5;
1690}
Paul Bakker05decb22013-08-15 13:33:48 +02001691#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001692
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001693#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001694static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1695 unsigned char *buf,
1696 size_t *olen )
1697{
1698 unsigned char *p = buf;
1699 ((void) ssl);
1700
Paul Bakker677377f2013-10-28 12:54:26 +01001701 if( ( ssl->handshake->cli_exts &
1702 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
1703 {
1704 *olen = 0;
1705 return;
1706 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001707
1708 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1709
1710 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1711 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1712
1713 *p++ = 0x00;
1714 *p++ = 2;
1715
1716 *p++ = 1;
1717 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1718
1719 *olen = 6;
1720}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001721#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001722
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001723#if defined(POLARSSL_SSL_ALPN )
1724static void ssl_write_alpn_ext( ssl_context *ssl,
1725 unsigned char *buf, size_t *olen )
1726{
1727 if( ssl->alpn_chosen == NULL )
1728 {
1729 *olen = 0;
1730 return;
1731 }
1732
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001733 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001734
1735 /*
1736 * 0 . 1 ext identifier
1737 * 2 . 3 ext length
1738 * 4 . 5 protocol list length
1739 * 6 . 6 protocol name length
1740 * 7 . 7+n protocol name
1741 */
1742 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
1743 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
1744
1745 *olen = 7 + strlen( ssl->alpn_chosen );
1746
1747 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
1748 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
1749
1750 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
1751 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
1752
1753 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
1754
1755 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
1756}
1757#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
1758
Paul Bakker5121ce52009-01-03 21:22:43 +00001759static int ssl_write_server_hello( ssl_context *ssl )
1760{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001761#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001762 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001763#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001764 int ret;
1765 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001766 unsigned char *buf, *p;
1767
1768 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1769
Paul Bakkera9a028e2013-11-21 17:31:06 +01001770 if( ssl->f_rng == NULL )
1771 {
1772 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
1773 return( POLARSSL_ERR_SSL_NO_RNG );
1774 }
1775
Paul Bakker5121ce52009-01-03 21:22:43 +00001776 /*
1777 * 0 . 0 handshake type
1778 * 1 . 3 handshake length
1779 * 4 . 5 protocol version
1780 * 6 . 9 UNIX time()
1781 * 10 . 37 random bytes
1782 */
1783 buf = ssl->out_msg;
1784 p = buf + 4;
1785
1786 *p++ = (unsigned char) ssl->major_ver;
1787 *p++ = (unsigned char) ssl->minor_ver;
1788
1789 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1790 buf[4], buf[5] ) );
1791
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001792#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001793 t = time( NULL );
1794 *p++ = (unsigned char)( t >> 24 );
1795 *p++ = (unsigned char)( t >> 16 );
1796 *p++ = (unsigned char)( t >> 8 );
1797 *p++ = (unsigned char)( t );
1798
1799 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001800#else
1801 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1802 return( ret );
1803
1804 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02001805#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001806
Paul Bakkera3d195c2011-11-27 21:07:34 +00001807 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1808 return( ret );
1809
1810 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001811
Paul Bakker48916f92012-09-16 19:57:18 +00001812 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001813
1814 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1815
1816 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001817 * Resume is 0 by default, see ssl_handshake_init().
1818 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1819 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001820 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001821 if( ssl->handshake->resume == 0 &&
1822 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001823 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001824 ssl->f_get_cache != NULL &&
1825 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1826 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01001827 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001828 ssl->handshake->resume = 1;
1829 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001830
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001831 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001832 {
1833 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001834 * New session, create a new session id,
1835 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001836 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001837 ssl->state++;
1838
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001839#if defined(POLARSSL_HAVE_TIME)
1840 ssl->session_negotiate->start = time( NULL );
1841#endif
1842
Paul Bakkera503a632013-08-14 13:48:06 +02001843#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001844 if( ssl->handshake->new_session_ticket != 0 )
1845 {
1846 ssl->session_negotiate->length = n = 0;
1847 memset( ssl->session_negotiate->id, 0, 32 );
1848 }
1849 else
1850#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001851 {
1852 ssl->session_negotiate->length = n = 32;
1853 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001854 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001855 return( ret );
1856 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001857 }
1858 else
1859 {
1860 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001861 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001862 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001863 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001864 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001865
1866 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1867 {
1868 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1869 return( ret );
1870 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001871 }
1872
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001873 /*
1874 * 38 . 38 session id length
1875 * 39 . 38+n session id
1876 * 39+n . 40+n chosen ciphersuite
1877 * 41+n . 41+n chosen compression alg.
1878 * 42+n . 43+n extensions length
1879 * 44+n . 43+n+m extensions
1880 */
1881 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001882 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1883 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001884
1885 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1886 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1887 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001888 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001889
Paul Bakker48916f92012-09-16 19:57:18 +00001890 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1891 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1892 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001893
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001894 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
1895 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001896 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001897 ssl->session_negotiate->compression ) );
1898
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001899 /*
1900 * First write extensions, then the total length
1901 */
1902 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1903 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001904
Paul Bakker05decb22013-08-15 13:33:48 +02001905#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001906 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1907 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001908#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001909
Paul Bakker1f2bc622013-08-15 13:45:55 +02001910#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001911 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1912 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001913#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001914
Paul Bakkera503a632013-08-14 13:48:06 +02001915#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001916 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1917 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001918#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001919
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001920#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001921 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1922 ext_len += olen;
1923#endif
1924
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001925#if defined(POLARSSL_SSL_ALPN)
1926 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
1927 ext_len += olen;
1928#endif
1929
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001930 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001931
Paul Bakkera7036632014-04-30 10:15:38 +02001932 if( ext_len > 0 )
1933 {
1934 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1935 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1936 p += ext_len;
1937 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001938
1939 ssl->out_msglen = p - buf;
1940 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1941 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1942
1943 ret = ssl_write_record( ssl );
1944
1945 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1946
1947 return( ret );
1948}
1949
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001950#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1951 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001952 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1953 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001954static int ssl_write_certificate_request( ssl_context *ssl )
1955{
Paul Bakkered27a042013-04-18 22:46:23 +02001956 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001957
1958 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1959
1960 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01001961 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001962 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1963 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001964 {
1965 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1966 ssl->state++;
1967 return( 0 );
1968 }
1969
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001970 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1971 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001972}
1973#else
1974static int ssl_write_certificate_request( ssl_context *ssl )
1975{
1976 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1977 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001978 size_t dn_size, total_dn_size; /* excluding length bytes */
1979 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00001980 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001981 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001982
1983 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1984
1985 ssl->state++;
1986
Paul Bakkerfbb17802013-04-17 19:10:21 +02001987 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01001988 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001989 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001990 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001991 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001992 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001993 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001994 return( 0 );
1995 }
1996
1997 /*
1998 * 0 . 0 handshake type
1999 * 1 . 3 handshake length
2000 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002001 * 5 .. m-1 cert types
2002 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002003 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002004 * n .. n+1 length of all DNs
2005 * n+2 .. n+3 length of DN 1
2006 * n+4 .. ... Distinguished Name #1
2007 * ... .. ... length of DN 2, etc.
2008 */
2009 buf = ssl->out_msg;
2010 p = buf + 4;
2011
2012 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002013 * Supported certificate types
2014 *
2015 * ClientCertificateType certificate_types<1..2^8-1>;
2016 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002017 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002018 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002019
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002020#if defined(POLARSSL_RSA_C)
2021 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2022#endif
2023#if defined(POLARSSL_ECDSA_C)
2024 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2025#endif
2026
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002027 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002028 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002029
Paul Bakker577e0062013-08-28 11:57:20 +02002030 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002031#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002032 /*
2033 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002034 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002035 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2036 *
2037 * struct {
2038 * HashAlgorithm hash;
2039 * SignatureAlgorithm signature;
2040 * } SignatureAndHashAlgorithm;
2041 *
2042 * enum { (255) } HashAlgorithm;
2043 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002044 */
Paul Bakker21dca692013-01-03 11:41:08 +01002045 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002046 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002047 /*
2048 * Only use current running hash algorithm that is already required
2049 * for requested ciphersuite.
2050 */
Paul Bakker926af752012-11-23 13:38:07 +01002051 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2052
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002053 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2054 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002055 {
2056 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2057 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002058
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002059 /*
2060 * Supported signature algorithms
2061 */
2062#if defined(POLARSSL_RSA_C)
2063 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2064 p[2 + sa_len++] = SSL_SIG_RSA;
2065#endif
2066#if defined(POLARSSL_ECDSA_C)
2067 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2068 p[2 + sa_len++] = SSL_SIG_ECDSA;
2069#endif
Paul Bakker926af752012-11-23 13:38:07 +01002070
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002071 p[0] = (unsigned char)( sa_len >> 8 );
2072 p[1] = (unsigned char)( sa_len );
2073 sa_len += 2;
2074 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002075 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002076#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002077
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002078 /*
2079 * DistinguishedName certificate_authorities<0..2^16-1>;
2080 * opaque DistinguishedName<1..2^16-1>;
2081 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002082 p += 2;
2083 crt = ssl->ca_chain;
2084
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002085 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002086 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002087 {
2088 if( p - buf > 4096 )
2089 break;
2090
Paul Bakker926af752012-11-23 13:38:07 +01002091 dn_size = crt->subject_raw.len;
2092 *p++ = (unsigned char)( dn_size >> 8 );
2093 *p++ = (unsigned char)( dn_size );
2094 memcpy( p, crt->subject_raw.p, dn_size );
2095 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002096
Paul Bakker926af752012-11-23 13:38:07 +01002097 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2098
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002099 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002100 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 }
2102
Paul Bakker926af752012-11-23 13:38:07 +01002103 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002104 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2105 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002106 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2107 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002108
2109 ret = ssl_write_record( ssl );
2110
2111 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2112
2113 return( ret );
2114}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002115#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2116 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002117 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2118 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002119
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002120#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2121 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2122static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2123{
2124 int ret;
2125
2126 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2127 {
2128 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2129 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2130 }
2131
2132 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2133 pk_ec( *ssl_own_key( ssl ) ),
2134 POLARSSL_ECDH_OURS ) ) != 0 )
2135 {
2136 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2137 return( ret );
2138 }
2139
2140 return( 0 );
2141}
2142#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2143 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2144
Paul Bakker41c83d32013-03-20 14:39:14 +01002145static int ssl_write_server_key_exchange( ssl_context *ssl )
2146{
Paul Bakker23986e52011-04-24 08:57:21 +00002147 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002148 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002149 const ssl_ciphersuite_t *ciphersuite_info =
2150 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002151
2152#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2153 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2154 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002155 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002156 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002157 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002158 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002159 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002160 ((void) dig_signed);
2161 ((void) dig_signed_len);
2162#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002163
Paul Bakker5121ce52009-01-03 21:22:43 +00002164 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2165
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002166#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2167 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2168 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002169 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2170 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2171 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002172 {
2173 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2174 ssl->state++;
2175 return( 0 );
2176 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002177#endif
2178
2179#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2180 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2181 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2182 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2183 {
2184 ssl_get_ecdh_params_from_cert( ssl );
2185
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002186 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002187 ssl->state++;
2188 return( 0 );
2189 }
2190#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002191
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002192#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2193 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2194 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2195 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002196 {
2197 /* TODO: Support identity hints */
2198 *(p++) = 0x00;
2199 *(p++) = 0x00;
2200
2201 n += 2;
2202 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002203#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2204 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002205
2206#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2207 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2208 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2209 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002210 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002211 /*
2212 * Ephemeral DH parameters:
2213 *
2214 * struct {
2215 * opaque dh_p<1..2^16-1>;
2216 * opaque dh_g<1..2^16-1>;
2217 * opaque dh_Ys<1..2^16-1>;
2218 * } ServerDHParams;
2219 */
2220 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2221 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2222 {
2223 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2224 return( ret );
2225 }
Paul Bakker48916f92012-09-16 19:57:18 +00002226
Paul Bakker41c83d32013-03-20 14:39:14 +01002227 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002228 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2229 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002230 {
2231 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2232 return( ret );
2233 }
2234
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002235 dig_signed = p;
2236 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002237
2238 p += len;
2239 n += len;
2240
Paul Bakker41c83d32013-03-20 14:39:14 +01002241 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2242 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2243 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2244 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2245 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002246#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2247 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002248
Gergely Budai987bfb52014-01-19 21:48:42 +01002249#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002250 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002251 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2252 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002254 /*
2255 * Ephemeral ECDH parameters:
2256 *
2257 * struct {
2258 * ECParameters curve_params;
2259 * ECPoint public;
2260 * } ServerECDHParams;
2261 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002262 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002263#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002264 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002265
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002266 /* Match our preference list against the offered curves */
2267 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2268 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2269 if( (*curve)->grp_id == *gid )
2270 goto curve_matching_done;
2271
2272curve_matching_done:
2273#else
2274 curve = ssl->handshake->curves;
2275#endif
2276
2277 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002278 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002279 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2280 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002281 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002282
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002283 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002284
Paul Bakker41c83d32013-03-20 14:39:14 +01002285 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002286 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002287 {
2288 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2289 return( ret );
2290 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002291
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002292 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2293 p, SSL_MAX_CONTENT_LEN - n,
2294 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002295 {
2296 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2297 return( ret );
2298 }
2299
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002300 dig_signed = p;
2301 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002302
2303 p += len;
2304 n += len;
2305
Paul Bakker41c83d32013-03-20 14:39:14 +01002306 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2307 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002308#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002309
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002310#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002311 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2312 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002313 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002314 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2315 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002316 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002317 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002318 unsigned int hashlen = 0;
2319 unsigned char hash[64];
2320 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002321
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002322 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002323 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2324 */
Paul Bakker577e0062013-08-28 11:57:20 +02002325#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002326 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2327 {
2328 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2329
2330 if( md_alg == POLARSSL_MD_NONE )
2331 {
2332 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002333 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002334 }
2335 }
Paul Bakker577e0062013-08-28 11:57:20 +02002336 else
2337#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002338#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2339 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker577e0062013-08-28 11:57:20 +02002340 if ( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002341 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2342 {
2343 md_alg = POLARSSL_MD_SHA1;
2344 }
2345 else
Paul Bakker577e0062013-08-28 11:57:20 +02002346#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002347 {
2348 md_alg = POLARSSL_MD_NONE;
2349 }
2350
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002351 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002352 * Compute the hash to be signed
2353 */
Paul Bakker577e0062013-08-28 11:57:20 +02002354#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2355 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002356 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002357 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002358 md5_context md5;
2359 sha1_context sha1;
2360
2361 /*
2362 * digitally-signed struct {
2363 * opaque md5_hash[16];
2364 * opaque sha_hash[20];
2365 * };
2366 *
2367 * md5_hash
2368 * MD5(ClientHello.random + ServerHello.random
2369 * + ServerParams);
2370 * sha_hash
2371 * SHA(ClientHello.random + ServerHello.random
2372 * + ServerParams);
2373 */
2374 md5_starts( &md5 );
2375 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002376 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002377 md5_finish( &md5, hash );
2378
2379 sha1_starts( &sha1 );
2380 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002381 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002382 sha1_finish( &sha1, hash + 16 );
2383
2384 hashlen = 36;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002385 }
2386 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002387#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2388 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002389#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2390 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002391 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002392 {
2393 md_context_t ctx;
2394
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002395 /* Info from md_alg will be used instead */
2396 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002397
2398 /*
2399 * digitally-signed struct {
2400 * opaque client_random[32];
2401 * opaque server_random[32];
2402 * ServerDHParams params;
2403 * };
2404 */
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002405 if( ( ret = md_init_ctx( &ctx, md_info_from_type(md_alg) ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002406 {
2407 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2408 return( ret );
2409 }
2410
2411 md_starts( &ctx );
2412 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002413 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002414 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02002415
2416 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
2417 {
2418 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
2419 return( ret );
2420 }
2421
Paul Bakker23f36802012-09-28 14:15:14 +00002422 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002423 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002424#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2425 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002426 {
2427 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002428 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002429 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002430
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002431 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2432 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002433
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002434 /*
2435 * Make the signature
2436 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002437 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002438 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002439 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2440 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002441 }
Paul Bakker23f36802012-09-28 14:15:14 +00002442
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002443#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002444 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2445 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002446 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002447 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002448
2449 n += 2;
2450 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002451#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002452
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002453 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002454 p + 2 , &signature_len,
2455 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002456 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002457 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002458 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002459 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002460
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002461 *(p++) = (unsigned char)( signature_len >> 8 );
2462 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002463 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002464
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002465 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002466
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002467 p += signature_len;
2468 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002469 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002470#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002471 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2472 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002473
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002474 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002475 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2476 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2477
2478 ssl->state++;
2479
2480 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2481 {
2482 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2483 return( ret );
2484 }
2485
2486 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2487
2488 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002489}
2490
2491static int ssl_write_server_hello_done( ssl_context *ssl )
2492{
2493 int ret;
2494
2495 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2496
2497 ssl->out_msglen = 4;
2498 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2499 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2500
2501 ssl->state++;
2502
2503 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2504 {
2505 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2506 return( ret );
2507 }
2508
2509 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2510
2511 return( 0 );
2512}
2513
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002514#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2515 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2516static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2517 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002518{
2519 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002520 size_t n;
2521
2522 /*
2523 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2524 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002525 if( *p + 2 > end )
2526 {
2527 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2528 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2529 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002530
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002531 n = ( (*p)[0] << 8 ) | (*p)[1];
2532 *p += 2;
2533
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002534 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002535 {
2536 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2537 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2538 }
2539
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002540 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002541 {
2542 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2543 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2544 }
2545
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002546 *p += n;
2547
Paul Bakker70df2fb2013-04-17 17:19:09 +02002548 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2549
Paul Bakker70df2fb2013-04-17 17:19:09 +02002550 return( ret );
2551}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002552#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2553 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002554
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002555#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2556 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002557static int ssl_parse_encrypted_pms( ssl_context *ssl,
2558 const unsigned char *p,
2559 const unsigned char *end,
2560 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002561{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002562 int ret;
2563 size_t len = pk_get_len( ssl_own_key( ssl ) );
2564 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002565
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002566 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002567 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002568 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002569 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2570 }
2571
2572 /*
2573 * Decrypt the premaster using own private RSA key
2574 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002575#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2576 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002577 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2578 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002579 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2580 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002581 {
2582 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2583 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2584 }
2585 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002586#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002587
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002588 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002589 {
2590 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2591 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2592 }
2593
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002594 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2595 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002596 sizeof( ssl->handshake->premaster ) - pms_offset,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002597 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002598
2599 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002600 pms[0] != ssl->handshake->max_major_ver ||
2601 pms[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002602 {
2603 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2604
2605 /*
2606 * Protection against Bleichenbacher's attack:
2607 * invalid PKCS#1 v1.5 padding must not cause
2608 * the connection to end immediately; instead,
2609 * send a bad_record_mac later in the handshake.
2610 */
2611 ssl->handshake->pmslen = 48;
2612
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002613 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002614 if( ret != 0 )
2615 return( ret );
2616 }
2617
2618 return( ret );
2619}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002620#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2621 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002622
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002623#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002624static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2625 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002626{
Paul Bakker6db455e2013-09-18 17:29:31 +02002627 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002628 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002629
Paul Bakker6db455e2013-09-18 17:29:31 +02002630 if( ssl->f_psk == NULL &&
2631 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2632 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002633 {
2634 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2635 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2636 }
2637
2638 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002639 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002640 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002641 if( *p + 2 > end )
2642 {
2643 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2644 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2645 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002646
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002647 n = ( (*p)[0] << 8 ) | (*p)[1];
2648 *p += 2;
2649
2650 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002651 {
2652 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2653 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2654 }
2655
Paul Bakker6db455e2013-09-18 17:29:31 +02002656 if( ssl->f_psk != NULL )
2657 {
2658 if( ( ret != ssl->f_psk( ssl->p_psk, ssl, *p, n ) ) != 0 )
2659 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2660 }
2661
2662 if( ret == 0 )
2663 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002664 /* Identity is not a big secret since clients send it in the clear,
2665 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02002666 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002667 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002668 {
2669 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2670 }
2671 }
2672
2673 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002674 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002675 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002676 if( ( ret = ssl_send_alert_message( ssl,
2677 SSL_ALERT_LEVEL_FATAL,
2678 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2679 {
2680 return( ret );
2681 }
2682
2683 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002684 }
2685
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002686 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002687 ret = 0;
2688
Paul Bakkerfbb17802013-04-17 19:10:21 +02002689 return( ret );
2690}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002691#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002692
Paul Bakker5121ce52009-01-03 21:22:43 +00002693static int ssl_parse_client_key_exchange( ssl_context *ssl )
2694{
Paul Bakker23986e52011-04-24 08:57:21 +00002695 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002696 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002697
Paul Bakker41c83d32013-03-20 14:39:14 +01002698 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002699
2700 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2701
2702 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2703 {
2704 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2705 return( ret );
2706 }
2707
2708 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2709 {
2710 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002711 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002712 }
2713
2714 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2715 {
2716 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002717 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002718 }
2719
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002720#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002721 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002722 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002723 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002724 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002725
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002726 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002727 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002728 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2729 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002730 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002731
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002732 if( p != end )
2733 {
2734 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2735 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2736 }
2737
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002738 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
2739
2740 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2741 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002742 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002743 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002744 {
2745 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2746 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2747 }
2748
2749 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002750 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002751 else
2752#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002753#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002754 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2755 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2756 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002757 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002758 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2759 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2760 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002761 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002762 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002763 ssl->in_msg + 4, ssl->in_hslen - 4 ) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002764 {
2765 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2766 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2767 }
2768
2769 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2770
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002771 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2772 &ssl->handshake->pmslen,
2773 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002774 POLARSSL_MPI_MAX_SIZE,
2775 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002776 {
2777 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2778 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2779 }
2780
2781 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002782 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002783 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002784#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002785 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2786 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2787 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002788#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2789 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002790 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002791 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002792 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002793
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002794 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002795 {
2796 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2797 return( ret );
2798 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002799
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002800 if( p != end )
2801 {
2802 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2803 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2804 }
2805
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002806 if( ( ret = ssl_psk_derive_premaster( ssl,
2807 ciphersuite_info->key_exchange ) ) != 0 )
2808 {
2809 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2810 return( ret );
2811 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002812 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002813 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002814#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002815#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2816 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2817 {
2818 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002819 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002820
2821 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2822 {
2823 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2824 return( ret );
2825 }
2826
2827 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
2828 {
2829 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
2830 return( ret );
2831 }
2832
2833 if( ( ret = ssl_psk_derive_premaster( ssl,
2834 ciphersuite_info->key_exchange ) ) != 0 )
2835 {
2836 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2837 return( ret );
2838 }
2839 }
2840 else
2841#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002842#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2843 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2844 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002845 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002846 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002847
2848 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2849 {
2850 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2851 return( ret );
2852 }
2853 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2854 {
2855 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2856 return( ret );
2857 }
2858
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002859 if( p != end )
2860 {
2861 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2862 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2863 }
2864
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002865 if( ( ret = ssl_psk_derive_premaster( ssl,
2866 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002867 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002868 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2869 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002870 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002871 }
2872 else
2873#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002874#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2875 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2876 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002877 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002878 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002879
2880 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2881 {
2882 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2883 return( ret );
2884 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002885
2886 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2887 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002888 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002889 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2890 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002891 }
2892
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002893 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2894
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002895 if( ( ret = ssl_psk_derive_premaster( ssl,
2896 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002897 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002898 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002899 return( ret );
2900 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002901 }
2902 else
2903#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002904#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2905 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002906 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002907 if( ( ret = ssl_parse_encrypted_pms( ssl,
2908 ssl->in_msg + 4,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002909 ssl->in_msg + ssl->in_hslen,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002910 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002911 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002912 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002913 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002914 }
2915 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002916 else
2917#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2918 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002919 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002920 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002921 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002922
Paul Bakkerff60ee62010-03-16 21:09:09 +00002923 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2924 {
2925 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2926 return( ret );
2927 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002928
Paul Bakker5121ce52009-01-03 21:22:43 +00002929 ssl->state++;
2930
2931 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2932
2933 return( 0 );
2934}
2935
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002936#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2937 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002938 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2939 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002940static int ssl_parse_certificate_verify( ssl_context *ssl )
2941{
Paul Bakkered27a042013-04-18 22:46:23 +02002942 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002943 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002944
2945 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2946
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002947 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002948 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002949 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002950 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002951 {
2952 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2953 ssl->state++;
2954 return( 0 );
2955 }
2956
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002957 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2958 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002959}
2960#else
2961static int ssl_parse_certificate_verify( ssl_context *ssl )
2962{
2963 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002964 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002965 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002966 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002967 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02002968#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002969 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02002970#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002971 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002972 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2973
2974 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2975
2976 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002977 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002978 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002979 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2980 {
2981 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2982 ssl->state++;
2983 return( 0 );
2984 }
2985
Paul Bakkered27a042013-04-18 22:46:23 +02002986 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002987 {
2988 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2989 ssl->state++;
2990 return( 0 );
2991 }
2992
Paul Bakker48916f92012-09-16 19:57:18 +00002993 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002994
2995 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2996 {
2997 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2998 return( ret );
2999 }
3000
3001 ssl->state++;
3002
3003 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3004 {
3005 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003006 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003007 }
3008
3009 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
3010 {
3011 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003012 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003013 }
3014
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003015 /*
3016 * 0 . 0 handshake type
3017 * 1 . 3 handshake length
3018 * 4 . 5 sig alg (TLS 1.2 only)
3019 * 4+n . 5+n signature length (n = sa_len)
3020 * 6+n . 6+n+m signature (m = sig_len)
3021 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003022
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003023#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3024 defined(POLARSSL_SSL_PROTO_TLS1_1)
3025 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003026 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003027 sa_len = 0;
3028
Paul Bakkerc70b9822013-04-07 22:00:46 +02003029 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003030 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003031
3032 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3033 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3034 POLARSSL_PK_ECDSA ) )
3035 {
3036 hash_start += 16;
3037 hashlen -= 16;
3038 md_alg = POLARSSL_MD_SHA1;
3039 }
Paul Bakker926af752012-11-23 13:38:07 +01003040 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003041 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003042#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3043 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003044#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3045 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003046 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003047 sa_len = 2;
3048
Paul Bakker5121ce52009-01-03 21:22:43 +00003049 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003050 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003051 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003052 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003053 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003054 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3055 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003056 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3057 }
3058
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003059 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003060
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003061 /* Info from md_alg will be used instead */
3062 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003063
3064 /*
3065 * Signature
3066 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003067 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
3068 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003069 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003070 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3071 " for verify message" ) );
3072 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003073 }
3074
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003075 /*
3076 * Check the certificate's key type matches the signature alg
3077 */
3078 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3079 {
3080 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3081 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3082 }
Paul Bakker577e0062013-08-28 11:57:20 +02003083 }
3084 else
3085#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3086 {
3087 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003088 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003089 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003090
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003091 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01003092
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003093 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003094 {
3095 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003096 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003097 }
3098
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003099 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003100 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003101 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003102 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003103 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003104 return( ret );
3105 }
3106
3107 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3108
Paul Bakkered27a042013-04-18 22:46:23 +02003109 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003110}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003111#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3112 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3113 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003114
Paul Bakkera503a632013-08-14 13:48:06 +02003115#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003116static int ssl_write_new_session_ticket( ssl_context *ssl )
3117{
3118 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003119 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003120 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003121
3122 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3123
3124 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3125 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3126
3127 /*
3128 * struct {
3129 * uint32 ticket_lifetime_hint;
3130 * opaque ticket<0..2^16-1>;
3131 * } NewSessionTicket;
3132 *
3133 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3134 * 8 . 9 ticket_len (n)
3135 * 10 . 9+n ticket content
3136 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003137
3138 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3139 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3140 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3141 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003142
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003143 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3144 {
3145 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3146 tlen = 0;
3147 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003148
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003149 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3150 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003151
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003152 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003153
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003154 /*
3155 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3156 * ChangeCipherSpec share the same state.
3157 */
3158 ssl->handshake->new_session_ticket = 0;
3159
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003160 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3161 {
3162 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3163 return( ret );
3164 }
3165
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003166 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3167
3168 return( 0 );
3169}
Paul Bakkera503a632013-08-14 13:48:06 +02003170#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003171
Paul Bakker5121ce52009-01-03 21:22:43 +00003172/*
Paul Bakker1961b702013-01-25 14:49:24 +01003173 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003174 */
Paul Bakker1961b702013-01-25 14:49:24 +01003175int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003176{
3177 int ret = 0;
3178
Paul Bakker1961b702013-01-25 14:49:24 +01003179 if( ssl->state == SSL_HANDSHAKE_OVER )
3180 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003181
Paul Bakker1961b702013-01-25 14:49:24 +01003182 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3183
3184 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3185 return( ret );
3186
3187 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003188 {
Paul Bakker1961b702013-01-25 14:49:24 +01003189 case SSL_HELLO_REQUEST:
3190 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003191 break;
3192
Paul Bakker1961b702013-01-25 14:49:24 +01003193 /*
3194 * <== ClientHello
3195 */
3196 case SSL_CLIENT_HELLO:
3197 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003198 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003199
3200 /*
3201 * ==> ServerHello
3202 * Certificate
3203 * ( ServerKeyExchange )
3204 * ( CertificateRequest )
3205 * ServerHelloDone
3206 */
3207 case SSL_SERVER_HELLO:
3208 ret = ssl_write_server_hello( ssl );
3209 break;
3210
3211 case SSL_SERVER_CERTIFICATE:
3212 ret = ssl_write_certificate( ssl );
3213 break;
3214
3215 case SSL_SERVER_KEY_EXCHANGE:
3216 ret = ssl_write_server_key_exchange( ssl );
3217 break;
3218
3219 case SSL_CERTIFICATE_REQUEST:
3220 ret = ssl_write_certificate_request( ssl );
3221 break;
3222
3223 case SSL_SERVER_HELLO_DONE:
3224 ret = ssl_write_server_hello_done( ssl );
3225 break;
3226
3227 /*
3228 * <== ( Certificate/Alert )
3229 * ClientKeyExchange
3230 * ( CertificateVerify )
3231 * ChangeCipherSpec
3232 * Finished
3233 */
3234 case SSL_CLIENT_CERTIFICATE:
3235 ret = ssl_parse_certificate( ssl );
3236 break;
3237
3238 case SSL_CLIENT_KEY_EXCHANGE:
3239 ret = ssl_parse_client_key_exchange( ssl );
3240 break;
3241
3242 case SSL_CERTIFICATE_VERIFY:
3243 ret = ssl_parse_certificate_verify( ssl );
3244 break;
3245
3246 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3247 ret = ssl_parse_change_cipher_spec( ssl );
3248 break;
3249
3250 case SSL_CLIENT_FINISHED:
3251 ret = ssl_parse_finished( ssl );
3252 break;
3253
3254 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003255 * ==> ( NewSessionTicket )
3256 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003257 * Finished
3258 */
3259 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003260#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003261 if( ssl->handshake->new_session_ticket != 0 )
3262 ret = ssl_write_new_session_ticket( ssl );
3263 else
Paul Bakkera503a632013-08-14 13:48:06 +02003264#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003265 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003266 break;
3267
3268 case SSL_SERVER_FINISHED:
3269 ret = ssl_write_finished( ssl );
3270 break;
3271
3272 case SSL_FLUSH_BUFFERS:
3273 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3274 ssl->state = SSL_HANDSHAKE_WRAPUP;
3275 break;
3276
3277 case SSL_HANDSHAKE_WRAPUP:
3278 ssl_handshake_wrapup( ssl );
3279 break;
3280
3281 default:
3282 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3283 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003284 }
3285
Paul Bakker5121ce52009-01-03 21:22:43 +00003286 return( ret );
3287}
Paul Bakker9af723c2014-05-01 13:03:14 +02003288#endif /* POLARSSL_SSL_SRV_C */