blob: 9bb2d92fddeb615b69eb83edb3e5cfd9aa8e7a9e [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
1212 * 41+x . .. ciphersuitelist
1213 * .. . .. compression alg.
1214 * .. . .. extensions
1215 */
1216 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1217
1218 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1219 buf[0] ) );
1220 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1221 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1222 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1223 buf[4], buf[5] ) );
1224
1225 /*
1226 * Check the handshake type and protocol version
1227 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001228 if( buf[0] != SSL_HS_CLIENT_HELLO )
Paul Bakkerec636f32012-09-09 19:17:02 +00001229 {
1230 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1231 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1232 }
1233
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001234 ssl->major_ver = buf[4];
1235 ssl->minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001236
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001237 ssl->handshake->max_major_ver = ssl->major_ver;
1238 ssl->handshake->max_minor_ver = ssl->minor_ver;
1239
1240 if( ssl->major_ver < ssl->min_major_ver ||
1241 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001242 {
1243 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001244 " [%d:%d] < [%d:%d]",
1245 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001246 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001247
1248 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1249 SSL_ALERT_MSG_PROTOCOL_VERSION );
1250
1251 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1252 }
1253
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001254 if( ssl->major_ver > ssl->max_major_ver )
1255 {
1256 ssl->major_ver = ssl->max_major_ver;
1257 ssl->minor_ver = ssl->max_minor_ver;
1258 }
1259 else if( ssl->minor_ver > ssl->max_minor_ver )
1260 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001261
Paul Bakker48916f92012-09-16 19:57:18 +00001262 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001263
1264 /*
1265 * Check the handshake message length
1266 */
1267 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1268 {
1269 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1270 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1271 }
1272
1273 /*
1274 * Check the session length
1275 */
1276 sess_len = buf[38];
1277
1278 if( sess_len > 32 )
1279 {
1280 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1281 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1282 }
1283
Paul Bakker48916f92012-09-16 19:57:18 +00001284 ssl->session_negotiate->length = sess_len;
1285 memset( ssl->session_negotiate->id, 0,
1286 sizeof( ssl->session_negotiate->id ) );
1287 memcpy( ssl->session_negotiate->id, buf + 39,
1288 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001289
1290 /*
1291 * Check the ciphersuitelist length
1292 */
1293 ciph_len = ( buf[39 + sess_len] << 8 )
1294 | ( buf[40 + sess_len] );
1295
Paul Bakker4f42c112014-04-17 14:48:23 +02001296 if( ciph_len < 2 || ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001297 {
1298 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1299 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1300 }
1301
1302 /*
1303 * Check the compression algorithms length
1304 */
1305 comp_len = buf[41 + sess_len + ciph_len];
1306
1307 if( comp_len < 1 || comp_len > 16 )
1308 {
1309 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1310 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1311 }
1312
Paul Bakker48916f92012-09-16 19:57:18 +00001313 /*
1314 * Check the extension length
1315 */
1316 if( n > 42 + sess_len + ciph_len + comp_len )
1317 {
1318 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1319 | ( buf[43 + sess_len + ciph_len + comp_len] );
1320
1321 if( ( ext_len > 0 && ext_len < 4 ) ||
1322 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1323 {
1324 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1325 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1326 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1327 }
1328 }
1329
1330 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001331#if defined(POLARSSL_ZLIB_SUPPORT)
1332 for( i = 0; i < comp_len; ++i )
1333 {
Paul Bakker48916f92012-09-16 19:57:18 +00001334 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001335 {
Paul Bakker48916f92012-09-16 19:57:18 +00001336 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001337 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001338 }
1339 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001340#endif
1341
Paul Bakkerec636f32012-09-09 19:17:02 +00001342 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1343 buf + 6, 32 );
1344 SSL_DEBUG_BUF( 3, "client hello, session id",
1345 buf + 38, sess_len );
1346 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1347 buf + 41 + sess_len, ciph_len );
1348 SSL_DEBUG_BUF( 3, "client hello, compression",
1349 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001350
Paul Bakkerec636f32012-09-09 19:17:02 +00001351 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001352 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1353 */
1354 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1355 {
1356 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1357 {
1358 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1359 if( ssl->renegotiation == SSL_RENEGOTIATION )
1360 {
1361 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001362
1363 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1364 return( ret );
1365
Paul Bakker48916f92012-09-16 19:57:18 +00001366 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1367 }
1368 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1369 break;
1370 }
1371 }
1372
Paul Bakker48916f92012-09-16 19:57:18 +00001373 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001374
1375 while( ext_len )
1376 {
1377 unsigned int ext_id = ( ( ext[0] << 8 )
1378 | ( ext[1] ) );
1379 unsigned int ext_size = ( ( ext[2] << 8 )
1380 | ( ext[3] ) );
1381
1382 if( ext_size + 4 > ext_len )
1383 {
1384 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1385 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1386 }
1387 switch( ext_id )
1388 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001389#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001390 case TLS_EXT_SERVERNAME:
1391 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1392 if( ssl->f_sni == NULL )
1393 break;
1394
1395 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1396 if( ret != 0 )
1397 return( ret );
1398 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001399#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001400
Paul Bakker48916f92012-09-16 19:57:18 +00001401 case TLS_EXT_RENEGOTIATION_INFO:
1402 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1403 renegotiation_info_seen = 1;
1404
Paul Bakker23f36802012-09-28 14:15:14 +00001405 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1406 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001407 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001408 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001409
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001410#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001411 case TLS_EXT_SIG_ALG:
1412 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1413 if( ssl->renegotiation == SSL_RENEGOTIATION )
1414 break;
1415
1416 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1417 if( ret != 0 )
1418 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001419 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001420#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001421
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001422#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001423 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1424 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1425
1426 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1427 if( ret != 0 )
1428 return( ret );
1429 break;
1430
1431 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1432 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001433 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001434
1435 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1436 if( ret != 0 )
1437 return( ret );
1438 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001439#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001440
Paul Bakker05decb22013-08-15 13:33:48 +02001441#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001442 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1443 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1444
1445 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1446 if( ret != 0 )
1447 return( ret );
1448 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001449#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001450
Paul Bakker1f2bc622013-08-15 13:45:55 +02001451#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001452 case TLS_EXT_TRUNCATED_HMAC:
1453 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1454
1455 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1456 if( ret != 0 )
1457 return( ret );
1458 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001459#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001460
Paul Bakkera503a632013-08-14 13:48:06 +02001461#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001462 case TLS_EXT_SESSION_TICKET:
1463 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1464
1465 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1466 if( ret != 0 )
1467 return( ret );
1468 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001469#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001470
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001471#if defined(POLARSSL_SSL_ALPN)
1472 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001473 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001474
1475 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1476 if( ret != 0 )
1477 return( ret );
1478 break;
1479#endif /* POLARSSL_SSL_SESSION_TICKETS */
1480
Paul Bakker48916f92012-09-16 19:57:18 +00001481 default:
1482 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1483 ext_id ) );
1484 }
1485
1486 ext_len -= 4 + ext_size;
1487 ext += 4 + ext_size;
1488
1489 if( ext_len > 0 && ext_len < 4 )
1490 {
1491 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1492 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1493 }
1494 }
1495
1496 /*
1497 * Renegotiation security checks
1498 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001499 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1500 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1501 {
1502 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1503 handshake_failure = 1;
1504 }
1505 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1506 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1507 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001508 {
1509 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001510 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001511 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001512 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1513 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1514 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001515 {
1516 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001517 handshake_failure = 1;
1518 }
1519 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1520 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1521 renegotiation_info_seen == 1 )
1522 {
1523 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1524 handshake_failure = 1;
1525 }
1526
1527 if( handshake_failure == 1 )
1528 {
1529 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1530 return( ret );
1531
Paul Bakker48916f92012-09-16 19:57:18 +00001532 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1533 }
Paul Bakker380da532012-04-18 16:10:25 +00001534
Paul Bakker41c83d32013-03-20 14:39:14 +01001535 /*
1536 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001537 * (At the end because we need information from the EC-based extensions
1538 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001539 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001540 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001541 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001542#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1543 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
1544 {
1545 for( i = 0; ciphersuites[i] != 0; i++ )
1546#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001547 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001548 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001549 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001550#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001551 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001552 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1553 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1554 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001555
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001556 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1557 &ciphersuite_info ) ) != 0 )
1558 return( ret );
1559
1560 if( ciphersuite_info != NULL )
1561 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001562 }
1563 }
1564
1565 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1566
1567 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1568 return( ret );
1569
1570 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1571
1572have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001573 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001574 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1575 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1576
Paul Bakker5121ce52009-01-03 21:22:43 +00001577 ssl->in_left = 0;
1578 ssl->state++;
1579
1580 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1581
1582 return( 0 );
1583}
1584
Paul Bakker1f2bc622013-08-15 13:45:55 +02001585#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001586static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1587 unsigned char *buf,
1588 size_t *olen )
1589{
1590 unsigned char *p = buf;
1591
1592 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1593 {
1594 *olen = 0;
1595 return;
1596 }
1597
1598 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1599
1600 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1601 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1602
1603 *p++ = 0x00;
1604 *p++ = 0x00;
1605
1606 *olen = 4;
1607}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001608#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001609
Paul Bakkera503a632013-08-14 13:48:06 +02001610#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001611static void ssl_write_session_ticket_ext( ssl_context *ssl,
1612 unsigned char *buf,
1613 size_t *olen )
1614{
1615 unsigned char *p = buf;
1616
1617 if( ssl->handshake->new_session_ticket == 0 )
1618 {
1619 *olen = 0;
1620 return;
1621 }
1622
1623 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1624
1625 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1626 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1627
1628 *p++ = 0x00;
1629 *p++ = 0x00;
1630
1631 *olen = 4;
1632}
Paul Bakkera503a632013-08-14 13:48:06 +02001633#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001634
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001635static void ssl_write_renegotiation_ext( ssl_context *ssl,
1636 unsigned char *buf,
1637 size_t *olen )
1638{
1639 unsigned char *p = buf;
1640
1641 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1642 {
1643 *olen = 0;
1644 return;
1645 }
1646
1647 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1648
1649 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1650 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1651
1652 *p++ = 0x00;
1653 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1654 *p++ = ssl->verify_data_len * 2 & 0xFF;
1655
1656 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1657 p += ssl->verify_data_len;
1658 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1659 p += ssl->verify_data_len;
1660
1661 *olen = 5 + ssl->verify_data_len * 2;
1662}
1663
Paul Bakker05decb22013-08-15 13:33:48 +02001664#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001665static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1666 unsigned char *buf,
1667 size_t *olen )
1668{
1669 unsigned char *p = buf;
1670
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001671 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1672 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001673 *olen = 0;
1674 return;
1675 }
1676
1677 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1678
1679 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1680 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1681
1682 *p++ = 0x00;
1683 *p++ = 1;
1684
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001685 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001686
1687 *olen = 5;
1688}
Paul Bakker05decb22013-08-15 13:33:48 +02001689#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001690
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001691#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001692static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1693 unsigned char *buf,
1694 size_t *olen )
1695{
1696 unsigned char *p = buf;
1697 ((void) ssl);
1698
Paul Bakker677377f2013-10-28 12:54:26 +01001699 if( ( ssl->handshake->cli_exts &
1700 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
1701 {
1702 *olen = 0;
1703 return;
1704 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001705
1706 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1707
1708 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1709 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1710
1711 *p++ = 0x00;
1712 *p++ = 2;
1713
1714 *p++ = 1;
1715 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1716
1717 *olen = 6;
1718}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001719#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001720
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001721#if defined(POLARSSL_SSL_ALPN )
1722static void ssl_write_alpn_ext( ssl_context *ssl,
1723 unsigned char *buf, size_t *olen )
1724{
1725 if( ssl->alpn_chosen == NULL )
1726 {
1727 *olen = 0;
1728 return;
1729 }
1730
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001731 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001732
1733 /*
1734 * 0 . 1 ext identifier
1735 * 2 . 3 ext length
1736 * 4 . 5 protocol list length
1737 * 6 . 6 protocol name length
1738 * 7 . 7+n protocol name
1739 */
1740 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
1741 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
1742
1743 *olen = 7 + strlen( ssl->alpn_chosen );
1744
1745 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
1746 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
1747
1748 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
1749 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
1750
1751 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
1752
1753 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
1754}
1755#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
1756
Paul Bakker5121ce52009-01-03 21:22:43 +00001757static int ssl_write_server_hello( ssl_context *ssl )
1758{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001759#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001760 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001761#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001762 int ret;
1763 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001764 unsigned char *buf, *p;
1765
1766 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1767
Paul Bakkera9a028e2013-11-21 17:31:06 +01001768 if( ssl->f_rng == NULL )
1769 {
1770 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
1771 return( POLARSSL_ERR_SSL_NO_RNG );
1772 }
1773
Paul Bakker5121ce52009-01-03 21:22:43 +00001774 /*
1775 * 0 . 0 handshake type
1776 * 1 . 3 handshake length
1777 * 4 . 5 protocol version
1778 * 6 . 9 UNIX time()
1779 * 10 . 37 random bytes
1780 */
1781 buf = ssl->out_msg;
1782 p = buf + 4;
1783
1784 *p++ = (unsigned char) ssl->major_ver;
1785 *p++ = (unsigned char) ssl->minor_ver;
1786
1787 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1788 buf[4], buf[5] ) );
1789
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001790#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001791 t = time( NULL );
1792 *p++ = (unsigned char)( t >> 24 );
1793 *p++ = (unsigned char)( t >> 16 );
1794 *p++ = (unsigned char)( t >> 8 );
1795 *p++ = (unsigned char)( t );
1796
1797 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001798#else
1799 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1800 return( ret );
1801
1802 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02001803#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001804
Paul Bakkera3d195c2011-11-27 21:07:34 +00001805 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1806 return( ret );
1807
1808 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001809
Paul Bakker48916f92012-09-16 19:57:18 +00001810 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001811
1812 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1813
1814 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001815 * Resume is 0 by default, see ssl_handshake_init().
1816 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1817 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001818 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001819 if( ssl->handshake->resume == 0 &&
1820 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001821 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001822 ssl->f_get_cache != NULL &&
1823 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1824 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01001825 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001826 ssl->handshake->resume = 1;
1827 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001828
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001829 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001830 {
1831 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001832 * New session, create a new session id,
1833 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001834 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001835 ssl->state++;
1836
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001837#if defined(POLARSSL_HAVE_TIME)
1838 ssl->session_negotiate->start = time( NULL );
1839#endif
1840
Paul Bakkera503a632013-08-14 13:48:06 +02001841#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001842 if( ssl->handshake->new_session_ticket != 0 )
1843 {
1844 ssl->session_negotiate->length = n = 0;
1845 memset( ssl->session_negotiate->id, 0, 32 );
1846 }
1847 else
1848#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001849 {
1850 ssl->session_negotiate->length = n = 32;
1851 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001852 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001853 return( ret );
1854 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001855 }
1856 else
1857 {
1858 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001859 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001860 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001861 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001862 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001863
1864 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1865 {
1866 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1867 return( ret );
1868 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001869 }
1870
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001871 /*
1872 * 38 . 38 session id length
1873 * 39 . 38+n session id
1874 * 39+n . 40+n chosen ciphersuite
1875 * 41+n . 41+n chosen compression alg.
1876 * 42+n . 43+n extensions length
1877 * 44+n . 43+n+m extensions
1878 */
1879 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001880 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1881 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001882
1883 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1884 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1885 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001886 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001887
Paul Bakker48916f92012-09-16 19:57:18 +00001888 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1889 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1890 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001891
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001892 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
1893 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001894 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001895 ssl->session_negotiate->compression ) );
1896
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001897 /*
1898 * First write extensions, then the total length
1899 */
1900 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1901 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001902
Paul Bakker05decb22013-08-15 13:33:48 +02001903#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001904 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1905 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001906#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001907
Paul Bakker1f2bc622013-08-15 13:45:55 +02001908#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001909 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1910 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001911#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001912
Paul Bakkera503a632013-08-14 13:48:06 +02001913#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001914 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1915 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001916#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001917
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001918#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001919 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1920 ext_len += olen;
1921#endif
1922
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001923#if defined(POLARSSL_SSL_ALPN)
1924 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
1925 ext_len += olen;
1926#endif
1927
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001928 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001929
Paul Bakkera7036632014-04-30 10:15:38 +02001930 if( ext_len > 0 )
1931 {
1932 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1933 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1934 p += ext_len;
1935 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001936
1937 ssl->out_msglen = p - buf;
1938 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1939 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1940
1941 ret = ssl_write_record( ssl );
1942
1943 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1944
1945 return( ret );
1946}
1947
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001948#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1949 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001950 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1951 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001952static int ssl_write_certificate_request( ssl_context *ssl )
1953{
Paul Bakkered27a042013-04-18 22:46:23 +02001954 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001955
1956 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1957
1958 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01001959 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001960 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1961 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001962 {
1963 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1964 ssl->state++;
1965 return( 0 );
1966 }
1967
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001968 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1969 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001970}
1971#else
1972static int ssl_write_certificate_request( ssl_context *ssl )
1973{
1974 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1975 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001976 size_t dn_size, total_dn_size; /* excluding length bytes */
1977 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00001978 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001979 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001980
1981 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1982
1983 ssl->state++;
1984
Paul Bakkerfbb17802013-04-17 19:10:21 +02001985 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01001986 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001987 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001988 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001989 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001990 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001991 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001992 return( 0 );
1993 }
1994
1995 /*
1996 * 0 . 0 handshake type
1997 * 1 . 3 handshake length
1998 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01001999 * 5 .. m-1 cert types
2000 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002001 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002002 * n .. n+1 length of all DNs
2003 * n+2 .. n+3 length of DN 1
2004 * n+4 .. ... Distinguished Name #1
2005 * ... .. ... length of DN 2, etc.
2006 */
2007 buf = ssl->out_msg;
2008 p = buf + 4;
2009
2010 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002011 * Supported certificate types
2012 *
2013 * ClientCertificateType certificate_types<1..2^8-1>;
2014 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002015 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002016 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002017
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002018#if defined(POLARSSL_RSA_C)
2019 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2020#endif
2021#if defined(POLARSSL_ECDSA_C)
2022 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2023#endif
2024
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002025 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002026 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002027
Paul Bakker577e0062013-08-28 11:57:20 +02002028 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002029#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002030 /*
2031 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002032 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002033 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2034 *
2035 * struct {
2036 * HashAlgorithm hash;
2037 * SignatureAlgorithm signature;
2038 * } SignatureAndHashAlgorithm;
2039 *
2040 * enum { (255) } HashAlgorithm;
2041 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002042 */
Paul Bakker21dca692013-01-03 11:41:08 +01002043 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002044 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002045 /*
2046 * Only use current running hash algorithm that is already required
2047 * for requested ciphersuite.
2048 */
Paul Bakker926af752012-11-23 13:38:07 +01002049 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2050
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002051 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2052 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002053 {
2054 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2055 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002056
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002057 /*
2058 * Supported signature algorithms
2059 */
2060#if defined(POLARSSL_RSA_C)
2061 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2062 p[2 + sa_len++] = SSL_SIG_RSA;
2063#endif
2064#if defined(POLARSSL_ECDSA_C)
2065 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2066 p[2 + sa_len++] = SSL_SIG_ECDSA;
2067#endif
Paul Bakker926af752012-11-23 13:38:07 +01002068
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002069 p[0] = (unsigned char)( sa_len >> 8 );
2070 p[1] = (unsigned char)( sa_len );
2071 sa_len += 2;
2072 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002073 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002074#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002075
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002076 /*
2077 * DistinguishedName certificate_authorities<0..2^16-1>;
2078 * opaque DistinguishedName<1..2^16-1>;
2079 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002080 p += 2;
2081 crt = ssl->ca_chain;
2082
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002083 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002084 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002085 {
2086 if( p - buf > 4096 )
2087 break;
2088
Paul Bakker926af752012-11-23 13:38:07 +01002089 dn_size = crt->subject_raw.len;
2090 *p++ = (unsigned char)( dn_size >> 8 );
2091 *p++ = (unsigned char)( dn_size );
2092 memcpy( p, crt->subject_raw.p, dn_size );
2093 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002094
Paul Bakker926af752012-11-23 13:38:07 +01002095 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2096
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002097 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002098 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002099 }
2100
Paul Bakker926af752012-11-23 13:38:07 +01002101 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002102 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2103 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002104 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2105 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002106
2107 ret = ssl_write_record( ssl );
2108
2109 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2110
2111 return( ret );
2112}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002113#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2114 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002115 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2116 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002117
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002118#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2119 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2120static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2121{
2122 int ret;
2123
2124 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2125 {
2126 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2127 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2128 }
2129
2130 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2131 pk_ec( *ssl_own_key( ssl ) ),
2132 POLARSSL_ECDH_OURS ) ) != 0 )
2133 {
2134 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2135 return( ret );
2136 }
2137
2138 return( 0 );
2139}
2140#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2141 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2142
Paul Bakker41c83d32013-03-20 14:39:14 +01002143static int ssl_write_server_key_exchange( ssl_context *ssl )
2144{
Paul Bakker23986e52011-04-24 08:57:21 +00002145 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002146 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002147 const ssl_ciphersuite_t *ciphersuite_info =
2148 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002149
2150#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2151 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2152 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002153 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002154 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002155 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002156 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002157 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002158 ((void) dig_signed);
2159 ((void) dig_signed_len);
2160#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002161
Paul Bakker5121ce52009-01-03 21:22:43 +00002162 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2163
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002164#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2165 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2166 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002167 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2168 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2169 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002170 {
2171 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2172 ssl->state++;
2173 return( 0 );
2174 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002175#endif
2176
2177#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2178 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2179 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2180 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2181 {
2182 ssl_get_ecdh_params_from_cert( ssl );
2183
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002184 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002185 ssl->state++;
2186 return( 0 );
2187 }
2188#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002189
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002190#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2191 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2192 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2193 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002194 {
2195 /* TODO: Support identity hints */
2196 *(p++) = 0x00;
2197 *(p++) = 0x00;
2198
2199 n += 2;
2200 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002201#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2202 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002203
2204#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2205 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2206 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2207 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002208 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002209 /*
2210 * Ephemeral DH parameters:
2211 *
2212 * struct {
2213 * opaque dh_p<1..2^16-1>;
2214 * opaque dh_g<1..2^16-1>;
2215 * opaque dh_Ys<1..2^16-1>;
2216 * } ServerDHParams;
2217 */
2218 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2219 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2220 {
2221 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2222 return( ret );
2223 }
Paul Bakker48916f92012-09-16 19:57:18 +00002224
Paul Bakker41c83d32013-03-20 14:39:14 +01002225 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002226 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2227 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002228 {
2229 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2230 return( ret );
2231 }
2232
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002233 dig_signed = p;
2234 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002235
2236 p += len;
2237 n += len;
2238
Paul Bakker41c83d32013-03-20 14:39:14 +01002239 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2240 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2241 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2242 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2243 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002244#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2245 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002246
Gergely Budai987bfb52014-01-19 21:48:42 +01002247#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002248 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002249 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2250 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002251 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002252 /*
2253 * Ephemeral ECDH parameters:
2254 *
2255 * struct {
2256 * ECParameters curve_params;
2257 * ECPoint public;
2258 * } ServerECDHParams;
2259 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002260 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002261#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002262 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002263
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002264 /* Match our preference list against the offered curves */
2265 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2266 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2267 if( (*curve)->grp_id == *gid )
2268 goto curve_matching_done;
2269
2270curve_matching_done:
2271#else
2272 curve = ssl->handshake->curves;
2273#endif
2274
2275 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002276 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002277 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2278 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002279 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002280
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002281 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002282
Paul Bakker41c83d32013-03-20 14:39:14 +01002283 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002284 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002285 {
2286 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2287 return( ret );
2288 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002289
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002290 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2291 p, SSL_MAX_CONTENT_LEN - n,
2292 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002293 {
2294 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2295 return( ret );
2296 }
2297
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002298 dig_signed = p;
2299 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002300
2301 p += len;
2302 n += len;
2303
Paul Bakker41c83d32013-03-20 14:39:14 +01002304 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2305 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002306#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002307
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002308#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002309 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2310 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002311 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002312 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2313 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002314 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002315 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002316 unsigned int hashlen = 0;
2317 unsigned char hash[64];
2318 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002319
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002320 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002321 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2322 */
Paul Bakker577e0062013-08-28 11:57:20 +02002323#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002324 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2325 {
2326 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2327
2328 if( md_alg == POLARSSL_MD_NONE )
2329 {
2330 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002331 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002332 }
2333 }
Paul Bakker577e0062013-08-28 11:57:20 +02002334 else
2335#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002336#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2337 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker577e0062013-08-28 11:57:20 +02002338 if ( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002339 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2340 {
2341 md_alg = POLARSSL_MD_SHA1;
2342 }
2343 else
Paul Bakker577e0062013-08-28 11:57:20 +02002344#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002345 {
2346 md_alg = POLARSSL_MD_NONE;
2347 }
2348
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002349 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002350 * Compute the hash to be signed
2351 */
Paul Bakker577e0062013-08-28 11:57:20 +02002352#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2353 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002354 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002355 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002356 md5_context md5;
2357 sha1_context sha1;
2358
2359 /*
2360 * digitally-signed struct {
2361 * opaque md5_hash[16];
2362 * opaque sha_hash[20];
2363 * };
2364 *
2365 * md5_hash
2366 * MD5(ClientHello.random + ServerHello.random
2367 * + ServerParams);
2368 * sha_hash
2369 * SHA(ClientHello.random + ServerHello.random
2370 * + ServerParams);
2371 */
2372 md5_starts( &md5 );
2373 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002374 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002375 md5_finish( &md5, hash );
2376
2377 sha1_starts( &sha1 );
2378 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002379 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002380 sha1_finish( &sha1, hash + 16 );
2381
2382 hashlen = 36;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002383 }
2384 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002385#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2386 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002387#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2388 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002389 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002390 {
2391 md_context_t ctx;
2392
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002393 /* Info from md_alg will be used instead */
2394 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002395
2396 /*
2397 * digitally-signed struct {
2398 * opaque client_random[32];
2399 * opaque server_random[32];
2400 * ServerDHParams params;
2401 * };
2402 */
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002403 if( ( ret = md_init_ctx( &ctx, md_info_from_type(md_alg) ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002404 {
2405 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2406 return( ret );
2407 }
2408
2409 md_starts( &ctx );
2410 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002411 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002412 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02002413
2414 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
2415 {
2416 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
2417 return( ret );
2418 }
2419
Paul Bakker23f36802012-09-28 14:15:14 +00002420 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002421 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002422#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2423 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002424 {
2425 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002426 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002427 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002428
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002429 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2430 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002431
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002432 /*
2433 * Make the signature
2434 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002435 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002436 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002437 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2438 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002439 }
Paul Bakker23f36802012-09-28 14:15:14 +00002440
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002441#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002442 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2443 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002444 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002445 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002446
2447 n += 2;
2448 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002449#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002450
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002451 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002452 p + 2 , &signature_len,
2453 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002454 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002455 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002456 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002457 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002458
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002459 *(p++) = (unsigned char)( signature_len >> 8 );
2460 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002461 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002462
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002463 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002464
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002465 p += signature_len;
2466 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002467 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002468#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002469 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2470 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002471
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002472 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002473 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2474 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2475
2476 ssl->state++;
2477
2478 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2479 {
2480 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2481 return( ret );
2482 }
2483
2484 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2485
2486 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002487}
2488
2489static int ssl_write_server_hello_done( ssl_context *ssl )
2490{
2491 int ret;
2492
2493 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2494
2495 ssl->out_msglen = 4;
2496 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2497 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2498
2499 ssl->state++;
2500
2501 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2502 {
2503 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2504 return( ret );
2505 }
2506
2507 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2508
2509 return( 0 );
2510}
2511
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002512#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2513 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2514static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2515 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002516{
2517 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002518 size_t n;
2519
2520 /*
2521 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2522 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002523 if( *p + 2 > end )
2524 {
2525 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2526 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2527 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002528
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002529 n = ( (*p)[0] << 8 ) | (*p)[1];
2530 *p += 2;
2531
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002532 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002533 {
2534 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2535 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2536 }
2537
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002538 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002539 {
2540 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2541 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2542 }
2543
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002544 *p += n;
2545
Paul Bakker70df2fb2013-04-17 17:19:09 +02002546 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2547
Paul Bakker70df2fb2013-04-17 17:19:09 +02002548 return( ret );
2549}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002550#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2551 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002552
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002553#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2554 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002555static int ssl_parse_encrypted_pms( ssl_context *ssl,
2556 const unsigned char *p,
2557 const unsigned char *end,
2558 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002559{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002560 int ret;
2561 size_t len = pk_get_len( ssl_own_key( ssl ) );
2562 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002563
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002564 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002565 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002566 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002567 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2568 }
2569
2570 /*
2571 * Decrypt the premaster using own private RSA key
2572 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002573#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2574 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002575 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2576 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002577 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2578 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002579 {
2580 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2581 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2582 }
2583 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002584#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002585
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002586 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002587 {
2588 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2589 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2590 }
2591
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002592 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2593 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002594 sizeof( ssl->handshake->premaster ) - pms_offset,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002595 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002596
2597 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002598 pms[0] != ssl->handshake->max_major_ver ||
2599 pms[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002600 {
2601 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2602
2603 /*
2604 * Protection against Bleichenbacher's attack:
2605 * invalid PKCS#1 v1.5 padding must not cause
2606 * the connection to end immediately; instead,
2607 * send a bad_record_mac later in the handshake.
2608 */
2609 ssl->handshake->pmslen = 48;
2610
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002611 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002612 if( ret != 0 )
2613 return( ret );
2614 }
2615
2616 return( ret );
2617}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002618#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2619 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002620
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002621#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002622static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2623 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002624{
Paul Bakker6db455e2013-09-18 17:29:31 +02002625 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002626 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002627
Paul Bakker6db455e2013-09-18 17:29:31 +02002628 if( ssl->f_psk == NULL &&
2629 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2630 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002631 {
2632 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2633 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2634 }
2635
2636 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002637 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002638 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002639 if( *p + 2 > end )
2640 {
2641 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2642 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2643 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002644
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002645 n = ( (*p)[0] << 8 ) | (*p)[1];
2646 *p += 2;
2647
2648 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002649 {
2650 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2651 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2652 }
2653
Paul Bakker6db455e2013-09-18 17:29:31 +02002654 if( ssl->f_psk != NULL )
2655 {
2656 if( ( ret != ssl->f_psk( ssl->p_psk, ssl, *p, n ) ) != 0 )
2657 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2658 }
2659
2660 if( ret == 0 )
2661 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002662 /* Identity is not a big secret since clients send it in the clear,
2663 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02002664 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002665 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002666 {
2667 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2668 }
2669 }
2670
2671 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002672 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002673 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002674 if( ( ret = ssl_send_alert_message( ssl,
2675 SSL_ALERT_LEVEL_FATAL,
2676 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2677 {
2678 return( ret );
2679 }
2680
2681 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002682 }
2683
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002684 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002685 ret = 0;
2686
Paul Bakkerfbb17802013-04-17 19:10:21 +02002687 return( ret );
2688}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002689#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002690
Paul Bakker5121ce52009-01-03 21:22:43 +00002691static int ssl_parse_client_key_exchange( ssl_context *ssl )
2692{
Paul Bakker23986e52011-04-24 08:57:21 +00002693 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002694 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002695
Paul Bakker41c83d32013-03-20 14:39:14 +01002696 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002697
2698 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2699
2700 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2701 {
2702 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2703 return( ret );
2704 }
2705
2706 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2707 {
2708 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002709 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002710 }
2711
2712 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2713 {
2714 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002715 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002716 }
2717
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002718#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002719 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002720 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002721 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002722 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002723
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002724 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002725 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002726 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2727 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002728 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002729
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002730 if( p != end )
2731 {
2732 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2733 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2734 }
2735
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002736 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
2737
2738 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2739 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002740 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002741 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002742 {
2743 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2744 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2745 }
2746
2747 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002748 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002749 else
2750#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002751#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002752 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2753 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2754 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002755 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002756 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2757 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2758 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002759 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002760 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002761 ssl->in_msg + 4, ssl->in_hslen - 4 ) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002762 {
2763 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2764 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2765 }
2766
2767 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2768
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002769 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2770 &ssl->handshake->pmslen,
2771 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002772 POLARSSL_MPI_MAX_SIZE,
2773 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002774 {
2775 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2776 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2777 }
2778
2779 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002780 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002781 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002782#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002783 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2784 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2785 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002786#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2787 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002788 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002789 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002790 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002791
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002792 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002793 {
2794 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2795 return( ret );
2796 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002797
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002798 if( p != end )
2799 {
2800 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2801 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2802 }
2803
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002804 if( ( ret = ssl_psk_derive_premaster( ssl,
2805 ciphersuite_info->key_exchange ) ) != 0 )
2806 {
2807 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2808 return( ret );
2809 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002810 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002811 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002812#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002813#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2814 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2815 {
2816 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002817 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002818
2819 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2820 {
2821 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2822 return( ret );
2823 }
2824
2825 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
2826 {
2827 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
2828 return( ret );
2829 }
2830
2831 if( ( ret = ssl_psk_derive_premaster( ssl,
2832 ciphersuite_info->key_exchange ) ) != 0 )
2833 {
2834 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2835 return( ret );
2836 }
2837 }
2838 else
2839#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002840#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2841 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2842 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002843 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002844 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002845
2846 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2847 {
2848 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2849 return( ret );
2850 }
2851 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2852 {
2853 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2854 return( ret );
2855 }
2856
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002857 if( p != end )
2858 {
2859 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2860 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2861 }
2862
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002863 if( ( ret = ssl_psk_derive_premaster( ssl,
2864 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002865 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002866 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2867 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002868 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002869 }
2870 else
2871#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002872#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2873 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2874 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002875 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002876 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002877
2878 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2879 {
2880 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2881 return( ret );
2882 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002883
2884 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2885 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002886 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002887 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2888 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002889 }
2890
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002891 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2892
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002893 if( ( ret = ssl_psk_derive_premaster( ssl,
2894 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002895 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002896 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002897 return( ret );
2898 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002899 }
2900 else
2901#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002902#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2903 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002904 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002905 if( ( ret = ssl_parse_encrypted_pms( ssl,
2906 ssl->in_msg + 4,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002907 ssl->in_msg + ssl->in_hslen,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002908 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002909 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002910 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002911 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002912 }
2913 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002914 else
2915#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2916 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002917 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002918 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002919 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002920
Paul Bakkerff60ee62010-03-16 21:09:09 +00002921 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2922 {
2923 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2924 return( ret );
2925 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002926
Paul Bakker5121ce52009-01-03 21:22:43 +00002927 ssl->state++;
2928
2929 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2930
2931 return( 0 );
2932}
2933
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002934#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2935 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002936 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2937 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002938static int ssl_parse_certificate_verify( ssl_context *ssl )
2939{
Paul Bakkered27a042013-04-18 22:46:23 +02002940 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002941 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002942
2943 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2944
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002945 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002946 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002947 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002948 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002949 {
2950 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2951 ssl->state++;
2952 return( 0 );
2953 }
2954
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002955 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2956 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002957}
2958#else
2959static int ssl_parse_certificate_verify( ssl_context *ssl )
2960{
2961 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002962 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002963 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002964 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002965 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02002966#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002967 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02002968#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002969 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002970 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2971
2972 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2973
2974 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002975 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002976 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002977 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2978 {
2979 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2980 ssl->state++;
2981 return( 0 );
2982 }
2983
Paul Bakkered27a042013-04-18 22:46:23 +02002984 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002985 {
2986 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2987 ssl->state++;
2988 return( 0 );
2989 }
2990
Paul Bakker48916f92012-09-16 19:57:18 +00002991 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002992
2993 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2994 {
2995 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2996 return( ret );
2997 }
2998
2999 ssl->state++;
3000
3001 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3002 {
3003 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003004 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003005 }
3006
3007 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
3008 {
3009 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003010 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003011 }
3012
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003013 /*
3014 * 0 . 0 handshake type
3015 * 1 . 3 handshake length
3016 * 4 . 5 sig alg (TLS 1.2 only)
3017 * 4+n . 5+n signature length (n = sa_len)
3018 * 6+n . 6+n+m signature (m = sig_len)
3019 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003020
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003021#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3022 defined(POLARSSL_SSL_PROTO_TLS1_1)
3023 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003024 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003025 sa_len = 0;
3026
Paul Bakkerc70b9822013-04-07 22:00:46 +02003027 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003028 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003029
3030 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3031 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3032 POLARSSL_PK_ECDSA ) )
3033 {
3034 hash_start += 16;
3035 hashlen -= 16;
3036 md_alg = POLARSSL_MD_SHA1;
3037 }
Paul Bakker926af752012-11-23 13:38:07 +01003038 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003039 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003040#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3041 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003042#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3043 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003044 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003045 sa_len = 2;
3046
Paul Bakker5121ce52009-01-03 21:22:43 +00003047 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003048 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003049 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003050 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003051 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003052 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3053 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003054 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3055 }
3056
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003057 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003058
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003059 /* Info from md_alg will be used instead */
3060 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003061
3062 /*
3063 * Signature
3064 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003065 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
3066 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003067 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003068 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3069 " for verify message" ) );
3070 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003071 }
3072
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003073 /*
3074 * Check the certificate's key type matches the signature alg
3075 */
3076 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3077 {
3078 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3079 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3080 }
Paul Bakker577e0062013-08-28 11:57:20 +02003081 }
3082 else
3083#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3084 {
3085 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003086 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003087 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003088
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003089 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01003090
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003091 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003092 {
3093 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003094 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003095 }
3096
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003097 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003098 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003099 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003100 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003101 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003102 return( ret );
3103 }
3104
3105 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3106
Paul Bakkered27a042013-04-18 22:46:23 +02003107 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003108}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003109#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3110 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3111 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003112
Paul Bakkera503a632013-08-14 13:48:06 +02003113#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003114static int ssl_write_new_session_ticket( ssl_context *ssl )
3115{
3116 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003117 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003118 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003119
3120 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3121
3122 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3123 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3124
3125 /*
3126 * struct {
3127 * uint32 ticket_lifetime_hint;
3128 * opaque ticket<0..2^16-1>;
3129 * } NewSessionTicket;
3130 *
3131 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3132 * 8 . 9 ticket_len (n)
3133 * 10 . 9+n ticket content
3134 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003135
3136 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3137 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3138 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3139 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003140
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003141 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3142 {
3143 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3144 tlen = 0;
3145 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003146
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003147 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3148 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003149
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003150 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003151
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003152 /*
3153 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3154 * ChangeCipherSpec share the same state.
3155 */
3156 ssl->handshake->new_session_ticket = 0;
3157
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003158 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3159 {
3160 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3161 return( ret );
3162 }
3163
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003164 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3165
3166 return( 0 );
3167}
Paul Bakkera503a632013-08-14 13:48:06 +02003168#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003169
Paul Bakker5121ce52009-01-03 21:22:43 +00003170/*
Paul Bakker1961b702013-01-25 14:49:24 +01003171 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003172 */
Paul Bakker1961b702013-01-25 14:49:24 +01003173int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003174{
3175 int ret = 0;
3176
Paul Bakker1961b702013-01-25 14:49:24 +01003177 if( ssl->state == SSL_HANDSHAKE_OVER )
3178 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003179
Paul Bakker1961b702013-01-25 14:49:24 +01003180 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3181
3182 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3183 return( ret );
3184
3185 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003186 {
Paul Bakker1961b702013-01-25 14:49:24 +01003187 case SSL_HELLO_REQUEST:
3188 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003189 break;
3190
Paul Bakker1961b702013-01-25 14:49:24 +01003191 /*
3192 * <== ClientHello
3193 */
3194 case SSL_CLIENT_HELLO:
3195 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003196 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003197
3198 /*
3199 * ==> ServerHello
3200 * Certificate
3201 * ( ServerKeyExchange )
3202 * ( CertificateRequest )
3203 * ServerHelloDone
3204 */
3205 case SSL_SERVER_HELLO:
3206 ret = ssl_write_server_hello( ssl );
3207 break;
3208
3209 case SSL_SERVER_CERTIFICATE:
3210 ret = ssl_write_certificate( ssl );
3211 break;
3212
3213 case SSL_SERVER_KEY_EXCHANGE:
3214 ret = ssl_write_server_key_exchange( ssl );
3215 break;
3216
3217 case SSL_CERTIFICATE_REQUEST:
3218 ret = ssl_write_certificate_request( ssl );
3219 break;
3220
3221 case SSL_SERVER_HELLO_DONE:
3222 ret = ssl_write_server_hello_done( ssl );
3223 break;
3224
3225 /*
3226 * <== ( Certificate/Alert )
3227 * ClientKeyExchange
3228 * ( CertificateVerify )
3229 * ChangeCipherSpec
3230 * Finished
3231 */
3232 case SSL_CLIENT_CERTIFICATE:
3233 ret = ssl_parse_certificate( ssl );
3234 break;
3235
3236 case SSL_CLIENT_KEY_EXCHANGE:
3237 ret = ssl_parse_client_key_exchange( ssl );
3238 break;
3239
3240 case SSL_CERTIFICATE_VERIFY:
3241 ret = ssl_parse_certificate_verify( ssl );
3242 break;
3243
3244 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3245 ret = ssl_parse_change_cipher_spec( ssl );
3246 break;
3247
3248 case SSL_CLIENT_FINISHED:
3249 ret = ssl_parse_finished( ssl );
3250 break;
3251
3252 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003253 * ==> ( NewSessionTicket )
3254 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003255 * Finished
3256 */
3257 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003258#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003259 if( ssl->handshake->new_session_ticket != 0 )
3260 ret = ssl_write_new_session_ticket( ssl );
3261 else
Paul Bakkera503a632013-08-14 13:48:06 +02003262#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003263 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003264 break;
3265
3266 case SSL_SERVER_FINISHED:
3267 ret = ssl_write_finished( ssl );
3268 break;
3269
3270 case SSL_FLUSH_BUFFERS:
3271 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3272 ssl->state = SSL_HANDSHAKE_WRAPUP;
3273 break;
3274
3275 case SSL_HANDSHAKE_WRAPUP:
3276 ssl_handshake_wrapup( ssl );
3277 break;
3278
3279 default:
3280 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3281 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003282 }
3283
Paul Bakker5121ce52009-01-03 21:22:43 +00003284 return( ret );
3285}
Paul Bakker9af723c2014-05-01 13:03:14 +02003286#endif /* POLARSSL_SSL_SRV_C */