blob: e22132bffa042aea3fb4a61c1a9ee5941ec325fb [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
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001091 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001092 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001093#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1094 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1095 {
1096 for( i = 0; ciphersuites[i] != 0; i++ )
1097#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001098 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001099 {
1100 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001101#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001102 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001103 if( p[0] != 0 ||
1104 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1105 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1106 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001107
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001108 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1109 &ciphersuite_info ) ) != 0 )
1110 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001111
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001112 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001113 goto have_ciphersuite_v2;
1114 }
1115 }
1116
1117 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1118
1119 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1120
1121have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001122 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001123 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001124 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001125
1126 /*
1127 * SSLv2 Client Hello relevant renegotiation security checks
1128 */
1129 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1130 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1131 {
1132 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1133
1134 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1135 return( ret );
1136
1137 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1138 }
1139
1140 ssl->in_left = 0;
1141 ssl->state++;
1142
1143 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1144
1145 return( 0 );
1146}
1147#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1148
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001149#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnarddd3cdb02014-07-22 20:40:40 +02001150
1151/*
1152 * If DTLS is in use, then at least one of SHA-1, SHA-256, SHA-512 is
1153 * available. Try SHA-256 first, 512 wastes resources since we need to stay
1154 * with max 32 bytes of cookie for DTLS 1.0
1155 */
1156#if defined(POLARSSL_SHA256_C)
1157#define HVR_MD POLARSSL_MD_SHA256
1158#define HVR_MD_LEN 32
1159#define HVR_MD_USE 32
1160#elif defined(POLARSSL_SHA512_C)
1161#define HVR_MD POLARSSL_MD_SHA384
1162#define HVR_MD_LEN 48
1163#define HVR_MD_USE 32
1164#elif defined(POLARSSL_SHA1_C)
1165#define HVR_MD POLARSSL_MD_SHA1
1166#define HVR_MD_LEN 20
1167#define HVR_MD_USE 20
1168#else
1169#error "DTLS hello verify needs SHA-1 or SHA-2"
1170#endif
1171
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001172void ssl_cookie_init( ssl_cookie_ctx *ctx )
1173{
1174 md_init( &ctx->hmac_ctx );
1175}
1176
1177void ssl_cookie_free( ssl_cookie_ctx *ctx )
1178{
1179 md_free( &ctx->hmac_ctx );
1180}
1181
1182int ssl_cookie_setup( ssl_cookie_ctx *ctx,
1183 int (*f_rng)(void *, unsigned char *, size_t),
1184 void *p_rng )
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001185{
1186 int ret;
1187 unsigned char key[HVR_MD_LEN];
1188
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001189 if( ( ret = f_rng( p_rng, key, sizeof( key ) ) ) != 0 )
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001190 return( ret );
1191
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001192 ret = md_init_ctx( &ctx->hmac_ctx, md_info_from_type( HVR_MD ) );
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001193 if( ret != 0 )
1194 return( ret );
1195
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001196 ret = md_hmac_starts( &ctx->hmac_ctx, key, sizeof( key ) );
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001197 if( ret != 0 )
1198 return( ret );
1199
1200 polarssl_zeroize( key, sizeof( key ) );
1201
1202 return( 0 );
1203}
1204
1205/*
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001206 * Generate cookie for DTLS ClientHello verification
1207 */
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001208int ssl_cookie_write( void *ctx,
1209 unsigned char **p, unsigned char *end,
1210 const unsigned char *cli_id, size_t cli_id_len )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001211{
Manuel Pégourié-Gonnarddd3cdb02014-07-22 20:40:40 +02001212 int ret;
Manuel Pégourié-Gonnarddd3cdb02014-07-22 20:40:40 +02001213 unsigned char hmac_out[HVR_MD_LEN];
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001214 md_context_t *hmac_ctx = (md_context_t *) ctx;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001215
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001216 if( (size_t)( end - *p ) < HVR_MD_USE )
1217 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001218
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001219 if( ( ret = md_hmac_reset( hmac_ctx ) ) != 0 ||
1220 ( ret = md_hmac_update( hmac_ctx, cli_id, cli_id_len ) ) != 0 ||
1221 ( ret = md_hmac_finish( hmac_ctx, hmac_out ) ) != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001222 {
Manuel Pégourié-Gonnarddd3cdb02014-07-22 20:40:40 +02001223 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1224 }
1225
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001226 memcpy( *p, hmac_out, HVR_MD_USE );
1227 *p += HVR_MD_USE;
Manuel Pégourié-Gonnarddd3cdb02014-07-22 20:40:40 +02001228
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001229 return( 0 );
1230}
1231
1232/*
1233 * Check a cookie
1234 */
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001235int ssl_cookie_check( void *ctx,
1236 const unsigned char *cookie, size_t cookie_len,
1237 const unsigned char *cli_id, size_t cli_id_len )
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001238{
1239 unsigned char ref_cookie[HVR_MD_USE];
1240 unsigned char *p = ref_cookie;
1241 md_context_t *hmac_ctx = (md_context_t *) ctx;
1242
1243 if( cookie_len != HVR_MD_USE )
1244 return( -1 );
1245
1246 if( ssl_cookie_write( hmac_ctx,
1247 &p, p + sizeof( ref_cookie ),
1248 cli_id, cli_id_len ) != 0 )
1249 return( -1 );
1250
1251 if( safer_memcmp( cookie, ref_cookie, sizeof( ref_cookie ) ) != 0 )
1252 return( -1 );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001253
1254 return( 0 );
1255}
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001256#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001257
Paul Bakker5121ce52009-01-03 21:22:43 +00001258static int ssl_parse_client_hello( ssl_context *ssl )
1259{
Paul Bakker23986e52011-04-24 08:57:21 +00001260 int ret;
1261 unsigned int i, j;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001262 unsigned int ciph_offset, comp_offset, ext_offset;
1263 unsigned int msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001264#if defined(POLARSSL_SSL_PROTO_DTLS)
1265 unsigned int cookie_offset, cookie_len;
1266#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001267 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001268 int renegotiation_info_seen = 0;
1269 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001270 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001271 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001272 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001273
1274 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1275
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001276 /*
1277 * If renegotiating, then the input was read with ssl_read_record(),
1278 * otherwise read it ourselves manually in order to support SSLv2
1279 * ClientHello, which doesn't use the same record layer format.
1280 */
Paul Bakker48916f92012-09-16 19:57:18 +00001281 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001282 ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001283 {
1284 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1285 return( ret );
1286 }
1287
1288 buf = ssl->in_hdr;
1289
Paul Bakker78a8c712013-03-06 17:01:52 +01001290#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001291 if( ssl->transport == SSL_TRANSPORT_STREAM && ( buf[0] & 0x80 ) != 0 )
Paul Bakker78a8c712013-03-06 17:01:52 +01001292 return ssl_parse_client_hello_v2( ssl );
1293#endif
1294
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001295 SSL_DEBUG_BUF( 4, "record header", buf, ssl_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001296
Paul Bakkerec636f32012-09-09 19:17:02 +00001297 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001298 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001299 *
1300 * Record layer:
1301 * 0 . 0 message type
1302 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001303 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001304 * 3 . 4 message length
1305 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001306 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1307 buf[0] ) );
1308
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001309 if( buf[0] != SSL_MSG_HANDSHAKE )
1310 {
1311 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1312 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1313 }
1314
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001315 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1316 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1317
1318 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol version: [%d:%d]",
1319 buf[1], buf[2] ) );
1320
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001321 ssl_read_version( &major, &minor, ssl->transport, buf + 1 );
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001322
1323 /* According to RFC 5246 Appendix E.1, the version here is typically
1324 * "{03,00}, the lowest version number supported by the client, [or] the
1325 * value of ClientHello.client_version", so the only meaningful check here
1326 * is the major version shouldn't be less than 3 */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001327 if( major < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001328 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001329 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1330 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1331 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001332
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001333 /* For DTLS if this is the initial handshake, remember the client sequence
1334 * number to use it in our next message (RFC 6347 4.2.1) */
1335#if defined(POLARSSL_SSL_PROTO_DTLS)
1336 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
1337 ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1338 {
1339 /* Epoch should be 0 for initial handshakes */
1340 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1341 {
1342 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1343 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1344 }
1345
1346 memcpy( ssl->out_ctr + 2, ssl->in_ctr + 2, 6 );
1347 }
1348#endif /* POLARSSL_SSL_PROTO_DTLS */
1349
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001350 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001351
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001352 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Paul Bakkerec636f32012-09-09 19:17:02 +00001353 {
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001354 if( msg_len > SSL_MAX_CONTENT_LEN )
1355 {
1356 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1357 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1358 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001359
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001360 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) + msg_len ) ) != 0 )
1361 {
1362 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1363 return( ret );
1364 }
1365 }
1366 else
Paul Bakkerec636f32012-09-09 19:17:02 +00001367 {
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001368 /* Set by ssl_read_record() */
1369 msg_len = ssl->in_hslen;
Paul Bakkerec636f32012-09-09 19:17:02 +00001370 }
1371
1372 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001373
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001374 SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001375
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001376 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001377
1378 /*
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001379 * The DTLS handshake layer contains additional fields. Use them, then
1380 * remove them so that it looks like TLS format to the rest of the code.
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001381 */
1382#if defined(POLARSSL_SSL_PROTO_DTLS)
1383 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1384 {
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001385 /* 1 (type) + 3 (len) + 2 (seq) + 3 (frag_offset) + 3 (frag_len) */
1386 if( msg_len < 12 )
1387 {
1388 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1389 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1390 }
1391
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001392 /*
1393 * Copy the client's handshake message_seq on initial handshakes
1394 */
1395 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1396 ssl->handshake->msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
1397
1398 // TODO: DTLS: check message_seq on non-initial handshakes?
1399 // (or already done in ssl_read_record?)
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001400
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001401 /*
1402 * For now we don't support fragmentation, so make sure
1403 * fragment_offset == 0 and fragment_length == length
1404 *
1405 * TODO: DTLS: support fragmentation??
1406 * Well, ClientHello is rarely much longer than 512 bytes
1407 * so it will probably never be fragmented anyway...
1408 */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001409 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1410 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1411 {
1412 SSL_DEBUG_MSG( 1, ( "handshake fragmentation not supported" ) );
1413 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1414 }
1415
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001416 /* Remove the additional fields */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001417 memmove( buf + 4, buf + 12, msg_len - 12 );
1418 msg_len -= 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001419 }
1420#endif /* POLARSSL_SSL_PROTO_DTLS */
1421
1422 /*
Paul Bakkerec636f32012-09-09 19:17:02 +00001423 * SSL layer:
1424 * 0 . 0 handshake type
1425 * 1 . 3 handshake length
1426 * 4 . 5 protocol version
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001427 * 6 . 37 random bytes (starting with 4 bytes of Unix time)
1428 * 38 . 38 session id length (1 byte)
Paul Bakkerec636f32012-09-09 19:17:02 +00001429 * 39 . 38+x session id
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001430 * 39+x . 39+x DTLS only: cookie length (1 byte)
1431 * 40+x . .. DTSL only: cookie
1432 * .. . .. ciphersuite list length (2 bytes)
1433 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001434 * .. . .. compression alg. list length (1 byte)
1435 * .. . .. compression alg. list
1436 * .. . .. extensions length (2 bytes, optional)
1437 * .. . .. extensions (optional)
1438 *
1439 * Minimal length (with everything empty and extensions ommitted) is
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001440 * 4 + 2 + 32 + 1 + 2 + 1 = 42 bytes. Check that first, so that we can
1441 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001442 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001443
1444 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001445 * Check the handshake type and message length
Paul Bakkerec636f32012-09-09 19:17:02 +00001446 */
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001447 if( msg_len < 42 )
1448 {
1449 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1450 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1451 }
1452
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001453 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
1454
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001455 if( buf[0] != SSL_HS_CLIENT_HELLO )
Paul Bakkerec636f32012-09-09 19:17:02 +00001456 {
1457 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1458 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1459 }
1460
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001461 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1462 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1463
1464 if( buf[1] != 0 ||
1465 msg_len != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1466 {
1467 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1468 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1469 }
1470
1471 /*
1472 * Check and save the protocol version
1473 */
1474 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1475 buf[4], buf[5] ) );
1476
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001477 ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
1478 ssl->transport, buf + 4 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001479
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001480 ssl->handshake->max_major_ver = ssl->major_ver;
1481 ssl->handshake->max_minor_ver = ssl->minor_ver;
1482
1483 if( ssl->major_ver < ssl->min_major_ver ||
1484 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001485 {
1486 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001487 " [%d:%d] < [%d:%d]",
1488 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001489 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001490
1491 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1492 SSL_ALERT_MSG_PROTOCOL_VERSION );
1493
1494 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1495 }
1496
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001497 if( ssl->major_ver > ssl->max_major_ver )
1498 {
1499 ssl->major_ver = ssl->max_major_ver;
1500 ssl->minor_ver = ssl->max_minor_ver;
1501 }
1502 else if( ssl->minor_ver > ssl->max_minor_ver )
1503 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001504
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001505 /*
1506 * Save client random (inc. Unix time)
1507 */
1508 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1509 buf + 6, 32 );
1510
Paul Bakker48916f92012-09-16 19:57:18 +00001511 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001512
1513 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001514 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001515 */
1516 sess_len = buf[38];
1517
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001518 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001519 sess_len + 38 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001520 {
1521 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1522 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1523 }
1524
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001525 SSL_DEBUG_BUF( 3, "client hello, session id",
1526 buf + 39, sess_len );
1527
Paul Bakker48916f92012-09-16 19:57:18 +00001528 ssl->session_negotiate->length = sess_len;
1529 memset( ssl->session_negotiate->id, 0,
1530 sizeof( ssl->session_negotiate->id ) );
1531 memcpy( ssl->session_negotiate->id, buf + 39,
1532 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001533
1534 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001535 * Check the cookie length and content
1536 */
1537#if defined(POLARSSL_SSL_PROTO_DTLS)
1538 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1539 {
1540 cookie_offset = 39 + sess_len;
1541 cookie_len = buf[cookie_offset];
1542
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001543 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001544 {
1545 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1546 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1547 }
1548
1549 SSL_DEBUG_BUF( 3, "client hello, cookie",
1550 buf + cookie_offset + 1, cookie_len );
1551
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001552#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02001553 if( ssl->f_cookie_check( ssl->p_cookie,
1554 buf + cookie_offset + 1, cookie_len,
1555 ssl->cli_id, ssl->cli_id_len ) != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001556 {
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001557 SSL_DEBUG_MSG( 2, ( "client hello, cookie verification failed" ) );
1558 ssl->handshake->verify_cookie_len = 1;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001559 }
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001560 else
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001561 {
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001562 SSL_DEBUG_MSG( 2, ( "client hello, cookie verification passed" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001563 ssl->handshake->verify_cookie_len = 0;
1564 }
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001565#else
1566 /* We know we didn't send a cookie, so it should be empty */
1567 if( cookie_len != 0 )
1568 {
1569 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1570 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1571 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001572
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001573 SSL_DEBUG_MSG( 2, ( "client hello, cookie verification skipped" ) );
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001574#endif
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001575 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001576#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001577
1578 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001579 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001580 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001581#if defined(POLARSSL_SSL_PROTO_DTLS)
1582 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1583 ciph_offset = cookie_offset + 1 + cookie_len;
1584 else
1585#endif
1586 ciph_offset = 39 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001587
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001588 ciph_len = ( buf[ciph_offset + 0] << 8 )
1589 | ( buf[ciph_offset + 1] );
1590
1591 if( ciph_len < 2 ||
1592 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1593 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001594 {
1595 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1596 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1597 }
1598
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001599 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1600 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001601
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001602 /*
1603 * Check the compression algorithms length and pick one
1604 */
1605 comp_offset = ciph_offset + 2 + ciph_len;
1606
1607 comp_len = buf[comp_offset];
1608
1609 if( comp_len < 1 ||
1610 comp_len > 16 ||
1611 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001612 {
1613 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1614 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1615 }
1616
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001617 SSL_DEBUG_BUF( 3, "client hello, compression",
1618 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001619
1620 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001621#if defined(POLARSSL_ZLIB_SUPPORT)
1622 for( i = 0; i < comp_len; ++i )
1623 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001624 if( buf[comp_offset + 1 + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001625 {
Paul Bakker48916f92012-09-16 19:57:18 +00001626 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001627 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001628 }
1629 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001630#endif
1631
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001632 /* See comments in ssl_write_client_hello() */
1633#if defined(POLARSSL_SSL_PROTO_DTLS)
1634 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1635 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
1636#endif
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001637
Paul Bakkerec636f32012-09-09 19:17:02 +00001638 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001639 * Check the extension length
Paul Bakker48916f92012-09-16 19:57:18 +00001640 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001641 ext_offset = comp_offset + 1 + comp_len;
1642 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001643 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001644 if( msg_len < ext_offset + 2 )
Paul Bakker48916f92012-09-16 19:57:18 +00001645 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001646 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1647 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1648 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001649
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001650 ext_len = ( buf[ext_offset + 0] << 8 )
1651 | ( buf[ext_offset + 1] );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001652
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001653 if( ( ext_len > 0 && ext_len < 4 ) ||
1654 msg_len != ext_offset + 2 + ext_len )
1655 {
1656 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1657 SSL_DEBUG_BUF( 3, "client hello extensions",
1658 buf + ext_offset + 2, ext_len );
1659 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +00001660 }
1661 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001662 else
1663 ext_len = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001664
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001665 ext = buf + ext_offset + 2;
Paul Bakker48916f92012-09-16 19:57:18 +00001666
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001667 while( ext_len != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001668 {
1669 unsigned int ext_id = ( ( ext[0] << 8 )
1670 | ( ext[1] ) );
1671 unsigned int ext_size = ( ( ext[2] << 8 )
1672 | ( ext[3] ) );
1673
1674 if( ext_size + 4 > ext_len )
1675 {
1676 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1677 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1678 }
1679 switch( ext_id )
1680 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001681#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001682 case TLS_EXT_SERVERNAME:
1683 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1684 if( ssl->f_sni == NULL )
1685 break;
1686
1687 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1688 if( ret != 0 )
1689 return( ret );
1690 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001691#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001692
Paul Bakker48916f92012-09-16 19:57:18 +00001693 case TLS_EXT_RENEGOTIATION_INFO:
1694 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1695 renegotiation_info_seen = 1;
1696
Paul Bakker23f36802012-09-28 14:15:14 +00001697 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1698 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001699 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001700 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001701
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001702#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001703 case TLS_EXT_SIG_ALG:
1704 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1705 if( ssl->renegotiation == SSL_RENEGOTIATION )
1706 break;
1707
1708 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1709 if( ret != 0 )
1710 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001711 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001712#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001713
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001714#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001715 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1716 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1717
1718 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1719 if( ret != 0 )
1720 return( ret );
1721 break;
1722
1723 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1724 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001725 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001726
1727 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1728 if( ret != 0 )
1729 return( ret );
1730 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001731#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001732
Paul Bakker05decb22013-08-15 13:33:48 +02001733#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001734 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1735 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1736
1737 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1738 if( ret != 0 )
1739 return( ret );
1740 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001741#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001742
Paul Bakker1f2bc622013-08-15 13:45:55 +02001743#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001744 case TLS_EXT_TRUNCATED_HMAC:
1745 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1746
1747 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1748 if( ret != 0 )
1749 return( ret );
1750 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001751#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001752
Paul Bakkera503a632013-08-14 13:48:06 +02001753#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001754 case TLS_EXT_SESSION_TICKET:
1755 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1756
1757 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1758 if( ret != 0 )
1759 return( ret );
1760 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001761#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001762
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001763#if defined(POLARSSL_SSL_ALPN)
1764 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001765 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001766
1767 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1768 if( ret != 0 )
1769 return( ret );
1770 break;
1771#endif /* POLARSSL_SSL_SESSION_TICKETS */
1772
Paul Bakker48916f92012-09-16 19:57:18 +00001773 default:
1774 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1775 ext_id ) );
1776 }
1777
1778 ext_len -= 4 + ext_size;
1779 ext += 4 + ext_size;
1780
1781 if( ext_len > 0 && ext_len < 4 )
1782 {
1783 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1784 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1785 }
1786 }
1787
1788 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001789 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1790 */
1791 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
1792 {
1793 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1794 {
1795 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1796 if( ssl->renegotiation == SSL_RENEGOTIATION )
1797 {
1798 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1799
1800 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1801 return( ret );
1802
1803 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1804 }
1805 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1806 break;
1807 }
1808 }
1809
1810 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001811 * Renegotiation security checks
1812 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001813 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1814 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1815 {
1816 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1817 handshake_failure = 1;
1818 }
1819 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1820 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1821 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001822 {
1823 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001824 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001825 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001826 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1827 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1828 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001829 {
1830 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001831 handshake_failure = 1;
1832 }
1833 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1834 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1835 renegotiation_info_seen == 1 )
1836 {
1837 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1838 handshake_failure = 1;
1839 }
1840
1841 if( handshake_failure == 1 )
1842 {
1843 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1844 return( ret );
1845
Paul Bakker48916f92012-09-16 19:57:18 +00001846 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1847 }
Paul Bakker380da532012-04-18 16:10:25 +00001848
Paul Bakker41c83d32013-03-20 14:39:14 +01001849 /*
1850 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001851 * (At the end because we need information from the EC-based extensions
1852 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001853 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001854 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001855 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001856#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001857 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001858 {
1859 for( i = 0; ciphersuites[i] != 0; i++ )
1860#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001861 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001862 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001863 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001864#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001865 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001866 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1867 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1868 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001869
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001870 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1871 &ciphersuite_info ) ) != 0 )
1872 return( ret );
1873
1874 if( ciphersuite_info != NULL )
1875 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001876 }
1877 }
1878
1879 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1880
1881 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1882 return( ret );
1883
1884 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1885
1886have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001887 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001888 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1889 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1890
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001891 /* ClientHello can't be bundled with another record in same datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00001892 ssl->in_left = 0;
1893 ssl->state++;
1894
1895 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1896
1897 return( 0 );
1898}
1899
Paul Bakker1f2bc622013-08-15 13:45:55 +02001900#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001901static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1902 unsigned char *buf,
1903 size_t *olen )
1904{
1905 unsigned char *p = buf;
1906
1907 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1908 {
1909 *olen = 0;
1910 return;
1911 }
1912
1913 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1914
1915 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1916 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1917
1918 *p++ = 0x00;
1919 *p++ = 0x00;
1920
1921 *olen = 4;
1922}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001923#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001924
Paul Bakkera503a632013-08-14 13:48:06 +02001925#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001926static void ssl_write_session_ticket_ext( ssl_context *ssl,
1927 unsigned char *buf,
1928 size_t *olen )
1929{
1930 unsigned char *p = buf;
1931
1932 if( ssl->handshake->new_session_ticket == 0 )
1933 {
1934 *olen = 0;
1935 return;
1936 }
1937
1938 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1939
1940 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1941 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1942
1943 *p++ = 0x00;
1944 *p++ = 0x00;
1945
1946 *olen = 4;
1947}
Paul Bakkera503a632013-08-14 13:48:06 +02001948#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001949
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001950static void ssl_write_renegotiation_ext( ssl_context *ssl,
1951 unsigned char *buf,
1952 size_t *olen )
1953{
1954 unsigned char *p = buf;
1955
1956 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1957 {
1958 *olen = 0;
1959 return;
1960 }
1961
1962 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1963
1964 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1965 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1966
1967 *p++ = 0x00;
1968 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1969 *p++ = ssl->verify_data_len * 2 & 0xFF;
1970
1971 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1972 p += ssl->verify_data_len;
1973 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1974 p += ssl->verify_data_len;
1975
1976 *olen = 5 + ssl->verify_data_len * 2;
1977}
1978
Paul Bakker05decb22013-08-15 13:33:48 +02001979#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001980static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1981 unsigned char *buf,
1982 size_t *olen )
1983{
1984 unsigned char *p = buf;
1985
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001986 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1987 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001988 *olen = 0;
1989 return;
1990 }
1991
1992 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1993
1994 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1995 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1996
1997 *p++ = 0x00;
1998 *p++ = 1;
1999
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002000 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002001
2002 *olen = 5;
2003}
Paul Bakker05decb22013-08-15 13:33:48 +02002004#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002005
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002006#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002007static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
2008 unsigned char *buf,
2009 size_t *olen )
2010{
2011 unsigned char *p = buf;
2012 ((void) ssl);
2013
Paul Bakker677377f2013-10-28 12:54:26 +01002014 if( ( ssl->handshake->cli_exts &
2015 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
2016 {
2017 *olen = 0;
2018 return;
2019 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002020
2021 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
2022
2023 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2024 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
2025
2026 *p++ = 0x00;
2027 *p++ = 2;
2028
2029 *p++ = 1;
2030 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
2031
2032 *olen = 6;
2033}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002034#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002035
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002036#if defined(POLARSSL_SSL_ALPN )
2037static void ssl_write_alpn_ext( ssl_context *ssl,
2038 unsigned char *buf, size_t *olen )
2039{
2040 if( ssl->alpn_chosen == NULL )
2041 {
2042 *olen = 0;
2043 return;
2044 }
2045
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02002046 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002047
2048 /*
2049 * 0 . 1 ext identifier
2050 * 2 . 3 ext length
2051 * 4 . 5 protocol list length
2052 * 6 . 6 protocol name length
2053 * 7 . 7+n protocol name
2054 */
2055 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
2056 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
2057
2058 *olen = 7 + strlen( ssl->alpn_chosen );
2059
2060 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2061 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2062
2063 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2064 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2065
2066 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2067
2068 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2069}
2070#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
2071
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002072#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002073static int ssl_write_hello_verify_request( ssl_context *ssl )
2074{
2075 int ret;
2076 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002077 unsigned char *cookie_len_byte;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002078
2079 SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
2080
2081 /*
2082 * struct {
2083 * ProtocolVersion server_version;
2084 * opaque cookie<0..2^8-1>;
2085 * } HelloVerifyRequest;
2086 */
2087
2088 /* For now, use fixed version = DTLS 1.0 */
2089 ssl_write_version( SSL_MAJOR_VERSION_3, SSL_MINOR_VERSION_1,
2090 ssl->transport, p );
2091 SSL_DEBUG_BUF( 3, "server version", (unsigned char *) p, 2 );
2092 p += 2;
2093
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002094 /* Skip length byte until we know the length */
2095 cookie_len_byte = p++;
2096
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002097 if( ( ret = ssl->f_cookie_write( ssl->p_cookie,
2098 &p, ssl->out_buf + SSL_BUFFER_LEN,
2099 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002100 {
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002101 SSL_DEBUG_RET( 1, "f_cookie_write", ret );
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002102 return( ret );
2103 }
2104
2105 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2106
2107 SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002108
2109 ssl->out_msglen = p - ssl->out_msg;
2110 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2111 ssl->out_msg[0] = SSL_HS_HELLO_VERIFY_REQUEST;
2112
2113 ssl->state = SSL_CLIENT_HELLO;
2114
2115 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2116 {
2117 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2118 return( ret );
2119 }
2120
2121 SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
2122
2123 return( 0 );
2124}
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002125#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002126
Paul Bakker5121ce52009-01-03 21:22:43 +00002127static int ssl_write_server_hello( ssl_context *ssl )
2128{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002129#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002130 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002131#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002132 int ret;
2133 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002134 unsigned char *buf, *p;
2135
2136 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
2137
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002138#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002139 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002140 ssl->handshake->verify_cookie_len != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002141 {
2142 SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2143 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2144
2145 if( ( ret = ssl_write_hello_verify_request( ssl ) ) != 0 )
2146 {
2147 SSL_DEBUG_RET( 1, "ssl_write_hello_verify_request", ret );
2148 return( ret );
2149 }
2150
2151 return( POLARSSL_ERR_SSL_HELLO_VERIFY_REQUIRED );
2152 }
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002153#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002154
Paul Bakkera9a028e2013-11-21 17:31:06 +01002155 if( ssl->f_rng == NULL )
2156 {
2157 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2158 return( POLARSSL_ERR_SSL_NO_RNG );
2159 }
2160
Paul Bakker5121ce52009-01-03 21:22:43 +00002161 /*
2162 * 0 . 0 handshake type
2163 * 1 . 3 handshake length
2164 * 4 . 5 protocol version
2165 * 6 . 9 UNIX time()
2166 * 10 . 37 random bytes
2167 */
2168 buf = ssl->out_msg;
2169 p = buf + 4;
2170
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002171 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2172 ssl->transport, p );
2173 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002174
2175 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002176 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002177
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002178#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002179 t = time( NULL );
2180 *p++ = (unsigned char)( t >> 24 );
2181 *p++ = (unsigned char)( t >> 16 );
2182 *p++ = (unsigned char)( t >> 8 );
2183 *p++ = (unsigned char)( t );
2184
2185 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002186#else
2187 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
2188 return( ret );
2189
2190 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02002191#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002192
Paul Bakkera3d195c2011-11-27 21:07:34 +00002193 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
2194 return( ret );
2195
2196 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002197
Paul Bakker48916f92012-09-16 19:57:18 +00002198 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002199
2200 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
2201
2202 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002203 * Resume is 0 by default, see ssl_handshake_init().
2204 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2205 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002206 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002207 if( ssl->handshake->resume == 0 &&
2208 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002209 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002210 ssl->f_get_cache != NULL &&
2211 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
2212 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01002213 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002214 ssl->handshake->resume = 1;
2215 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002216
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002217 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002218 {
2219 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002220 * New session, create a new session id,
2221 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002222 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002223 ssl->state++;
2224
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002225#if defined(POLARSSL_HAVE_TIME)
2226 ssl->session_negotiate->start = time( NULL );
2227#endif
2228
Paul Bakkera503a632013-08-14 13:48:06 +02002229#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002230 if( ssl->handshake->new_session_ticket != 0 )
2231 {
2232 ssl->session_negotiate->length = n = 0;
2233 memset( ssl->session_negotiate->id, 0, 32 );
2234 }
2235 else
2236#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002237 {
2238 ssl->session_negotiate->length = n = 32;
2239 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002240 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002241 return( ret );
2242 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002243 }
2244 else
2245 {
2246 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002247 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002248 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02002249 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002250 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002251
2252 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2253 {
2254 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2255 return( ret );
2256 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002257 }
2258
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002259 /*
2260 * 38 . 38 session id length
2261 * 39 . 38+n session id
2262 * 39+n . 40+n chosen ciphersuite
2263 * 41+n . 41+n chosen compression alg.
2264 * 42+n . 43+n extensions length
2265 * 44+n . 43+n+m extensions
2266 */
2267 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00002268 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
2269 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002270
2271 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2272 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2273 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002274 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002275
Paul Bakker48916f92012-09-16 19:57:18 +00002276 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2277 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2278 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002279
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02002280 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2281 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02002282 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002283 ssl->session_negotiate->compression ) );
2284
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002285 /*
2286 * First write extensions, then the total length
2287 */
2288 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2289 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002290
Paul Bakker05decb22013-08-15 13:33:48 +02002291#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002292 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2293 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002294#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002295
Paul Bakker1f2bc622013-08-15 13:45:55 +02002296#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002297 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2298 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002299#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002300
Paul Bakkera503a632013-08-14 13:48:06 +02002301#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002302 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2303 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002304#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002305
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002306#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002307 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2308 ext_len += olen;
2309#endif
2310
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002311#if defined(POLARSSL_SSL_ALPN)
2312 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2313 ext_len += olen;
2314#endif
2315
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002316 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002317
Paul Bakkera7036632014-04-30 10:15:38 +02002318 if( ext_len > 0 )
2319 {
2320 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2321 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2322 p += ext_len;
2323 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002324
2325 ssl->out_msglen = p - buf;
2326 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2327 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
2328
2329 ret = ssl_write_record( ssl );
2330
2331 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2332
2333 return( ret );
2334}
2335
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002336#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2337 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002338 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2339 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002340static int ssl_write_certificate_request( ssl_context *ssl )
2341{
Paul Bakkered27a042013-04-18 22:46:23 +02002342 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002343
2344 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2345
2346 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002347 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002348 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2349 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002350 {
2351 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2352 ssl->state++;
2353 return( 0 );
2354 }
2355
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002356 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2357 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002358}
2359#else
2360static int ssl_write_certificate_request( ssl_context *ssl )
2361{
2362 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2363 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002364 size_t dn_size, total_dn_size; /* excluding length bytes */
2365 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002366 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002367 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00002368
2369 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2370
2371 ssl->state++;
2372
Paul Bakkerfbb17802013-04-17 19:10:21 +02002373 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002374 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002375 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002376 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002377 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002378 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002379 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002380 return( 0 );
2381 }
2382
2383 /*
2384 * 0 . 0 handshake type
2385 * 1 . 3 handshake length
2386 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002387 * 5 .. m-1 cert types
2388 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002389 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002390 * n .. n+1 length of all DNs
2391 * n+2 .. n+3 length of DN 1
2392 * n+4 .. ... Distinguished Name #1
2393 * ... .. ... length of DN 2, etc.
2394 */
2395 buf = ssl->out_msg;
2396 p = buf + 4;
2397
2398 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002399 * Supported certificate types
2400 *
2401 * ClientCertificateType certificate_types<1..2^8-1>;
2402 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002403 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002404 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002405
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002406#if defined(POLARSSL_RSA_C)
2407 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2408#endif
2409#if defined(POLARSSL_ECDSA_C)
2410 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2411#endif
2412
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002413 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002414 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002415
Paul Bakker577e0062013-08-28 11:57:20 +02002416 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002417#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002418 /*
2419 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002420 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002421 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2422 *
2423 * struct {
2424 * HashAlgorithm hash;
2425 * SignatureAlgorithm signature;
2426 * } SignatureAndHashAlgorithm;
2427 *
2428 * enum { (255) } HashAlgorithm;
2429 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002430 */
Paul Bakker21dca692013-01-03 11:41:08 +01002431 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002432 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002433 /*
2434 * Only use current running hash algorithm that is already required
2435 * for requested ciphersuite.
2436 */
Paul Bakker926af752012-11-23 13:38:07 +01002437 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2438
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002439 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2440 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002441 {
2442 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2443 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002444
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002445 /*
2446 * Supported signature algorithms
2447 */
2448#if defined(POLARSSL_RSA_C)
2449 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2450 p[2 + sa_len++] = SSL_SIG_RSA;
2451#endif
2452#if defined(POLARSSL_ECDSA_C)
2453 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2454 p[2 + sa_len++] = SSL_SIG_ECDSA;
2455#endif
Paul Bakker926af752012-11-23 13:38:07 +01002456
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002457 p[0] = (unsigned char)( sa_len >> 8 );
2458 p[1] = (unsigned char)( sa_len );
2459 sa_len += 2;
2460 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002461 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002462#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002463
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002464 /*
2465 * DistinguishedName certificate_authorities<0..2^16-1>;
2466 * opaque DistinguishedName<1..2^16-1>;
2467 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002468 p += 2;
2469 crt = ssl->ca_chain;
2470
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002471 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002472 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002473 {
2474 if( p - buf > 4096 )
2475 break;
2476
Paul Bakker926af752012-11-23 13:38:07 +01002477 dn_size = crt->subject_raw.len;
2478 *p++ = (unsigned char)( dn_size >> 8 );
2479 *p++ = (unsigned char)( dn_size );
2480 memcpy( p, crt->subject_raw.p, dn_size );
2481 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002482
Paul Bakker926af752012-11-23 13:38:07 +01002483 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2484
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002485 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002486 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002487 }
2488
Paul Bakker926af752012-11-23 13:38:07 +01002489 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002490 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2491 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002492 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2493 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002494
2495 ret = ssl_write_record( ssl );
2496
2497 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2498
2499 return( ret );
2500}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002501#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2502 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002503 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2504 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002505
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002506#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2507 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2508static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2509{
2510 int ret;
2511
2512 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2513 {
2514 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2515 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2516 }
2517
2518 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2519 pk_ec( *ssl_own_key( ssl ) ),
2520 POLARSSL_ECDH_OURS ) ) != 0 )
2521 {
2522 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2523 return( ret );
2524 }
2525
2526 return( 0 );
2527}
2528#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2529 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2530
Paul Bakker41c83d32013-03-20 14:39:14 +01002531static int ssl_write_server_key_exchange( ssl_context *ssl )
2532{
Paul Bakker23986e52011-04-24 08:57:21 +00002533 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002534 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002535 const ssl_ciphersuite_t *ciphersuite_info =
2536 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002537
2538#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2539 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2540 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002541 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002542 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002543 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002544 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002545 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002546 ((void) dig_signed);
2547 ((void) dig_signed_len);
2548#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002549
Paul Bakker5121ce52009-01-03 21:22:43 +00002550 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2551
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002552#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2553 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2554 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002555 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2556 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2557 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002558 {
2559 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2560 ssl->state++;
2561 return( 0 );
2562 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002563#endif
2564
2565#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2566 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2567 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2568 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2569 {
2570 ssl_get_ecdh_params_from_cert( ssl );
2571
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002572 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002573 ssl->state++;
2574 return( 0 );
2575 }
2576#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002577
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002578#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2579 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2580 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2581 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002582 {
2583 /* TODO: Support identity hints */
2584 *(p++) = 0x00;
2585 *(p++) = 0x00;
2586
2587 n += 2;
2588 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002589#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2590 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002591
2592#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2593 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2594 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2595 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002596 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002597 /*
2598 * Ephemeral DH parameters:
2599 *
2600 * struct {
2601 * opaque dh_p<1..2^16-1>;
2602 * opaque dh_g<1..2^16-1>;
2603 * opaque dh_Ys<1..2^16-1>;
2604 * } ServerDHParams;
2605 */
2606 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2607 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2608 {
2609 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2610 return( ret );
2611 }
Paul Bakker48916f92012-09-16 19:57:18 +00002612
Paul Bakker41c83d32013-03-20 14:39:14 +01002613 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002614 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2615 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002616 {
2617 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2618 return( ret );
2619 }
2620
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002621 dig_signed = p;
2622 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002623
2624 p += len;
2625 n += len;
2626
Paul Bakker41c83d32013-03-20 14:39:14 +01002627 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2628 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2629 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2630 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2631 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002632#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2633 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002634
Gergely Budai987bfb52014-01-19 21:48:42 +01002635#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002636 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002637 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2638 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002639 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002640 /*
2641 * Ephemeral ECDH parameters:
2642 *
2643 * struct {
2644 * ECParameters curve_params;
2645 * ECPoint public;
2646 * } ServerECDHParams;
2647 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002648 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002649#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002650 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002651
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002652 /* Match our preference list against the offered curves */
2653 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2654 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2655 if( (*curve)->grp_id == *gid )
2656 goto curve_matching_done;
2657
2658curve_matching_done:
2659#else
2660 curve = ssl->handshake->curves;
2661#endif
2662
2663 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002664 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002665 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2666 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002667 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002668
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002669 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002670
Paul Bakker41c83d32013-03-20 14:39:14 +01002671 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002672 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002673 {
2674 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2675 return( ret );
2676 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002677
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002678 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2679 p, SSL_MAX_CONTENT_LEN - n,
2680 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002681 {
2682 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2683 return( ret );
2684 }
2685
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002686 dig_signed = p;
2687 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002688
2689 p += len;
2690 n += len;
2691
Paul Bakker41c83d32013-03-20 14:39:14 +01002692 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2693 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002694#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002695
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002696#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002697 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2698 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002699 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002700 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2701 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002702 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002703 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002704 unsigned int hashlen = 0;
2705 unsigned char hash[64];
2706 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002707
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002708 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002709 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2710 */
Paul Bakker577e0062013-08-28 11:57:20 +02002711#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002712 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2713 {
2714 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2715
2716 if( md_alg == POLARSSL_MD_NONE )
2717 {
2718 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002719 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002720 }
2721 }
Paul Bakker577e0062013-08-28 11:57:20 +02002722 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002723#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002724#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2725 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002726 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002727 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2728 {
2729 md_alg = POLARSSL_MD_SHA1;
2730 }
2731 else
Paul Bakker577e0062013-08-28 11:57:20 +02002732#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002733 {
2734 md_alg = POLARSSL_MD_NONE;
2735 }
2736
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002737 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002738 * Compute the hash to be signed
2739 */
Paul Bakker577e0062013-08-28 11:57:20 +02002740#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2741 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002742 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002743 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002744 md5_context md5;
2745 sha1_context sha1;
2746
Paul Bakker5b4af392014-06-26 12:09:34 +02002747 md5_init( &md5 );
2748 sha1_init( &sha1 );
2749
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002750 /*
2751 * digitally-signed struct {
2752 * opaque md5_hash[16];
2753 * opaque sha_hash[20];
2754 * };
2755 *
2756 * md5_hash
2757 * MD5(ClientHello.random + ServerHello.random
2758 * + ServerParams);
2759 * sha_hash
2760 * SHA(ClientHello.random + ServerHello.random
2761 * + ServerParams);
2762 */
2763 md5_starts( &md5 );
2764 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002765 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002766 md5_finish( &md5, hash );
2767
2768 sha1_starts( &sha1 );
2769 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002770 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002771 sha1_finish( &sha1, hash + 16 );
2772
2773 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02002774
2775 md5_free( &md5 );
2776 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002777 }
2778 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002779#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2780 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002781#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2782 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002783 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002784 {
2785 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02002786 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002787
Paul Bakker84bbeb52014-07-01 14:53:22 +02002788 md_init( &ctx );
2789
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002790 /* Info from md_alg will be used instead */
2791 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002792
2793 /*
2794 * digitally-signed struct {
2795 * opaque client_random[32];
2796 * opaque server_random[32];
2797 * ServerDHParams params;
2798 * };
2799 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002800 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002801 {
2802 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2803 return( ret );
2804 }
2805
2806 md_starts( &ctx );
2807 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002808 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002809 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002810 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00002811 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002812 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002813#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2814 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002815 {
2816 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002817 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002818 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002819
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002820 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2821 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002822
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002823 /*
2824 * Make the signature
2825 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002826 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002827 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002828 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2829 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002830 }
Paul Bakker23f36802012-09-28 14:15:14 +00002831
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002832#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002833 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2834 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002835 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002836 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002837
2838 n += 2;
2839 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002840#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002841
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002842 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002843 p + 2 , &signature_len,
2844 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002845 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002846 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002847 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002848 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002849
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002850 *(p++) = (unsigned char)( signature_len >> 8 );
2851 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002852 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002853
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002854 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002855
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002856 p += signature_len;
2857 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002858 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002859#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002860 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2861 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002862
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002863 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002864 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2865 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2866
2867 ssl->state++;
2868
2869 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2870 {
2871 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2872 return( ret );
2873 }
2874
2875 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2876
2877 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002878}
2879
2880static int ssl_write_server_hello_done( ssl_context *ssl )
2881{
2882 int ret;
2883
2884 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2885
2886 ssl->out_msglen = 4;
2887 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2888 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2889
2890 ssl->state++;
2891
2892 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2893 {
2894 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2895 return( ret );
2896 }
2897
2898 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2899
2900 return( 0 );
2901}
2902
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002903#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2904 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2905static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2906 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002907{
2908 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002909 size_t n;
2910
2911 /*
2912 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2913 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002914 if( *p + 2 > end )
2915 {
2916 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2917 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2918 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002919
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002920 n = ( (*p)[0] << 8 ) | (*p)[1];
2921 *p += 2;
2922
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002923 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002924 {
2925 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2926 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2927 }
2928
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002929 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002930 {
2931 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2932 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2933 }
2934
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002935 *p += n;
2936
Paul Bakker70df2fb2013-04-17 17:19:09 +02002937 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2938
Paul Bakker70df2fb2013-04-17 17:19:09 +02002939 return( ret );
2940}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002941#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2942 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002943
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002944#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2945 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002946static int ssl_parse_encrypted_pms( ssl_context *ssl,
2947 const unsigned char *p,
2948 const unsigned char *end,
2949 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002950{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002951 int ret;
2952 size_t len = pk_get_len( ssl_own_key( ssl ) );
2953 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002954 unsigned char ver[2];
Paul Bakker70df2fb2013-04-17 17:19:09 +02002955
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002956 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002957 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002958 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002959 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2960 }
2961
2962 /*
2963 * Decrypt the premaster using own private RSA key
2964 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002965#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2966 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002967 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2968 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002969 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2970 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002971 {
2972 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2973 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2974 }
2975 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002976#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002977
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002978 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002979 {
2980 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2981 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2982 }
2983
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002984 ssl_write_version( ssl->handshake->max_major_ver,
2985 ssl->handshake->max_minor_ver,
2986 ssl->transport, ver );
2987
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002988 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2989 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002990 sizeof( ssl->handshake->premaster ) - pms_offset,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002991 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002992
2993 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002994 pms[0] != ver[0] ||
2995 pms[1] != ver[1] )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002996 {
2997 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2998
2999 /*
3000 * Protection against Bleichenbacher's attack:
3001 * invalid PKCS#1 v1.5 padding must not cause
3002 * the connection to end immediately; instead,
3003 * send a bad_record_mac later in the handshake.
3004 */
3005 ssl->handshake->pmslen = 48;
3006
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003007 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003008 if( ret != 0 )
3009 return( ret );
3010 }
3011
3012 return( ret );
3013}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003014#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
3015 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003016
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003017#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003018static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
3019 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003020{
Paul Bakker6db455e2013-09-18 17:29:31 +02003021 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003022 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003023
Paul Bakker6db455e2013-09-18 17:29:31 +02003024 if( ssl->f_psk == NULL &&
3025 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
3026 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003027 {
3028 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
3029 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3030 }
3031
3032 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003033 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003034 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003035 if( *p + 2 > end )
3036 {
3037 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3038 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3039 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003040
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003041 n = ( (*p)[0] << 8 ) | (*p)[1];
3042 *p += 2;
3043
3044 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003045 {
3046 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3047 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3048 }
3049
Paul Bakker6db455e2013-09-18 17:29:31 +02003050 if( ssl->f_psk != NULL )
3051 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003052 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003053 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3054 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003055 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003056 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003057 /* Identity is not a big secret since clients send it in the clear,
3058 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02003059 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003060 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003061 {
3062 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3063 }
3064 }
3065
3066 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003067 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003068 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02003069 if( ( ret = ssl_send_alert_message( ssl,
3070 SSL_ALERT_LEVEL_FATAL,
3071 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
3072 {
3073 return( ret );
3074 }
3075
3076 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003077 }
3078
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003079 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003080
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003081 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003082}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003083#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003084
Paul Bakker5121ce52009-01-03 21:22:43 +00003085static int ssl_parse_client_key_exchange( ssl_context *ssl )
3086{
Paul Bakker23986e52011-04-24 08:57:21 +00003087 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003088 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003089
Paul Bakker41c83d32013-03-20 14:39:14 +01003090 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003091
3092 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
3093
3094 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3095 {
3096 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3097 return( ret );
3098 }
3099
3100 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3101 {
3102 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003103 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003104 }
3105
3106 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
3107 {
3108 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003109 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003110 }
3111
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003112#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01003113 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003114 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003115 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003116 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003117
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003118 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003119 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02003120 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3121 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003122 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003123
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003124 if( p != end )
3125 {
3126 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3127 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3128 }
3129
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02003130 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003131
3132 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
3133 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003134 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02003135 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003136 {
3137 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
3138 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3139 }
3140
3141 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003142 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003143 else
3144#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003145#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003146 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3147 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3148 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003149 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003150 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
3151 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
3152 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003153 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003154 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003155 ssl->in_msg + 4, ssl->in_hslen - 4 ) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003156 {
3157 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3158 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3159 }
3160
3161 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3162
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003163 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
3164 &ssl->handshake->pmslen,
3165 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02003166 POLARSSL_MPI_MAX_SIZE,
3167 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003168 {
3169 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
3170 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3171 }
3172
3173 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003174 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003175 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003176#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003177 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3178 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3179 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003180#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
3181 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003182 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003183 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003184 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003185
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003186 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003187 {
3188 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3189 return( ret );
3190 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003191
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003192 if( p != end )
3193 {
3194 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3195 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3196 }
3197
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003198 if( ( ret = ssl_psk_derive_premaster( ssl,
3199 ciphersuite_info->key_exchange ) ) != 0 )
3200 {
3201 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3202 return( ret );
3203 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003204 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003205 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003206#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003207#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
3208 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
3209 {
3210 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003211 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003212
3213 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3214 {
3215 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3216 return( ret );
3217 }
3218
3219 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3220 {
3221 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
3222 return( ret );
3223 }
3224
3225 if( ( ret = ssl_psk_derive_premaster( ssl,
3226 ciphersuite_info->key_exchange ) ) != 0 )
3227 {
3228 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3229 return( ret );
3230 }
3231 }
3232 else
3233#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003234#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3235 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3236 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003237 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003238 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003239
3240 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3241 {
3242 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3243 return( ret );
3244 }
3245 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3246 {
3247 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3248 return( ret );
3249 }
3250
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003251 if( p != end )
3252 {
3253 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3254 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3255 }
3256
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003257 if( ( ret = ssl_psk_derive_premaster( ssl,
3258 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003259 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003260 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3261 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003262 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003263 }
3264 else
3265#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003266#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3267 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
3268 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003269 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003270 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003271
3272 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3273 {
3274 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3275 return( ret );
3276 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003277
3278 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
3279 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003280 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003281 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3282 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003283 }
3284
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003285 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3286
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003287 if( ( ret = ssl_psk_derive_premaster( ssl,
3288 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003289 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003290 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003291 return( ret );
3292 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003293 }
3294 else
3295#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003296#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
3297 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003298 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003299 if( ( ret = ssl_parse_encrypted_pms( ssl,
3300 ssl->in_msg + 4,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003301 ssl->in_msg + ssl->in_hslen,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003302 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003303 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003304 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003305 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003306 }
3307 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003308 else
3309#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
3310 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003311 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003312 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003313 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003314
Paul Bakkerff60ee62010-03-16 21:09:09 +00003315 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
3316 {
3317 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
3318 return( ret );
3319 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003320
Paul Bakker5121ce52009-01-03 21:22:43 +00003321 ssl->state++;
3322
3323 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3324
3325 return( 0 );
3326}
3327
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003328#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3329 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003330 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3331 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003332static int ssl_parse_certificate_verify( ssl_context *ssl )
3333{
Paul Bakkerfbb17802013-04-17 19:10:21 +02003334 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003335
3336 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3337
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003338 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003339 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003340 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003341 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02003342 {
3343 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3344 ssl->state++;
3345 return( 0 );
3346 }
3347
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003348 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3349 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003350}
3351#else
3352static int ssl_parse_certificate_verify( ssl_context *ssl )
3353{
3354 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003355 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003356 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003357 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003358 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003359#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003360 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003361#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003362 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003363 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3364
3365 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3366
3367 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003368 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003369 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003370 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3371 {
3372 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3373 ssl->state++;
3374 return( 0 );
3375 }
3376
Paul Bakkered27a042013-04-18 22:46:23 +02003377 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003378 {
3379 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3380 ssl->state++;
3381 return( 0 );
3382 }
3383
Paul Bakker48916f92012-09-16 19:57:18 +00003384 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003385
3386 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3387 {
3388 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3389 return( ret );
3390 }
3391
3392 ssl->state++;
3393
3394 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3395 {
3396 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003397 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003398 }
3399
3400 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
3401 {
3402 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003403 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003404 }
3405
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003406 /*
3407 * 0 . 0 handshake type
3408 * 1 . 3 handshake length
3409 * 4 . 5 sig alg (TLS 1.2 only)
3410 * 4+n . 5+n signature length (n = sa_len)
3411 * 6+n . 6+n+m signature (m = sig_len)
3412 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003413
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003414#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3415 defined(POLARSSL_SSL_PROTO_TLS1_1)
3416 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003417 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003418 sa_len = 0;
3419
Paul Bakkerc70b9822013-04-07 22:00:46 +02003420 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003421 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003422
3423 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3424 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3425 POLARSSL_PK_ECDSA ) )
3426 {
3427 hash_start += 16;
3428 hashlen -= 16;
3429 md_alg = POLARSSL_MD_SHA1;
3430 }
Paul Bakker926af752012-11-23 13:38:07 +01003431 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003432 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003433#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3434 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003435#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3436 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003437 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003438 sa_len = 2;
3439
Paul Bakker5121ce52009-01-03 21:22:43 +00003440 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003441 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003442 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003443 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003444 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003445 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3446 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003447 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3448 }
3449
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003450 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003451
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003452 /* Info from md_alg will be used instead */
3453 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003454
3455 /*
3456 * Signature
3457 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003458 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
3459 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003460 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003461 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3462 " for verify message" ) );
3463 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003464 }
3465
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003466 /*
3467 * Check the certificate's key type matches the signature alg
3468 */
3469 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3470 {
3471 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3472 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3473 }
Paul Bakker577e0062013-08-28 11:57:20 +02003474 }
3475 else
3476#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3477 {
3478 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003479 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003480 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003481
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003482 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01003483
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003484 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003485 {
3486 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003487 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003488 }
3489
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003490 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003491 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003492 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003493 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003494 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003495 return( ret );
3496 }
3497
3498 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3499
Paul Bakkered27a042013-04-18 22:46:23 +02003500 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003501}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003502#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3503 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3504 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003505
Paul Bakkera503a632013-08-14 13:48:06 +02003506#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003507static int ssl_write_new_session_ticket( ssl_context *ssl )
3508{
3509 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003510 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003511 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003512
3513 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3514
3515 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3516 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3517
3518 /*
3519 * struct {
3520 * uint32 ticket_lifetime_hint;
3521 * opaque ticket<0..2^16-1>;
3522 * } NewSessionTicket;
3523 *
3524 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3525 * 8 . 9 ticket_len (n)
3526 * 10 . 9+n ticket content
3527 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003528
3529 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3530 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3531 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3532 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003533
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003534 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3535 {
3536 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3537 tlen = 0;
3538 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003539
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003540 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3541 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003542
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003543 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003544
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003545 /*
3546 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3547 * ChangeCipherSpec share the same state.
3548 */
3549 ssl->handshake->new_session_ticket = 0;
3550
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003551 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3552 {
3553 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3554 return( ret );
3555 }
3556
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003557 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3558
3559 return( 0 );
3560}
Paul Bakkera503a632013-08-14 13:48:06 +02003561#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003562
Paul Bakker5121ce52009-01-03 21:22:43 +00003563/*
Paul Bakker1961b702013-01-25 14:49:24 +01003564 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003565 */
Paul Bakker1961b702013-01-25 14:49:24 +01003566int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003567{
3568 int ret = 0;
3569
Paul Bakker1961b702013-01-25 14:49:24 +01003570 if( ssl->state == SSL_HANDSHAKE_OVER )
3571 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003572
Paul Bakker1961b702013-01-25 14:49:24 +01003573 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3574
3575 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3576 return( ret );
3577
3578 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003579 {
Paul Bakker1961b702013-01-25 14:49:24 +01003580 case SSL_HELLO_REQUEST:
3581 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003582 break;
3583
Paul Bakker1961b702013-01-25 14:49:24 +01003584 /*
3585 * <== ClientHello
3586 */
3587 case SSL_CLIENT_HELLO:
3588 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003589 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003590
3591 /*
3592 * ==> ServerHello
3593 * Certificate
3594 * ( ServerKeyExchange )
3595 * ( CertificateRequest )
3596 * ServerHelloDone
3597 */
3598 case SSL_SERVER_HELLO:
3599 ret = ssl_write_server_hello( ssl );
3600 break;
3601
3602 case SSL_SERVER_CERTIFICATE:
3603 ret = ssl_write_certificate( ssl );
3604 break;
3605
3606 case SSL_SERVER_KEY_EXCHANGE:
3607 ret = ssl_write_server_key_exchange( ssl );
3608 break;
3609
3610 case SSL_CERTIFICATE_REQUEST:
3611 ret = ssl_write_certificate_request( ssl );
3612 break;
3613
3614 case SSL_SERVER_HELLO_DONE:
3615 ret = ssl_write_server_hello_done( ssl );
3616 break;
3617
3618 /*
3619 * <== ( Certificate/Alert )
3620 * ClientKeyExchange
3621 * ( CertificateVerify )
3622 * ChangeCipherSpec
3623 * Finished
3624 */
3625 case SSL_CLIENT_CERTIFICATE:
3626 ret = ssl_parse_certificate( ssl );
3627 break;
3628
3629 case SSL_CLIENT_KEY_EXCHANGE:
3630 ret = ssl_parse_client_key_exchange( ssl );
3631 break;
3632
3633 case SSL_CERTIFICATE_VERIFY:
3634 ret = ssl_parse_certificate_verify( ssl );
3635 break;
3636
3637 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3638 ret = ssl_parse_change_cipher_spec( ssl );
3639 break;
3640
3641 case SSL_CLIENT_FINISHED:
3642 ret = ssl_parse_finished( ssl );
3643 break;
3644
3645 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003646 * ==> ( NewSessionTicket )
3647 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003648 * Finished
3649 */
3650 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003651#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003652 if( ssl->handshake->new_session_ticket != 0 )
3653 ret = ssl_write_new_session_ticket( ssl );
3654 else
Paul Bakkera503a632013-08-14 13:48:06 +02003655#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003656 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003657 break;
3658
3659 case SSL_SERVER_FINISHED:
3660 ret = ssl_write_finished( ssl );
3661 break;
3662
3663 case SSL_FLUSH_BUFFERS:
3664 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3665 ssl->state = SSL_HANDSHAKE_WRAPUP;
3666 break;
3667
3668 case SSL_HANDSHAKE_WRAPUP:
3669 ssl_handshake_wrapup( ssl );
3670 break;
3671
3672 default:
3673 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3674 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003675 }
3676
Paul Bakker5121ce52009-01-03 21:22:43 +00003677 return( ret );
3678}
Paul Bakker9af723c2014-05-01 13:03:14 +02003679#endif /* POLARSSL_SSL_SRV_C */