blob: eac553f5953266768fd2e18a636e67b822a51b91 [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)
Paul Bakker34617722014-06-13 17:20:13 +020055/* Implementation that should never be optimized out by the compiler */
56static void polarssl_zeroize( void *v, size_t n ) {
57 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
58}
59
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020060/*
61 * Serialize a session in the following format:
62 * 0 . n-1 session structure, n = sizeof(ssl_session)
63 * n . n+2 peer_cert length = m (0 if no certificate)
64 * n+3 . n+2+m peer cert ASN.1
65 *
66 * Assumes ticket is NULL (always true on server side).
67 */
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020068static int ssl_save_session( const ssl_session *session,
69 unsigned char *buf, size_t buf_len,
70 size_t *olen )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020071{
72 unsigned char *p = buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020073 size_t left = buf_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020075 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020077
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020078 if( left < sizeof( ssl_session ) )
79 return( -1 );
80
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020081 memcpy( p, session, sizeof( ssl_session ) );
82 p += sizeof( ssl_session );
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020083 left -= sizeof( ssl_session );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020084
Paul Bakker7c6b2c32013-09-16 13:49:26 +020085#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020086 if( session->peer_cert == NULL )
87 cert_len = 0;
88 else
89 cert_len = session->peer_cert->raw.len;
90
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020091 if( left < 3 + cert_len )
92 return( -1 );
93
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020094 *p++ = (unsigned char)( cert_len >> 16 & 0xFF );
95 *p++ = (unsigned char)( cert_len >> 8 & 0xFF );
96 *p++ = (unsigned char)( cert_len & 0xFF );
97
98 if( session->peer_cert != NULL )
99 memcpy( p, session->peer_cert->raw.p, cert_len );
100
101 p += cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200102#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200103
104 *olen = p - buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200105
106 return( 0 );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200107}
108
109/*
110 * Unserialise session, see ssl_save_session()
111 */
112static int ssl_load_session( ssl_session *session,
113 const unsigned char *buf, size_t len )
114{
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200115 const unsigned char *p = buf;
116 const unsigned char * const end = buf + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200117#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200118 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200119#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200120
121 if( p + sizeof( ssl_session ) > end )
122 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
123
124 memcpy( session, p, sizeof( ssl_session ) );
125 p += sizeof( ssl_session );
126
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200127#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200128 if( p + 3 > end )
129 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
130
131 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
132 p += 3;
133
134 if( cert_len == 0 )
135 {
136 session->peer_cert = NULL;
137 }
138 else
139 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200140 int ret;
141
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200142 if( p + cert_len > end )
143 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
144
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200145 session->peer_cert = polarssl_malloc( sizeof( x509_crt ) );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200146
147 if( session->peer_cert == NULL )
148 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
149
Paul Bakkerb6b09562013-09-18 14:17:41 +0200150 x509_crt_init( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200151
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200152 if( ( ret = x509_crt_parse_der( session->peer_cert,
153 p, cert_len ) ) != 0 )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200154 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200155 x509_crt_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200156 polarssl_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200157 session->peer_cert = NULL;
158 return( ret );
159 }
160
161 p += cert_len;
162 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200163#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200164
165 if( p != end )
166 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
167
168 return( 0 );
169}
170
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200171/*
172 * Create session ticket, secured as recommended in RFC 5077 section 4:
173 *
174 * struct {
175 * opaque key_name[16];
176 * opaque iv[16];
177 * opaque encrypted_state<0..2^16-1>;
178 * opaque mac[32];
179 * } ticket;
180 *
181 * (the internal state structure differs, however).
182 */
183static int ssl_write_ticket( ssl_context *ssl, size_t *tlen )
184{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200185 int ret;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200186 unsigned char * const start = ssl->out_msg + 10;
187 unsigned char *p = start;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200188 unsigned char *state;
189 unsigned char iv[16];
190 size_t clear_len, enc_len, pad_len, i;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200191
Manuel Pégourié-Gonnard0a201712013-08-23 16:25:16 +0200192 *tlen = 0;
193
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200194 if( ssl->ticket_keys == NULL )
195 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
196
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200197 /* Write key name */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200198 memcpy( p, ssl->ticket_keys->key_name, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200199 p += 16;
200
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200201 /* Generate and write IV (with a copy for aes_crypt) */
202 if( ( ret = ssl->f_rng( ssl->p_rng, p, 16 ) ) != 0 )
203 return( ret );
204 memcpy( iv, p, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200205 p += 16;
206
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200207 /*
208 * Dump session state
209 *
210 * After the session state itself, we still need room for 16 bytes of
211 * padding and 32 bytes of MAC, so there's only so much room left
212 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200213 state = p + 2;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200214 if( ssl_save_session( ssl->session_negotiate, state,
Manuel Pégourié-Gonnard5d53cbe2014-07-14 13:51:41 +0200215 SSL_MAX_CONTENT_LEN - ( state - ssl->out_msg ) - 48,
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200216 &clear_len ) != 0 )
217 {
218 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
219 }
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200220 SSL_DEBUG_BUF( 3, "session ticket cleartext", state, clear_len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200221
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200222 /* Apply PKCS padding */
223 pad_len = 16 - clear_len % 16;
224 enc_len = clear_len + pad_len;
225 for( i = clear_len; i < enc_len; i++ )
226 state[i] = (unsigned char) pad_len;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200227
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200228 /* Encrypt */
229 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->enc, AES_ENCRYPT,
230 enc_len, iv, state, state ) ) != 0 )
231 {
232 return( ret );
233 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200234
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200235 /* Write length */
236 *p++ = (unsigned char)( ( enc_len >> 8 ) & 0xFF );
237 *p++ = (unsigned char)( ( enc_len ) & 0xFF );
238 p = state + enc_len;
239
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200240 /* Compute and write MAC( key_name + iv + enc_state_len + enc_state ) */
241 sha256_hmac( ssl->ticket_keys->mac_key, 16, start, p - start, p, 0 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200242 p += 32;
243
244 *tlen = p - start;
245
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200246 SSL_DEBUG_BUF( 3, "session ticket structure", start, *tlen );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200247
248 return( 0 );
249}
250
251/*
252 * Load session ticket (see ssl_write_ticket for structure)
253 */
254static int ssl_parse_ticket( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200255 unsigned char *buf,
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200256 size_t len )
257{
258 int ret;
259 ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200260 unsigned char *key_name = buf;
261 unsigned char *iv = buf + 16;
262 unsigned char *enc_len_p = iv + 16;
263 unsigned char *ticket = enc_len_p + 2;
264 unsigned char *mac;
Manuel Pégourié-Gonnard34ced2d2013-09-20 11:37:39 +0200265 unsigned char computed_mac[32];
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200266 size_t enc_len, clear_len, i;
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100267 unsigned char pad_len, diff;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200268
269 SSL_DEBUG_BUF( 3, "session ticket structure", buf, len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200270
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200271 if( len < 34 || ssl->ticket_keys == NULL )
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200272 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
273
274 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
275 mac = ticket + enc_len;
276
277 if( len != enc_len + 66 )
278 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
279
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100280 /* Check name, in constant time though it's not a big secret */
281 diff = 0;
282 for( i = 0; i < 16; i++ )
283 diff |= key_name[i] ^ ssl->ticket_keys->key_name[i];
284 /* don't return yet, check the MAC anyway */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200285
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100286 /* Check mac, with constant-time buffer comparison */
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200287 sha256_hmac( ssl->ticket_keys->mac_key, 16, buf, len - 32,
288 computed_mac, 0 );
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100289
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200290 for( i = 0; i < 32; i++ )
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100291 diff |= mac[i] ^ computed_mac[i];
292
293 /* Now return if ticket is not authentic, since we want to avoid
294 * decrypting arbitrary attacker-chosen data */
295 if( diff != 0 )
296 return( POLARSSL_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200297
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200298 /* Decrypt */
299 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->dec, AES_DECRYPT,
300 enc_len, iv, ticket, ticket ) ) != 0 )
301 {
302 return( ret );
303 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200304
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200305 /* Check PKCS padding */
306 pad_len = ticket[enc_len - 1];
307
308 ret = 0;
309 for( i = 2; i < pad_len; i++ )
310 if( ticket[enc_len - i] != pad_len )
311 ret = POLARSSL_ERR_SSL_BAD_INPUT_DATA;
312 if( ret != 0 )
313 return( ret );
314
315 clear_len = enc_len - pad_len;
316
317 SSL_DEBUG_BUF( 3, "session ticket cleartext", ticket, clear_len );
318
319 /* Actually load session */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200320 if( ( ret = ssl_load_session( &session, ticket, clear_len ) ) != 0 )
321 {
322 SSL_DEBUG_MSG( 1, ( "failed to parse ticket content" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100323 ssl_session_free( &session );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200324 return( ret );
325 }
326
Paul Bakker606b4ba2013-08-14 16:52:14 +0200327#if defined(POLARSSL_HAVE_TIME)
328 /* Check if still valid */
329 if( (int) ( time( NULL) - session.start ) > ssl->ticket_lifetime )
330 {
331 SSL_DEBUG_MSG( 1, ( "session ticket expired" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100332 ssl_session_free( &session );
Paul Bakker606b4ba2013-08-14 16:52:14 +0200333 return( POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED );
334 }
335#endif
336
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200337 /*
338 * Keep the session ID sent by the client, since we MUST send it back to
339 * inform him we're accepting the ticket (RFC 5077 section 3.4)
340 */
341 session.length = ssl->session_negotiate->length;
342 memcpy( &session.id, ssl->session_negotiate->id, session.length );
343
344 ssl_session_free( ssl->session_negotiate );
345 memcpy( ssl->session_negotiate, &session, sizeof( ssl_session ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200346
347 /* Zeroize instead of free as we copied the content */
Paul Bakker34617722014-06-13 17:20:13 +0200348 polarssl_zeroize( &session, sizeof( ssl_session ) );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200349
350 return( 0 );
351}
Paul Bakkera503a632013-08-14 13:48:06 +0200352#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200353
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +0200354#if defined(POLARSSL_SSL_PROTO_DTLS)
355int ssl_set_client_transport_id( ssl_context *ssl,
356 const unsigned char *info,
357 size_t ilen )
358{
359 if( ssl->endpoint != SSL_IS_SERVER )
360 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
361
362 polarssl_free( ssl->cli_id );
363
364 if( ( ssl->cli_id = polarssl_malloc( ilen ) ) == NULL )
365 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
366
367 memcpy( ssl->cli_id, info, ilen );
368 ssl->cli_id_len = ilen;
369
370 return( 0 );
371}
372#endif /* POLARSSL_SSL_PROTO_DTLS */
373
Paul Bakker0be444a2013-08-27 21:55:01 +0200374#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200375/*
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200376 * Wrapper around f_sni, allowing use of ssl_set_own_cert() but
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +0100377 * making it act on ssl->handshake->sni_key_cert instead.
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200378 */
379static int ssl_sni_wrapper( ssl_context *ssl,
380 const unsigned char* name, size_t len )
381{
382 int ret;
383 ssl_key_cert *key_cert_ori = ssl->key_cert;
384
385 ssl->key_cert = NULL;
386 ret = ssl->f_sni( ssl->p_sni, ssl, name, len );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200387 ssl->handshake->sni_key_cert = ssl->key_cert;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200388
389 ssl->key_cert = key_cert_ori;
390
391 return( ret );
392}
393
Paul Bakker5701cdc2012-09-27 21:49:42 +0000394static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000395 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000396 size_t len )
397{
398 int ret;
399 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000400 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000401
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100402 SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
403
Paul Bakker5701cdc2012-09-27 21:49:42 +0000404 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
405 if( servername_list_size + 2 != len )
406 {
407 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
408 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
409 }
410
411 p = buf + 2;
412 while( servername_list_size > 0 )
413 {
414 hostname_len = ( ( p[1] << 8 ) | p[2] );
415 if( hostname_len + 3 > servername_list_size )
416 {
417 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
418 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
419 }
420
421 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
422 {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200423 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000424 if( ret != 0 )
425 {
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100426 SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000427 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
428 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
429 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
430 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000431 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000432 }
433
434 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000435 p += hostname_len + 3;
436 }
437
438 if( servername_list_size != 0 )
439 {
440 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
441 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000442 }
443
444 return( 0 );
445}
Paul Bakker0be444a2013-08-27 21:55:01 +0200446#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000447
Paul Bakker48916f92012-09-16 19:57:18 +0000448static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000449 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000450 size_t len )
451{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000452 int ret;
453
Paul Bakker48916f92012-09-16 19:57:18 +0000454 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
455 {
456 if( len != 1 || buf[0] != 0x0 )
457 {
458 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000459
460 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
461 return( ret );
462
Paul Bakker48916f92012-09-16 19:57:18 +0000463 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
464 }
465
466 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
467 }
468 else
469 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100470 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000471 if( len != 1 + ssl->verify_data_len ||
472 buf[0] != ssl->verify_data_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100473 safer_memcmp( buf + 1, ssl->peer_verify_data,
474 ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000475 {
476 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000477
478 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
479 return( ret );
480
Paul Bakker48916f92012-09-16 19:57:18 +0000481 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
482 }
483 }
484
485 return( 0 );
486}
487
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200488#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +0000489static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
490 const unsigned char *buf,
491 size_t len )
492{
493 size_t sig_alg_list_size;
494 const unsigned char *p;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200495 const unsigned char *end = buf + len;
496 const int *md_cur;
497
Paul Bakker23f36802012-09-28 14:15:14 +0000498
499 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
500 if( sig_alg_list_size + 2 != len ||
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200501 sig_alg_list_size % 2 != 0 )
Paul Bakker23f36802012-09-28 14:15:14 +0000502 {
503 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
504 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
505 }
506
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200507 /*
508 * For now, ignore the SignatureAlgorithm part and rely on offered
509 * ciphersuites only for that part. To be fixed later.
510 *
511 * So, just look at the HashAlgorithm part.
512 */
513 for( md_cur = md_list(); *md_cur != POLARSSL_MD_NONE; md_cur++ ) {
514 for( p = buf + 2; p < end; p += 2 ) {
515 if( *md_cur == (int) ssl_md_alg_from_hash( p[0] ) ) {
516 ssl->handshake->sig_alg = p[0];
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200517 goto have_sig_alg;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200518 }
Paul Bakker23f36802012-09-28 14:15:14 +0000519 }
Paul Bakker23f36802012-09-28 14:15:14 +0000520 }
521
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200522 /* Some key echanges do not need signatures at all */
523 SSL_DEBUG_MSG( 3, ( "no signature_algorithm in common" ) );
524 return( 0 );
525
526have_sig_alg:
Paul Bakker23f36802012-09-28 14:15:14 +0000527 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
528 ssl->handshake->sig_alg ) );
529
530 return( 0 );
531}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200532#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker23f36802012-09-28 14:15:14 +0000533
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200534#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200535static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
536 const unsigned char *buf,
537 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100538{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200539 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100540 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200541 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100542
543 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
544 if( list_size + 2 != len ||
545 list_size % 2 != 0 )
546 {
547 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
548 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
549 }
550
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200551 /* Should never happen unless client duplicates the extension */
552 if( ssl->handshake->curves != NULL )
553 {
554 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
555 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
556 }
557
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +0100558 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200559 * and leave room for a final 0 */
560 our_size = list_size / 2 + 1;
561 if( our_size > POLARSSL_ECP_DP_MAX )
562 our_size = POLARSSL_ECP_DP_MAX;
563
564 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
565 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
566
Paul Bakker9af723c2014-05-01 13:03:14 +0200567 /* explicit void pointer cast for buggy MS compiler */
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200568 memset( (void *) curves, 0, our_size * sizeof( *curves ) );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200569 ssl->handshake->curves = curves;
570
Paul Bakker41c83d32013-03-20 14:39:14 +0100571 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200572 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100573 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200574 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200575
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200576 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100577 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200578 *curves++ = curve_info;
579 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100580 }
581
582 list_size -= 2;
583 p += 2;
584 }
585
586 return( 0 );
587}
588
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200589static int ssl_parse_supported_point_formats( ssl_context *ssl,
590 const unsigned char *buf,
591 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100592{
593 size_t list_size;
594 const unsigned char *p;
595
596 list_size = buf[0];
597 if( list_size + 1 != len )
598 {
599 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
600 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
601 }
602
603 p = buf + 2;
604 while( list_size > 0 )
605 {
606 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
607 p[0] == POLARSSL_ECP_PF_COMPRESSED )
608 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200609 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200610 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100611 return( 0 );
612 }
613
614 list_size--;
615 p++;
616 }
617
618 return( 0 );
619}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200620#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100621
Paul Bakker05decb22013-08-15 13:33:48 +0200622#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200623static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
624 const unsigned char *buf,
625 size_t len )
626{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200627 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200628 {
629 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
630 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
631 }
632
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200633 ssl->session_negotiate->mfl_code = buf[0];
634
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200635 return( 0 );
636}
Paul Bakker05decb22013-08-15 13:33:48 +0200637#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200638
Paul Bakker1f2bc622013-08-15 13:45:55 +0200639#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200640static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
641 const unsigned char *buf,
642 size_t len )
643{
644 if( len != 0 )
645 {
646 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
647 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
648 }
649
650 ((void) buf);
651
652 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
653
654 return( 0 );
655}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200656#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200657
Paul Bakkera503a632013-08-14 13:48:06 +0200658#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200659static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200660 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200661 size_t len )
662{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200663 int ret;
664
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200665 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
666 return( 0 );
667
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200668 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200669 ssl->handshake->new_session_ticket = 1;
670
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200671 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
672
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200673 if( len == 0 )
674 return( 0 );
675
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200676 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
677 {
678 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
679 return( 0 );
680 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200681
682 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200683 * Failures are ok: just ignore the ticket and proceed.
684 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200685 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
686 {
687 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200688 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200689 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200690
691 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
692
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200693 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200694
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200695 /* Don't send a new ticket after all, this one is OK */
696 ssl->handshake->new_session_ticket = 0;
697
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200698 return( 0 );
699}
Paul Bakkera503a632013-08-14 13:48:06 +0200700#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200701
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200702#if defined(POLARSSL_SSL_ALPN)
703static int ssl_parse_alpn_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200704 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200705{
Paul Bakker14b16c62014-05-28 11:33:54 +0200706 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200707 const unsigned char *theirs, *start, *end;
708 const char **ours;
709
710 /* If ALPN not configured, just ignore the extension */
711 if( ssl->alpn_list == NULL )
712 return( 0 );
713
714 /*
715 * opaque ProtocolName<1..2^8-1>;
716 *
717 * struct {
718 * ProtocolName protocol_name_list<2..2^16-1>
719 * } ProtocolNameList;
720 */
721
722 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
723 if( len < 4 )
724 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
725
726 list_len = ( buf[0] << 8 ) | buf[1];
727 if( list_len != len - 2 )
728 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
729
730 /*
731 * Use our order of preference
732 */
733 start = buf + 2;
734 end = buf + len;
735 for( ours = ssl->alpn_list; *ours != NULL; ours++ )
736 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200737 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200738 for( theirs = start; theirs != end; theirs += cur_len )
739 {
740 /* If the list is well formed, we should get equality first */
741 if( theirs > end )
742 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
743
744 cur_len = *theirs++;
745
746 /* Empty strings MUST NOT be included */
747 if( cur_len == 0 )
748 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
749
Paul Bakker14b16c62014-05-28 11:33:54 +0200750 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200751 memcmp( theirs, *ours, cur_len ) == 0 )
752 {
753 ssl->alpn_chosen = *ours;
754 return( 0 );
755 }
756 }
757 }
758
759 /* If we get there, no match was found */
760 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
761 SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
762 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
763}
764#endif /* POLARSSL_SSL_ALPN */
765
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100766/*
767 * Auxiliary functions for ServerHello parsing and related actions
768 */
769
770#if defined(POLARSSL_X509_CRT_PARSE_C)
771/*
772 * Return 1 if the given EC key uses the given curve, 0 otherwise
773 */
774#if defined(POLARSSL_ECDSA_C)
775static int ssl_key_matches_curves( pk_context *pk,
776 const ecp_curve_info **curves )
777{
778 const ecp_curve_info **crv = curves;
779 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
780
781 while( *crv != NULL )
782 {
783 if( (*crv)->grp_id == grp_id )
784 return( 1 );
785 crv++;
786 }
787
788 return( 0 );
789}
790#endif /* POLARSSL_ECDSA_C */
791
792/*
793 * Try picking a certificate for this ciphersuite,
794 * return 0 on success and -1 on failure.
795 */
796static int ssl_pick_cert( ssl_context *ssl,
797 const ssl_ciphersuite_t * ciphersuite_info )
798{
799 ssl_key_cert *cur, *list;
800 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
801
802#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
803 if( ssl->handshake->sni_key_cert != NULL )
804 list = ssl->handshake->sni_key_cert;
805 else
806#endif
807 list = ssl->handshake->key_cert;
808
809 if( pk_alg == POLARSSL_PK_NONE )
810 return( 0 );
811
812 for( cur = list; cur != NULL; cur = cur->next )
813 {
814 if( ! pk_can_do( cur->key, pk_alg ) )
815 continue;
816
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200817 /*
818 * This avoids sending the client a cert it'll reject based on
819 * keyUsage or other extensions.
820 *
821 * It also allows the user to provision different certificates for
822 * different uses based on keyUsage, eg if they want to avoid signing
823 * and decrypting with the same RSA key.
824 */
825 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
826 SSL_IS_SERVER ) != 0 )
827 {
828 continue;
829 }
830
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100831#if defined(POLARSSL_ECDSA_C)
832 if( pk_alg == POLARSSL_PK_ECDSA )
833 {
834 if( ssl_key_matches_curves( cur->key, ssl->handshake->curves ) )
835 break;
836 }
837 else
838#endif
839 break;
840 }
841
842 if( cur == NULL )
843 return( -1 );
844
845 ssl->handshake->key_cert = cur;
846 return( 0 );
847}
848#endif /* POLARSSL_X509_CRT_PARSE_C */
849
850/*
851 * Check if a given ciphersuite is suitable for use with our config/keys/etc
852 * Sets ciphersuite_info only if the suite matches.
853 */
854static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
855 const ssl_ciphersuite_t **ciphersuite_info )
856{
857 const ssl_ciphersuite_t *suite_info;
858
859 suite_info = ssl_ciphersuite_from_id( suite_id );
860 if( suite_info == NULL )
861 {
862 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", suite_id ) );
863 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
864 }
865
866 if( suite_info->min_minor_ver > ssl->minor_ver ||
867 suite_info->max_minor_ver < ssl->minor_ver )
868 return( 0 );
869
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +0100870#if defined(POLARSSL_SSL_PROTO_DTLS)
871 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
872 ( suite_info->flags & POLARSSL_CIPHERSUITE_NODTLS ) )
873 return( 0 );
874#endif
875
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100876#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
877 if( ssl_ciphersuite_uses_ec( suite_info ) &&
878 ( ssl->handshake->curves == NULL ||
879 ssl->handshake->curves[0] == NULL ) )
880 return( 0 );
881#endif
882
883#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
884 /* If the ciphersuite requires a pre-shared key and we don't
885 * have one, skip it now rather than failing later */
886 if( ssl_ciphersuite_uses_psk( suite_info ) &&
887 ssl->f_psk == NULL &&
888 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
889 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
890 return( 0 );
891#endif
892
893#if defined(POLARSSL_X509_CRT_PARSE_C)
894 /*
895 * Final check: if ciphersuite requires us to have a
896 * certificate/key of a particular type:
897 * - select the appropriate certificate if we have one, or
898 * - try the next ciphersuite if we don't
899 * This must be done last since we modify the key_cert list.
900 */
901 if( ssl_pick_cert( ssl, suite_info ) != 0 )
902 return( 0 );
903#endif
904
905 *ciphersuite_info = suite_info;
906 return( 0 );
907}
908
Paul Bakker78a8c712013-03-06 17:01:52 +0100909#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
910static int ssl_parse_client_hello_v2( ssl_context *ssl )
911{
912 int ret;
913 unsigned int i, j;
914 size_t n;
915 unsigned int ciph_len, sess_len, chal_len;
916 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200917 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200918 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100919
920 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
921
922 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
923 {
924 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
925
926 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
927 return( ret );
928
929 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
930 }
931
932 buf = ssl->in_hdr;
933
934 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
935
936 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
937 buf[2] ) );
938 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
939 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
940 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
941 buf[3], buf[4] ) );
942
943 /*
944 * SSLv2 Client Hello
945 *
946 * Record layer:
947 * 0 . 1 message length
948 *
949 * SSL layer:
950 * 2 . 2 message type
951 * 3 . 4 protocol version
952 */
953 if( buf[2] != SSL_HS_CLIENT_HELLO ||
954 buf[3] != SSL_MAJOR_VERSION_3 )
955 {
956 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
957 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
958 }
959
960 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
961
962 if( n < 17 || n > 512 )
963 {
964 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
965 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
966 }
967
968 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200969 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
970 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100971
972 if( ssl->minor_ver < ssl->min_minor_ver )
973 {
974 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200975 " [%d:%d] < [%d:%d]",
976 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +0100977 ssl->min_major_ver, ssl->min_minor_ver ) );
978
979 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
980 SSL_ALERT_MSG_PROTOCOL_VERSION );
981 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
982 }
983
Paul Bakker2fbefde2013-06-29 16:01:15 +0200984 ssl->handshake->max_major_ver = buf[3];
985 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100986
987 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
988 {
989 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
990 return( ret );
991 }
992
993 ssl->handshake->update_checksum( ssl, buf + 2, n );
994
995 buf = ssl->in_msg;
996 n = ssl->in_left - 5;
997
998 /*
999 * 0 . 1 ciphersuitelist length
1000 * 2 . 3 session id length
1001 * 4 . 5 challenge length
1002 * 6 . .. ciphersuitelist
1003 * .. . .. session id
1004 * .. . .. challenge
1005 */
1006 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1007
1008 ciph_len = ( buf[0] << 8 ) | buf[1];
1009 sess_len = ( buf[2] << 8 ) | buf[3];
1010 chal_len = ( buf[4] << 8 ) | buf[5];
1011
1012 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
1013 ciph_len, sess_len, chal_len ) );
1014
1015 /*
1016 * Make sure each parameter length is valid
1017 */
1018 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1019 {
1020 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1021 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1022 }
1023
1024 if( sess_len > 32 )
1025 {
1026 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1027 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1028 }
1029
1030 if( chal_len < 8 || chal_len > 32 )
1031 {
1032 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1033 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1034 }
1035
1036 if( n != 6 + ciph_len + sess_len + chal_len )
1037 {
1038 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1039 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1040 }
1041
1042 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1043 buf + 6, ciph_len );
1044 SSL_DEBUG_BUF( 3, "client hello, session id",
1045 buf + 6 + ciph_len, sess_len );
1046 SSL_DEBUG_BUF( 3, "client hello, challenge",
1047 buf + 6 + ciph_len + sess_len, chal_len );
1048
1049 p = buf + 6 + ciph_len;
1050 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001051 memset( ssl->session_negotiate->id, 0,
1052 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001053 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1054
1055 p += sess_len;
1056 memset( ssl->handshake->randbytes, 0, 64 );
1057 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1058
1059 /*
1060 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1061 */
1062 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1063 {
1064 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1065 {
1066 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1067 if( ssl->renegotiation == SSL_RENEGOTIATION )
1068 {
1069 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1070
1071 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1072 return( ret );
1073
1074 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1075 }
1076 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1077 break;
1078 }
1079 }
1080
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001081 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001082 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001083#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1084 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1085 {
1086 for( i = 0; ciphersuites[i] != 0; i++ )
1087#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001088 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001089 {
1090 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001091#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001092 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001093 if( p[0] != 0 ||
1094 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1095 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1096 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001097
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001098 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1099 &ciphersuite_info ) ) != 0 )
1100 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001101
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001102 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001103 goto have_ciphersuite_v2;
1104 }
1105 }
1106
1107 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1108
1109 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1110
1111have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001112 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001113 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001114 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001115
1116 /*
1117 * SSLv2 Client Hello relevant renegotiation security checks
1118 */
1119 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1120 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1121 {
1122 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1123
1124 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1125 return( ret );
1126
1127 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1128 }
1129
1130 ssl->in_left = 0;
1131 ssl->state++;
1132
1133 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1134
1135 return( 0 );
1136}
1137#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1138
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001139#if defined(POLARSSL_SSL_PROTO_DTLS)
1140/*
1141 * Generate cookie for DTLS ClientHello verification
1142 */
1143static int ssl_generate_verify_cookie( ssl_context *ssl )
1144{
1145 unsigned char *cookie = ssl->handshake->verify_cookie;
1146 unsigned char cookie_len;
1147
1148 polarssl_free( cookie );
1149
1150 cookie_len = 16; /* fixed for now */
1151 if( ( cookie = polarssl_malloc( cookie_len ) ) == NULL )
1152 {
1153 SSL_DEBUG_MSG( 1, ( "malloc (%d bytes) failed\n", cookie_len ) );
1154 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
1155 }
1156
1157 /* Dummy, fixed string for now */
1158 memset( cookie, 0x2a, cookie_len );
1159
1160 ssl->handshake->verify_cookie = cookie;
1161 ssl->handshake->verify_cookie_len = cookie_len;
1162
1163 return( 0 );
1164}
1165#endif /* POLARSSL_SSL_PROTO_DTLS */
1166
Paul Bakker5121ce52009-01-03 21:22:43 +00001167static int ssl_parse_client_hello( ssl_context *ssl )
1168{
Paul Bakker23986e52011-04-24 08:57:21 +00001169 int ret;
1170 unsigned int i, j;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001171 unsigned int ciph_offset, comp_offset, ext_offset;
1172 unsigned int msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001173#if defined(POLARSSL_SSL_PROTO_DTLS)
1174 unsigned int cookie_offset, cookie_len;
1175#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001176 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001177 int renegotiation_info_seen = 0;
1178 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001179 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001180 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001181 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001182
1183 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1184
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001185 /*
1186 * If renegotiating, then the input was read with ssl_read_record(),
1187 * otherwise read it ourselves manually in order to support SSLv2
1188 * ClientHello, which doesn't use the same record layer format.
1189 */
Paul Bakker48916f92012-09-16 19:57:18 +00001190 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001191 ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001192 {
1193 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1194 return( ret );
1195 }
1196
1197 buf = ssl->in_hdr;
1198
Paul Bakker78a8c712013-03-06 17:01:52 +01001199#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001200 if( ssl->transport == SSL_TRANSPORT_STREAM && ( buf[0] & 0x80 ) != 0 )
Paul Bakker78a8c712013-03-06 17:01:52 +01001201 return ssl_parse_client_hello_v2( ssl );
1202#endif
1203
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001204 SSL_DEBUG_BUF( 4, "record header", buf, ssl_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001205
Paul Bakkerec636f32012-09-09 19:17:02 +00001206 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001207 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001208 *
1209 * Record layer:
1210 * 0 . 0 message type
1211 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001212 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001213 * 3 . 4 message length
1214 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001215 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1216 buf[0] ) );
1217
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001218 if( buf[0] != SSL_MSG_HANDSHAKE )
1219 {
1220 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1221 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1222 }
1223
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001224 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1225 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1226
1227 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol version: [%d:%d]",
1228 buf[1], buf[2] ) );
1229
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001230 ssl_read_version( &major, &minor, ssl->transport, buf + 1 );
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001231
1232 /* According to RFC 5246 Appendix E.1, the version here is typically
1233 * "{03,00}, the lowest version number supported by the client, [or] the
1234 * value of ClientHello.client_version", so the only meaningful check here
1235 * is the major version shouldn't be less than 3 */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001236 if( major < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001238 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1239 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1240 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001241
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001242 /* For DTLS if this is the initial handshake, remember the client sequence
1243 * number to use it in our next message (RFC 6347 4.2.1) */
1244#if defined(POLARSSL_SSL_PROTO_DTLS)
1245 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
1246 ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1247 {
1248 /* Epoch should be 0 for initial handshakes */
1249 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1250 {
1251 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1252 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1253 }
1254
1255 memcpy( ssl->out_ctr + 2, ssl->in_ctr + 2, 6 );
1256 }
1257#endif /* POLARSSL_SSL_PROTO_DTLS */
1258
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001259 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001260
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001261 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Paul Bakkerec636f32012-09-09 19:17:02 +00001262 {
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001263 if( msg_len > SSL_MAX_CONTENT_LEN )
1264 {
1265 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1266 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1267 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001268
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001269 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) + msg_len ) ) != 0 )
1270 {
1271 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1272 return( ret );
1273 }
1274 }
1275 else
Paul Bakkerec636f32012-09-09 19:17:02 +00001276 {
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001277 /* Set by ssl_read_record() */
1278 msg_len = ssl->in_hslen;
Paul Bakkerec636f32012-09-09 19:17:02 +00001279 }
1280
1281 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001282
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001283 SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001284
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001285 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001286
1287 /*
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001288 * The DTLS handshake layer contains additional fields. Use them, then
1289 * remove them so that it looks like TLS format to the rest of the code.
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001290 */
1291#if defined(POLARSSL_SSL_PROTO_DTLS)
1292 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1293 {
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001294 /* 1 (type) + 3 (len) + 2 (seq) + 3 (frag_offset) + 3 (frag_len) */
1295 if( msg_len < 12 )
1296 {
1297 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1298 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1299 }
1300
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001301 /*
1302 * Copy the client's handshake message_seq on initial handshakes
1303 */
1304 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1305 ssl->handshake->msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
1306
1307 // TODO: DTLS: check message_seq on non-initial handshakes?
1308 // (or already done in ssl_read_record?)
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001309
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001310 /*
1311 * For now we don't support fragmentation, so make sure
1312 * fragment_offset == 0 and fragment_length == length
1313 *
1314 * TODO: DTLS: support fragmentation??
1315 * Well, ClientHello is rarely much longer than 512 bytes
1316 * so it will probably never be fragmented anyway...
1317 */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001318 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1319 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1320 {
1321 SSL_DEBUG_MSG( 1, ( "handshake fragmentation not supported" ) );
1322 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1323 }
1324
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001325 /* Remove the additional fields */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001326 memmove( buf + 4, buf + 12, msg_len - 12 );
1327 msg_len -= 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001328 }
1329#endif /* POLARSSL_SSL_PROTO_DTLS */
1330
1331 /*
Paul Bakkerec636f32012-09-09 19:17:02 +00001332 * SSL layer:
1333 * 0 . 0 handshake type
1334 * 1 . 3 handshake length
1335 * 4 . 5 protocol version
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001336 * 6 . 37 random bytes (starting with 4 bytes of Unix time)
1337 * 38 . 38 session id length (1 byte)
Paul Bakkerec636f32012-09-09 19:17:02 +00001338 * 39 . 38+x session id
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001339 * 39+x . 39+x DTLS only: cookie length (1 byte)
1340 * 40+x . .. DTSL only: cookie
1341 * .. . .. ciphersuite list length (2 bytes)
1342 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001343 * .. . .. compression alg. list length (1 byte)
1344 * .. . .. compression alg. list
1345 * .. . .. extensions length (2 bytes, optional)
1346 * .. . .. extensions (optional)
1347 *
1348 * Minimal length (with everything empty and extensions ommitted) is
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001349 * 4 + 2 + 32 + 1 + 2 + 1 = 42 bytes. Check that first, so that we can
1350 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001351 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001352
1353 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001354 * Check the handshake type and message length
Paul Bakkerec636f32012-09-09 19:17:02 +00001355 */
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001356 if( msg_len < 42 )
1357 {
1358 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1359 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1360 }
1361
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001362 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
1363
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001364 if( buf[0] != SSL_HS_CLIENT_HELLO )
Paul Bakkerec636f32012-09-09 19:17:02 +00001365 {
1366 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1367 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1368 }
1369
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001370 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1371 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1372
1373 if( buf[1] != 0 ||
1374 msg_len != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1375 {
1376 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1377 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1378 }
1379
1380 /*
1381 * Check and save the protocol version
1382 */
1383 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1384 buf[4], buf[5] ) );
1385
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001386 ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
1387 ssl->transport, buf + 4 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001388
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001389 ssl->handshake->max_major_ver = ssl->major_ver;
1390 ssl->handshake->max_minor_ver = ssl->minor_ver;
1391
1392 if( ssl->major_ver < ssl->min_major_ver ||
1393 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001394 {
1395 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001396 " [%d:%d] < [%d:%d]",
1397 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001398 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001399
1400 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1401 SSL_ALERT_MSG_PROTOCOL_VERSION );
1402
1403 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1404 }
1405
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001406 if( ssl->major_ver > ssl->max_major_ver )
1407 {
1408 ssl->major_ver = ssl->max_major_ver;
1409 ssl->minor_ver = ssl->max_minor_ver;
1410 }
1411 else if( ssl->minor_ver > ssl->max_minor_ver )
1412 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001413
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001414 /*
1415 * Save client random (inc. Unix time)
1416 */
1417 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1418 buf + 6, 32 );
1419
Paul Bakker48916f92012-09-16 19:57:18 +00001420 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001421
1422 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001423 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001424 */
1425 sess_len = buf[38];
1426
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001427 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001428 sess_len + 38 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001429 {
1430 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1431 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1432 }
1433
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001434 SSL_DEBUG_BUF( 3, "client hello, session id",
1435 buf + 39, sess_len );
1436
Paul Bakker48916f92012-09-16 19:57:18 +00001437 ssl->session_negotiate->length = sess_len;
1438 memset( ssl->session_negotiate->id, 0,
1439 sizeof( ssl->session_negotiate->id ) );
1440 memcpy( ssl->session_negotiate->id, buf + 39,
1441 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001442
1443 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001444 * Check the cookie length and content
1445 */
1446#if defined(POLARSSL_SSL_PROTO_DTLS)
1447 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1448 {
1449 cookie_offset = 39 + sess_len;
1450 cookie_len = buf[cookie_offset];
1451
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001452 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001453 {
1454 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1455 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1456 }
1457
1458 SSL_DEBUG_BUF( 3, "client hello, cookie",
1459 buf + cookie_offset + 1, cookie_len );
1460
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001461 /*
1462 * Generate reference cookie content:
1463 * - used for verification below,
1464 * - stored to be sent if verification fails
1465 */
1466 if( ( ret = ssl_generate_verify_cookie( ssl ) ) != 0 )
1467 {
1468 SSL_DEBUG_RET( 1, "ssl_generate_verify_cookie", ret );
1469 return( ret );
1470 }
1471
1472 /* If the received cookie is OK, no need to send one */
1473 if( cookie_len == ssl->handshake->verify_cookie_len &&
1474 safer_memcmp( buf + cookie_offset + 1,
1475 ssl->handshake->verify_cookie,
1476 ssl->handshake->verify_cookie_len ) == 0 )
1477 {
1478 polarssl_free( ssl->handshake->verify_cookie );
1479 ssl->handshake->verify_cookie = NULL;
1480 ssl->handshake->verify_cookie_len = 0;
1481 }
1482
1483 SSL_DEBUG_MSG( 2, ( "client hello, cookie verification %s",
1484 ssl->handshake->verify_cookie == NULL ?
1485 "passed" : "failed" ) );
1486
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001487 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001488#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001489
1490 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001491 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001492 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001493#if defined(POLARSSL_SSL_PROTO_DTLS)
1494 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1495 ciph_offset = cookie_offset + 1 + cookie_len;
1496 else
1497#endif
1498 ciph_offset = 39 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001499
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001500 ciph_len = ( buf[ciph_offset + 0] << 8 )
1501 | ( buf[ciph_offset + 1] );
1502
1503 if( ciph_len < 2 ||
1504 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1505 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001506 {
1507 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1508 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1509 }
1510
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001511 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1512 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001513
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001514 /*
1515 * Check the compression algorithms length and pick one
1516 */
1517 comp_offset = ciph_offset + 2 + ciph_len;
1518
1519 comp_len = buf[comp_offset];
1520
1521 if( comp_len < 1 ||
1522 comp_len > 16 ||
1523 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001524 {
1525 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1526 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1527 }
1528
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001529 SSL_DEBUG_BUF( 3, "client hello, compression",
1530 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001531
1532 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001533#if defined(POLARSSL_ZLIB_SUPPORT)
1534 for( i = 0; i < comp_len; ++i )
1535 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001536 if( buf[comp_offset + 1 + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001537 {
Paul Bakker48916f92012-09-16 19:57:18 +00001538 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001539 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001540 }
1541 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001542#endif
1543
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001544 /* See comments in ssl_write_client_hello() */
1545#if defined(POLARSSL_SSL_PROTO_DTLS)
1546 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1547 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
1548#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001549 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001550 * Check the extension length
Paul Bakker48916f92012-09-16 19:57:18 +00001551 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001552 ext_offset = comp_offset + 1 + comp_len;
1553 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001554 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001555 if( msg_len < ext_offset + 2 )
Paul Bakker48916f92012-09-16 19:57:18 +00001556 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001557 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1558 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1559 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001560
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001561 ext_len = ( buf[ext_offset + 0] << 8 )
1562 | ( buf[ext_offset + 1] );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001563
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001564 if( ( ext_len > 0 && ext_len < 4 ) ||
1565 msg_len != ext_offset + 2 + ext_len )
1566 {
1567 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1568 SSL_DEBUG_BUF( 3, "client hello extensions",
1569 buf + ext_offset + 2, ext_len );
1570 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +00001571 }
1572 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001573 else
1574 ext_len = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001575
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001576 ext = buf + ext_offset + 2;
Paul Bakker48916f92012-09-16 19:57:18 +00001577
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001578 while( ext_len != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001579 {
1580 unsigned int ext_id = ( ( ext[0] << 8 )
1581 | ( ext[1] ) );
1582 unsigned int ext_size = ( ( ext[2] << 8 )
1583 | ( ext[3] ) );
1584
1585 if( ext_size + 4 > ext_len )
1586 {
1587 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1588 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1589 }
1590 switch( ext_id )
1591 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001592#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001593 case TLS_EXT_SERVERNAME:
1594 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1595 if( ssl->f_sni == NULL )
1596 break;
1597
1598 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1599 if( ret != 0 )
1600 return( ret );
1601 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001602#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001603
Paul Bakker48916f92012-09-16 19:57:18 +00001604 case TLS_EXT_RENEGOTIATION_INFO:
1605 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1606 renegotiation_info_seen = 1;
1607
Paul Bakker23f36802012-09-28 14:15:14 +00001608 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1609 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001610 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001611 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001612
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001613#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001614 case TLS_EXT_SIG_ALG:
1615 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1616 if( ssl->renegotiation == SSL_RENEGOTIATION )
1617 break;
1618
1619 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1620 if( ret != 0 )
1621 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001622 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001623#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001624
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001625#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001626 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1627 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1628
1629 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1630 if( ret != 0 )
1631 return( ret );
1632 break;
1633
1634 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1635 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001636 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001637
1638 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1639 if( ret != 0 )
1640 return( ret );
1641 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001642#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001643
Paul Bakker05decb22013-08-15 13:33:48 +02001644#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001645 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1646 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1647
1648 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1649 if( ret != 0 )
1650 return( ret );
1651 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001652#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001653
Paul Bakker1f2bc622013-08-15 13:45:55 +02001654#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001655 case TLS_EXT_TRUNCATED_HMAC:
1656 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1657
1658 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1659 if( ret != 0 )
1660 return( ret );
1661 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001662#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001663
Paul Bakkera503a632013-08-14 13:48:06 +02001664#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001665 case TLS_EXT_SESSION_TICKET:
1666 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1667
1668 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1669 if( ret != 0 )
1670 return( ret );
1671 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001672#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001673
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001674#if defined(POLARSSL_SSL_ALPN)
1675 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001676 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001677
1678 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1679 if( ret != 0 )
1680 return( ret );
1681 break;
1682#endif /* POLARSSL_SSL_SESSION_TICKETS */
1683
Paul Bakker48916f92012-09-16 19:57:18 +00001684 default:
1685 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1686 ext_id ) );
1687 }
1688
1689 ext_len -= 4 + ext_size;
1690 ext += 4 + ext_size;
1691
1692 if( ext_len > 0 && ext_len < 4 )
1693 {
1694 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1695 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1696 }
1697 }
1698
1699 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001700 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1701 */
1702 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
1703 {
1704 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1705 {
1706 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1707 if( ssl->renegotiation == SSL_RENEGOTIATION )
1708 {
1709 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1710
1711 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1712 return( ret );
1713
1714 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1715 }
1716 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1717 break;
1718 }
1719 }
1720
1721 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001722 * Renegotiation security checks
1723 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001724 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1725 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1726 {
1727 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1728 handshake_failure = 1;
1729 }
1730 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1731 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1732 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001733 {
1734 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001735 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001736 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001737 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1738 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1739 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001740 {
1741 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001742 handshake_failure = 1;
1743 }
1744 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1745 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1746 renegotiation_info_seen == 1 )
1747 {
1748 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1749 handshake_failure = 1;
1750 }
1751
1752 if( handshake_failure == 1 )
1753 {
1754 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1755 return( ret );
1756
Paul Bakker48916f92012-09-16 19:57:18 +00001757 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1758 }
Paul Bakker380da532012-04-18 16:10:25 +00001759
Paul Bakker41c83d32013-03-20 14:39:14 +01001760 /*
1761 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001762 * (At the end because we need information from the EC-based extensions
1763 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001764 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001765 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001766 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001767#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001768 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001769 {
1770 for( i = 0; ciphersuites[i] != 0; i++ )
1771#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001772 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001773 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001774 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001775#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001776 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001777 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1778 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1779 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001780
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001781 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1782 &ciphersuite_info ) ) != 0 )
1783 return( ret );
1784
1785 if( ciphersuite_info != NULL )
1786 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001787 }
1788 }
1789
1790 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1791
1792 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1793 return( ret );
1794
1795 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1796
1797have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001798 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001799 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1800 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1801
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001802 /* ClientHello can't be bundled with another record in same datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00001803 ssl->in_left = 0;
1804 ssl->state++;
1805
1806 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1807
1808 return( 0 );
1809}
1810
Paul Bakker1f2bc622013-08-15 13:45:55 +02001811#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001812static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1813 unsigned char *buf,
1814 size_t *olen )
1815{
1816 unsigned char *p = buf;
1817
1818 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1819 {
1820 *olen = 0;
1821 return;
1822 }
1823
1824 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1825
1826 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1827 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1828
1829 *p++ = 0x00;
1830 *p++ = 0x00;
1831
1832 *olen = 4;
1833}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001834#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001835
Paul Bakkera503a632013-08-14 13:48:06 +02001836#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001837static void ssl_write_session_ticket_ext( ssl_context *ssl,
1838 unsigned char *buf,
1839 size_t *olen )
1840{
1841 unsigned char *p = buf;
1842
1843 if( ssl->handshake->new_session_ticket == 0 )
1844 {
1845 *olen = 0;
1846 return;
1847 }
1848
1849 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1850
1851 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1852 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1853
1854 *p++ = 0x00;
1855 *p++ = 0x00;
1856
1857 *olen = 4;
1858}
Paul Bakkera503a632013-08-14 13:48:06 +02001859#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001860
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001861static void ssl_write_renegotiation_ext( ssl_context *ssl,
1862 unsigned char *buf,
1863 size_t *olen )
1864{
1865 unsigned char *p = buf;
1866
1867 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1868 {
1869 *olen = 0;
1870 return;
1871 }
1872
1873 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1874
1875 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1876 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1877
1878 *p++ = 0x00;
1879 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1880 *p++ = ssl->verify_data_len * 2 & 0xFF;
1881
1882 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1883 p += ssl->verify_data_len;
1884 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1885 p += ssl->verify_data_len;
1886
1887 *olen = 5 + ssl->verify_data_len * 2;
1888}
1889
Paul Bakker05decb22013-08-15 13:33:48 +02001890#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001891static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1892 unsigned char *buf,
1893 size_t *olen )
1894{
1895 unsigned char *p = buf;
1896
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001897 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1898 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001899 *olen = 0;
1900 return;
1901 }
1902
1903 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1904
1905 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1906 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1907
1908 *p++ = 0x00;
1909 *p++ = 1;
1910
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001911 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001912
1913 *olen = 5;
1914}
Paul Bakker05decb22013-08-15 13:33:48 +02001915#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001916
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001917#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001918static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1919 unsigned char *buf,
1920 size_t *olen )
1921{
1922 unsigned char *p = buf;
1923 ((void) ssl);
1924
Paul Bakker677377f2013-10-28 12:54:26 +01001925 if( ( ssl->handshake->cli_exts &
1926 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
1927 {
1928 *olen = 0;
1929 return;
1930 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001931
1932 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1933
1934 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1935 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1936
1937 *p++ = 0x00;
1938 *p++ = 2;
1939
1940 *p++ = 1;
1941 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1942
1943 *olen = 6;
1944}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001945#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001946
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001947#if defined(POLARSSL_SSL_ALPN )
1948static void ssl_write_alpn_ext( ssl_context *ssl,
1949 unsigned char *buf, size_t *olen )
1950{
1951 if( ssl->alpn_chosen == NULL )
1952 {
1953 *olen = 0;
1954 return;
1955 }
1956
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001957 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001958
1959 /*
1960 * 0 . 1 ext identifier
1961 * 2 . 3 ext length
1962 * 4 . 5 protocol list length
1963 * 6 . 6 protocol name length
1964 * 7 . 7+n protocol name
1965 */
1966 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
1967 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
1968
1969 *olen = 7 + strlen( ssl->alpn_chosen );
1970
1971 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
1972 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
1973
1974 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
1975 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
1976
1977 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
1978
1979 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
1980}
1981#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
1982
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001983#if defined(POLARSSL_SSL_PROTO_DTLS)
1984static int ssl_write_hello_verify_request( ssl_context *ssl )
1985{
1986 int ret;
1987 unsigned char *p = ssl->out_msg + 4;
1988
1989 SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
1990
1991 /*
1992 * struct {
1993 * ProtocolVersion server_version;
1994 * opaque cookie<0..2^8-1>;
1995 * } HelloVerifyRequest;
1996 */
1997
1998 /* For now, use fixed version = DTLS 1.0 */
1999 ssl_write_version( SSL_MAJOR_VERSION_3, SSL_MINOR_VERSION_1,
2000 ssl->transport, p );
2001 SSL_DEBUG_BUF( 3, "server version", (unsigned char *) p, 2 );
2002 p += 2;
2003
2004 SSL_DEBUG_BUF( 3, "cookie", ssl->handshake->verify_cookie,
2005 ssl->handshake->verify_cookie_len );
2006 *p++ = ssl->handshake->verify_cookie_len;
2007 memcpy( p, ssl->handshake->verify_cookie,
2008 ssl->handshake->verify_cookie_len );
2009 p += ssl->handshake->verify_cookie_len;
2010
2011 ssl->out_msglen = p - ssl->out_msg;
2012 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2013 ssl->out_msg[0] = SSL_HS_HELLO_VERIFY_REQUEST;
2014
2015 ssl->state = SSL_CLIENT_HELLO;
2016
2017 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2018 {
2019 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2020 return( ret );
2021 }
2022
2023 SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
2024
2025 return( 0 );
2026}
2027#endif /* POLARSSL_SSL_PROTO_DTLS */
2028
Paul Bakker5121ce52009-01-03 21:22:43 +00002029static int ssl_write_server_hello( ssl_context *ssl )
2030{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002031#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002032 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002033#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002034 int ret;
2035 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002036 unsigned char *buf, *p;
2037
2038 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
2039
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002040#if defined(POLARSSL_SSL_PROTO_DTLS)
2041 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2042 ssl->handshake->verify_cookie != NULL )
2043 {
2044 SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2045 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2046
2047 if( ( ret = ssl_write_hello_verify_request( ssl ) ) != 0 )
2048 {
2049 SSL_DEBUG_RET( 1, "ssl_write_hello_verify_request", ret );
2050 return( ret );
2051 }
2052
2053 return( POLARSSL_ERR_SSL_HELLO_VERIFY_REQUIRED );
2054 }
2055#endif /* POLARSSL_SSL_PROTO_DTLS */
2056
Paul Bakkera9a028e2013-11-21 17:31:06 +01002057 if( ssl->f_rng == NULL )
2058 {
2059 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2060 return( POLARSSL_ERR_SSL_NO_RNG );
2061 }
2062
Paul Bakker5121ce52009-01-03 21:22:43 +00002063 /*
2064 * 0 . 0 handshake type
2065 * 1 . 3 handshake length
2066 * 4 . 5 protocol version
2067 * 6 . 9 UNIX time()
2068 * 10 . 37 random bytes
2069 */
2070 buf = ssl->out_msg;
2071 p = buf + 4;
2072
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002073 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2074 ssl->transport, p );
2075 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002076
2077 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002078 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002079
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002080#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002081 t = time( NULL );
2082 *p++ = (unsigned char)( t >> 24 );
2083 *p++ = (unsigned char)( t >> 16 );
2084 *p++ = (unsigned char)( t >> 8 );
2085 *p++ = (unsigned char)( t );
2086
2087 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002088#else
2089 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
2090 return( ret );
2091
2092 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02002093#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002094
Paul Bakkera3d195c2011-11-27 21:07:34 +00002095 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
2096 return( ret );
2097
2098 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002099
Paul Bakker48916f92012-09-16 19:57:18 +00002100 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002101
2102 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
2103
2104 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002105 * Resume is 0 by default, see ssl_handshake_init().
2106 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2107 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002109 if( ssl->handshake->resume == 0 &&
2110 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002111 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002112 ssl->f_get_cache != NULL &&
2113 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
2114 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01002115 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002116 ssl->handshake->resume = 1;
2117 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002118
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002119 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002120 {
2121 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002122 * New session, create a new session id,
2123 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002124 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 ssl->state++;
2126
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002127#if defined(POLARSSL_HAVE_TIME)
2128 ssl->session_negotiate->start = time( NULL );
2129#endif
2130
Paul Bakkera503a632013-08-14 13:48:06 +02002131#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002132 if( ssl->handshake->new_session_ticket != 0 )
2133 {
2134 ssl->session_negotiate->length = n = 0;
2135 memset( ssl->session_negotiate->id, 0, 32 );
2136 }
2137 else
2138#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002139 {
2140 ssl->session_negotiate->length = n = 32;
2141 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002142 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002143 return( ret );
2144 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002145 }
2146 else
2147 {
2148 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002149 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002150 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02002151 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002152 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002153
2154 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2155 {
2156 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2157 return( ret );
2158 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002159 }
2160
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002161 /*
2162 * 38 . 38 session id length
2163 * 39 . 38+n session id
2164 * 39+n . 40+n chosen ciphersuite
2165 * 41+n . 41+n chosen compression alg.
2166 * 42+n . 43+n extensions length
2167 * 44+n . 43+n+m extensions
2168 */
2169 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00002170 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
2171 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002172
2173 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2174 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2175 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002176 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002177
Paul Bakker48916f92012-09-16 19:57:18 +00002178 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2179 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2180 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002181
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02002182 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2183 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02002184 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002185 ssl->session_negotiate->compression ) );
2186
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002187 /*
2188 * First write extensions, then the total length
2189 */
2190 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2191 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002192
Paul Bakker05decb22013-08-15 13:33:48 +02002193#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002194 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2195 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002196#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002197
Paul Bakker1f2bc622013-08-15 13:45:55 +02002198#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002199 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2200 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002201#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002202
Paul Bakkera503a632013-08-14 13:48:06 +02002203#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002204 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2205 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002206#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002207
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002208#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002209 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2210 ext_len += olen;
2211#endif
2212
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002213#if defined(POLARSSL_SSL_ALPN)
2214 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2215 ext_len += olen;
2216#endif
2217
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002218 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002219
Paul Bakkera7036632014-04-30 10:15:38 +02002220 if( ext_len > 0 )
2221 {
2222 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2223 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2224 p += ext_len;
2225 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002226
2227 ssl->out_msglen = p - buf;
2228 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2229 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
2230
2231 ret = ssl_write_record( ssl );
2232
2233 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2234
2235 return( ret );
2236}
2237
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002238#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2239 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002240 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2241 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002242static int ssl_write_certificate_request( ssl_context *ssl )
2243{
Paul Bakkered27a042013-04-18 22:46:23 +02002244 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002245
2246 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2247
2248 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002249 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002250 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2251 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002252 {
2253 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2254 ssl->state++;
2255 return( 0 );
2256 }
2257
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002258 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2259 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002260}
2261#else
2262static int ssl_write_certificate_request( ssl_context *ssl )
2263{
2264 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2265 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002266 size_t dn_size, total_dn_size; /* excluding length bytes */
2267 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002268 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002269 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00002270
2271 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2272
2273 ssl->state++;
2274
Paul Bakkerfbb17802013-04-17 19:10:21 +02002275 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002276 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002277 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002278 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002279 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002280 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002281 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002282 return( 0 );
2283 }
2284
2285 /*
2286 * 0 . 0 handshake type
2287 * 1 . 3 handshake length
2288 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002289 * 5 .. m-1 cert types
2290 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002291 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002292 * n .. n+1 length of all DNs
2293 * n+2 .. n+3 length of DN 1
2294 * n+4 .. ... Distinguished Name #1
2295 * ... .. ... length of DN 2, etc.
2296 */
2297 buf = ssl->out_msg;
2298 p = buf + 4;
2299
2300 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002301 * Supported certificate types
2302 *
2303 * ClientCertificateType certificate_types<1..2^8-1>;
2304 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002305 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002306 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002307
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002308#if defined(POLARSSL_RSA_C)
2309 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2310#endif
2311#if defined(POLARSSL_ECDSA_C)
2312 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2313#endif
2314
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002315 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002316 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002317
Paul Bakker577e0062013-08-28 11:57:20 +02002318 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002319#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002320 /*
2321 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002322 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002323 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2324 *
2325 * struct {
2326 * HashAlgorithm hash;
2327 * SignatureAlgorithm signature;
2328 * } SignatureAndHashAlgorithm;
2329 *
2330 * enum { (255) } HashAlgorithm;
2331 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002332 */
Paul Bakker21dca692013-01-03 11:41:08 +01002333 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002334 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002335 /*
2336 * Only use current running hash algorithm that is already required
2337 * for requested ciphersuite.
2338 */
Paul Bakker926af752012-11-23 13:38:07 +01002339 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2340
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002341 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2342 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002343 {
2344 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2345 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002346
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002347 /*
2348 * Supported signature algorithms
2349 */
2350#if defined(POLARSSL_RSA_C)
2351 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2352 p[2 + sa_len++] = SSL_SIG_RSA;
2353#endif
2354#if defined(POLARSSL_ECDSA_C)
2355 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2356 p[2 + sa_len++] = SSL_SIG_ECDSA;
2357#endif
Paul Bakker926af752012-11-23 13:38:07 +01002358
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002359 p[0] = (unsigned char)( sa_len >> 8 );
2360 p[1] = (unsigned char)( sa_len );
2361 sa_len += 2;
2362 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002363 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002364#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002365
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002366 /*
2367 * DistinguishedName certificate_authorities<0..2^16-1>;
2368 * opaque DistinguishedName<1..2^16-1>;
2369 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002370 p += 2;
2371 crt = ssl->ca_chain;
2372
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002373 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002374 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002375 {
2376 if( p - buf > 4096 )
2377 break;
2378
Paul Bakker926af752012-11-23 13:38:07 +01002379 dn_size = crt->subject_raw.len;
2380 *p++ = (unsigned char)( dn_size >> 8 );
2381 *p++ = (unsigned char)( dn_size );
2382 memcpy( p, crt->subject_raw.p, dn_size );
2383 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002384
Paul Bakker926af752012-11-23 13:38:07 +01002385 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2386
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002387 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002388 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002389 }
2390
Paul Bakker926af752012-11-23 13:38:07 +01002391 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002392 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2393 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002394 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2395 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002396
2397 ret = ssl_write_record( ssl );
2398
2399 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2400
2401 return( ret );
2402}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002403#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2404 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002405 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2406 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002407
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002408#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2409 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2410static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2411{
2412 int ret;
2413
2414 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2415 {
2416 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2417 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2418 }
2419
2420 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2421 pk_ec( *ssl_own_key( ssl ) ),
2422 POLARSSL_ECDH_OURS ) ) != 0 )
2423 {
2424 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2425 return( ret );
2426 }
2427
2428 return( 0 );
2429}
2430#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2431 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2432
Paul Bakker41c83d32013-03-20 14:39:14 +01002433static int ssl_write_server_key_exchange( ssl_context *ssl )
2434{
Paul Bakker23986e52011-04-24 08:57:21 +00002435 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002436 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002437 const ssl_ciphersuite_t *ciphersuite_info =
2438 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002439
2440#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2441 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2442 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002443 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002444 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002445 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002446 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002447 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002448 ((void) dig_signed);
2449 ((void) dig_signed_len);
2450#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002451
Paul Bakker5121ce52009-01-03 21:22:43 +00002452 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2453
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002454#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2455 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2456 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002457 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2458 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2459 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002460 {
2461 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2462 ssl->state++;
2463 return( 0 );
2464 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002465#endif
2466
2467#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2468 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2469 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2470 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2471 {
2472 ssl_get_ecdh_params_from_cert( ssl );
2473
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002474 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002475 ssl->state++;
2476 return( 0 );
2477 }
2478#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002479
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002480#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2481 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2482 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2483 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002484 {
2485 /* TODO: Support identity hints */
2486 *(p++) = 0x00;
2487 *(p++) = 0x00;
2488
2489 n += 2;
2490 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002491#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2492 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002493
2494#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2495 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2496 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2497 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002498 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002499 /*
2500 * Ephemeral DH parameters:
2501 *
2502 * struct {
2503 * opaque dh_p<1..2^16-1>;
2504 * opaque dh_g<1..2^16-1>;
2505 * opaque dh_Ys<1..2^16-1>;
2506 * } ServerDHParams;
2507 */
2508 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2509 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2510 {
2511 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2512 return( ret );
2513 }
Paul Bakker48916f92012-09-16 19:57:18 +00002514
Paul Bakker41c83d32013-03-20 14:39:14 +01002515 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002516 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2517 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002518 {
2519 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2520 return( ret );
2521 }
2522
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002523 dig_signed = p;
2524 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002525
2526 p += len;
2527 n += len;
2528
Paul Bakker41c83d32013-03-20 14:39:14 +01002529 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2530 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2531 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2532 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2533 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002534#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2535 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002536
Gergely Budai987bfb52014-01-19 21:48:42 +01002537#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002538 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002539 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2540 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002541 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002542 /*
2543 * Ephemeral ECDH parameters:
2544 *
2545 * struct {
2546 * ECParameters curve_params;
2547 * ECPoint public;
2548 * } ServerECDHParams;
2549 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002550 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002551#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002552 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002553
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002554 /* Match our preference list against the offered curves */
2555 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2556 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2557 if( (*curve)->grp_id == *gid )
2558 goto curve_matching_done;
2559
2560curve_matching_done:
2561#else
2562 curve = ssl->handshake->curves;
2563#endif
2564
2565 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002566 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002567 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2568 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002569 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002570
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002571 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002572
Paul Bakker41c83d32013-03-20 14:39:14 +01002573 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002574 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002575 {
2576 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2577 return( ret );
2578 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002579
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002580 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2581 p, SSL_MAX_CONTENT_LEN - n,
2582 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002583 {
2584 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2585 return( ret );
2586 }
2587
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002588 dig_signed = p;
2589 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002590
2591 p += len;
2592 n += len;
2593
Paul Bakker41c83d32013-03-20 14:39:14 +01002594 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2595 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002596#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002597
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002598#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002599 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2600 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002601 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002602 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2603 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002604 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002605 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002606 unsigned int hashlen = 0;
2607 unsigned char hash[64];
2608 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002609
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002610 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002611 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2612 */
Paul Bakker577e0062013-08-28 11:57:20 +02002613#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002614 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2615 {
2616 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2617
2618 if( md_alg == POLARSSL_MD_NONE )
2619 {
2620 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002621 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002622 }
2623 }
Paul Bakker577e0062013-08-28 11:57:20 +02002624 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002625#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002626#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2627 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002628 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002629 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2630 {
2631 md_alg = POLARSSL_MD_SHA1;
2632 }
2633 else
Paul Bakker577e0062013-08-28 11:57:20 +02002634#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002635 {
2636 md_alg = POLARSSL_MD_NONE;
2637 }
2638
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002639 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002640 * Compute the hash to be signed
2641 */
Paul Bakker577e0062013-08-28 11:57:20 +02002642#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2643 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002644 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002645 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002646 md5_context md5;
2647 sha1_context sha1;
2648
Paul Bakker5b4af392014-06-26 12:09:34 +02002649 md5_init( &md5 );
2650 sha1_init( &sha1 );
2651
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002652 /*
2653 * digitally-signed struct {
2654 * opaque md5_hash[16];
2655 * opaque sha_hash[20];
2656 * };
2657 *
2658 * md5_hash
2659 * MD5(ClientHello.random + ServerHello.random
2660 * + ServerParams);
2661 * sha_hash
2662 * SHA(ClientHello.random + ServerHello.random
2663 * + ServerParams);
2664 */
2665 md5_starts( &md5 );
2666 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002667 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002668 md5_finish( &md5, hash );
2669
2670 sha1_starts( &sha1 );
2671 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002672 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002673 sha1_finish( &sha1, hash + 16 );
2674
2675 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02002676
2677 md5_free( &md5 );
2678 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002679 }
2680 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002681#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2682 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002683#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2684 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002685 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002686 {
2687 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02002688 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002689
Paul Bakker84bbeb52014-07-01 14:53:22 +02002690 md_init( &ctx );
2691
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002692 /* Info from md_alg will be used instead */
2693 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002694
2695 /*
2696 * digitally-signed struct {
2697 * opaque client_random[32];
2698 * opaque server_random[32];
2699 * ServerDHParams params;
2700 * };
2701 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002702 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002703 {
2704 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2705 return( ret );
2706 }
2707
2708 md_starts( &ctx );
2709 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002710 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002711 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002712 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00002713 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002714 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002715#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2716 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002717 {
2718 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002719 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002720 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002721
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002722 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2723 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002724
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002725 /*
2726 * Make the signature
2727 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002728 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002729 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002730 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2731 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002732 }
Paul Bakker23f36802012-09-28 14:15:14 +00002733
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002734#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002735 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2736 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002737 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002738 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002739
2740 n += 2;
2741 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002742#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002743
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002744 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002745 p + 2 , &signature_len,
2746 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002747 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002748 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002749 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002750 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002751
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002752 *(p++) = (unsigned char)( signature_len >> 8 );
2753 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002754 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002755
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002756 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002757
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002758 p += signature_len;
2759 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002760 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002761#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002762 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2763 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002764
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002765 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002766 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2767 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2768
2769 ssl->state++;
2770
2771 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2772 {
2773 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2774 return( ret );
2775 }
2776
2777 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2778
2779 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002780}
2781
2782static int ssl_write_server_hello_done( ssl_context *ssl )
2783{
2784 int ret;
2785
2786 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2787
2788 ssl->out_msglen = 4;
2789 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2790 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2791
2792 ssl->state++;
2793
2794 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2795 {
2796 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2797 return( ret );
2798 }
2799
2800 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2801
2802 return( 0 );
2803}
2804
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002805#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2806 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2807static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2808 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002809{
2810 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002811 size_t n;
2812
2813 /*
2814 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2815 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002816 if( *p + 2 > end )
2817 {
2818 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2819 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2820 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002821
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002822 n = ( (*p)[0] << 8 ) | (*p)[1];
2823 *p += 2;
2824
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002825 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002826 {
2827 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2828 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2829 }
2830
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002831 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002832 {
2833 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2834 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2835 }
2836
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002837 *p += n;
2838
Paul Bakker70df2fb2013-04-17 17:19:09 +02002839 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2840
Paul Bakker70df2fb2013-04-17 17:19:09 +02002841 return( ret );
2842}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002843#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2844 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002845
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002846#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2847 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002848static int ssl_parse_encrypted_pms( ssl_context *ssl,
2849 const unsigned char *p,
2850 const unsigned char *end,
2851 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002852{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002853 int ret;
2854 size_t len = pk_get_len( ssl_own_key( ssl ) );
2855 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002856 unsigned char ver[2];
Paul Bakker70df2fb2013-04-17 17:19:09 +02002857
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002858 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002859 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002860 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002861 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2862 }
2863
2864 /*
2865 * Decrypt the premaster using own private RSA key
2866 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002867#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2868 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002869 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2870 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002871 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2872 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002873 {
2874 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2875 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2876 }
2877 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002878#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002879
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002880 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002881 {
2882 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2883 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2884 }
2885
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002886 ssl_write_version( ssl->handshake->max_major_ver,
2887 ssl->handshake->max_minor_ver,
2888 ssl->transport, ver );
2889
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002890 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2891 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002892 sizeof( ssl->handshake->premaster ) - pms_offset,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002893 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002894
2895 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002896 pms[0] != ver[0] ||
2897 pms[1] != ver[1] )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002898 {
2899 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2900
2901 /*
2902 * Protection against Bleichenbacher's attack:
2903 * invalid PKCS#1 v1.5 padding must not cause
2904 * the connection to end immediately; instead,
2905 * send a bad_record_mac later in the handshake.
2906 */
2907 ssl->handshake->pmslen = 48;
2908
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002909 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002910 if( ret != 0 )
2911 return( ret );
2912 }
2913
2914 return( ret );
2915}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002916#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2917 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002918
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002919#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002920static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2921 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002922{
Paul Bakker6db455e2013-09-18 17:29:31 +02002923 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002924 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002925
Paul Bakker6db455e2013-09-18 17:29:31 +02002926 if( ssl->f_psk == NULL &&
2927 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2928 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002929 {
2930 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2931 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2932 }
2933
2934 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002935 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002936 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002937 if( *p + 2 > end )
2938 {
2939 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2940 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2941 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002942
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002943 n = ( (*p)[0] << 8 ) | (*p)[1];
2944 *p += 2;
2945
2946 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002947 {
2948 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2949 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2950 }
2951
Paul Bakker6db455e2013-09-18 17:29:31 +02002952 if( ssl->f_psk != NULL )
2953 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002954 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002955 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2956 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002957 else
Paul Bakker6db455e2013-09-18 17:29:31 +02002958 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002959 /* Identity is not a big secret since clients send it in the clear,
2960 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02002961 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002962 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002963 {
2964 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2965 }
2966 }
2967
2968 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002969 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002970 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002971 if( ( ret = ssl_send_alert_message( ssl,
2972 SSL_ALERT_LEVEL_FATAL,
2973 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2974 {
2975 return( ret );
2976 }
2977
2978 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002979 }
2980
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002981 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002982
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002983 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002984}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002985#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002986
Paul Bakker5121ce52009-01-03 21:22:43 +00002987static int ssl_parse_client_key_exchange( ssl_context *ssl )
2988{
Paul Bakker23986e52011-04-24 08:57:21 +00002989 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002990 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002991
Paul Bakker41c83d32013-03-20 14:39:14 +01002992 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002993
2994 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2995
2996 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2997 {
2998 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2999 return( ret );
3000 }
3001
3002 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3003 {
3004 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003005 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003006 }
3007
3008 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
3009 {
3010 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003011 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003012 }
3013
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003014#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01003015 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003016 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003017 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003018 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003019
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003020 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003021 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02003022 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3023 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003024 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003025
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003026 if( p != end )
3027 {
3028 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3029 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3030 }
3031
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02003032 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003033
3034 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
3035 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003036 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02003037 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003038 {
3039 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
3040 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3041 }
3042
3043 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003044 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003045 else
3046#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003047#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003048 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3049 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3050 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003051 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003052 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
3053 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
3054 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003055 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003056 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003057 ssl->in_msg + 4, ssl->in_hslen - 4 ) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003058 {
3059 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3060 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3061 }
3062
3063 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3064
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003065 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
3066 &ssl->handshake->pmslen,
3067 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02003068 POLARSSL_MPI_MAX_SIZE,
3069 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003070 {
3071 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
3072 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3073 }
3074
3075 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003076 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003077 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003078#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003079 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3080 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3081 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003082#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
3083 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003084 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003085 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003086 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003087
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003088 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003089 {
3090 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3091 return( ret );
3092 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003093
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003094 if( p != end )
3095 {
3096 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3097 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3098 }
3099
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003100 if( ( ret = ssl_psk_derive_premaster( ssl,
3101 ciphersuite_info->key_exchange ) ) != 0 )
3102 {
3103 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3104 return( ret );
3105 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003106 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003107 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003108#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003109#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
3110 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
3111 {
3112 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003113 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003114
3115 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3116 {
3117 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3118 return( ret );
3119 }
3120
3121 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3122 {
3123 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
3124 return( ret );
3125 }
3126
3127 if( ( ret = ssl_psk_derive_premaster( ssl,
3128 ciphersuite_info->key_exchange ) ) != 0 )
3129 {
3130 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3131 return( ret );
3132 }
3133 }
3134 else
3135#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003136#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3137 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3138 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003139 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003140 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003141
3142 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3143 {
3144 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3145 return( ret );
3146 }
3147 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3148 {
3149 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3150 return( ret );
3151 }
3152
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003153 if( p != end )
3154 {
3155 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3156 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3157 }
3158
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003159 if( ( ret = ssl_psk_derive_premaster( ssl,
3160 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003161 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003162 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3163 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003164 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003165 }
3166 else
3167#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003168#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3169 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
3170 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003171 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003172 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003173
3174 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3175 {
3176 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3177 return( ret );
3178 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003179
3180 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
3181 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003182 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003183 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3184 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003185 }
3186
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003187 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3188
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003189 if( ( ret = ssl_psk_derive_premaster( ssl,
3190 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003191 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003192 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003193 return( ret );
3194 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003195 }
3196 else
3197#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003198#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
3199 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003200 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003201 if( ( ret = ssl_parse_encrypted_pms( ssl,
3202 ssl->in_msg + 4,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003203 ssl->in_msg + ssl->in_hslen,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003204 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003205 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003206 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003207 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003208 }
3209 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003210 else
3211#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
3212 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003213 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003214 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003215 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003216
Paul Bakkerff60ee62010-03-16 21:09:09 +00003217 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
3218 {
3219 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
3220 return( ret );
3221 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003222
Paul Bakker5121ce52009-01-03 21:22:43 +00003223 ssl->state++;
3224
3225 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3226
3227 return( 0 );
3228}
3229
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003230#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3231 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003232 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3233 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003234static int ssl_parse_certificate_verify( ssl_context *ssl )
3235{
Paul Bakkerfbb17802013-04-17 19:10:21 +02003236 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003237
3238 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3239
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003240 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003241 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003242 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003243 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02003244 {
3245 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3246 ssl->state++;
3247 return( 0 );
3248 }
3249
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003250 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3251 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003252}
3253#else
3254static int ssl_parse_certificate_verify( ssl_context *ssl )
3255{
3256 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003257 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003258 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003259 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003260 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003261#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003262 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003263#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003264 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003265 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3266
3267 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3268
3269 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003270 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003271 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003272 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3273 {
3274 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3275 ssl->state++;
3276 return( 0 );
3277 }
3278
Paul Bakkered27a042013-04-18 22:46:23 +02003279 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003280 {
3281 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3282 ssl->state++;
3283 return( 0 );
3284 }
3285
Paul Bakker48916f92012-09-16 19:57:18 +00003286 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003287
3288 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3289 {
3290 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3291 return( ret );
3292 }
3293
3294 ssl->state++;
3295
3296 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3297 {
3298 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003299 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003300 }
3301
3302 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
3303 {
3304 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003305 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003306 }
3307
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003308 /*
3309 * 0 . 0 handshake type
3310 * 1 . 3 handshake length
3311 * 4 . 5 sig alg (TLS 1.2 only)
3312 * 4+n . 5+n signature length (n = sa_len)
3313 * 6+n . 6+n+m signature (m = sig_len)
3314 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003315
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003316#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3317 defined(POLARSSL_SSL_PROTO_TLS1_1)
3318 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003319 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003320 sa_len = 0;
3321
Paul Bakkerc70b9822013-04-07 22:00:46 +02003322 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003323 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003324
3325 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3326 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3327 POLARSSL_PK_ECDSA ) )
3328 {
3329 hash_start += 16;
3330 hashlen -= 16;
3331 md_alg = POLARSSL_MD_SHA1;
3332 }
Paul Bakker926af752012-11-23 13:38:07 +01003333 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003334 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003335#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3336 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003337#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3338 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003339 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003340 sa_len = 2;
3341
Paul Bakker5121ce52009-01-03 21:22:43 +00003342 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003343 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003344 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003345 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003346 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003347 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3348 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003349 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3350 }
3351
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003352 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003353
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003354 /* Info from md_alg will be used instead */
3355 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003356
3357 /*
3358 * Signature
3359 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003360 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
3361 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003362 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003363 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3364 " for verify message" ) );
3365 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003366 }
3367
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003368 /*
3369 * Check the certificate's key type matches the signature alg
3370 */
3371 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3372 {
3373 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3374 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3375 }
Paul Bakker577e0062013-08-28 11:57:20 +02003376 }
3377 else
3378#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3379 {
3380 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003381 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003382 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003383
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003384 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01003385
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003386 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003387 {
3388 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003389 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003390 }
3391
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003392 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003393 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003394 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003395 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003396 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003397 return( ret );
3398 }
3399
3400 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3401
Paul Bakkered27a042013-04-18 22:46:23 +02003402 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003403}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003404#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3405 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3406 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003407
Paul Bakkera503a632013-08-14 13:48:06 +02003408#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003409static int ssl_write_new_session_ticket( ssl_context *ssl )
3410{
3411 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003412 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003413 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003414
3415 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3416
3417 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3418 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3419
3420 /*
3421 * struct {
3422 * uint32 ticket_lifetime_hint;
3423 * opaque ticket<0..2^16-1>;
3424 * } NewSessionTicket;
3425 *
3426 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3427 * 8 . 9 ticket_len (n)
3428 * 10 . 9+n ticket content
3429 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003430
3431 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3432 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3433 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3434 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003435
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003436 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3437 {
3438 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3439 tlen = 0;
3440 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003441
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003442 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3443 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003444
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003445 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003446
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003447 /*
3448 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3449 * ChangeCipherSpec share the same state.
3450 */
3451 ssl->handshake->new_session_ticket = 0;
3452
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003453 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3454 {
3455 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3456 return( ret );
3457 }
3458
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003459 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3460
3461 return( 0 );
3462}
Paul Bakkera503a632013-08-14 13:48:06 +02003463#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003464
Paul Bakker5121ce52009-01-03 21:22:43 +00003465/*
Paul Bakker1961b702013-01-25 14:49:24 +01003466 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003467 */
Paul Bakker1961b702013-01-25 14:49:24 +01003468int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003469{
3470 int ret = 0;
3471
Paul Bakker1961b702013-01-25 14:49:24 +01003472 if( ssl->state == SSL_HANDSHAKE_OVER )
3473 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003474
Paul Bakker1961b702013-01-25 14:49:24 +01003475 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3476
3477 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3478 return( ret );
3479
3480 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003481 {
Paul Bakker1961b702013-01-25 14:49:24 +01003482 case SSL_HELLO_REQUEST:
3483 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003484 break;
3485
Paul Bakker1961b702013-01-25 14:49:24 +01003486 /*
3487 * <== ClientHello
3488 */
3489 case SSL_CLIENT_HELLO:
3490 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003491 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003492
3493 /*
3494 * ==> ServerHello
3495 * Certificate
3496 * ( ServerKeyExchange )
3497 * ( CertificateRequest )
3498 * ServerHelloDone
3499 */
3500 case SSL_SERVER_HELLO:
3501 ret = ssl_write_server_hello( ssl );
3502 break;
3503
3504 case SSL_SERVER_CERTIFICATE:
3505 ret = ssl_write_certificate( ssl );
3506 break;
3507
3508 case SSL_SERVER_KEY_EXCHANGE:
3509 ret = ssl_write_server_key_exchange( ssl );
3510 break;
3511
3512 case SSL_CERTIFICATE_REQUEST:
3513 ret = ssl_write_certificate_request( ssl );
3514 break;
3515
3516 case SSL_SERVER_HELLO_DONE:
3517 ret = ssl_write_server_hello_done( ssl );
3518 break;
3519
3520 /*
3521 * <== ( Certificate/Alert )
3522 * ClientKeyExchange
3523 * ( CertificateVerify )
3524 * ChangeCipherSpec
3525 * Finished
3526 */
3527 case SSL_CLIENT_CERTIFICATE:
3528 ret = ssl_parse_certificate( ssl );
3529 break;
3530
3531 case SSL_CLIENT_KEY_EXCHANGE:
3532 ret = ssl_parse_client_key_exchange( ssl );
3533 break;
3534
3535 case SSL_CERTIFICATE_VERIFY:
3536 ret = ssl_parse_certificate_verify( ssl );
3537 break;
3538
3539 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3540 ret = ssl_parse_change_cipher_spec( ssl );
3541 break;
3542
3543 case SSL_CLIENT_FINISHED:
3544 ret = ssl_parse_finished( ssl );
3545 break;
3546
3547 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003548 * ==> ( NewSessionTicket )
3549 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003550 * Finished
3551 */
3552 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003553#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003554 if( ssl->handshake->new_session_ticket != 0 )
3555 ret = ssl_write_new_session_ticket( ssl );
3556 else
Paul Bakkera503a632013-08-14 13:48:06 +02003557#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003558 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003559 break;
3560
3561 case SSL_SERVER_FINISHED:
3562 ret = ssl_write_finished( ssl );
3563 break;
3564
3565 case SSL_FLUSH_BUFFERS:
3566 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3567 ssl->state = SSL_HANDSHAKE_WRAPUP;
3568 break;
3569
3570 case SSL_HANDSHAKE_WRAPUP:
3571 ssl_handshake_wrapup( ssl );
3572 break;
3573
3574 default:
3575 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3576 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003577 }
3578
Paul Bakker5121ce52009-01-03 21:22:43 +00003579 return( ret );
3580}
Paul Bakker9af723c2014-05-01 13:03:14 +02003581#endif /* POLARSSL_SSL_SRV_C */