blob: 809a930095b5306b7d0d89ffa2e5afb5e11389d6 [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é-Gonnard82202f02014-07-23 00:28:58 +0200354#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +0200355int 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}
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +0200372
373void ssl_set_dtls_cookies( ssl_context *ssl,
374 ssl_cookie_write_t *f_cookie_write,
375 ssl_cookie_check_t *f_cookie_check,
376 void *p_cookie )
377{
378 ssl->f_cookie_write = f_cookie_write;
379 ssl->f_cookie_check = f_cookie_check;
380 ssl->p_cookie = p_cookie;
381}
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +0200382#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +0200383
Paul Bakker0be444a2013-08-27 21:55:01 +0200384#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200385/*
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200386 * Wrapper around f_sni, allowing use of ssl_set_own_cert() but
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +0100387 * making it act on ssl->handshake->sni_key_cert instead.
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200388 */
389static int ssl_sni_wrapper( ssl_context *ssl,
390 const unsigned char* name, size_t len )
391{
392 int ret;
393 ssl_key_cert *key_cert_ori = ssl->key_cert;
394
395 ssl->key_cert = NULL;
396 ret = ssl->f_sni( ssl->p_sni, ssl, name, len );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200397 ssl->handshake->sni_key_cert = ssl->key_cert;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200398
399 ssl->key_cert = key_cert_ori;
400
401 return( ret );
402}
403
Paul Bakker5701cdc2012-09-27 21:49:42 +0000404static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000405 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000406 size_t len )
407{
408 int ret;
409 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000410 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000411
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100412 SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
413
Paul Bakker5701cdc2012-09-27 21:49:42 +0000414 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
415 if( servername_list_size + 2 != len )
416 {
417 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
418 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
419 }
420
421 p = buf + 2;
422 while( servername_list_size > 0 )
423 {
424 hostname_len = ( ( p[1] << 8 ) | p[2] );
425 if( hostname_len + 3 > servername_list_size )
426 {
427 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
428 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
429 }
430
431 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
432 {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200433 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000434 if( ret != 0 )
435 {
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100436 SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000437 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
438 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
439 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
440 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000441 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000442 }
443
444 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000445 p += hostname_len + 3;
446 }
447
448 if( servername_list_size != 0 )
449 {
450 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
451 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000452 }
453
454 return( 0 );
455}
Paul Bakker0be444a2013-08-27 21:55:01 +0200456#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000457
Paul Bakker48916f92012-09-16 19:57:18 +0000458static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000459 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000460 size_t len )
461{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000462 int ret;
463
Paul Bakker48916f92012-09-16 19:57:18 +0000464 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
465 {
466 if( len != 1 || buf[0] != 0x0 )
467 {
468 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000469
470 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
471 return( ret );
472
Paul Bakker48916f92012-09-16 19:57:18 +0000473 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
474 }
475
476 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
477 }
478 else
479 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100480 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000481 if( len != 1 + ssl->verify_data_len ||
482 buf[0] != ssl->verify_data_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100483 safer_memcmp( buf + 1, ssl->peer_verify_data,
484 ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000485 {
486 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000487
488 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
489 return( ret );
490
Paul Bakker48916f92012-09-16 19:57:18 +0000491 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
492 }
493 }
494
495 return( 0 );
496}
497
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200498#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +0000499static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
500 const unsigned char *buf,
501 size_t len )
502{
503 size_t sig_alg_list_size;
504 const unsigned char *p;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200505 const unsigned char *end = buf + len;
506 const int *md_cur;
507
Paul Bakker23f36802012-09-28 14:15:14 +0000508
509 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
510 if( sig_alg_list_size + 2 != len ||
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200511 sig_alg_list_size % 2 != 0 )
Paul Bakker23f36802012-09-28 14:15:14 +0000512 {
513 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
514 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
515 }
516
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200517 /*
518 * For now, ignore the SignatureAlgorithm part and rely on offered
519 * ciphersuites only for that part. To be fixed later.
520 *
521 * So, just look at the HashAlgorithm part.
522 */
523 for( md_cur = md_list(); *md_cur != POLARSSL_MD_NONE; md_cur++ ) {
524 for( p = buf + 2; p < end; p += 2 ) {
525 if( *md_cur == (int) ssl_md_alg_from_hash( p[0] ) ) {
526 ssl->handshake->sig_alg = p[0];
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200527 goto have_sig_alg;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200528 }
Paul Bakker23f36802012-09-28 14:15:14 +0000529 }
Paul Bakker23f36802012-09-28 14:15:14 +0000530 }
531
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200532 /* Some key echanges do not need signatures at all */
533 SSL_DEBUG_MSG( 3, ( "no signature_algorithm in common" ) );
534 return( 0 );
535
536have_sig_alg:
Paul Bakker23f36802012-09-28 14:15:14 +0000537 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
538 ssl->handshake->sig_alg ) );
539
540 return( 0 );
541}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200542#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker23f36802012-09-28 14:15:14 +0000543
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200544#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200545static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
546 const unsigned char *buf,
547 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100548{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200549 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100550 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200551 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100552
553 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
554 if( list_size + 2 != len ||
555 list_size % 2 != 0 )
556 {
557 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
558 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
559 }
560
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200561 /* Should never happen unless client duplicates the extension */
562 if( ssl->handshake->curves != NULL )
563 {
564 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
565 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
566 }
567
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +0100568 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200569 * and leave room for a final 0 */
570 our_size = list_size / 2 + 1;
571 if( our_size > POLARSSL_ECP_DP_MAX )
572 our_size = POLARSSL_ECP_DP_MAX;
573
574 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
575 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
576
Paul Bakker9af723c2014-05-01 13:03:14 +0200577 /* explicit void pointer cast for buggy MS compiler */
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200578 memset( (void *) curves, 0, our_size * sizeof( *curves ) );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200579 ssl->handshake->curves = curves;
580
Paul Bakker41c83d32013-03-20 14:39:14 +0100581 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200582 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100583 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200584 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200585
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200586 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100587 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200588 *curves++ = curve_info;
589 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100590 }
591
592 list_size -= 2;
593 p += 2;
594 }
595
596 return( 0 );
597}
598
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200599static int ssl_parse_supported_point_formats( ssl_context *ssl,
600 const unsigned char *buf,
601 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100602{
603 size_t list_size;
604 const unsigned char *p;
605
606 list_size = buf[0];
607 if( list_size + 1 != len )
608 {
609 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
610 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
611 }
612
613 p = buf + 2;
614 while( list_size > 0 )
615 {
616 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
617 p[0] == POLARSSL_ECP_PF_COMPRESSED )
618 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200619 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200620 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100621 return( 0 );
622 }
623
624 list_size--;
625 p++;
626 }
627
628 return( 0 );
629}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200630#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100631
Paul Bakker05decb22013-08-15 13:33:48 +0200632#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200633static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
634 const unsigned char *buf,
635 size_t len )
636{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200637 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200638 {
639 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
640 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
641 }
642
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200643 ssl->session_negotiate->mfl_code = buf[0];
644
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200645 return( 0 );
646}
Paul Bakker05decb22013-08-15 13:33:48 +0200647#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200648
Paul Bakker1f2bc622013-08-15 13:45:55 +0200649#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200650static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
651 const unsigned char *buf,
652 size_t len )
653{
654 if( len != 0 )
655 {
656 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
657 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
658 }
659
660 ((void) buf);
661
662 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
663
664 return( 0 );
665}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200666#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200667
Paul Bakkera503a632013-08-14 13:48:06 +0200668#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200669static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200670 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200671 size_t len )
672{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200673 int ret;
674
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200675 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
676 return( 0 );
677
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200678 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200679 ssl->handshake->new_session_ticket = 1;
680
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200681 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
682
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200683 if( len == 0 )
684 return( 0 );
685
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200686 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
687 {
688 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
689 return( 0 );
690 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200691
692 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200693 * Failures are ok: just ignore the ticket and proceed.
694 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200695 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
696 {
697 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200698 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200699 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200700
701 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
702
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200703 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200704
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200705 /* Don't send a new ticket after all, this one is OK */
706 ssl->handshake->new_session_ticket = 0;
707
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200708 return( 0 );
709}
Paul Bakkera503a632013-08-14 13:48:06 +0200710#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200711
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200712#if defined(POLARSSL_SSL_ALPN)
713static int ssl_parse_alpn_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200714 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200715{
Paul Bakker14b16c62014-05-28 11:33:54 +0200716 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200717 const unsigned char *theirs, *start, *end;
718 const char **ours;
719
720 /* If ALPN not configured, just ignore the extension */
721 if( ssl->alpn_list == NULL )
722 return( 0 );
723
724 /*
725 * opaque ProtocolName<1..2^8-1>;
726 *
727 * struct {
728 * ProtocolName protocol_name_list<2..2^16-1>
729 * } ProtocolNameList;
730 */
731
732 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
733 if( len < 4 )
734 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
735
736 list_len = ( buf[0] << 8 ) | buf[1];
737 if( list_len != len - 2 )
738 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
739
740 /*
741 * Use our order of preference
742 */
743 start = buf + 2;
744 end = buf + len;
745 for( ours = ssl->alpn_list; *ours != NULL; ours++ )
746 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200747 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200748 for( theirs = start; theirs != end; theirs += cur_len )
749 {
750 /* If the list is well formed, we should get equality first */
751 if( theirs > end )
752 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
753
754 cur_len = *theirs++;
755
756 /* Empty strings MUST NOT be included */
757 if( cur_len == 0 )
758 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
759
Paul Bakker14b16c62014-05-28 11:33:54 +0200760 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200761 memcmp( theirs, *ours, cur_len ) == 0 )
762 {
763 ssl->alpn_chosen = *ours;
764 return( 0 );
765 }
766 }
767 }
768
769 /* If we get there, no match was found */
770 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
771 SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
772 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
773}
774#endif /* POLARSSL_SSL_ALPN */
775
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100776/*
777 * Auxiliary functions for ServerHello parsing and related actions
778 */
779
780#if defined(POLARSSL_X509_CRT_PARSE_C)
781/*
782 * Return 1 if the given EC key uses the given curve, 0 otherwise
783 */
784#if defined(POLARSSL_ECDSA_C)
785static int ssl_key_matches_curves( pk_context *pk,
786 const ecp_curve_info **curves )
787{
788 const ecp_curve_info **crv = curves;
789 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
790
791 while( *crv != NULL )
792 {
793 if( (*crv)->grp_id == grp_id )
794 return( 1 );
795 crv++;
796 }
797
798 return( 0 );
799}
800#endif /* POLARSSL_ECDSA_C */
801
802/*
803 * Try picking a certificate for this ciphersuite,
804 * return 0 on success and -1 on failure.
805 */
806static int ssl_pick_cert( ssl_context *ssl,
807 const ssl_ciphersuite_t * ciphersuite_info )
808{
809 ssl_key_cert *cur, *list;
810 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
811
812#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
813 if( ssl->handshake->sni_key_cert != NULL )
814 list = ssl->handshake->sni_key_cert;
815 else
816#endif
817 list = ssl->handshake->key_cert;
818
819 if( pk_alg == POLARSSL_PK_NONE )
820 return( 0 );
821
822 for( cur = list; cur != NULL; cur = cur->next )
823 {
824 if( ! pk_can_do( cur->key, pk_alg ) )
825 continue;
826
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200827 /*
828 * This avoids sending the client a cert it'll reject based on
829 * keyUsage or other extensions.
830 *
831 * It also allows the user to provision different certificates for
832 * different uses based on keyUsage, eg if they want to avoid signing
833 * and decrypting with the same RSA key.
834 */
835 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
836 SSL_IS_SERVER ) != 0 )
837 {
838 continue;
839 }
840
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100841#if defined(POLARSSL_ECDSA_C)
842 if( pk_alg == POLARSSL_PK_ECDSA )
843 {
844 if( ssl_key_matches_curves( cur->key, ssl->handshake->curves ) )
845 break;
846 }
847 else
848#endif
849 break;
850 }
851
852 if( cur == NULL )
853 return( -1 );
854
855 ssl->handshake->key_cert = cur;
856 return( 0 );
857}
858#endif /* POLARSSL_X509_CRT_PARSE_C */
859
860/*
861 * Check if a given ciphersuite is suitable for use with our config/keys/etc
862 * Sets ciphersuite_info only if the suite matches.
863 */
864static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
865 const ssl_ciphersuite_t **ciphersuite_info )
866{
867 const ssl_ciphersuite_t *suite_info;
868
869 suite_info = ssl_ciphersuite_from_id( suite_id );
870 if( suite_info == NULL )
871 {
872 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", suite_id ) );
873 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
874 }
875
876 if( suite_info->min_minor_ver > ssl->minor_ver ||
877 suite_info->max_minor_ver < ssl->minor_ver )
878 return( 0 );
879
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +0100880#if defined(POLARSSL_SSL_PROTO_DTLS)
881 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
882 ( suite_info->flags & POLARSSL_CIPHERSUITE_NODTLS ) )
883 return( 0 );
884#endif
885
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100886#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
887 if( ssl_ciphersuite_uses_ec( suite_info ) &&
888 ( ssl->handshake->curves == NULL ||
889 ssl->handshake->curves[0] == NULL ) )
890 return( 0 );
891#endif
892
893#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
894 /* If the ciphersuite requires a pre-shared key and we don't
895 * have one, skip it now rather than failing later */
896 if( ssl_ciphersuite_uses_psk( suite_info ) &&
897 ssl->f_psk == NULL &&
898 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
899 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
900 return( 0 );
901#endif
902
903#if defined(POLARSSL_X509_CRT_PARSE_C)
904 /*
905 * Final check: if ciphersuite requires us to have a
906 * certificate/key of a particular type:
907 * - select the appropriate certificate if we have one, or
908 * - try the next ciphersuite if we don't
909 * This must be done last since we modify the key_cert list.
910 */
911 if( ssl_pick_cert( ssl, suite_info ) != 0 )
912 return( 0 );
913#endif
914
915 *ciphersuite_info = suite_info;
916 return( 0 );
917}
918
Paul Bakker78a8c712013-03-06 17:01:52 +0100919#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
920static int ssl_parse_client_hello_v2( ssl_context *ssl )
921{
922 int ret;
923 unsigned int i, j;
924 size_t n;
925 unsigned int ciph_len, sess_len, chal_len;
926 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200927 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200928 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100929
930 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
931
932 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
933 {
934 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
935
936 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
937 return( ret );
938
939 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
940 }
941
942 buf = ssl->in_hdr;
943
944 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
945
946 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
947 buf[2] ) );
948 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
949 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
950 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
951 buf[3], buf[4] ) );
952
953 /*
954 * SSLv2 Client Hello
955 *
956 * Record layer:
957 * 0 . 1 message length
958 *
959 * SSL layer:
960 * 2 . 2 message type
961 * 3 . 4 protocol version
962 */
963 if( buf[2] != SSL_HS_CLIENT_HELLO ||
964 buf[3] != SSL_MAJOR_VERSION_3 )
965 {
966 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
967 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
968 }
969
970 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
971
972 if( n < 17 || n > 512 )
973 {
974 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
975 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
976 }
977
978 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200979 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
980 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100981
982 if( ssl->minor_ver < ssl->min_minor_ver )
983 {
984 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200985 " [%d:%d] < [%d:%d]",
986 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +0100987 ssl->min_major_ver, ssl->min_minor_ver ) );
988
989 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
990 SSL_ALERT_MSG_PROTOCOL_VERSION );
991 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
992 }
993
Paul Bakker2fbefde2013-06-29 16:01:15 +0200994 ssl->handshake->max_major_ver = buf[3];
995 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100996
997 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
998 {
999 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1000 return( ret );
1001 }
1002
1003 ssl->handshake->update_checksum( ssl, buf + 2, n );
1004
1005 buf = ssl->in_msg;
1006 n = ssl->in_left - 5;
1007
1008 /*
1009 * 0 . 1 ciphersuitelist length
1010 * 2 . 3 session id length
1011 * 4 . 5 challenge length
1012 * 6 . .. ciphersuitelist
1013 * .. . .. session id
1014 * .. . .. challenge
1015 */
1016 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1017
1018 ciph_len = ( buf[0] << 8 ) | buf[1];
1019 sess_len = ( buf[2] << 8 ) | buf[3];
1020 chal_len = ( buf[4] << 8 ) | buf[5];
1021
1022 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
1023 ciph_len, sess_len, chal_len ) );
1024
1025 /*
1026 * Make sure each parameter length is valid
1027 */
1028 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1029 {
1030 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1031 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1032 }
1033
1034 if( sess_len > 32 )
1035 {
1036 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1037 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1038 }
1039
1040 if( chal_len < 8 || chal_len > 32 )
1041 {
1042 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1043 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1044 }
1045
1046 if( n != 6 + ciph_len + sess_len + chal_len )
1047 {
1048 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1049 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1050 }
1051
1052 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1053 buf + 6, ciph_len );
1054 SSL_DEBUG_BUF( 3, "client hello, session id",
1055 buf + 6 + ciph_len, sess_len );
1056 SSL_DEBUG_BUF( 3, "client hello, challenge",
1057 buf + 6 + ciph_len + sess_len, chal_len );
1058
1059 p = buf + 6 + ciph_len;
1060 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001061 memset( ssl->session_negotiate->id, 0,
1062 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001063 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1064
1065 p += sess_len;
1066 memset( ssl->handshake->randbytes, 0, 64 );
1067 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1068
1069 /*
1070 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1071 */
1072 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1073 {
1074 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1075 {
1076 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1077 if( ssl->renegotiation == SSL_RENEGOTIATION )
1078 {
1079 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1080
1081 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1082 return( ret );
1083
1084 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1085 }
1086 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1087 break;
1088 }
1089 }
1090
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001091#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1092 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1093 {
1094 if( p[0] == 0 &&
1095 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1096 p[2] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1097 {
1098 SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
1099
1100 if( ssl->minor_ver < ssl->max_minor_ver )
1101 {
1102 SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
1103
1104 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1105 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1106
1107 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1108 }
1109
1110 break;
1111 }
1112 }
1113#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1114
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001115 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001116 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001117#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1118 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1119 {
1120 for( i = 0; ciphersuites[i] != 0; i++ )
1121#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001122 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001123 {
1124 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001125#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001126 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001127 if( p[0] != 0 ||
1128 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1129 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1130 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001131
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001132 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1133 &ciphersuite_info ) ) != 0 )
1134 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001135
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001136 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001137 goto have_ciphersuite_v2;
1138 }
1139 }
1140
1141 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1142
1143 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1144
1145have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001146 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001147 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001148 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001149
1150 /*
1151 * SSLv2 Client Hello relevant renegotiation security checks
1152 */
1153 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1154 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1155 {
1156 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1157
1158 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1159 return( ret );
1160
1161 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1162 }
1163
1164 ssl->in_left = 0;
1165 ssl->state++;
1166
1167 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1168
1169 return( 0 );
1170}
1171#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1172
Paul Bakker5121ce52009-01-03 21:22:43 +00001173static int ssl_parse_client_hello( ssl_context *ssl )
1174{
Paul Bakker23986e52011-04-24 08:57:21 +00001175 int ret;
1176 unsigned int i, j;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001177 unsigned int ciph_offset, comp_offset, ext_offset;
1178 unsigned int msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001179#if defined(POLARSSL_SSL_PROTO_DTLS)
1180 unsigned int cookie_offset, cookie_len;
1181#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001182 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001183 int renegotiation_info_seen = 0;
1184 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001185 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001186 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001187 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001188
1189 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1190
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001191#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1192read_record_header:
1193#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001194 /*
1195 * If renegotiating, then the input was read with ssl_read_record(),
1196 * otherwise read it ourselves manually in order to support SSLv2
1197 * ClientHello, which doesn't use the same record layer format.
1198 */
Paul Bakker48916f92012-09-16 19:57:18 +00001199 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001200 ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001201 {
1202 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1203 return( ret );
1204 }
1205
1206 buf = ssl->in_hdr;
1207
Paul Bakker78a8c712013-03-06 17:01:52 +01001208#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02001209#if defined(POLARSSL_SSL_PROTO_DTLS)
1210 if( ssl->transport == SSL_TRANSPORT_STREAM )
1211#endif
1212 if( ( buf[0] & 0x80 ) != 0 )
1213 return ssl_parse_client_hello_v2( ssl );
Paul Bakker78a8c712013-03-06 17:01:52 +01001214#endif
1215
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001216 SSL_DEBUG_BUF( 4, "record header", buf, ssl_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001217
Paul Bakkerec636f32012-09-09 19:17:02 +00001218 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001219 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001220 *
1221 * Record layer:
1222 * 0 . 0 message type
1223 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001224 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001225 * 3 . 4 message length
1226 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001227 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1228 buf[0] ) );
1229
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001230 if( buf[0] != SSL_MSG_HANDSHAKE )
1231 {
1232 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1233 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1234 }
1235
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001236 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1237 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1238
1239 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol version: [%d:%d]",
1240 buf[1], buf[2] ) );
1241
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001242 ssl_read_version( &major, &minor, ssl->transport, buf + 1 );
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001243
1244 /* According to RFC 5246 Appendix E.1, the version here is typically
1245 * "{03,00}, the lowest version number supported by the client, [or] the
1246 * value of ClientHello.client_version", so the only meaningful check here
1247 * is the major version shouldn't be less than 3 */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001248 if( major < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001249 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001250 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1251 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1252 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001253
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001254 /* For DTLS if this is the initial handshake, remember the client sequence
1255 * number to use it in our next message (RFC 6347 4.2.1) */
1256#if defined(POLARSSL_SSL_PROTO_DTLS)
1257 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
1258 ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1259 {
1260 /* Epoch should be 0 for initial handshakes */
1261 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1262 {
1263 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1264 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1265 }
1266
1267 memcpy( ssl->out_ctr + 2, ssl->in_ctr + 2, 6 );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001268
1269#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1270 if( ssl_dtls_replay_check( ssl ) != 0 )
1271 {
1272 SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
1273 ssl->next_record_offset = 0;
1274 ssl->in_left = 0;
1275 goto read_record_header;
1276 }
1277
1278 /* No MAC to check yet, so we can update right now */
1279 ssl_dtls_replay_update( ssl );
1280#endif
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001281 }
1282#endif /* POLARSSL_SSL_PROTO_DTLS */
1283
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001284 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001285
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001286 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Paul Bakkerec636f32012-09-09 19:17:02 +00001287 {
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001288 if( msg_len > SSL_MAX_CONTENT_LEN )
1289 {
1290 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1291 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1292 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001293
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001294 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) + msg_len ) ) != 0 )
1295 {
1296 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1297 return( ret );
1298 }
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001299
1300 /* Done reading this record, get ready for the next one */
1301#if defined(POLARSSL_SSL_PROTO_DTLS)
1302 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1303 ssl->next_record_offset = msg_len + ssl_hdr_len( ssl );
1304 else
1305#endif
1306 ssl->in_left = 0;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001307 }
1308 else
Paul Bakkerec636f32012-09-09 19:17:02 +00001309 {
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001310 /* Set by ssl_read_record() */
1311 msg_len = ssl->in_hslen;
Paul Bakkerec636f32012-09-09 19:17:02 +00001312 }
1313
1314 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001315
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001316 SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001317
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001318 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001319
1320 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001321 * Handshake layer:
1322 * 0 . 0 handshake type
1323 * 1 . 3 handshake length
1324 * 4 . 5 DTLS only: message seqence number
1325 * 6 . 8 DTLS only: fragment offset
1326 * 9 . 11 DTLS only: fragment length
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001327 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001328 if( msg_len < ssl_hs_hdr_len( ssl ) )
1329 {
1330 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1331 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1332 }
1333
1334 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
1335
1336 if( buf[0] != SSL_HS_CLIENT_HELLO )
1337 {
1338 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1339 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1340 }
1341
1342 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1343 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1344
1345 /* We don't support fragmentation of ClientHello (yet?) */
1346 if( buf[1] != 0 ||
1347 msg_len != ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) )
1348 {
1349 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1350 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1351 }
1352
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001353#if defined(POLARSSL_SSL_PROTO_DTLS)
1354 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1355 {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001356 /*
1357 * Copy the client's handshake message_seq on initial handshakes
1358 */
1359 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02001360 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001361 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1362 ssl->in_msg[5];
1363 ssl->handshake->out_msg_seq = cli_msg_seq;
1364 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02001365 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001366 else
1367 {
1368 /* This couldn't be done in ssl_prepare_handshake_record() */
1369 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1370 ssl->in_msg[5];
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001371
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001372 if( cli_msg_seq != ssl->handshake->in_msg_seq )
1373 {
1374 SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
1375 "%d (expected %d)", cli_msg_seq,
1376 ssl->handshake->in_msg_seq ) );
1377 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1378 }
1379
1380 ssl->handshake->in_msg_seq++;
1381 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001382
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001383 /*
1384 * For now we don't support fragmentation, so make sure
1385 * fragment_offset == 0 and fragment_length == length
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001386 */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001387 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1388 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1389 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001390 SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001391 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1392 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001393 }
1394#endif /* POLARSSL_SSL_PROTO_DTLS */
1395
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001396 buf += ssl_hs_hdr_len( ssl );
1397 msg_len -= ssl_hs_hdr_len( ssl );
1398
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001399 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001400 * ClientHello layer:
1401 * 0 . 1 protocol version
1402 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1403 * 34 . 35 session id length (1 byte)
1404 * 35 . 34+x session id
1405 * 35+x . 35+x DTLS only: cookie length (1 byte)
1406 * 36+x . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001407 * .. . .. ciphersuite list length (2 bytes)
1408 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001409 * .. . .. compression alg. list length (1 byte)
1410 * .. . .. compression alg. list
1411 * .. . .. extensions length (2 bytes, optional)
1412 * .. . .. extensions (optional)
Paul Bakkerec636f32012-09-09 19:17:02 +00001413 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001414
1415 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001416 * Minimal length (with everything empty and extensions ommitted) is
1417 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1418 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001419 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001420 if( msg_len < 38 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001421 {
1422 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1423 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1424 }
1425
1426 /*
1427 * Check and save the protocol version
1428 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001429 SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001430
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001431 ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001432 ssl->transport, buf );
Paul Bakkerec636f32012-09-09 19:17:02 +00001433
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001434 ssl->handshake->max_major_ver = ssl->major_ver;
1435 ssl->handshake->max_minor_ver = ssl->minor_ver;
1436
1437 if( ssl->major_ver < ssl->min_major_ver ||
1438 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001439 {
1440 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001441 " [%d:%d] < [%d:%d]",
1442 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001443 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001444
1445 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1446 SSL_ALERT_MSG_PROTOCOL_VERSION );
1447
1448 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1449 }
1450
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001451 if( ssl->major_ver > ssl->max_major_ver )
1452 {
1453 ssl->major_ver = ssl->max_major_ver;
1454 ssl->minor_ver = ssl->max_minor_ver;
1455 }
1456 else if( ssl->minor_ver > ssl->max_minor_ver )
1457 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001458
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001459 /*
1460 * Save client random (inc. Unix time)
1461 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001462 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001463
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001464 memcpy( ssl->handshake->randbytes, buf + 2, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001465
1466 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001467 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001468 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001469 sess_len = buf[34];
Paul Bakkerec636f32012-09-09 19:17:02 +00001470
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001471 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001472 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001473 {
1474 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1475 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1476 }
1477
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001478 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001479
Paul Bakker48916f92012-09-16 19:57:18 +00001480 ssl->session_negotiate->length = sess_len;
1481 memset( ssl->session_negotiate->id, 0,
1482 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001483 memcpy( ssl->session_negotiate->id, buf + 35,
Paul Bakker48916f92012-09-16 19:57:18 +00001484 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001485
1486 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001487 * Check the cookie length and content
1488 */
1489#if defined(POLARSSL_SSL_PROTO_DTLS)
1490 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1491 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001492 cookie_offset = 35 + sess_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001493 cookie_len = buf[cookie_offset];
1494
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001495 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001496 {
1497 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1498 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1499 }
1500
1501 SSL_DEBUG_BUF( 3, "client hello, cookie",
1502 buf + cookie_offset + 1, cookie_len );
1503
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001504#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001505 if( ssl->f_cookie_check != NULL &&
1506 ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001507 {
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001508 if( ssl->f_cookie_check( ssl->p_cookie,
1509 buf + cookie_offset + 1, cookie_len,
1510 ssl->cli_id, ssl->cli_id_len ) != 0 )
1511 {
1512 SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
1513 ssl->handshake->verify_cookie_len = 1;
1514 }
1515 else
1516 {
1517 SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
1518 ssl->handshake->verify_cookie_len = 0;
1519 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001520 }
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001521 else
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +02001522#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001523 {
1524 /* We know we didn't send a cookie, so it should be empty */
1525 if( cookie_len != 0 )
1526 {
1527 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1528 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1529 }
1530
1531 SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
1532 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001533 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001534#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001535
1536 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001537 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001538 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001539#if defined(POLARSSL_SSL_PROTO_DTLS)
1540 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1541 ciph_offset = cookie_offset + 1 + cookie_len;
1542 else
1543#endif
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001544 ciph_offset = 35 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001545
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001546 ciph_len = ( buf[ciph_offset + 0] << 8 )
1547 | ( buf[ciph_offset + 1] );
1548
1549 if( ciph_len < 2 ||
1550 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1551 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001552 {
1553 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1554 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1555 }
1556
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001557 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1558 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001559
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001560 /*
1561 * Check the compression algorithms length and pick one
1562 */
1563 comp_offset = ciph_offset + 2 + ciph_len;
1564
1565 comp_len = buf[comp_offset];
1566
1567 if( comp_len < 1 ||
1568 comp_len > 16 ||
1569 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001570 {
1571 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1572 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1573 }
1574
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001575 SSL_DEBUG_BUF( 3, "client hello, compression",
1576 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001577
1578 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001579#if defined(POLARSSL_ZLIB_SUPPORT)
1580 for( i = 0; i < comp_len; ++i )
1581 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001582 if( buf[comp_offset + 1 + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001583 {
Paul Bakker48916f92012-09-16 19:57:18 +00001584 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001585 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001586 }
1587 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001588#endif
1589
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001590 /* See comments in ssl_write_client_hello() */
1591#if defined(POLARSSL_SSL_PROTO_DTLS)
1592 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1593 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
1594#endif
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001595
Paul Bakkerec636f32012-09-09 19:17:02 +00001596 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001597 * Check the extension length
Paul Bakker48916f92012-09-16 19:57:18 +00001598 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001599 ext_offset = comp_offset + 1 + comp_len;
1600 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001601 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001602 if( msg_len < ext_offset + 2 )
Paul Bakker48916f92012-09-16 19:57:18 +00001603 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001604 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1605 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1606 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001607
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001608 ext_len = ( buf[ext_offset + 0] << 8 )
1609 | ( buf[ext_offset + 1] );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001610
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001611 if( ( ext_len > 0 && ext_len < 4 ) ||
1612 msg_len != ext_offset + 2 + ext_len )
1613 {
1614 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1615 SSL_DEBUG_BUF( 3, "client hello extensions",
1616 buf + ext_offset + 2, ext_len );
1617 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +00001618 }
1619 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001620 else
1621 ext_len = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001622
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001623 ext = buf + ext_offset + 2;
Paul Bakker48916f92012-09-16 19:57:18 +00001624
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001625 while( ext_len != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001626 {
1627 unsigned int ext_id = ( ( ext[0] << 8 )
1628 | ( ext[1] ) );
1629 unsigned int ext_size = ( ( ext[2] << 8 )
1630 | ( ext[3] ) );
1631
1632 if( ext_size + 4 > ext_len )
1633 {
1634 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1635 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1636 }
1637 switch( ext_id )
1638 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001639#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001640 case TLS_EXT_SERVERNAME:
1641 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1642 if( ssl->f_sni == NULL )
1643 break;
1644
1645 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1646 if( ret != 0 )
1647 return( ret );
1648 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001649#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001650
Paul Bakker48916f92012-09-16 19:57:18 +00001651 case TLS_EXT_RENEGOTIATION_INFO:
1652 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1653 renegotiation_info_seen = 1;
1654
Paul Bakker23f36802012-09-28 14:15:14 +00001655 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1656 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001657 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001658 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001659
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001660#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001661 case TLS_EXT_SIG_ALG:
1662 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1663 if( ssl->renegotiation == SSL_RENEGOTIATION )
1664 break;
1665
1666 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1667 if( ret != 0 )
1668 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001669 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001670#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001671
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001672#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001673 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1674 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1675
1676 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1677 if( ret != 0 )
1678 return( ret );
1679 break;
1680
1681 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1682 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001683 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001684
1685 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1686 if( ret != 0 )
1687 return( ret );
1688 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001689#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001690
Paul Bakker05decb22013-08-15 13:33:48 +02001691#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001692 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1693 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1694
1695 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1696 if( ret != 0 )
1697 return( ret );
1698 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001699#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001700
Paul Bakker1f2bc622013-08-15 13:45:55 +02001701#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001702 case TLS_EXT_TRUNCATED_HMAC:
1703 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1704
1705 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1706 if( ret != 0 )
1707 return( ret );
1708 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001709#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001710
Paul Bakkera503a632013-08-14 13:48:06 +02001711#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001712 case TLS_EXT_SESSION_TICKET:
1713 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1714
1715 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1716 if( ret != 0 )
1717 return( ret );
1718 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001719#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001720
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001721#if defined(POLARSSL_SSL_ALPN)
1722 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001723 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001724
1725 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1726 if( ret != 0 )
1727 return( ret );
1728 break;
1729#endif /* POLARSSL_SSL_SESSION_TICKETS */
1730
Paul Bakker48916f92012-09-16 19:57:18 +00001731 default:
1732 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1733 ext_id ) );
1734 }
1735
1736 ext_len -= 4 + ext_size;
1737 ext += 4 + ext_size;
1738
1739 if( ext_len > 0 && ext_len < 4 )
1740 {
1741 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1742 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1743 }
1744 }
1745
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01001746#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1747 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1748 {
1749 if( p[0] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1750 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1751 {
1752 SSL_DEBUG_MSG( 0, ( "received FALLBACK_SCSV" ) );
1753
1754 if( ssl->minor_ver < ssl->max_minor_ver )
1755 {
1756 SSL_DEBUG_MSG( 0, ( "inapropriate fallback" ) );
1757
1758 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1759 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1760
1761 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1762 }
1763
1764 break;
1765 }
1766 }
1767#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1768
Paul Bakker48916f92012-09-16 19:57:18 +00001769 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001770 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1771 */
1772 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
1773 {
1774 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1775 {
1776 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1777 if( ssl->renegotiation == SSL_RENEGOTIATION )
1778 {
1779 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1780
1781 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1782 return( ret );
1783
1784 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1785 }
1786 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1787 break;
1788 }
1789 }
1790
1791 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001792 * Renegotiation security checks
1793 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001794 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1795 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1796 {
1797 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1798 handshake_failure = 1;
1799 }
1800 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1801 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1802 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001803 {
1804 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001805 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001806 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001807 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1808 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1809 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001810 {
1811 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001812 handshake_failure = 1;
1813 }
1814 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1815 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1816 renegotiation_info_seen == 1 )
1817 {
1818 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1819 handshake_failure = 1;
1820 }
1821
1822 if( handshake_failure == 1 )
1823 {
1824 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1825 return( ret );
1826
Paul Bakker48916f92012-09-16 19:57:18 +00001827 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1828 }
Paul Bakker380da532012-04-18 16:10:25 +00001829
Paul Bakker41c83d32013-03-20 14:39:14 +01001830 /*
1831 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001832 * (At the end because we need information from the EC-based extensions
1833 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001834 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001835 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001836 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001837#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001838 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001839 {
1840 for( i = 0; ciphersuites[i] != 0; i++ )
1841#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001842 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001843 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001844 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001845#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001846 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001847 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1848 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1849 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001850
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001851 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1852 &ciphersuite_info ) ) != 0 )
1853 return( ret );
1854
1855 if( ciphersuite_info != NULL )
1856 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001857 }
1858 }
1859
1860 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1861
1862 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1863 return( ret );
1864
1865 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1866
1867have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001868 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001869 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1870 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1871
Paul Bakker5121ce52009-01-03 21:22:43 +00001872 ssl->state++;
1873
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001874#if defined(POLARSSL_SSL_PROTO_DTLS)
1875 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1876 ssl_recv_flight_completed( ssl );
1877#endif
1878
Paul Bakker5121ce52009-01-03 21:22:43 +00001879 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1880
1881 return( 0 );
1882}
1883
Paul Bakker1f2bc622013-08-15 13:45:55 +02001884#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001885static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1886 unsigned char *buf,
1887 size_t *olen )
1888{
1889 unsigned char *p = buf;
1890
1891 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1892 {
1893 *olen = 0;
1894 return;
1895 }
1896
1897 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1898
1899 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1900 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1901
1902 *p++ = 0x00;
1903 *p++ = 0x00;
1904
1905 *olen = 4;
1906}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001907#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001908
Paul Bakkera503a632013-08-14 13:48:06 +02001909#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001910static void ssl_write_session_ticket_ext( ssl_context *ssl,
1911 unsigned char *buf,
1912 size_t *olen )
1913{
1914 unsigned char *p = buf;
1915
1916 if( ssl->handshake->new_session_ticket == 0 )
1917 {
1918 *olen = 0;
1919 return;
1920 }
1921
1922 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1923
1924 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1925 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1926
1927 *p++ = 0x00;
1928 *p++ = 0x00;
1929
1930 *olen = 4;
1931}
Paul Bakkera503a632013-08-14 13:48:06 +02001932#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001933
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001934static void ssl_write_renegotiation_ext( ssl_context *ssl,
1935 unsigned char *buf,
1936 size_t *olen )
1937{
1938 unsigned char *p = buf;
1939
1940 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1941 {
1942 *olen = 0;
1943 return;
1944 }
1945
1946 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1947
1948 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1949 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1950
1951 *p++ = 0x00;
1952 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1953 *p++ = ssl->verify_data_len * 2 & 0xFF;
1954
1955 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1956 p += ssl->verify_data_len;
1957 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1958 p += ssl->verify_data_len;
1959
1960 *olen = 5 + ssl->verify_data_len * 2;
1961}
1962
Paul Bakker05decb22013-08-15 13:33:48 +02001963#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001964static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1965 unsigned char *buf,
1966 size_t *olen )
1967{
1968 unsigned char *p = buf;
1969
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001970 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1971 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001972 *olen = 0;
1973 return;
1974 }
1975
1976 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1977
1978 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1979 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1980
1981 *p++ = 0x00;
1982 *p++ = 1;
1983
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001984 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001985
1986 *olen = 5;
1987}
Paul Bakker05decb22013-08-15 13:33:48 +02001988#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001989
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001990#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001991static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1992 unsigned char *buf,
1993 size_t *olen )
1994{
1995 unsigned char *p = buf;
1996 ((void) ssl);
1997
Paul Bakker677377f2013-10-28 12:54:26 +01001998 if( ( ssl->handshake->cli_exts &
1999 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
2000 {
2001 *olen = 0;
2002 return;
2003 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002004
2005 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
2006
2007 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2008 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
2009
2010 *p++ = 0x00;
2011 *p++ = 2;
2012
2013 *p++ = 1;
2014 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
2015
2016 *olen = 6;
2017}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002018#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002019
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002020#if defined(POLARSSL_SSL_ALPN )
2021static void ssl_write_alpn_ext( ssl_context *ssl,
2022 unsigned char *buf, size_t *olen )
2023{
2024 if( ssl->alpn_chosen == NULL )
2025 {
2026 *olen = 0;
2027 return;
2028 }
2029
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02002030 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002031
2032 /*
2033 * 0 . 1 ext identifier
2034 * 2 . 3 ext length
2035 * 4 . 5 protocol list length
2036 * 6 . 6 protocol name length
2037 * 7 . 7+n protocol name
2038 */
2039 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
2040 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
2041
2042 *olen = 7 + strlen( ssl->alpn_chosen );
2043
2044 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2045 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2046
2047 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2048 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2049
2050 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2051
2052 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2053}
2054#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
2055
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002056#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002057static int ssl_write_hello_verify_request( ssl_context *ssl )
2058{
2059 int ret;
2060 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002061 unsigned char *cookie_len_byte;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002062
2063 SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
2064
2065 /*
2066 * struct {
2067 * ProtocolVersion server_version;
2068 * opaque cookie<0..2^8-1>;
2069 * } HelloVerifyRequest;
2070 */
2071
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02002072 /* The RFC is not clear on this point, but sending the actual negotiated
2073 * version looks like the most interoperable thing to do. */
2074 ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002075 ssl->transport, p );
2076 SSL_DEBUG_BUF( 3, "server version", (unsigned char *) p, 2 );
2077 p += 2;
2078
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002079 /* If we get here, f_cookie_check is not null */
2080 if( ssl->f_cookie_write == NULL )
2081 {
2082 SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
2083 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2084 }
2085
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002086 /* Skip length byte until we know the length */
2087 cookie_len_byte = p++;
2088
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002089 if( ( ret = ssl->f_cookie_write( ssl->p_cookie,
2090 &p, ssl->out_buf + SSL_BUFFER_LEN,
2091 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002092 {
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002093 SSL_DEBUG_RET( 1, "f_cookie_write", ret );
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002094 return( ret );
2095 }
2096
2097 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2098
2099 SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002100
2101 ssl->out_msglen = p - ssl->out_msg;
2102 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2103 ssl->out_msg[0] = SSL_HS_HELLO_VERIFY_REQUEST;
2104
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002105 ssl->state = SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002106
2107 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2108 {
2109 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2110 return( ret );
2111 }
2112
2113 SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
2114
2115 return( 0 );
2116}
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002117#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002118
Paul Bakker5121ce52009-01-03 21:22:43 +00002119static int ssl_write_server_hello( ssl_context *ssl )
2120{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002121#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002122 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002123#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002124 int ret;
2125 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002126 unsigned char *buf, *p;
2127
2128 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
2129
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002130#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002131 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002132 ssl->handshake->verify_cookie_len != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002133 {
2134 SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2135 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2136
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002137 return( ssl_write_hello_verify_request( ssl ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002138 }
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002139#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002140
Paul Bakkera9a028e2013-11-21 17:31:06 +01002141 if( ssl->f_rng == NULL )
2142 {
2143 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2144 return( POLARSSL_ERR_SSL_NO_RNG );
2145 }
2146
Paul Bakker5121ce52009-01-03 21:22:43 +00002147 /*
2148 * 0 . 0 handshake type
2149 * 1 . 3 handshake length
2150 * 4 . 5 protocol version
2151 * 6 . 9 UNIX time()
2152 * 10 . 37 random bytes
2153 */
2154 buf = ssl->out_msg;
2155 p = buf + 4;
2156
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002157 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2158 ssl->transport, p );
2159 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002160
2161 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002162 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002163
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002164#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002165 t = time( NULL );
2166 *p++ = (unsigned char)( t >> 24 );
2167 *p++ = (unsigned char)( t >> 16 );
2168 *p++ = (unsigned char)( t >> 8 );
2169 *p++ = (unsigned char)( t );
2170
2171 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002172#else
2173 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
2174 return( ret );
2175
2176 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02002177#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002178
Paul Bakkera3d195c2011-11-27 21:07:34 +00002179 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
2180 return( ret );
2181
2182 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002183
Paul Bakker48916f92012-09-16 19:57:18 +00002184 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002185
2186 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
2187
2188 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002189 * Resume is 0 by default, see ssl_handshake_init().
2190 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2191 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002192 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002193 if( ssl->handshake->resume == 0 &&
2194 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002195 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002196 ssl->f_get_cache != NULL &&
2197 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
2198 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01002199 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002200 ssl->handshake->resume = 1;
2201 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002202
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002203 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002204 {
2205 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002206 * New session, create a new session id,
2207 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002208 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002209 ssl->state++;
2210
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002211#if defined(POLARSSL_HAVE_TIME)
2212 ssl->session_negotiate->start = time( NULL );
2213#endif
2214
Paul Bakkera503a632013-08-14 13:48:06 +02002215#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002216 if( ssl->handshake->new_session_ticket != 0 )
2217 {
2218 ssl->session_negotiate->length = n = 0;
2219 memset( ssl->session_negotiate->id, 0, 32 );
2220 }
2221 else
2222#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002223 {
2224 ssl->session_negotiate->length = n = 32;
2225 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002226 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002227 return( ret );
2228 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002229 }
2230 else
2231 {
2232 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002233 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002234 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02002235 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002236 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002237
2238 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2239 {
2240 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2241 return( ret );
2242 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002243 }
2244
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002245 /*
2246 * 38 . 38 session id length
2247 * 39 . 38+n session id
2248 * 39+n . 40+n chosen ciphersuite
2249 * 41+n . 41+n chosen compression alg.
2250 * 42+n . 43+n extensions length
2251 * 44+n . 43+n+m extensions
2252 */
2253 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00002254 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
2255 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002256
2257 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2258 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2259 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002260 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002261
Paul Bakker48916f92012-09-16 19:57:18 +00002262 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2263 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2264 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002265
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02002266 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2267 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02002268 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002269 ssl->session_negotiate->compression ) );
2270
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002271 /*
2272 * First write extensions, then the total length
2273 */
2274 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2275 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002276
Paul Bakker05decb22013-08-15 13:33:48 +02002277#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002278 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2279 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002280#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002281
Paul Bakker1f2bc622013-08-15 13:45:55 +02002282#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002283 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2284 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002285#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002286
Paul Bakkera503a632013-08-14 13:48:06 +02002287#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002288 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2289 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002290#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002291
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002292#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002293 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2294 ext_len += olen;
2295#endif
2296
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002297#if defined(POLARSSL_SSL_ALPN)
2298 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2299 ext_len += olen;
2300#endif
2301
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002302 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002303
Paul Bakkera7036632014-04-30 10:15:38 +02002304 if( ext_len > 0 )
2305 {
2306 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2307 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2308 p += ext_len;
2309 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002310
2311 ssl->out_msglen = p - buf;
2312 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2313 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
2314
2315 ret = ssl_write_record( ssl );
2316
2317 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2318
2319 return( ret );
2320}
2321
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002322#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2323 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002324 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2325 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002326static int ssl_write_certificate_request( ssl_context *ssl )
2327{
Paul Bakkered27a042013-04-18 22:46:23 +02002328 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002329
2330 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2331
2332 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002333 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002334 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2335 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002336 {
2337 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2338 ssl->state++;
2339 return( 0 );
2340 }
2341
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002342 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2343 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002344}
2345#else
2346static int ssl_write_certificate_request( ssl_context *ssl )
2347{
2348 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2349 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002350 size_t dn_size, total_dn_size; /* excluding length bytes */
2351 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002352 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002353 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00002354
2355 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2356
2357 ssl->state++;
2358
Paul Bakkerfbb17802013-04-17 19:10:21 +02002359 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002360 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002361 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002362 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002363 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002364 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002365 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002366 return( 0 );
2367 }
2368
2369 /*
2370 * 0 . 0 handshake type
2371 * 1 . 3 handshake length
2372 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002373 * 5 .. m-1 cert types
2374 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002375 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002376 * n .. n+1 length of all DNs
2377 * n+2 .. n+3 length of DN 1
2378 * n+4 .. ... Distinguished Name #1
2379 * ... .. ... length of DN 2, etc.
2380 */
2381 buf = ssl->out_msg;
2382 p = buf + 4;
2383
2384 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002385 * Supported certificate types
2386 *
2387 * ClientCertificateType certificate_types<1..2^8-1>;
2388 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002389 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002390 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002391
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002392#if defined(POLARSSL_RSA_C)
2393 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2394#endif
2395#if defined(POLARSSL_ECDSA_C)
2396 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2397#endif
2398
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002399 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002400 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002401
Paul Bakker577e0062013-08-28 11:57:20 +02002402 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002403#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002404 /*
2405 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002406 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002407 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2408 *
2409 * struct {
2410 * HashAlgorithm hash;
2411 * SignatureAlgorithm signature;
2412 * } SignatureAndHashAlgorithm;
2413 *
2414 * enum { (255) } HashAlgorithm;
2415 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002416 */
Paul Bakker21dca692013-01-03 11:41:08 +01002417 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002418 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002419 /*
2420 * Only use current running hash algorithm that is already required
2421 * for requested ciphersuite.
2422 */
Paul Bakker926af752012-11-23 13:38:07 +01002423 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2424
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002425 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2426 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002427 {
2428 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2429 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002430
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002431 /*
2432 * Supported signature algorithms
2433 */
2434#if defined(POLARSSL_RSA_C)
2435 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2436 p[2 + sa_len++] = SSL_SIG_RSA;
2437#endif
2438#if defined(POLARSSL_ECDSA_C)
2439 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2440 p[2 + sa_len++] = SSL_SIG_ECDSA;
2441#endif
Paul Bakker926af752012-11-23 13:38:07 +01002442
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002443 p[0] = (unsigned char)( sa_len >> 8 );
2444 p[1] = (unsigned char)( sa_len );
2445 sa_len += 2;
2446 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002447 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002448#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002449
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002450 /*
2451 * DistinguishedName certificate_authorities<0..2^16-1>;
2452 * opaque DistinguishedName<1..2^16-1>;
2453 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002454 p += 2;
2455 crt = ssl->ca_chain;
2456
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002457 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002458 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002459 {
2460 if( p - buf > 4096 )
2461 break;
2462
Paul Bakker926af752012-11-23 13:38:07 +01002463 dn_size = crt->subject_raw.len;
2464 *p++ = (unsigned char)( dn_size >> 8 );
2465 *p++ = (unsigned char)( dn_size );
2466 memcpy( p, crt->subject_raw.p, dn_size );
2467 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002468
Paul Bakker926af752012-11-23 13:38:07 +01002469 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2470
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002471 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002472 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002473 }
2474
Paul Bakker926af752012-11-23 13:38:07 +01002475 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002476 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2477 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002478 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2479 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002480
2481 ret = ssl_write_record( ssl );
2482
2483 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2484
2485 return( ret );
2486}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002487#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2488 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002489 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2490 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002491
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002492#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2493 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2494static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2495{
2496 int ret;
2497
2498 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2499 {
2500 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2501 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2502 }
2503
2504 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2505 pk_ec( *ssl_own_key( ssl ) ),
2506 POLARSSL_ECDH_OURS ) ) != 0 )
2507 {
2508 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2509 return( ret );
2510 }
2511
2512 return( 0 );
2513}
2514#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2515 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2516
Paul Bakker41c83d32013-03-20 14:39:14 +01002517static int ssl_write_server_key_exchange( ssl_context *ssl )
2518{
Paul Bakker23986e52011-04-24 08:57:21 +00002519 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002520 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002521 const ssl_ciphersuite_t *ciphersuite_info =
2522 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002523
2524#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2525 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2526 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002527 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002528 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002529 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002530 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002531 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002532 ((void) dig_signed);
2533 ((void) dig_signed_len);
2534#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002535
Paul Bakker5121ce52009-01-03 21:22:43 +00002536 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2537
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002538#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2539 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2540 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002541 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2542 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2543 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002544 {
2545 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2546 ssl->state++;
2547 return( 0 );
2548 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002549#endif
2550
2551#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2552 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2553 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2554 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2555 {
2556 ssl_get_ecdh_params_from_cert( ssl );
2557
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002558 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002559 ssl->state++;
2560 return( 0 );
2561 }
2562#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002563
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002564#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2565 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2566 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2567 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002568 {
2569 /* TODO: Support identity hints */
2570 *(p++) = 0x00;
2571 *(p++) = 0x00;
2572
2573 n += 2;
2574 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002575#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2576 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002577
2578#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2579 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2580 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2581 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002582 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002583 /*
2584 * Ephemeral DH parameters:
2585 *
2586 * struct {
2587 * opaque dh_p<1..2^16-1>;
2588 * opaque dh_g<1..2^16-1>;
2589 * opaque dh_Ys<1..2^16-1>;
2590 * } ServerDHParams;
2591 */
2592 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2593 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2594 {
2595 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2596 return( ret );
2597 }
Paul Bakker48916f92012-09-16 19:57:18 +00002598
Paul Bakker41c83d32013-03-20 14:39:14 +01002599 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002600 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2601 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002602 {
2603 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2604 return( ret );
2605 }
2606
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002607 dig_signed = p;
2608 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002609
2610 p += len;
2611 n += len;
2612
Paul Bakker41c83d32013-03-20 14:39:14 +01002613 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2614 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2615 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2616 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2617 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002618#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2619 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002620
Gergely Budai987bfb52014-01-19 21:48:42 +01002621#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002622 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002623 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2624 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002625 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002626 /*
2627 * Ephemeral ECDH parameters:
2628 *
2629 * struct {
2630 * ECParameters curve_params;
2631 * ECPoint public;
2632 * } ServerECDHParams;
2633 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002634 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002635#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002636 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002637
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002638 /* Match our preference list against the offered curves */
2639 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2640 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2641 if( (*curve)->grp_id == *gid )
2642 goto curve_matching_done;
2643
2644curve_matching_done:
2645#else
2646 curve = ssl->handshake->curves;
2647#endif
2648
2649 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002650 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002651 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2652 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002653 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002654
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002655 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002656
Paul Bakker41c83d32013-03-20 14:39:14 +01002657 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002658 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002659 {
2660 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2661 return( ret );
2662 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002663
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002664 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2665 p, SSL_MAX_CONTENT_LEN - n,
2666 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002667 {
2668 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2669 return( ret );
2670 }
2671
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002672 dig_signed = p;
2673 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002674
2675 p += len;
2676 n += len;
2677
Paul Bakker41c83d32013-03-20 14:39:14 +01002678 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2679 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002680#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002681
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002682#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002683 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2684 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002685 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002686 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2687 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002688 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002689 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002690 unsigned int hashlen = 0;
2691 unsigned char hash[64];
2692 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002693
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002694 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002695 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2696 */
Paul Bakker577e0062013-08-28 11:57:20 +02002697#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002698 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2699 {
2700 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2701
2702 if( md_alg == POLARSSL_MD_NONE )
2703 {
2704 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002705 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002706 }
2707 }
Paul Bakker577e0062013-08-28 11:57:20 +02002708 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002709#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002710#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2711 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002712 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002713 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2714 {
2715 md_alg = POLARSSL_MD_SHA1;
2716 }
2717 else
Paul Bakker577e0062013-08-28 11:57:20 +02002718#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002719 {
2720 md_alg = POLARSSL_MD_NONE;
2721 }
2722
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002723 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002724 * Compute the hash to be signed
2725 */
Paul Bakker577e0062013-08-28 11:57:20 +02002726#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2727 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002728 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002729 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002730 md5_context md5;
2731 sha1_context sha1;
2732
Paul Bakker5b4af392014-06-26 12:09:34 +02002733 md5_init( &md5 );
2734 sha1_init( &sha1 );
2735
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002736 /*
2737 * digitally-signed struct {
2738 * opaque md5_hash[16];
2739 * opaque sha_hash[20];
2740 * };
2741 *
2742 * md5_hash
2743 * MD5(ClientHello.random + ServerHello.random
2744 * + ServerParams);
2745 * sha_hash
2746 * SHA(ClientHello.random + ServerHello.random
2747 * + ServerParams);
2748 */
2749 md5_starts( &md5 );
2750 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002751 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002752 md5_finish( &md5, hash );
2753
2754 sha1_starts( &sha1 );
2755 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002756 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002757 sha1_finish( &sha1, hash + 16 );
2758
2759 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02002760
2761 md5_free( &md5 );
2762 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002763 }
2764 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002765#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2766 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002767#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2768 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002769 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002770 {
2771 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02002772 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002773
Paul Bakker84bbeb52014-07-01 14:53:22 +02002774 md_init( &ctx );
2775
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002776 /* Info from md_alg will be used instead */
2777 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002778
2779 /*
2780 * digitally-signed struct {
2781 * opaque client_random[32];
2782 * opaque server_random[32];
2783 * ServerDHParams params;
2784 * };
2785 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002786 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002787 {
2788 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2789 return( ret );
2790 }
2791
2792 md_starts( &ctx );
2793 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002794 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002795 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002796 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00002797 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002798 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002799#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2800 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002801 {
2802 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002803 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002804 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002805
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002806 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2807 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002808
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002809 /*
2810 * Make the signature
2811 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002812 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002813 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002814 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2815 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002816 }
Paul Bakker23f36802012-09-28 14:15:14 +00002817
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002818#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002819 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2820 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002821 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002822 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002823
2824 n += 2;
2825 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002826#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002827
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002828 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002829 p + 2 , &signature_len,
2830 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002831 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002832 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002833 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002834 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002835
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002836 *(p++) = (unsigned char)( signature_len >> 8 );
2837 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002838 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002839
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002840 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002841
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002842 p += signature_len;
2843 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002844 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002845#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002846 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2847 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002848
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002849 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002850 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2851 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2852
2853 ssl->state++;
2854
2855 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2856 {
2857 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2858 return( ret );
2859 }
2860
2861 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2862
2863 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002864}
2865
2866static int ssl_write_server_hello_done( ssl_context *ssl )
2867{
2868 int ret;
2869
2870 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2871
2872 ssl->out_msglen = 4;
2873 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2874 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2875
2876 ssl->state++;
2877
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002878#if defined(POLARSSL_SSL_PROTO_DTLS)
2879 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2880 ssl_send_flight_completed( ssl );
2881#endif
2882
Paul Bakker5121ce52009-01-03 21:22:43 +00002883 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2884 {
2885 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2886 return( ret );
2887 }
2888
2889 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2890
2891 return( 0 );
2892}
2893
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002894#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2895 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2896static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2897 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002898{
2899 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002900 size_t n;
2901
2902 /*
2903 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2904 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002905 if( *p + 2 > end )
2906 {
2907 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2908 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2909 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002910
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002911 n = ( (*p)[0] << 8 ) | (*p)[1];
2912 *p += 2;
2913
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002914 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002915 {
2916 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2917 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2918 }
2919
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002920 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002921 {
2922 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2923 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2924 }
2925
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002926 *p += n;
2927
Paul Bakker70df2fb2013-04-17 17:19:09 +02002928 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2929
Paul Bakker70df2fb2013-04-17 17:19:09 +02002930 return( ret );
2931}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002932#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2933 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002934
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002935#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2936 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002937static int ssl_parse_encrypted_pms( ssl_context *ssl,
2938 const unsigned char *p,
2939 const unsigned char *end,
2940 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002941{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002942 int ret;
2943 size_t len = pk_get_len( ssl_own_key( ssl ) );
2944 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002945 unsigned char ver[2];
Paul Bakker70df2fb2013-04-17 17:19:09 +02002946
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002947 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002948 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002949 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002950 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2951 }
2952
2953 /*
2954 * Decrypt the premaster using own private RSA key
2955 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002956#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2957 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002958 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2959 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002960 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2961 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002962 {
2963 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2964 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2965 }
2966 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002967#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002968
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002969 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002970 {
2971 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2972 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2973 }
2974
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002975 ssl_write_version( ssl->handshake->max_major_ver,
2976 ssl->handshake->max_minor_ver,
2977 ssl->transport, ver );
2978
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002979 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2980 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002981 sizeof( ssl->handshake->premaster ) - pms_offset,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002982 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002983
2984 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002985 pms[0] != ver[0] ||
2986 pms[1] != ver[1] )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002987 {
2988 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2989
2990 /*
2991 * Protection against Bleichenbacher's attack:
2992 * invalid PKCS#1 v1.5 padding must not cause
2993 * the connection to end immediately; instead,
2994 * send a bad_record_mac later in the handshake.
2995 */
2996 ssl->handshake->pmslen = 48;
2997
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002998 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002999 if( ret != 0 )
3000 return( ret );
3001 }
3002
3003 return( ret );
3004}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003005#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
3006 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003007
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003008#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003009static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
3010 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003011{
Paul Bakker6db455e2013-09-18 17:29:31 +02003012 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003013 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003014
Paul Bakker6db455e2013-09-18 17:29:31 +02003015 if( ssl->f_psk == NULL &&
3016 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
3017 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003018 {
3019 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
3020 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3021 }
3022
3023 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003024 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003025 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003026 if( *p + 2 > end )
3027 {
3028 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3029 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3030 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003031
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003032 n = ( (*p)[0] << 8 ) | (*p)[1];
3033 *p += 2;
3034
3035 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003036 {
3037 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3038 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3039 }
3040
Paul Bakker6db455e2013-09-18 17:29:31 +02003041 if( ssl->f_psk != NULL )
3042 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003043 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003044 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3045 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003046 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003047 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003048 /* Identity is not a big secret since clients send it in the clear,
3049 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02003050 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003051 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003052 {
3053 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3054 }
3055 }
3056
3057 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003058 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003059 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02003060 if( ( ret = ssl_send_alert_message( ssl,
3061 SSL_ALERT_LEVEL_FATAL,
3062 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
3063 {
3064 return( ret );
3065 }
3066
3067 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003068 }
3069
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003070 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003071
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003072 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003073}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003074#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003075
Paul Bakker5121ce52009-01-03 21:22:43 +00003076static int ssl_parse_client_key_exchange( ssl_context *ssl )
3077{
Paul Bakker23986e52011-04-24 08:57:21 +00003078 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003079 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003080 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003081
Paul Bakker41c83d32013-03-20 14:39:14 +01003082 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003083
3084 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
3085
3086 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3087 {
3088 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3089 return( ret );
3090 }
3091
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003092 p = ssl->in_msg + ssl_hs_hdr_len( ssl );
3093 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00003094
Paul Bakker5121ce52009-01-03 21:22:43 +00003095 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3096 {
3097 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003098 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003099 }
3100
3101 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
3102 {
3103 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003104 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003105 }
3106
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003107#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01003108 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003109 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003110 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003111 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02003112 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3113 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003114 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003115
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003116 if( p != end )
3117 {
3118 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3119 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3120 }
3121
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02003122 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003123
3124 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
3125 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003126 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02003127 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003128 {
3129 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
3130 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3131 }
3132
3133 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003134 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003135 else
3136#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003137#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003138 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3139 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3140 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003141 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003142 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
3143 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
3144 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003145 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003146 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003147 p, end - p) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003148 {
3149 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3150 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3151 }
3152
3153 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3154
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003155 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
3156 &ssl->handshake->pmslen,
3157 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02003158 POLARSSL_MPI_MAX_SIZE,
3159 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003160 {
3161 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
3162 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3163 }
3164
3165 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003166 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003167 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003168#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003169 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3170 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3171 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003172#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
3173 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003174 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003175 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003176 {
3177 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3178 return( ret );
3179 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003180
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003181 if( p != end )
3182 {
3183 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3184 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3185 }
3186
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003187 if( ( ret = ssl_psk_derive_premaster( ssl,
3188 ciphersuite_info->key_exchange ) ) != 0 )
3189 {
3190 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3191 return( ret );
3192 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003193 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003194 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003195#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003196#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
3197 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
3198 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003199 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3200 {
3201 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3202 return( ret );
3203 }
3204
3205 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3206 {
3207 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
3208 return( ret );
3209 }
3210
3211 if( ( ret = ssl_psk_derive_premaster( ssl,
3212 ciphersuite_info->key_exchange ) ) != 0 )
3213 {
3214 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3215 return( ret );
3216 }
3217 }
3218 else
3219#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003220#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3221 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3222 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003223 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3224 {
3225 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3226 return( ret );
3227 }
3228 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3229 {
3230 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3231 return( ret );
3232 }
3233
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003234 if( p != end )
3235 {
3236 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3237 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3238 }
3239
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003240 if( ( ret = ssl_psk_derive_premaster( ssl,
3241 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003242 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003243 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3244 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003245 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003246 }
3247 else
3248#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003249#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3250 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
3251 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003252 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3253 {
3254 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3255 return( ret );
3256 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003257
3258 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
3259 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003260 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003261 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3262 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003263 }
3264
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003265 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3266
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003267 if( ( ret = ssl_psk_derive_premaster( ssl,
3268 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003269 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003270 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003271 return( ret );
3272 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003273 }
3274 else
3275#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003276#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
3277 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003278 {
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003279 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003280 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003281 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003282 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003283 }
3284 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003285 else
3286#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
3287 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003288 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003289 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003290 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003291
Paul Bakkerff60ee62010-03-16 21:09:09 +00003292 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
3293 {
3294 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
3295 return( ret );
3296 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003297
Paul Bakker5121ce52009-01-03 21:22:43 +00003298 ssl->state++;
3299
3300 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3301
3302 return( 0 );
3303}
3304
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003305#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3306 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003307 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3308 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003309static int ssl_parse_certificate_verify( ssl_context *ssl )
3310{
Paul Bakkerfbb17802013-04-17 19:10:21 +02003311 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003312
3313 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3314
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003315 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003316 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003317 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003318 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02003319 {
3320 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3321 ssl->state++;
3322 return( 0 );
3323 }
3324
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003325 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3326 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003327}
3328#else
3329static int ssl_parse_certificate_verify( ssl_context *ssl )
3330{
3331 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003332 size_t i, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003333 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003334 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003335 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003336#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003337 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003338#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003339 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003340 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3341
3342 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3343
3344 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003345 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003346 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003347 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3348 ssl->session_negotiate->peer_cert == NULL )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003349 {
3350 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3351 ssl->state++;
3352 return( 0 );
3353 }
3354
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003355 /* Needs to be done before read_record() to exclude current message */
Paul Bakker48916f92012-09-16 19:57:18 +00003356 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003357
3358 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3359 {
3360 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3361 return( ret );
3362 }
3363
3364 ssl->state++;
3365
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003366 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE ||
3367 ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003368 {
3369 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003370 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003371 }
3372
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003373 i = ssl_hs_hdr_len( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003374
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003375 /*
3376 * struct {
3377 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
3378 * opaque signature<0..2^16-1>;
3379 * } DigitallySigned;
3380 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003381#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3382 defined(POLARSSL_SSL_PROTO_TLS1_1)
3383 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003384 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003385 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003386 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003387
3388 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3389 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3390 POLARSSL_PK_ECDSA ) )
3391 {
3392 hash_start += 16;
3393 hashlen -= 16;
3394 md_alg = POLARSSL_MD_SHA1;
3395 }
Paul Bakker926af752012-11-23 13:38:07 +01003396 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003397 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003398#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3399 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003400#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3401 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003402 {
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00003403 if( i + 2 > ssl->in_hslen )
3404 {
3405 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3406 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3407 }
3408
Paul Bakker5121ce52009-01-03 21:22:43 +00003409 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003410 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003411 */
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003412 if( ssl->in_msg[i] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003413 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003414 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3415 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003416 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3417 }
3418
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003419 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003420
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003421 /* Info from md_alg will be used instead */
3422 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003423
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003424 i++;
3425
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003426 /*
3427 * Signature
3428 */
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003429 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003430 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003431 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003432 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3433 " for verify message" ) );
3434 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003435 }
3436
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003437 /*
3438 * Check the certificate's key type matches the signature alg
3439 */
3440 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3441 {
3442 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3443 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3444 }
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003445
3446 i++;
Paul Bakker577e0062013-08-28 11:57:20 +02003447 }
3448 else
3449#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3450 {
3451 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003452 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003453 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003454
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00003455 if( i + 2 > ssl->in_hslen )
3456 {
3457 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3458 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3459 }
3460
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003461 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
3462 i += 2;
Paul Bakker926af752012-11-23 13:38:07 +01003463
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003464 if( i + sig_len != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003465 {
3466 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003467 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003468 }
3469
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003470 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003471 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003472 ssl->in_msg + i, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003473 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003474 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003475 return( ret );
3476 }
3477
3478 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3479
Paul Bakkered27a042013-04-18 22:46:23 +02003480 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003481}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003482#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3483 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3484 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003485
Paul Bakkera503a632013-08-14 13:48:06 +02003486#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003487static int ssl_write_new_session_ticket( ssl_context *ssl )
3488{
3489 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003490 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003491 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003492
3493 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3494
3495 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3496 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3497
3498 /*
3499 * struct {
3500 * uint32 ticket_lifetime_hint;
3501 * opaque ticket<0..2^16-1>;
3502 * } NewSessionTicket;
3503 *
3504 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3505 * 8 . 9 ticket_len (n)
3506 * 10 . 9+n ticket content
3507 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003508
3509 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3510 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3511 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3512 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003513
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003514 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3515 {
3516 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3517 tlen = 0;
3518 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003519
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003520 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3521 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003522
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003523 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003524
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003525 /*
3526 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3527 * ChangeCipherSpec share the same state.
3528 */
3529 ssl->handshake->new_session_ticket = 0;
3530
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003531 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3532 {
3533 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3534 return( ret );
3535 }
3536
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003537 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3538
3539 return( 0 );
3540}
Paul Bakkera503a632013-08-14 13:48:06 +02003541#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003542
Paul Bakker5121ce52009-01-03 21:22:43 +00003543/*
Paul Bakker1961b702013-01-25 14:49:24 +01003544 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003545 */
Paul Bakker1961b702013-01-25 14:49:24 +01003546int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003547{
3548 int ret = 0;
3549
Paul Bakker1961b702013-01-25 14:49:24 +01003550 if( ssl->state == SSL_HANDSHAKE_OVER )
3551 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003552
Paul Bakker1961b702013-01-25 14:49:24 +01003553 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3554
3555 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3556 return( ret );
3557
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003558#if defined(POLARSSL_SSL_PROTO_DTLS)
3559 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3560 ssl->handshake != NULL &&
3561 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
3562 {
3563 if( ( ret = ssl_resend( ssl ) ) != 0 )
3564 return( ret );
3565 }
3566#endif
3567
Paul Bakker1961b702013-01-25 14:49:24 +01003568 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003569 {
Paul Bakker1961b702013-01-25 14:49:24 +01003570 case SSL_HELLO_REQUEST:
3571 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003572 break;
3573
Paul Bakker1961b702013-01-25 14:49:24 +01003574 /*
3575 * <== ClientHello
3576 */
3577 case SSL_CLIENT_HELLO:
3578 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003579 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003580
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02003581#if defined(POLARSSL_SSL_PROTO_DTLS)
3582 case SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
3583 return( POLARSSL_ERR_SSL_HELLO_VERIFY_REQUIRED );
3584#endif
3585
Paul Bakker1961b702013-01-25 14:49:24 +01003586 /*
3587 * ==> ServerHello
3588 * Certificate
3589 * ( ServerKeyExchange )
3590 * ( CertificateRequest )
3591 * ServerHelloDone
3592 */
3593 case SSL_SERVER_HELLO:
3594 ret = ssl_write_server_hello( ssl );
3595 break;
3596
3597 case SSL_SERVER_CERTIFICATE:
3598 ret = ssl_write_certificate( ssl );
3599 break;
3600
3601 case SSL_SERVER_KEY_EXCHANGE:
3602 ret = ssl_write_server_key_exchange( ssl );
3603 break;
3604
3605 case SSL_CERTIFICATE_REQUEST:
3606 ret = ssl_write_certificate_request( ssl );
3607 break;
3608
3609 case SSL_SERVER_HELLO_DONE:
3610 ret = ssl_write_server_hello_done( ssl );
3611 break;
3612
3613 /*
3614 * <== ( Certificate/Alert )
3615 * ClientKeyExchange
3616 * ( CertificateVerify )
3617 * ChangeCipherSpec
3618 * Finished
3619 */
3620 case SSL_CLIENT_CERTIFICATE:
3621 ret = ssl_parse_certificate( ssl );
3622 break;
3623
3624 case SSL_CLIENT_KEY_EXCHANGE:
3625 ret = ssl_parse_client_key_exchange( ssl );
3626 break;
3627
3628 case SSL_CERTIFICATE_VERIFY:
3629 ret = ssl_parse_certificate_verify( ssl );
3630 break;
3631
3632 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3633 ret = ssl_parse_change_cipher_spec( ssl );
3634 break;
3635
3636 case SSL_CLIENT_FINISHED:
3637 ret = ssl_parse_finished( ssl );
3638 break;
3639
3640 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003641 * ==> ( NewSessionTicket )
3642 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003643 * Finished
3644 */
3645 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003646#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003647 if( ssl->handshake->new_session_ticket != 0 )
3648 ret = ssl_write_new_session_ticket( ssl );
3649 else
Paul Bakkera503a632013-08-14 13:48:06 +02003650#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003651 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003652 break;
3653
3654 case SSL_SERVER_FINISHED:
3655 ret = ssl_write_finished( ssl );
3656 break;
3657
3658 case SSL_FLUSH_BUFFERS:
3659 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3660 ssl->state = SSL_HANDSHAKE_WRAPUP;
3661 break;
3662
3663 case SSL_HANDSHAKE_WRAPUP:
3664 ssl_handshake_wrapup( ssl );
3665 break;
3666
3667 default:
3668 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3669 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003670 }
3671
Paul Bakker5121ce52009-01-03 21:22:43 +00003672 return( ret );
3673}
Paul Bakker9af723c2014-05-01 13:03:14 +02003674#endif /* POLARSSL_SSL_SRV_C */