blob: 8c8fa2cbf7d403c872153bb3c622fae8596a0f1f [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,
Paul Bakker66d5d072014-06-17 16:39:18 +0200215 SSL_MAX_CONTENT_LEN - ( state - ssl->out_ctr ) - 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 Bakker34617722014-06-13 17:20:13 +0200346 polarssl_zeroize( &session, sizeof( ssl_session ) );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200347
348 return( 0 );
349}
Paul Bakkera503a632013-08-14 13:48:06 +0200350#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200351
Paul Bakker0be444a2013-08-27 21:55:01 +0200352#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200353/*
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200354 * Wrapper around f_sni, allowing use of ssl_set_own_cert() but
355 * making it act on ssl->hanshake->sni_key_cert instead.
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200356 */
357static int ssl_sni_wrapper( ssl_context *ssl,
358 const unsigned char* name, size_t len )
359{
360 int ret;
361 ssl_key_cert *key_cert_ori = ssl->key_cert;
362
363 ssl->key_cert = NULL;
364 ret = ssl->f_sni( ssl->p_sni, ssl, name, len );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200365 ssl->handshake->sni_key_cert = ssl->key_cert;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200366
367 ssl->key_cert = key_cert_ori;
368
369 return( ret );
370}
371
Paul Bakker5701cdc2012-09-27 21:49:42 +0000372static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000373 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000374 size_t len )
375{
376 int ret;
377 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000378 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000379
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100380 SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
381
Paul Bakker5701cdc2012-09-27 21:49:42 +0000382 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
383 if( servername_list_size + 2 != len )
384 {
385 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
386 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
387 }
388
389 p = buf + 2;
390 while( servername_list_size > 0 )
391 {
392 hostname_len = ( ( p[1] << 8 ) | p[2] );
393 if( hostname_len + 3 > servername_list_size )
394 {
395 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
396 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
397 }
398
399 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
400 {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200401 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000402 if( ret != 0 )
403 {
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100404 SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000405 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
406 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
407 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
408 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000409 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000410 }
411
412 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000413 p += hostname_len + 3;
414 }
415
416 if( servername_list_size != 0 )
417 {
418 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
419 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000420 }
421
422 return( 0 );
423}
Paul Bakker0be444a2013-08-27 21:55:01 +0200424#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000425
Paul Bakker48916f92012-09-16 19:57:18 +0000426static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000427 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000428 size_t len )
429{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000430 int ret;
431
Paul Bakker48916f92012-09-16 19:57:18 +0000432 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
433 {
434 if( len != 1 || buf[0] != 0x0 )
435 {
436 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000437
438 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
439 return( ret );
440
Paul Bakker48916f92012-09-16 19:57:18 +0000441 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
442 }
443
444 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
445 }
446 else
447 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100448 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000449 if( len != 1 + ssl->verify_data_len ||
450 buf[0] != ssl->verify_data_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100451 safer_memcmp( buf + 1, ssl->peer_verify_data,
452 ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000453 {
454 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000455
456 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
457 return( ret );
458
Paul Bakker48916f92012-09-16 19:57:18 +0000459 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
460 }
461 }
462
463 return( 0 );
464}
465
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200466#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +0000467static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
468 const unsigned char *buf,
469 size_t len )
470{
471 size_t sig_alg_list_size;
472 const unsigned char *p;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200473 const unsigned char *end = buf + len;
474 const int *md_cur;
475
Paul Bakker23f36802012-09-28 14:15:14 +0000476
477 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
478 if( sig_alg_list_size + 2 != len ||
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200479 sig_alg_list_size % 2 != 0 )
Paul Bakker23f36802012-09-28 14:15:14 +0000480 {
481 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
482 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
483 }
484
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200485 /*
486 * For now, ignore the SignatureAlgorithm part and rely on offered
487 * ciphersuites only for that part. To be fixed later.
488 *
489 * So, just look at the HashAlgorithm part.
490 */
491 for( md_cur = md_list(); *md_cur != POLARSSL_MD_NONE; md_cur++ ) {
492 for( p = buf + 2; p < end; p += 2 ) {
493 if( *md_cur == (int) ssl_md_alg_from_hash( p[0] ) ) {
494 ssl->handshake->sig_alg = p[0];
495 break;
496 }
Paul Bakker23f36802012-09-28 14:15:14 +0000497 }
Paul Bakker23f36802012-09-28 14:15:14 +0000498 }
499
500 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
501 ssl->handshake->sig_alg ) );
502
503 return( 0 );
504}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200505#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker23f36802012-09-28 14:15:14 +0000506
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200507#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200508static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
509 const unsigned char *buf,
510 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100511{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200512 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100513 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200514 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100515
516 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
517 if( list_size + 2 != len ||
518 list_size % 2 != 0 )
519 {
520 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
521 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
522 }
523
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +0100524 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200525 * and leave room for a final 0 */
526 our_size = list_size / 2 + 1;
527 if( our_size > POLARSSL_ECP_DP_MAX )
528 our_size = POLARSSL_ECP_DP_MAX;
529
530 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
531 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
532
Paul Bakker9af723c2014-05-01 13:03:14 +0200533 /* explicit void pointer cast for buggy MS compiler */
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200534 memset( (void *) curves, 0, our_size * sizeof( *curves ) );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200535 ssl->handshake->curves = curves;
536
Paul Bakker41c83d32013-03-20 14:39:14 +0100537 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200538 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100539 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200540 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200541
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200542 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100543 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200544 *curves++ = curve_info;
545 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100546 }
547
548 list_size -= 2;
549 p += 2;
550 }
551
552 return( 0 );
553}
554
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200555static int ssl_parse_supported_point_formats( ssl_context *ssl,
556 const unsigned char *buf,
557 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100558{
559 size_t list_size;
560 const unsigned char *p;
561
562 list_size = buf[0];
563 if( list_size + 1 != len )
564 {
565 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
566 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
567 }
568
569 p = buf + 2;
570 while( list_size > 0 )
571 {
572 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
573 p[0] == POLARSSL_ECP_PF_COMPRESSED )
574 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200575 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200576 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100577 return( 0 );
578 }
579
580 list_size--;
581 p++;
582 }
583
584 return( 0 );
585}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200586#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100587
Paul Bakker05decb22013-08-15 13:33:48 +0200588#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200589static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
590 const unsigned char *buf,
591 size_t len )
592{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200593 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200594 {
595 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
596 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
597 }
598
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200599 ssl->session_negotiate->mfl_code = buf[0];
600
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200601 return( 0 );
602}
Paul Bakker05decb22013-08-15 13:33:48 +0200603#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200604
Paul Bakker1f2bc622013-08-15 13:45:55 +0200605#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200606static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
607 const unsigned char *buf,
608 size_t len )
609{
610 if( len != 0 )
611 {
612 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
613 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
614 }
615
616 ((void) buf);
617
618 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
619
620 return( 0 );
621}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200622#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200623
Paul Bakkera503a632013-08-14 13:48:06 +0200624#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200625static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200626 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200627 size_t len )
628{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200629 int ret;
630
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200631 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
632 return( 0 );
633
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200634 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200635 ssl->handshake->new_session_ticket = 1;
636
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200637 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
638
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200639 if( len == 0 )
640 return( 0 );
641
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200642 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
643 {
644 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
645 return( 0 );
646 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200647
648 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200649 * Failures are ok: just ignore the ticket and proceed.
650 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200651 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
652 {
653 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200654 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200655 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200656
657 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
658
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200659 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200660
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200661 /* Don't send a new ticket after all, this one is OK */
662 ssl->handshake->new_session_ticket = 0;
663
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200664 return( 0 );
665}
Paul Bakkera503a632013-08-14 13:48:06 +0200666#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200667
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200668#if defined(POLARSSL_SSL_ALPN)
669static int ssl_parse_alpn_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200670 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200671{
Paul Bakker14b16c62014-05-28 11:33:54 +0200672 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200673 const unsigned char *theirs, *start, *end;
674 const char **ours;
675
676 /* If ALPN not configured, just ignore the extension */
677 if( ssl->alpn_list == NULL )
678 return( 0 );
679
680 /*
681 * opaque ProtocolName<1..2^8-1>;
682 *
683 * struct {
684 * ProtocolName protocol_name_list<2..2^16-1>
685 * } ProtocolNameList;
686 */
687
688 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
689 if( len < 4 )
690 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
691
692 list_len = ( buf[0] << 8 ) | buf[1];
693 if( list_len != len - 2 )
694 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
695
696 /*
697 * Use our order of preference
698 */
699 start = buf + 2;
700 end = buf + len;
701 for( ours = ssl->alpn_list; *ours != NULL; ours++ )
702 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200703 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200704 for( theirs = start; theirs != end; theirs += cur_len )
705 {
706 /* If the list is well formed, we should get equality first */
707 if( theirs > end )
708 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
709
710 cur_len = *theirs++;
711
712 /* Empty strings MUST NOT be included */
713 if( cur_len == 0 )
714 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
715
Paul Bakker14b16c62014-05-28 11:33:54 +0200716 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200717 memcmp( theirs, *ours, cur_len ) == 0 )
718 {
719 ssl->alpn_chosen = *ours;
720 return( 0 );
721 }
722 }
723 }
724
725 /* If we get there, no match was found */
726 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
727 SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
728 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
729}
730#endif /* POLARSSL_SSL_ALPN */
731
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100732/*
733 * Auxiliary functions for ServerHello parsing and related actions
734 */
735
736#if defined(POLARSSL_X509_CRT_PARSE_C)
737/*
738 * Return 1 if the given EC key uses the given curve, 0 otherwise
739 */
740#if defined(POLARSSL_ECDSA_C)
741static int ssl_key_matches_curves( pk_context *pk,
742 const ecp_curve_info **curves )
743{
744 const ecp_curve_info **crv = curves;
745 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
746
747 while( *crv != NULL )
748 {
749 if( (*crv)->grp_id == grp_id )
750 return( 1 );
751 crv++;
752 }
753
754 return( 0 );
755}
756#endif /* POLARSSL_ECDSA_C */
757
758/*
759 * Try picking a certificate for this ciphersuite,
760 * return 0 on success and -1 on failure.
761 */
762static int ssl_pick_cert( ssl_context *ssl,
763 const ssl_ciphersuite_t * ciphersuite_info )
764{
765 ssl_key_cert *cur, *list;
766 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
767
768#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
769 if( ssl->handshake->sni_key_cert != NULL )
770 list = ssl->handshake->sni_key_cert;
771 else
772#endif
773 list = ssl->handshake->key_cert;
774
775 if( pk_alg == POLARSSL_PK_NONE )
776 return( 0 );
777
778 for( cur = list; cur != NULL; cur = cur->next )
779 {
780 if( ! pk_can_do( cur->key, pk_alg ) )
781 continue;
782
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200783 /*
784 * This avoids sending the client a cert it'll reject based on
785 * keyUsage or other extensions.
786 *
787 * It also allows the user to provision different certificates for
788 * different uses based on keyUsage, eg if they want to avoid signing
789 * and decrypting with the same RSA key.
790 */
791 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
792 SSL_IS_SERVER ) != 0 )
793 {
794 continue;
795 }
796
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100797#if defined(POLARSSL_ECDSA_C)
798 if( pk_alg == POLARSSL_PK_ECDSA )
799 {
800 if( ssl_key_matches_curves( cur->key, ssl->handshake->curves ) )
801 break;
802 }
803 else
804#endif
805 break;
806 }
807
808 if( cur == NULL )
809 return( -1 );
810
811 ssl->handshake->key_cert = cur;
812 return( 0 );
813}
814#endif /* POLARSSL_X509_CRT_PARSE_C */
815
816/*
817 * Check if a given ciphersuite is suitable for use with our config/keys/etc
818 * Sets ciphersuite_info only if the suite matches.
819 */
820static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
821 const ssl_ciphersuite_t **ciphersuite_info )
822{
823 const ssl_ciphersuite_t *suite_info;
824
825 suite_info = ssl_ciphersuite_from_id( suite_id );
826 if( suite_info == NULL )
827 {
828 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", suite_id ) );
829 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
830 }
831
832 if( suite_info->min_minor_ver > ssl->minor_ver ||
833 suite_info->max_minor_ver < ssl->minor_ver )
834 return( 0 );
835
836#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
837 if( ssl_ciphersuite_uses_ec( suite_info ) &&
838 ( ssl->handshake->curves == NULL ||
839 ssl->handshake->curves[0] == NULL ) )
840 return( 0 );
841#endif
842
843#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
844 /* If the ciphersuite requires a pre-shared key and we don't
845 * have one, skip it now rather than failing later */
846 if( ssl_ciphersuite_uses_psk( suite_info ) &&
847 ssl->f_psk == NULL &&
848 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
849 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
850 return( 0 );
851#endif
852
853#if defined(POLARSSL_X509_CRT_PARSE_C)
854 /*
855 * Final check: if ciphersuite requires us to have a
856 * certificate/key of a particular type:
857 * - select the appropriate certificate if we have one, or
858 * - try the next ciphersuite if we don't
859 * This must be done last since we modify the key_cert list.
860 */
861 if( ssl_pick_cert( ssl, suite_info ) != 0 )
862 return( 0 );
863#endif
864
865 *ciphersuite_info = suite_info;
866 return( 0 );
867}
868
Paul Bakker78a8c712013-03-06 17:01:52 +0100869#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
870static int ssl_parse_client_hello_v2( ssl_context *ssl )
871{
872 int ret;
873 unsigned int i, j;
874 size_t n;
875 unsigned int ciph_len, sess_len, chal_len;
876 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200877 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200878 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100879
880 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
881
882 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
883 {
884 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
885
886 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
887 return( ret );
888
889 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
890 }
891
892 buf = ssl->in_hdr;
893
894 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
895
896 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
897 buf[2] ) );
898 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
899 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
900 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
901 buf[3], buf[4] ) );
902
903 /*
904 * SSLv2 Client Hello
905 *
906 * Record layer:
907 * 0 . 1 message length
908 *
909 * SSL layer:
910 * 2 . 2 message type
911 * 3 . 4 protocol version
912 */
913 if( buf[2] != SSL_HS_CLIENT_HELLO ||
914 buf[3] != SSL_MAJOR_VERSION_3 )
915 {
916 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
917 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
918 }
919
920 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
921
922 if( n < 17 || n > 512 )
923 {
924 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
925 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
926 }
927
928 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200929 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
930 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100931
932 if( ssl->minor_ver < ssl->min_minor_ver )
933 {
934 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200935 " [%d:%d] < [%d:%d]",
936 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +0100937 ssl->min_major_ver, ssl->min_minor_ver ) );
938
939 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
940 SSL_ALERT_MSG_PROTOCOL_VERSION );
941 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
942 }
943
Paul Bakker2fbefde2013-06-29 16:01:15 +0200944 ssl->handshake->max_major_ver = buf[3];
945 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100946
947 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
948 {
949 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
950 return( ret );
951 }
952
953 ssl->handshake->update_checksum( ssl, buf + 2, n );
954
955 buf = ssl->in_msg;
956 n = ssl->in_left - 5;
957
958 /*
959 * 0 . 1 ciphersuitelist length
960 * 2 . 3 session id length
961 * 4 . 5 challenge length
962 * 6 . .. ciphersuitelist
963 * .. . .. session id
964 * .. . .. challenge
965 */
966 SSL_DEBUG_BUF( 4, "record contents", buf, n );
967
968 ciph_len = ( buf[0] << 8 ) | buf[1];
969 sess_len = ( buf[2] << 8 ) | buf[3];
970 chal_len = ( buf[4] << 8 ) | buf[5];
971
972 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
973 ciph_len, sess_len, chal_len ) );
974
975 /*
976 * Make sure each parameter length is valid
977 */
978 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
979 {
980 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
981 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
982 }
983
984 if( sess_len > 32 )
985 {
986 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
987 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
988 }
989
990 if( chal_len < 8 || chal_len > 32 )
991 {
992 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
993 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
994 }
995
996 if( n != 6 + ciph_len + sess_len + chal_len )
997 {
998 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
999 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1000 }
1001
1002 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1003 buf + 6, ciph_len );
1004 SSL_DEBUG_BUF( 3, "client hello, session id",
1005 buf + 6 + ciph_len, sess_len );
1006 SSL_DEBUG_BUF( 3, "client hello, challenge",
1007 buf + 6 + ciph_len + sess_len, chal_len );
1008
1009 p = buf + 6 + ciph_len;
1010 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001011 memset( ssl->session_negotiate->id, 0,
1012 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001013 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1014
1015 p += sess_len;
1016 memset( ssl->handshake->randbytes, 0, 64 );
1017 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1018
1019 /*
1020 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1021 */
1022 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1023 {
1024 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1025 {
1026 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1027 if( ssl->renegotiation == SSL_RENEGOTIATION )
1028 {
1029 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1030
1031 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1032 return( ret );
1033
1034 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1035 }
1036 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1037 break;
1038 }
1039 }
1040
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001041 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001042 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001043#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1044 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1045 {
1046 for( i = 0; ciphersuites[i] != 0; i++ )
1047#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001048 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001049 {
1050 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001051#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001052 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001053 if( p[0] != 0 ||
1054 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1055 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1056 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001057
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001058 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1059 &ciphersuite_info ) ) != 0 )
1060 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001061
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001062 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001063 goto have_ciphersuite_v2;
1064 }
1065 }
1066
1067 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1068
1069 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1070
1071have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001072 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001073 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001074 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001075
1076 /*
1077 * SSLv2 Client Hello relevant renegotiation security checks
1078 */
1079 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1080 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1081 {
1082 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1083
1084 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1085 return( ret );
1086
1087 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1088 }
1089
1090 ssl->in_left = 0;
1091 ssl->state++;
1092
1093 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1094
1095 return( 0 );
1096}
1097#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1098
Paul Bakker5121ce52009-01-03 21:22:43 +00001099static int ssl_parse_client_hello( ssl_context *ssl )
1100{
Paul Bakker23986e52011-04-24 08:57:21 +00001101 int ret;
1102 unsigned int i, j;
1103 size_t n;
1104 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001105 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001106 unsigned int ext_len = 0;
1107 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001108 int renegotiation_info_seen = 0;
1109 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001110 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001111 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001112
1113 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1114
Paul Bakker48916f92012-09-16 19:57:18 +00001115 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1116 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001117 {
1118 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1119 return( ret );
1120 }
1121
1122 buf = ssl->in_hdr;
1123
Paul Bakker78a8c712013-03-06 17:01:52 +01001124#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1125 if( ( buf[0] & 0x80 ) != 0 )
1126 return ssl_parse_client_hello_v2( ssl );
1127#endif
1128
Paul Bakkerec636f32012-09-09 19:17:02 +00001129 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1130
1131 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1132 buf[0] ) );
1133 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1134 ( buf[3] << 8 ) | buf[4] ) );
1135 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
1136 buf[1], buf[2] ) );
1137
1138 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001139 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001140 *
1141 * Record layer:
1142 * 0 . 0 message type
1143 * 1 . 2 protocol version
1144 * 3 . 4 message length
1145 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001146
1147 /* According to RFC 5246 Appendix E.1, the version here is typically
1148 * "{03,00}, the lowest version number supported by the client, [or] the
1149 * value of ClientHello.client_version", so the only meaningful check here
1150 * is the major version shouldn't be less than 3 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001151 if( buf[0] != SSL_MSG_HANDSHAKE ||
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001152 buf[1] < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001153 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001154 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1155 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1156 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001157
Paul Bakkerec636f32012-09-09 19:17:02 +00001158 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +00001159
Paul Bakker4f42c112014-04-17 14:48:23 +02001160 if( n < 45 || n > SSL_MAX_CONTENT_LEN )
Paul Bakkerec636f32012-09-09 19:17:02 +00001161 {
1162 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1163 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1164 }
1165
Paul Bakker48916f92012-09-16 19:57:18 +00001166 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1167 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001168 {
1169 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1170 return( ret );
1171 }
1172
1173 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +00001174 if( !ssl->renegotiation )
1175 n = ssl->in_left - 5;
1176 else
1177 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +00001178
Paul Bakker48916f92012-09-16 19:57:18 +00001179 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +00001180
1181 /*
1182 * SSL layer:
1183 * 0 . 0 handshake type
1184 * 1 . 3 handshake length
1185 * 4 . 5 protocol version
1186 * 6 . 9 UNIX time()
1187 * 10 . 37 random bytes
1188 * 38 . 38 session id length
1189 * 39 . 38+x session id
1190 * 39+x . 40+x ciphersuitelist length
Paul Bakker0f651c72014-05-22 15:12:19 +02001191 * 41+x . 40+y ciphersuitelist
1192 * 41+y . 41+y compression alg length
1193 * 42+y . 41+z compression algs
Paul Bakkerec636f32012-09-09 19:17:02 +00001194 * .. . .. extensions
1195 */
1196 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1197
1198 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1199 buf[0] ) );
1200 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1201 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1202 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1203 buf[4], buf[5] ) );
1204
1205 /*
1206 * Check the handshake type and protocol version
1207 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001208 if( buf[0] != SSL_HS_CLIENT_HELLO )
Paul Bakkerec636f32012-09-09 19:17:02 +00001209 {
1210 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1211 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1212 }
1213
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001214 ssl->major_ver = buf[4];
1215 ssl->minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001216
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001217 ssl->handshake->max_major_ver = ssl->major_ver;
1218 ssl->handshake->max_minor_ver = ssl->minor_ver;
1219
1220 if( ssl->major_ver < ssl->min_major_ver ||
1221 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001222 {
1223 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001224 " [%d:%d] < [%d:%d]",
1225 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001226 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001227
1228 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1229 SSL_ALERT_MSG_PROTOCOL_VERSION );
1230
1231 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1232 }
1233
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001234 if( ssl->major_ver > ssl->max_major_ver )
1235 {
1236 ssl->major_ver = ssl->max_major_ver;
1237 ssl->minor_ver = ssl->max_minor_ver;
1238 }
1239 else if( ssl->minor_ver > ssl->max_minor_ver )
1240 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001241
Paul Bakker48916f92012-09-16 19:57:18 +00001242 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001243
1244 /*
1245 * Check the handshake message length
1246 */
1247 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1248 {
1249 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1250 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1251 }
1252
1253 /*
1254 * Check the session length
1255 */
1256 sess_len = buf[38];
1257
Paul Bakker0f651c72014-05-22 15:12:19 +02001258 if( sess_len > 32 || sess_len > n - 42 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001259 {
1260 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1261 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1262 }
1263
Paul Bakker48916f92012-09-16 19:57:18 +00001264 ssl->session_negotiate->length = sess_len;
1265 memset( ssl->session_negotiate->id, 0,
1266 sizeof( ssl->session_negotiate->id ) );
1267 memcpy( ssl->session_negotiate->id, buf + 39,
1268 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001269
1270 /*
1271 * Check the ciphersuitelist length
1272 */
1273 ciph_len = ( buf[39 + sess_len] << 8 )
1274 | ( buf[40 + sess_len] );
1275
Paul Bakker0f651c72014-05-22 15:12:19 +02001276 if( ciph_len < 2 || ( ciph_len % 2 ) != 0 || ciph_len > n - 42 - sess_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001277 {
1278 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1279 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1280 }
1281
1282 /*
1283 * Check the compression algorithms length
1284 */
1285 comp_len = buf[41 + sess_len + ciph_len];
1286
Paul Bakker0f651c72014-05-22 15:12:19 +02001287 if( comp_len < 1 || comp_len > 16 ||
1288 comp_len > n - 42 - sess_len - ciph_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001289 {
1290 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1291 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1292 }
1293
Paul Bakker48916f92012-09-16 19:57:18 +00001294 /*
1295 * Check the extension length
1296 */
1297 if( n > 42 + sess_len + ciph_len + comp_len )
1298 {
1299 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1300 | ( buf[43 + sess_len + ciph_len + comp_len] );
1301
1302 if( ( ext_len > 0 && ext_len < 4 ) ||
1303 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1304 {
1305 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1306 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1307 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1308 }
1309 }
1310
1311 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001312#if defined(POLARSSL_ZLIB_SUPPORT)
1313 for( i = 0; i < comp_len; ++i )
1314 {
Paul Bakker48916f92012-09-16 19:57:18 +00001315 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001316 {
Paul Bakker48916f92012-09-16 19:57:18 +00001317 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001318 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001319 }
1320 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001321#endif
1322
Paul Bakkerec636f32012-09-09 19:17:02 +00001323 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1324 buf + 6, 32 );
1325 SSL_DEBUG_BUF( 3, "client hello, session id",
1326 buf + 38, sess_len );
1327 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1328 buf + 41 + sess_len, ciph_len );
1329 SSL_DEBUG_BUF( 3, "client hello, compression",
1330 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001331
Paul Bakkerec636f32012-09-09 19:17:02 +00001332 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001333 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1334 */
1335 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1336 {
1337 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1338 {
1339 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1340 if( ssl->renegotiation == SSL_RENEGOTIATION )
1341 {
1342 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001343
1344 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1345 return( ret );
1346
Paul Bakker48916f92012-09-16 19:57:18 +00001347 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1348 }
1349 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1350 break;
1351 }
1352 }
1353
Paul Bakker48916f92012-09-16 19:57:18 +00001354 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001355
1356 while( ext_len )
1357 {
1358 unsigned int ext_id = ( ( ext[0] << 8 )
1359 | ( ext[1] ) );
1360 unsigned int ext_size = ( ( ext[2] << 8 )
1361 | ( ext[3] ) );
1362
1363 if( ext_size + 4 > ext_len )
1364 {
1365 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1366 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1367 }
1368 switch( ext_id )
1369 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001370#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001371 case TLS_EXT_SERVERNAME:
1372 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1373 if( ssl->f_sni == NULL )
1374 break;
1375
1376 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1377 if( ret != 0 )
1378 return( ret );
1379 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001380#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001381
Paul Bakker48916f92012-09-16 19:57:18 +00001382 case TLS_EXT_RENEGOTIATION_INFO:
1383 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1384 renegotiation_info_seen = 1;
1385
Paul Bakker23f36802012-09-28 14:15:14 +00001386 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1387 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001388 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001389 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001390
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001391#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001392 case TLS_EXT_SIG_ALG:
1393 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1394 if( ssl->renegotiation == SSL_RENEGOTIATION )
1395 break;
1396
1397 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1398 if( ret != 0 )
1399 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001400 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001401#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001402
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001403#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001404 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1405 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1406
1407 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1408 if( ret != 0 )
1409 return( ret );
1410 break;
1411
1412 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1413 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001414 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001415
1416 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1417 if( ret != 0 )
1418 return( ret );
1419 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001420#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001421
Paul Bakker05decb22013-08-15 13:33:48 +02001422#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001423 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1424 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1425
1426 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1427 if( ret != 0 )
1428 return( ret );
1429 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001430#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001431
Paul Bakker1f2bc622013-08-15 13:45:55 +02001432#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001433 case TLS_EXT_TRUNCATED_HMAC:
1434 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1435
1436 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1437 if( ret != 0 )
1438 return( ret );
1439 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001440#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001441
Paul Bakkera503a632013-08-14 13:48:06 +02001442#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001443 case TLS_EXT_SESSION_TICKET:
1444 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1445
1446 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1447 if( ret != 0 )
1448 return( ret );
1449 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001450#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001451
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001452#if defined(POLARSSL_SSL_ALPN)
1453 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001454 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001455
1456 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1457 if( ret != 0 )
1458 return( ret );
1459 break;
1460#endif /* POLARSSL_SSL_SESSION_TICKETS */
1461
Paul Bakker48916f92012-09-16 19:57:18 +00001462 default:
1463 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1464 ext_id ) );
1465 }
1466
1467 ext_len -= 4 + ext_size;
1468 ext += 4 + ext_size;
1469
1470 if( ext_len > 0 && ext_len < 4 )
1471 {
1472 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1473 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1474 }
1475 }
1476
1477 /*
1478 * Renegotiation security checks
1479 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001480 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1481 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1482 {
1483 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1484 handshake_failure = 1;
1485 }
1486 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1487 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1488 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001489 {
1490 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001491 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001492 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001493 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1494 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1495 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001496 {
1497 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001498 handshake_failure = 1;
1499 }
1500 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1501 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1502 renegotiation_info_seen == 1 )
1503 {
1504 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1505 handshake_failure = 1;
1506 }
1507
1508 if( handshake_failure == 1 )
1509 {
1510 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1511 return( ret );
1512
Paul Bakker48916f92012-09-16 19:57:18 +00001513 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1514 }
Paul Bakker380da532012-04-18 16:10:25 +00001515
Paul Bakker41c83d32013-03-20 14:39:14 +01001516 /*
1517 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001518 * (At the end because we need information from the EC-based extensions
1519 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001520 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001521 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001522 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001523#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1524 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
1525 {
1526 for( i = 0; ciphersuites[i] != 0; i++ )
1527#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001528 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001529 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001530 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001531#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001532 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001533 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1534 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1535 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001536
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001537 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1538 &ciphersuite_info ) ) != 0 )
1539 return( ret );
1540
1541 if( ciphersuite_info != NULL )
1542 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001543 }
1544 }
1545
1546 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1547
1548 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1549 return( ret );
1550
1551 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1552
1553have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001554 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001555 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1556 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1557
Paul Bakker5121ce52009-01-03 21:22:43 +00001558 ssl->in_left = 0;
1559 ssl->state++;
1560
1561 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1562
1563 return( 0 );
1564}
1565
Paul Bakker1f2bc622013-08-15 13:45:55 +02001566#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001567static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1568 unsigned char *buf,
1569 size_t *olen )
1570{
1571 unsigned char *p = buf;
1572
1573 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1574 {
1575 *olen = 0;
1576 return;
1577 }
1578
1579 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1580
1581 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1582 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1583
1584 *p++ = 0x00;
1585 *p++ = 0x00;
1586
1587 *olen = 4;
1588}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001589#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001590
Paul Bakkera503a632013-08-14 13:48:06 +02001591#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001592static void ssl_write_session_ticket_ext( ssl_context *ssl,
1593 unsigned char *buf,
1594 size_t *olen )
1595{
1596 unsigned char *p = buf;
1597
1598 if( ssl->handshake->new_session_ticket == 0 )
1599 {
1600 *olen = 0;
1601 return;
1602 }
1603
1604 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1605
1606 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1607 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1608
1609 *p++ = 0x00;
1610 *p++ = 0x00;
1611
1612 *olen = 4;
1613}
Paul Bakkera503a632013-08-14 13:48:06 +02001614#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001615
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001616static void ssl_write_renegotiation_ext( ssl_context *ssl,
1617 unsigned char *buf,
1618 size_t *olen )
1619{
1620 unsigned char *p = buf;
1621
1622 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1623 {
1624 *olen = 0;
1625 return;
1626 }
1627
1628 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1629
1630 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1631 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1632
1633 *p++ = 0x00;
1634 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1635 *p++ = ssl->verify_data_len * 2 & 0xFF;
1636
1637 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1638 p += ssl->verify_data_len;
1639 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1640 p += ssl->verify_data_len;
1641
1642 *olen = 5 + ssl->verify_data_len * 2;
1643}
1644
Paul Bakker05decb22013-08-15 13:33:48 +02001645#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001646static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1647 unsigned char *buf,
1648 size_t *olen )
1649{
1650 unsigned char *p = buf;
1651
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001652 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1653 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001654 *olen = 0;
1655 return;
1656 }
1657
1658 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1659
1660 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1661 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1662
1663 *p++ = 0x00;
1664 *p++ = 1;
1665
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001666 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001667
1668 *olen = 5;
1669}
Paul Bakker05decb22013-08-15 13:33:48 +02001670#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001671
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001672#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001673static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1674 unsigned char *buf,
1675 size_t *olen )
1676{
1677 unsigned char *p = buf;
1678 ((void) ssl);
1679
Paul Bakker677377f2013-10-28 12:54:26 +01001680 if( ( ssl->handshake->cli_exts &
1681 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
1682 {
1683 *olen = 0;
1684 return;
1685 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001686
1687 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1688
1689 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1690 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1691
1692 *p++ = 0x00;
1693 *p++ = 2;
1694
1695 *p++ = 1;
1696 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1697
1698 *olen = 6;
1699}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001700#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001701
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001702#if defined(POLARSSL_SSL_ALPN )
1703static void ssl_write_alpn_ext( ssl_context *ssl,
1704 unsigned char *buf, size_t *olen )
1705{
1706 if( ssl->alpn_chosen == NULL )
1707 {
1708 *olen = 0;
1709 return;
1710 }
1711
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001712 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001713
1714 /*
1715 * 0 . 1 ext identifier
1716 * 2 . 3 ext length
1717 * 4 . 5 protocol list length
1718 * 6 . 6 protocol name length
1719 * 7 . 7+n protocol name
1720 */
1721 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
1722 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
1723
1724 *olen = 7 + strlen( ssl->alpn_chosen );
1725
1726 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
1727 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
1728
1729 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
1730 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
1731
1732 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
1733
1734 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
1735}
1736#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
1737
Paul Bakker5121ce52009-01-03 21:22:43 +00001738static int ssl_write_server_hello( ssl_context *ssl )
1739{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001740#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001741 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001742#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001743 int ret;
1744 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001745 unsigned char *buf, *p;
1746
1747 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1748
Paul Bakkera9a028e2013-11-21 17:31:06 +01001749 if( ssl->f_rng == NULL )
1750 {
1751 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
1752 return( POLARSSL_ERR_SSL_NO_RNG );
1753 }
1754
Paul Bakker5121ce52009-01-03 21:22:43 +00001755 /*
1756 * 0 . 0 handshake type
1757 * 1 . 3 handshake length
1758 * 4 . 5 protocol version
1759 * 6 . 9 UNIX time()
1760 * 10 . 37 random bytes
1761 */
1762 buf = ssl->out_msg;
1763 p = buf + 4;
1764
1765 *p++ = (unsigned char) ssl->major_ver;
1766 *p++ = (unsigned char) ssl->minor_ver;
1767
1768 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1769 buf[4], buf[5] ) );
1770
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001771#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001772 t = time( NULL );
1773 *p++ = (unsigned char)( t >> 24 );
1774 *p++ = (unsigned char)( t >> 16 );
1775 *p++ = (unsigned char)( t >> 8 );
1776 *p++ = (unsigned char)( t );
1777
1778 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001779#else
1780 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1781 return( ret );
1782
1783 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02001784#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001785
Paul Bakkera3d195c2011-11-27 21:07:34 +00001786 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1787 return( ret );
1788
1789 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001790
Paul Bakker48916f92012-09-16 19:57:18 +00001791 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001792
1793 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1794
1795 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001796 * Resume is 0 by default, see ssl_handshake_init().
1797 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1798 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001799 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001800 if( ssl->handshake->resume == 0 &&
1801 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001802 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001803 ssl->f_get_cache != NULL &&
1804 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1805 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01001806 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001807 ssl->handshake->resume = 1;
1808 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001809
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001810 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001811 {
1812 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001813 * New session, create a new session id,
1814 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001815 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001816 ssl->state++;
1817
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001818#if defined(POLARSSL_HAVE_TIME)
1819 ssl->session_negotiate->start = time( NULL );
1820#endif
1821
Paul Bakkera503a632013-08-14 13:48:06 +02001822#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001823 if( ssl->handshake->new_session_ticket != 0 )
1824 {
1825 ssl->session_negotiate->length = n = 0;
1826 memset( ssl->session_negotiate->id, 0, 32 );
1827 }
1828 else
1829#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001830 {
1831 ssl->session_negotiate->length = n = 32;
1832 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001833 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001834 return( ret );
1835 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001836 }
1837 else
1838 {
1839 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001840 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001841 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001842 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001843 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001844
1845 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1846 {
1847 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1848 return( ret );
1849 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001850 }
1851
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001852 /*
1853 * 38 . 38 session id length
1854 * 39 . 38+n session id
1855 * 39+n . 40+n chosen ciphersuite
1856 * 41+n . 41+n chosen compression alg.
1857 * 42+n . 43+n extensions length
1858 * 44+n . 43+n+m extensions
1859 */
1860 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001861 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1862 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001863
1864 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1865 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1866 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001867 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001868
Paul Bakker48916f92012-09-16 19:57:18 +00001869 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1870 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1871 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001872
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001873 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
1874 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001875 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001876 ssl->session_negotiate->compression ) );
1877
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001878 /*
1879 * First write extensions, then the total length
1880 */
1881 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1882 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001883
Paul Bakker05decb22013-08-15 13:33:48 +02001884#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001885 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1886 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001887#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001888
Paul Bakker1f2bc622013-08-15 13:45:55 +02001889#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001890 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1891 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001892#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001893
Paul Bakkera503a632013-08-14 13:48:06 +02001894#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001895 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1896 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001897#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001898
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001899#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001900 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1901 ext_len += olen;
1902#endif
1903
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001904#if defined(POLARSSL_SSL_ALPN)
1905 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
1906 ext_len += olen;
1907#endif
1908
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001909 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001910
Paul Bakkera7036632014-04-30 10:15:38 +02001911 if( ext_len > 0 )
1912 {
1913 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1914 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1915 p += ext_len;
1916 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001917
1918 ssl->out_msglen = p - buf;
1919 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1920 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1921
1922 ret = ssl_write_record( ssl );
1923
1924 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1925
1926 return( ret );
1927}
1928
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001929#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1930 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001931 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1932 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001933static int ssl_write_certificate_request( ssl_context *ssl )
1934{
Paul Bakkered27a042013-04-18 22:46:23 +02001935 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001936
1937 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1938
1939 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01001940 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001941 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1942 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001943 {
1944 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1945 ssl->state++;
1946 return( 0 );
1947 }
1948
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001949 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1950 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001951}
1952#else
1953static int ssl_write_certificate_request( ssl_context *ssl )
1954{
1955 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1956 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001957 size_t dn_size, total_dn_size; /* excluding length bytes */
1958 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00001959 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001960 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001961
1962 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1963
1964 ssl->state++;
1965
Paul Bakkerfbb17802013-04-17 19:10:21 +02001966 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01001967 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001968 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001969 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001970 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001971 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001972 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001973 return( 0 );
1974 }
1975
1976 /*
1977 * 0 . 0 handshake type
1978 * 1 . 3 handshake length
1979 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01001980 * 5 .. m-1 cert types
1981 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02001982 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001983 * n .. n+1 length of all DNs
1984 * n+2 .. n+3 length of DN 1
1985 * n+4 .. ... Distinguished Name #1
1986 * ... .. ... length of DN 2, etc.
1987 */
1988 buf = ssl->out_msg;
1989 p = buf + 4;
1990
1991 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001992 * Supported certificate types
1993 *
1994 * ClientCertificateType certificate_types<1..2^8-1>;
1995 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00001996 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001997 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01001998
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001999#if defined(POLARSSL_RSA_C)
2000 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2001#endif
2002#if defined(POLARSSL_ECDSA_C)
2003 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2004#endif
2005
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002006 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002007 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002008
Paul Bakker577e0062013-08-28 11:57:20 +02002009 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002010#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002011 /*
2012 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002013 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002014 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2015 *
2016 * struct {
2017 * HashAlgorithm hash;
2018 * SignatureAlgorithm signature;
2019 * } SignatureAndHashAlgorithm;
2020 *
2021 * enum { (255) } HashAlgorithm;
2022 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002023 */
Paul Bakker21dca692013-01-03 11:41:08 +01002024 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002025 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002026 /*
2027 * Only use current running hash algorithm that is already required
2028 * for requested ciphersuite.
2029 */
Paul Bakker926af752012-11-23 13:38:07 +01002030 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2031
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002032 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2033 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002034 {
2035 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2036 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002037
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002038 /*
2039 * Supported signature algorithms
2040 */
2041#if defined(POLARSSL_RSA_C)
2042 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2043 p[2 + sa_len++] = SSL_SIG_RSA;
2044#endif
2045#if defined(POLARSSL_ECDSA_C)
2046 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2047 p[2 + sa_len++] = SSL_SIG_ECDSA;
2048#endif
Paul Bakker926af752012-11-23 13:38:07 +01002049
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002050 p[0] = (unsigned char)( sa_len >> 8 );
2051 p[1] = (unsigned char)( sa_len );
2052 sa_len += 2;
2053 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002054 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002055#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002056
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002057 /*
2058 * DistinguishedName certificate_authorities<0..2^16-1>;
2059 * opaque DistinguishedName<1..2^16-1>;
2060 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002061 p += 2;
2062 crt = ssl->ca_chain;
2063
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002064 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002065 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002066 {
2067 if( p - buf > 4096 )
2068 break;
2069
Paul Bakker926af752012-11-23 13:38:07 +01002070 dn_size = crt->subject_raw.len;
2071 *p++ = (unsigned char)( dn_size >> 8 );
2072 *p++ = (unsigned char)( dn_size );
2073 memcpy( p, crt->subject_raw.p, dn_size );
2074 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002075
Paul Bakker926af752012-11-23 13:38:07 +01002076 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2077
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002078 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002079 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002080 }
2081
Paul Bakker926af752012-11-23 13:38:07 +01002082 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002083 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2084 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002085 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2086 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002087
2088 ret = ssl_write_record( ssl );
2089
2090 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2091
2092 return( ret );
2093}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002094#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2095 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002096 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2097 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002098
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002099#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2100 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2101static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2102{
2103 int ret;
2104
2105 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2106 {
2107 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2108 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2109 }
2110
2111 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2112 pk_ec( *ssl_own_key( ssl ) ),
2113 POLARSSL_ECDH_OURS ) ) != 0 )
2114 {
2115 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2116 return( ret );
2117 }
2118
2119 return( 0 );
2120}
2121#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2122 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2123
Paul Bakker41c83d32013-03-20 14:39:14 +01002124static int ssl_write_server_key_exchange( ssl_context *ssl )
2125{
Paul Bakker23986e52011-04-24 08:57:21 +00002126 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002127 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002128 const ssl_ciphersuite_t *ciphersuite_info =
2129 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002130
2131#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2132 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2133 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002134 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002135 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002136 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002137 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002138 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002139 ((void) dig_signed);
2140 ((void) dig_signed_len);
2141#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002142
Paul Bakker5121ce52009-01-03 21:22:43 +00002143 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2144
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002145#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2146 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2147 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002148 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2149 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2150 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002151 {
2152 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2153 ssl->state++;
2154 return( 0 );
2155 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002156#endif
2157
2158#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2159 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2160 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2161 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2162 {
2163 ssl_get_ecdh_params_from_cert( ssl );
2164
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002165 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002166 ssl->state++;
2167 return( 0 );
2168 }
2169#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002170
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002171#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2172 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2173 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2174 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002175 {
2176 /* TODO: Support identity hints */
2177 *(p++) = 0x00;
2178 *(p++) = 0x00;
2179
2180 n += 2;
2181 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002182#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2183 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002184
2185#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2186 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2187 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2188 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002189 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002190 /*
2191 * Ephemeral DH parameters:
2192 *
2193 * struct {
2194 * opaque dh_p<1..2^16-1>;
2195 * opaque dh_g<1..2^16-1>;
2196 * opaque dh_Ys<1..2^16-1>;
2197 * } ServerDHParams;
2198 */
2199 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2200 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2201 {
2202 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2203 return( ret );
2204 }
Paul Bakker48916f92012-09-16 19:57:18 +00002205
Paul Bakker41c83d32013-03-20 14:39:14 +01002206 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002207 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2208 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002209 {
2210 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2211 return( ret );
2212 }
2213
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002214 dig_signed = p;
2215 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002216
2217 p += len;
2218 n += len;
2219
Paul Bakker41c83d32013-03-20 14:39:14 +01002220 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2221 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2222 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2223 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2224 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002225#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2226 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002227
Gergely Budai987bfb52014-01-19 21:48:42 +01002228#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002229 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002230 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2231 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002232 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002233 /*
2234 * Ephemeral ECDH parameters:
2235 *
2236 * struct {
2237 * ECParameters curve_params;
2238 * ECPoint public;
2239 * } ServerECDHParams;
2240 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002241 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002242#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002243 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002244
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002245 /* Match our preference list against the offered curves */
2246 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2247 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2248 if( (*curve)->grp_id == *gid )
2249 goto curve_matching_done;
2250
2251curve_matching_done:
2252#else
2253 curve = ssl->handshake->curves;
2254#endif
2255
2256 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002257 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002258 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2259 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002260 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002261
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002262 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002263
Paul Bakker41c83d32013-03-20 14:39:14 +01002264 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002265 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002266 {
2267 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2268 return( ret );
2269 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002270
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002271 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2272 p, SSL_MAX_CONTENT_LEN - n,
2273 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002274 {
2275 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2276 return( ret );
2277 }
2278
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002279 dig_signed = p;
2280 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002281
2282 p += len;
2283 n += len;
2284
Paul Bakker41c83d32013-03-20 14:39:14 +01002285 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2286 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002287#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002288
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002289#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002290 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2291 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002292 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002293 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2294 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002295 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002296 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002297 unsigned int hashlen = 0;
2298 unsigned char hash[64];
2299 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002300
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002301 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002302 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2303 */
Paul Bakker577e0062013-08-28 11:57:20 +02002304#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002305 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2306 {
2307 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2308
2309 if( md_alg == POLARSSL_MD_NONE )
2310 {
2311 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002312 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002313 }
2314 }
Paul Bakker577e0062013-08-28 11:57:20 +02002315 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002316#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002317#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2318 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002319 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002320 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2321 {
2322 md_alg = POLARSSL_MD_SHA1;
2323 }
2324 else
Paul Bakker577e0062013-08-28 11:57:20 +02002325#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002326 {
2327 md_alg = POLARSSL_MD_NONE;
2328 }
2329
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002330 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002331 * Compute the hash to be signed
2332 */
Paul Bakker577e0062013-08-28 11:57:20 +02002333#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2334 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002335 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002336 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002337 md5_context md5;
2338 sha1_context sha1;
2339
2340 /*
2341 * digitally-signed struct {
2342 * opaque md5_hash[16];
2343 * opaque sha_hash[20];
2344 * };
2345 *
2346 * md5_hash
2347 * MD5(ClientHello.random + ServerHello.random
2348 * + ServerParams);
2349 * sha_hash
2350 * SHA(ClientHello.random + ServerHello.random
2351 * + ServerParams);
2352 */
2353 md5_starts( &md5 );
2354 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002355 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002356 md5_finish( &md5, hash );
2357
2358 sha1_starts( &sha1 );
2359 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002360 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002361 sha1_finish( &sha1, hash + 16 );
2362
2363 hashlen = 36;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002364 }
2365 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002366#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2367 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002368#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2369 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002370 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002371 {
2372 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02002373 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002374
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002375 /* Info from md_alg will be used instead */
2376 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002377
2378 /*
2379 * digitally-signed struct {
2380 * opaque client_random[32];
2381 * opaque server_random[32];
2382 * ServerDHParams params;
2383 * };
2384 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002385 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002386 {
2387 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2388 return( ret );
2389 }
2390
2391 md_starts( &ctx );
2392 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002393 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002394 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02002395
2396 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
2397 {
2398 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
2399 return( ret );
2400 }
2401
Paul Bakker23f36802012-09-28 14:15:14 +00002402 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002403 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002404#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2405 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002406 {
2407 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002408 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002409 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002410
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002411 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2412 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002413
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002414 /*
2415 * Make the signature
2416 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002417 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002418 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002419 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2420 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002421 }
Paul Bakker23f36802012-09-28 14:15:14 +00002422
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002423#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002424 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2425 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002426 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002427 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002428
2429 n += 2;
2430 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002431#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002432
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002433 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002434 p + 2 , &signature_len,
2435 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002436 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002437 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002438 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002439 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002440
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002441 *(p++) = (unsigned char)( signature_len >> 8 );
2442 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002443 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002444
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002445 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002446
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002447 p += signature_len;
2448 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002449 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002450#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002451 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2452 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002453
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002454 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002455 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2456 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2457
2458 ssl->state++;
2459
2460 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2461 {
2462 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2463 return( ret );
2464 }
2465
2466 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2467
2468 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002469}
2470
2471static int ssl_write_server_hello_done( ssl_context *ssl )
2472{
2473 int ret;
2474
2475 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2476
2477 ssl->out_msglen = 4;
2478 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2479 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2480
2481 ssl->state++;
2482
2483 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2484 {
2485 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2486 return( ret );
2487 }
2488
2489 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2490
2491 return( 0 );
2492}
2493
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002494#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2495 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2496static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2497 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002498{
2499 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002500 size_t n;
2501
2502 /*
2503 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2504 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002505 if( *p + 2 > end )
2506 {
2507 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2508 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2509 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002510
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002511 n = ( (*p)[0] << 8 ) | (*p)[1];
2512 *p += 2;
2513
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002514 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002515 {
2516 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2517 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2518 }
2519
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002520 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002521 {
2522 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2523 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2524 }
2525
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002526 *p += n;
2527
Paul Bakker70df2fb2013-04-17 17:19:09 +02002528 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2529
Paul Bakker70df2fb2013-04-17 17:19:09 +02002530 return( ret );
2531}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002532#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2533 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002534
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002535#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2536 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002537static int ssl_parse_encrypted_pms( ssl_context *ssl,
2538 const unsigned char *p,
2539 const unsigned char *end,
2540 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002541{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002542 int ret;
2543 size_t len = pk_get_len( ssl_own_key( ssl ) );
2544 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002545
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002546 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002547 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002548 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002549 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2550 }
2551
2552 /*
2553 * Decrypt the premaster using own private RSA key
2554 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002555#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2556 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002557 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2558 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002559 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2560 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002561 {
2562 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2563 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2564 }
2565 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002566#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002567
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002568 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002569 {
2570 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2571 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2572 }
2573
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002574 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2575 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002576 sizeof( ssl->handshake->premaster ) - pms_offset,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002577 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002578
2579 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002580 pms[0] != ssl->handshake->max_major_ver ||
2581 pms[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002582 {
2583 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2584
2585 /*
2586 * Protection against Bleichenbacher's attack:
2587 * invalid PKCS#1 v1.5 padding must not cause
2588 * the connection to end immediately; instead,
2589 * send a bad_record_mac later in the handshake.
2590 */
2591 ssl->handshake->pmslen = 48;
2592
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002593 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002594 if( ret != 0 )
2595 return( ret );
2596 }
2597
2598 return( ret );
2599}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002600#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2601 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002602
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002603#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002604static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2605 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002606{
Paul Bakker6db455e2013-09-18 17:29:31 +02002607 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002608 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002609
Paul Bakker6db455e2013-09-18 17:29:31 +02002610 if( ssl->f_psk == NULL &&
2611 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2612 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002613 {
2614 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2615 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2616 }
2617
2618 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002619 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002620 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002621 if( *p + 2 > end )
2622 {
2623 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2624 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2625 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002626
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002627 n = ( (*p)[0] << 8 ) | (*p)[1];
2628 *p += 2;
2629
2630 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002631 {
2632 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2633 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2634 }
2635
Paul Bakker6db455e2013-09-18 17:29:31 +02002636 if( ssl->f_psk != NULL )
2637 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002638 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002639 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2640 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002641 else
Paul Bakker6db455e2013-09-18 17:29:31 +02002642 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002643 /* Identity is not a big secret since clients send it in the clear,
2644 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02002645 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002646 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002647 {
2648 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2649 }
2650 }
2651
2652 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002653 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002654 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002655 if( ( ret = ssl_send_alert_message( ssl,
2656 SSL_ALERT_LEVEL_FATAL,
2657 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2658 {
2659 return( ret );
2660 }
2661
2662 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002663 }
2664
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002665 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002666
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002667 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002668}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002669#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002670
Paul Bakker5121ce52009-01-03 21:22:43 +00002671static int ssl_parse_client_key_exchange( ssl_context *ssl )
2672{
Paul Bakker23986e52011-04-24 08:57:21 +00002673 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002674 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002675
Paul Bakker41c83d32013-03-20 14:39:14 +01002676 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002677
2678 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2679
2680 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2681 {
2682 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2683 return( ret );
2684 }
2685
2686 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2687 {
2688 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002689 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002690 }
2691
2692 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2693 {
2694 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002695 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002696 }
2697
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002698#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002699 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002700 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002701 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002702 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002703
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002704 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002705 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002706 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2707 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002708 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002709
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002710 if( p != end )
2711 {
2712 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2713 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2714 }
2715
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002716 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002717
2718 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2719 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002720 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002721 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002722 {
2723 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2724 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2725 }
2726
2727 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002728 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002729 else
2730#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002731#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002732 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2733 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2734 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002735 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002736 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2737 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2738 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002739 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002740 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002741 ssl->in_msg + 4, ssl->in_hslen - 4 ) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002742 {
2743 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2744 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2745 }
2746
2747 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2748
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002749 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2750 &ssl->handshake->pmslen,
2751 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002752 POLARSSL_MPI_MAX_SIZE,
2753 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002754 {
2755 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2756 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2757 }
2758
2759 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002760 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002761 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002762#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002763 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2764 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2765 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002766#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2767 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002768 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002769 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002770 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002771
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002772 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002773 {
2774 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2775 return( ret );
2776 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002777
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002778 if( p != end )
2779 {
2780 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2781 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2782 }
2783
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002784 if( ( ret = ssl_psk_derive_premaster( ssl,
2785 ciphersuite_info->key_exchange ) ) != 0 )
2786 {
2787 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2788 return( ret );
2789 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002790 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002791 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002792#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002793#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2794 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2795 {
2796 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002797 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002798
2799 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2800 {
2801 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2802 return( ret );
2803 }
2804
2805 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
2806 {
2807 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
2808 return( ret );
2809 }
2810
2811 if( ( ret = ssl_psk_derive_premaster( ssl,
2812 ciphersuite_info->key_exchange ) ) != 0 )
2813 {
2814 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2815 return( ret );
2816 }
2817 }
2818 else
2819#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002820#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2821 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2822 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002823 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002824 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002825
2826 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2827 {
2828 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2829 return( ret );
2830 }
2831 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2832 {
2833 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2834 return( ret );
2835 }
2836
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002837 if( p != end )
2838 {
2839 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2840 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2841 }
2842
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002843 if( ( ret = ssl_psk_derive_premaster( ssl,
2844 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002845 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002846 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2847 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002848 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002849 }
2850 else
2851#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002852#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2853 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2854 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002855 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002856 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002857
2858 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2859 {
2860 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2861 return( ret );
2862 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002863
2864 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2865 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002866 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002867 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2868 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002869 }
2870
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002871 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2872
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002873 if( ( ret = ssl_psk_derive_premaster( ssl,
2874 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002875 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002876 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002877 return( ret );
2878 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002879 }
2880 else
2881#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002882#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2883 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002884 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002885 if( ( ret = ssl_parse_encrypted_pms( ssl,
2886 ssl->in_msg + 4,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002887 ssl->in_msg + ssl->in_hslen,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002888 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002889 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002890 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002891 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002892 }
2893 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002894 else
2895#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2896 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002897 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002898 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002899 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002900
Paul Bakkerff60ee62010-03-16 21:09:09 +00002901 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2902 {
2903 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2904 return( ret );
2905 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002906
Paul Bakker5121ce52009-01-03 21:22:43 +00002907 ssl->state++;
2908
2909 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2910
2911 return( 0 );
2912}
2913
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002914#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2915 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002916 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2917 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002918static int ssl_parse_certificate_verify( ssl_context *ssl )
2919{
Paul Bakkerfbb17802013-04-17 19:10:21 +02002920 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002921
2922 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2923
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002924 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002925 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002926 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002927 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002928 {
2929 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2930 ssl->state++;
2931 return( 0 );
2932 }
2933
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002934 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2935 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002936}
2937#else
2938static int ssl_parse_certificate_verify( ssl_context *ssl )
2939{
2940 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002941 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002942 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002943 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002944 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02002945#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002946 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02002947#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002948 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002949 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2950
2951 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2952
2953 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002954 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002955 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002956 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2957 {
2958 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2959 ssl->state++;
2960 return( 0 );
2961 }
2962
Paul Bakkered27a042013-04-18 22:46:23 +02002963 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002964 {
2965 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2966 ssl->state++;
2967 return( 0 );
2968 }
2969
Paul Bakker48916f92012-09-16 19:57:18 +00002970 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002971
2972 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2973 {
2974 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2975 return( ret );
2976 }
2977
2978 ssl->state++;
2979
2980 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2981 {
2982 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002983 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002984 }
2985
2986 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
2987 {
2988 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002989 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002990 }
2991
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002992 /*
2993 * 0 . 0 handshake type
2994 * 1 . 3 handshake length
2995 * 4 . 5 sig alg (TLS 1.2 only)
2996 * 4+n . 5+n signature length (n = sa_len)
2997 * 6+n . 6+n+m signature (m = sig_len)
2998 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002999
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003000#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3001 defined(POLARSSL_SSL_PROTO_TLS1_1)
3002 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003003 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003004 sa_len = 0;
3005
Paul Bakkerc70b9822013-04-07 22:00:46 +02003006 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003007 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003008
3009 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3010 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3011 POLARSSL_PK_ECDSA ) )
3012 {
3013 hash_start += 16;
3014 hashlen -= 16;
3015 md_alg = POLARSSL_MD_SHA1;
3016 }
Paul Bakker926af752012-11-23 13:38:07 +01003017 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003018 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003019#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3020 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003021#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3022 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003023 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003024 sa_len = 2;
3025
Paul Bakker5121ce52009-01-03 21:22:43 +00003026 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003027 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003028 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003029 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003030 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003031 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3032 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003033 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3034 }
3035
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003036 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003037
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003038 /* Info from md_alg will be used instead */
3039 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003040
3041 /*
3042 * Signature
3043 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003044 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
3045 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003046 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003047 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3048 " for verify message" ) );
3049 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003050 }
3051
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003052 /*
3053 * Check the certificate's key type matches the signature alg
3054 */
3055 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3056 {
3057 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3058 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3059 }
Paul Bakker577e0062013-08-28 11:57:20 +02003060 }
3061 else
3062#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3063 {
3064 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003065 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003066 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003067
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003068 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01003069
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003070 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003071 {
3072 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003073 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003074 }
3075
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003076 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003077 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003078 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003079 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003080 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003081 return( ret );
3082 }
3083
3084 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3085
Paul Bakkered27a042013-04-18 22:46:23 +02003086 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003087}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003088#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3089 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3090 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003091
Paul Bakkera503a632013-08-14 13:48:06 +02003092#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003093static int ssl_write_new_session_ticket( ssl_context *ssl )
3094{
3095 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003096 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003097 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003098
3099 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3100
3101 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3102 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3103
3104 /*
3105 * struct {
3106 * uint32 ticket_lifetime_hint;
3107 * opaque ticket<0..2^16-1>;
3108 * } NewSessionTicket;
3109 *
3110 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3111 * 8 . 9 ticket_len (n)
3112 * 10 . 9+n ticket content
3113 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003114
3115 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3116 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3117 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3118 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003119
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003120 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3121 {
3122 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3123 tlen = 0;
3124 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003125
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003126 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3127 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003128
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003129 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003130
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003131 /*
3132 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3133 * ChangeCipherSpec share the same state.
3134 */
3135 ssl->handshake->new_session_ticket = 0;
3136
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003137 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3138 {
3139 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3140 return( ret );
3141 }
3142
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003143 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3144
3145 return( 0 );
3146}
Paul Bakkera503a632013-08-14 13:48:06 +02003147#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003148
Paul Bakker5121ce52009-01-03 21:22:43 +00003149/*
Paul Bakker1961b702013-01-25 14:49:24 +01003150 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003151 */
Paul Bakker1961b702013-01-25 14:49:24 +01003152int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003153{
3154 int ret = 0;
3155
Paul Bakker1961b702013-01-25 14:49:24 +01003156 if( ssl->state == SSL_HANDSHAKE_OVER )
3157 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003158
Paul Bakker1961b702013-01-25 14:49:24 +01003159 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3160
3161 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3162 return( ret );
3163
3164 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003165 {
Paul Bakker1961b702013-01-25 14:49:24 +01003166 case SSL_HELLO_REQUEST:
3167 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003168 break;
3169
Paul Bakker1961b702013-01-25 14:49:24 +01003170 /*
3171 * <== ClientHello
3172 */
3173 case SSL_CLIENT_HELLO:
3174 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003175 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003176
3177 /*
3178 * ==> ServerHello
3179 * Certificate
3180 * ( ServerKeyExchange )
3181 * ( CertificateRequest )
3182 * ServerHelloDone
3183 */
3184 case SSL_SERVER_HELLO:
3185 ret = ssl_write_server_hello( ssl );
3186 break;
3187
3188 case SSL_SERVER_CERTIFICATE:
3189 ret = ssl_write_certificate( ssl );
3190 break;
3191
3192 case SSL_SERVER_KEY_EXCHANGE:
3193 ret = ssl_write_server_key_exchange( ssl );
3194 break;
3195
3196 case SSL_CERTIFICATE_REQUEST:
3197 ret = ssl_write_certificate_request( ssl );
3198 break;
3199
3200 case SSL_SERVER_HELLO_DONE:
3201 ret = ssl_write_server_hello_done( ssl );
3202 break;
3203
3204 /*
3205 * <== ( Certificate/Alert )
3206 * ClientKeyExchange
3207 * ( CertificateVerify )
3208 * ChangeCipherSpec
3209 * Finished
3210 */
3211 case SSL_CLIENT_CERTIFICATE:
3212 ret = ssl_parse_certificate( ssl );
3213 break;
3214
3215 case SSL_CLIENT_KEY_EXCHANGE:
3216 ret = ssl_parse_client_key_exchange( ssl );
3217 break;
3218
3219 case SSL_CERTIFICATE_VERIFY:
3220 ret = ssl_parse_certificate_verify( ssl );
3221 break;
3222
3223 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3224 ret = ssl_parse_change_cipher_spec( ssl );
3225 break;
3226
3227 case SSL_CLIENT_FINISHED:
3228 ret = ssl_parse_finished( ssl );
3229 break;
3230
3231 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003232 * ==> ( NewSessionTicket )
3233 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003234 * Finished
3235 */
3236 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003237#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003238 if( ssl->handshake->new_session_ticket != 0 )
3239 ret = ssl_write_new_session_ticket( ssl );
3240 else
Paul Bakkera503a632013-08-14 13:48:06 +02003241#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003242 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003243 break;
3244
3245 case SSL_SERVER_FINISHED:
3246 ret = ssl_write_finished( ssl );
3247 break;
3248
3249 case SSL_FLUSH_BUFFERS:
3250 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3251 ssl->state = SSL_HANDSHAKE_WRAPUP;
3252 break;
3253
3254 case SSL_HANDSHAKE_WRAPUP:
3255 ssl_handshake_wrapup( ssl );
3256 break;
3257
3258 default:
3259 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3260 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003261 }
3262
Paul Bakker5121ce52009-01-03 21:22:43 +00003263 return( ret );
3264}
Paul Bakker9af723c2014-05-01 13:03:14 +02003265#endif /* POLARSSL_SSL_SRV_C */