blob: 6d8626cc27df8c52771c60dbcee1e1eb3be3cf29 [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 Bakkeraccaffe2014-06-26 13:37:14 +0200346
347 /* Zeroize instead of free as we copied the content */
Paul Bakker34617722014-06-13 17:20:13 +0200348 polarssl_zeroize( &session, sizeof( ssl_session ) );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200349
350 return( 0 );
351}
Paul Bakkera503a632013-08-14 13:48:06 +0200352#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200353
Paul Bakker0be444a2013-08-27 21:55:01 +0200354#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200355/*
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200356 * Wrapper around f_sni, allowing use of ssl_set_own_cert() but
357 * making it act on ssl->hanshake->sni_key_cert instead.
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200358 */
359static int ssl_sni_wrapper( ssl_context *ssl,
360 const unsigned char* name, size_t len )
361{
362 int ret;
363 ssl_key_cert *key_cert_ori = ssl->key_cert;
364
365 ssl->key_cert = NULL;
366 ret = ssl->f_sni( ssl->p_sni, ssl, name, len );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200367 ssl->handshake->sni_key_cert = ssl->key_cert;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200368
369 ssl->key_cert = key_cert_ori;
370
371 return( ret );
372}
373
Paul Bakker5701cdc2012-09-27 21:49:42 +0000374static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000375 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000376 size_t len )
377{
378 int ret;
379 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000380 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000381
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100382 SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
383
Paul Bakker5701cdc2012-09-27 21:49:42 +0000384 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
385 if( servername_list_size + 2 != len )
386 {
387 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
388 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
389 }
390
391 p = buf + 2;
392 while( servername_list_size > 0 )
393 {
394 hostname_len = ( ( p[1] << 8 ) | p[2] );
395 if( hostname_len + 3 > servername_list_size )
396 {
397 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
398 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
399 }
400
401 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
402 {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200403 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000404 if( ret != 0 )
405 {
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100406 SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000407 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
408 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
409 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
410 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000411 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000412 }
413
414 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000415 p += hostname_len + 3;
416 }
417
418 if( servername_list_size != 0 )
419 {
420 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
421 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000422 }
423
424 return( 0 );
425}
Paul Bakker0be444a2013-08-27 21:55:01 +0200426#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000427
Paul Bakker48916f92012-09-16 19:57:18 +0000428static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000429 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000430 size_t len )
431{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000432 int ret;
433
Paul Bakker48916f92012-09-16 19:57:18 +0000434 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
435 {
436 if( len != 1 || buf[0] != 0x0 )
437 {
438 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000439
440 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
441 return( ret );
442
Paul Bakker48916f92012-09-16 19:57:18 +0000443 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
444 }
445
446 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
447 }
448 else
449 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100450 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000451 if( len != 1 + ssl->verify_data_len ||
452 buf[0] != ssl->verify_data_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100453 safer_memcmp( buf + 1, ssl->peer_verify_data,
454 ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000455 {
456 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000457
458 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
459 return( ret );
460
Paul Bakker48916f92012-09-16 19:57:18 +0000461 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
462 }
463 }
464
465 return( 0 );
466}
467
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200468#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +0000469static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
470 const unsigned char *buf,
471 size_t len )
472{
473 size_t sig_alg_list_size;
474 const unsigned char *p;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200475 const unsigned char *end = buf + len;
476 const int *md_cur;
477
Paul Bakker23f36802012-09-28 14:15:14 +0000478
479 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
480 if( sig_alg_list_size + 2 != len ||
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200481 sig_alg_list_size % 2 != 0 )
Paul Bakker23f36802012-09-28 14:15:14 +0000482 {
483 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
484 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
485 }
486
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200487 /*
488 * For now, ignore the SignatureAlgorithm part and rely on offered
489 * ciphersuites only for that part. To be fixed later.
490 *
491 * So, just look at the HashAlgorithm part.
492 */
493 for( md_cur = md_list(); *md_cur != POLARSSL_MD_NONE; md_cur++ ) {
494 for( p = buf + 2; p < end; p += 2 ) {
495 if( *md_cur == (int) ssl_md_alg_from_hash( p[0] ) ) {
496 ssl->handshake->sig_alg = p[0];
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200497 goto have_sig_alg;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200498 }
Paul Bakker23f36802012-09-28 14:15:14 +0000499 }
Paul Bakker23f36802012-09-28 14:15:14 +0000500 }
501
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200502 /* Some key echanges do not need signatures at all */
503 SSL_DEBUG_MSG( 3, ( "no signature_algorithm in common" ) );
504 return( 0 );
505
506have_sig_alg:
Paul Bakker23f36802012-09-28 14:15:14 +0000507 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
508 ssl->handshake->sig_alg ) );
509
510 return( 0 );
511}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200512#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker23f36802012-09-28 14:15:14 +0000513
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200514#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200515static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
516 const unsigned char *buf,
517 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100518{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200519 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100520 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200521 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100522
523 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
524 if( list_size + 2 != len ||
525 list_size % 2 != 0 )
526 {
527 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
528 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
529 }
530
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200531 /* Should never happen unless client duplicates the extension */
532 if( ssl->handshake->curves != NULL )
533 {
534 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
535 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
536 }
537
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +0100538 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200539 * and leave room for a final 0 */
540 our_size = list_size / 2 + 1;
541 if( our_size > POLARSSL_ECP_DP_MAX )
542 our_size = POLARSSL_ECP_DP_MAX;
543
544 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
545 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
546
Paul Bakker9af723c2014-05-01 13:03:14 +0200547 /* explicit void pointer cast for buggy MS compiler */
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200548 memset( (void *) curves, 0, our_size * sizeof( *curves ) );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200549 ssl->handshake->curves = curves;
550
Paul Bakker41c83d32013-03-20 14:39:14 +0100551 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200552 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100553 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200554 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200555
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200556 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100557 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200558 *curves++ = curve_info;
559 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100560 }
561
562 list_size -= 2;
563 p += 2;
564 }
565
566 return( 0 );
567}
568
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200569static int ssl_parse_supported_point_formats( ssl_context *ssl,
570 const unsigned char *buf,
571 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100572{
573 size_t list_size;
574 const unsigned char *p;
575
576 list_size = buf[0];
577 if( list_size + 1 != len )
578 {
579 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
580 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
581 }
582
583 p = buf + 2;
584 while( list_size > 0 )
585 {
586 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
587 p[0] == POLARSSL_ECP_PF_COMPRESSED )
588 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200589 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200590 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100591 return( 0 );
592 }
593
594 list_size--;
595 p++;
596 }
597
598 return( 0 );
599}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200600#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100601
Paul Bakker05decb22013-08-15 13:33:48 +0200602#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200603static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
604 const unsigned char *buf,
605 size_t len )
606{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200607 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200608 {
609 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
610 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
611 }
612
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200613 ssl->session_negotiate->mfl_code = buf[0];
614
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200615 return( 0 );
616}
Paul Bakker05decb22013-08-15 13:33:48 +0200617#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200618
Paul Bakker1f2bc622013-08-15 13:45:55 +0200619#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200620static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
621 const unsigned char *buf,
622 size_t len )
623{
624 if( len != 0 )
625 {
626 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
627 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
628 }
629
630 ((void) buf);
631
632 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
633
634 return( 0 );
635}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200636#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200637
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100638#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
639static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
640 const unsigned char *buf,
641 size_t len )
642{
643 if( len != 0 )
644 {
645 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
646 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
647 }
648
649 ((void) buf);
650
651 if( ssl->encrypt_then_mac == SSL_ETM_ENABLED &&
652 ssl->minor_ver != SSL_MINOR_VERSION_0 )
653 {
654 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
655 }
656
657 return( 0 );
658}
659#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
660
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200661#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
662static int ssl_parse_extended_ms_ext( ssl_context *ssl,
663 const unsigned char *buf,
664 size_t len )
665{
666 if( len != 0 )
667 {
668 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
669 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
670 }
671
672 ((void) buf);
673
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200674 if( ssl->extended_ms == SSL_EXTENDED_MS_ENABLED &&
675 ssl->minor_ver != SSL_MINOR_VERSION_0 )
676 {
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200677 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200678 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200679
680 return( 0 );
681}
682#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
683
Paul Bakkera503a632013-08-14 13:48:06 +0200684#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200685static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200686 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200687 size_t len )
688{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200689 int ret;
690
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200691 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
692 return( 0 );
693
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200694 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200695 ssl->handshake->new_session_ticket = 1;
696
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200697 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
698
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200699 if( len == 0 )
700 return( 0 );
701
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200702 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
703 {
704 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
705 return( 0 );
706 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200707
708 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200709 * Failures are ok: just ignore the ticket and proceed.
710 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200711 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
712 {
713 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200714 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200715 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200716
717 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
718
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200719 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200720
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200721 /* Don't send a new ticket after all, this one is OK */
722 ssl->handshake->new_session_ticket = 0;
723
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200724 return( 0 );
725}
Paul Bakkera503a632013-08-14 13:48:06 +0200726#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200727
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200728#if defined(POLARSSL_SSL_ALPN)
729static int ssl_parse_alpn_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200730 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200731{
Paul Bakker14b16c62014-05-28 11:33:54 +0200732 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200733 const unsigned char *theirs, *start, *end;
734 const char **ours;
735
736 /* If ALPN not configured, just ignore the extension */
737 if( ssl->alpn_list == NULL )
738 return( 0 );
739
740 /*
741 * opaque ProtocolName<1..2^8-1>;
742 *
743 * struct {
744 * ProtocolName protocol_name_list<2..2^16-1>
745 * } ProtocolNameList;
746 */
747
748 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
749 if( len < 4 )
750 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
751
752 list_len = ( buf[0] << 8 ) | buf[1];
753 if( list_len != len - 2 )
754 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
755
756 /*
757 * Use our order of preference
758 */
759 start = buf + 2;
760 end = buf + len;
761 for( ours = ssl->alpn_list; *ours != NULL; ours++ )
762 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200763 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200764 for( theirs = start; theirs != end; theirs += cur_len )
765 {
766 /* If the list is well formed, we should get equality first */
767 if( theirs > end )
768 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
769
770 cur_len = *theirs++;
771
772 /* Empty strings MUST NOT be included */
773 if( cur_len == 0 )
774 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
775
Paul Bakker14b16c62014-05-28 11:33:54 +0200776 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200777 memcmp( theirs, *ours, cur_len ) == 0 )
778 {
779 ssl->alpn_chosen = *ours;
780 return( 0 );
781 }
782 }
783 }
784
785 /* If we get there, no match was found */
786 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
787 SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
788 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
789}
790#endif /* POLARSSL_SSL_ALPN */
791
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100792/*
793 * Auxiliary functions for ServerHello parsing and related actions
794 */
795
796#if defined(POLARSSL_X509_CRT_PARSE_C)
797/*
798 * Return 1 if the given EC key uses the given curve, 0 otherwise
799 */
800#if defined(POLARSSL_ECDSA_C)
801static int ssl_key_matches_curves( pk_context *pk,
802 const ecp_curve_info **curves )
803{
804 const ecp_curve_info **crv = curves;
805 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
806
807 while( *crv != NULL )
808 {
809 if( (*crv)->grp_id == grp_id )
810 return( 1 );
811 crv++;
812 }
813
814 return( 0 );
815}
816#endif /* POLARSSL_ECDSA_C */
817
818/*
819 * Try picking a certificate for this ciphersuite,
820 * return 0 on success and -1 on failure.
821 */
822static int ssl_pick_cert( ssl_context *ssl,
823 const ssl_ciphersuite_t * ciphersuite_info )
824{
825 ssl_key_cert *cur, *list;
826 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
827
828#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
829 if( ssl->handshake->sni_key_cert != NULL )
830 list = ssl->handshake->sni_key_cert;
831 else
832#endif
833 list = ssl->handshake->key_cert;
834
835 if( pk_alg == POLARSSL_PK_NONE )
836 return( 0 );
837
838 for( cur = list; cur != NULL; cur = cur->next )
839 {
840 if( ! pk_can_do( cur->key, pk_alg ) )
841 continue;
842
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200843 /*
844 * This avoids sending the client a cert it'll reject based on
845 * keyUsage or other extensions.
846 *
847 * It also allows the user to provision different certificates for
848 * different uses based on keyUsage, eg if they want to avoid signing
849 * and decrypting with the same RSA key.
850 */
851 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
852 SSL_IS_SERVER ) != 0 )
853 {
854 continue;
855 }
856
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100857#if defined(POLARSSL_ECDSA_C)
858 if( pk_alg == POLARSSL_PK_ECDSA )
859 {
860 if( ssl_key_matches_curves( cur->key, ssl->handshake->curves ) )
861 break;
862 }
863 else
864#endif
865 break;
866 }
867
868 if( cur == NULL )
869 return( -1 );
870
871 ssl->handshake->key_cert = cur;
872 return( 0 );
873}
874#endif /* POLARSSL_X509_CRT_PARSE_C */
875
876/*
877 * Check if a given ciphersuite is suitable for use with our config/keys/etc
878 * Sets ciphersuite_info only if the suite matches.
879 */
880static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
881 const ssl_ciphersuite_t **ciphersuite_info )
882{
883 const ssl_ciphersuite_t *suite_info;
884
885 suite_info = ssl_ciphersuite_from_id( suite_id );
886 if( suite_info == NULL )
887 {
888 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", suite_id ) );
889 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
890 }
891
892 if( suite_info->min_minor_ver > ssl->minor_ver ||
893 suite_info->max_minor_ver < ssl->minor_ver )
894 return( 0 );
895
896#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
897 if( ssl_ciphersuite_uses_ec( suite_info ) &&
898 ( ssl->handshake->curves == NULL ||
899 ssl->handshake->curves[0] == NULL ) )
900 return( 0 );
901#endif
902
903#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
904 /* If the ciphersuite requires a pre-shared key and we don't
905 * have one, skip it now rather than failing later */
906 if( ssl_ciphersuite_uses_psk( suite_info ) &&
907 ssl->f_psk == NULL &&
908 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
909 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
910 return( 0 );
911#endif
912
913#if defined(POLARSSL_X509_CRT_PARSE_C)
914 /*
915 * Final check: if ciphersuite requires us to have a
916 * certificate/key of a particular type:
917 * - select the appropriate certificate if we have one, or
918 * - try the next ciphersuite if we don't
919 * This must be done last since we modify the key_cert list.
920 */
921 if( ssl_pick_cert( ssl, suite_info ) != 0 )
922 return( 0 );
923#endif
924
925 *ciphersuite_info = suite_info;
926 return( 0 );
927}
928
Paul Bakker78a8c712013-03-06 17:01:52 +0100929#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
930static int ssl_parse_client_hello_v2( ssl_context *ssl )
931{
932 int ret;
933 unsigned int i, j;
934 size_t n;
935 unsigned int ciph_len, sess_len, chal_len;
936 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200937 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200938 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100939
940 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
941
942 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
943 {
944 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
945
946 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
947 return( ret );
948
949 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
950 }
951
952 buf = ssl->in_hdr;
953
954 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
955
956 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
957 buf[2] ) );
958 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
959 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
960 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
961 buf[3], buf[4] ) );
962
963 /*
964 * SSLv2 Client Hello
965 *
966 * Record layer:
967 * 0 . 1 message length
968 *
969 * SSL layer:
970 * 2 . 2 message type
971 * 3 . 4 protocol version
972 */
973 if( buf[2] != SSL_HS_CLIENT_HELLO ||
974 buf[3] != SSL_MAJOR_VERSION_3 )
975 {
976 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
977 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
978 }
979
980 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
981
982 if( n < 17 || n > 512 )
983 {
984 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
985 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
986 }
987
988 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200989 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
990 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100991
992 if( ssl->minor_ver < ssl->min_minor_ver )
993 {
994 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200995 " [%d:%d] < [%d:%d]",
996 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +0100997 ssl->min_major_ver, ssl->min_minor_ver ) );
998
999 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1000 SSL_ALERT_MSG_PROTOCOL_VERSION );
1001 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1002 }
1003
Paul Bakker2fbefde2013-06-29 16:01:15 +02001004 ssl->handshake->max_major_ver = buf[3];
1005 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +01001006
1007 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
1008 {
1009 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1010 return( ret );
1011 }
1012
1013 ssl->handshake->update_checksum( ssl, buf + 2, n );
1014
1015 buf = ssl->in_msg;
1016 n = ssl->in_left - 5;
1017
1018 /*
1019 * 0 . 1 ciphersuitelist length
1020 * 2 . 3 session id length
1021 * 4 . 5 challenge length
1022 * 6 . .. ciphersuitelist
1023 * .. . .. session id
1024 * .. . .. challenge
1025 */
1026 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1027
1028 ciph_len = ( buf[0] << 8 ) | buf[1];
1029 sess_len = ( buf[2] << 8 ) | buf[3];
1030 chal_len = ( buf[4] << 8 ) | buf[5];
1031
1032 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
1033 ciph_len, sess_len, chal_len ) );
1034
1035 /*
1036 * Make sure each parameter length is valid
1037 */
1038 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1039 {
1040 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1041 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1042 }
1043
1044 if( sess_len > 32 )
1045 {
1046 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1047 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1048 }
1049
1050 if( chal_len < 8 || chal_len > 32 )
1051 {
1052 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1053 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1054 }
1055
1056 if( n != 6 + ciph_len + sess_len + chal_len )
1057 {
1058 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1059 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1060 }
1061
1062 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1063 buf + 6, ciph_len );
1064 SSL_DEBUG_BUF( 3, "client hello, session id",
1065 buf + 6 + ciph_len, sess_len );
1066 SSL_DEBUG_BUF( 3, "client hello, challenge",
1067 buf + 6 + ciph_len + sess_len, chal_len );
1068
1069 p = buf + 6 + ciph_len;
1070 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001071 memset( ssl->session_negotiate->id, 0,
1072 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001073 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1074
1075 p += sess_len;
1076 memset( ssl->handshake->randbytes, 0, 64 );
1077 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1078
1079 /*
1080 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1081 */
1082 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1083 {
1084 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1085 {
1086 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1087 if( ssl->renegotiation == SSL_RENEGOTIATION )
1088 {
1089 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1090
1091 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1092 return( ret );
1093
1094 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1095 }
1096 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1097 break;
1098 }
1099 }
1100
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001101#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1102 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1103 {
1104 if( p[0] == 0 &&
1105 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1106 p[2] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1107 {
1108 SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
1109
1110 if( ssl->minor_ver < ssl->max_minor_ver )
1111 {
1112 SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
1113
1114 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1115 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1116
1117 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1118 }
1119
1120 break;
1121 }
1122 }
1123#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1124
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001125 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001126 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001127#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1128 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1129 {
1130 for( i = 0; ciphersuites[i] != 0; i++ )
1131#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001132 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001133 {
1134 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001135#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001136 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001137 if( p[0] != 0 ||
1138 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1139 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1140 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001141
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001142 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1143 &ciphersuite_info ) ) != 0 )
1144 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001145
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001146 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001147 goto have_ciphersuite_v2;
1148 }
1149 }
1150
1151 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1152
1153 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1154
1155have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001156 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001157 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001158 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001159
1160 /*
1161 * SSLv2 Client Hello relevant renegotiation security checks
1162 */
1163 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1164 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1165 {
1166 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1167
1168 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1169 return( ret );
1170
1171 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1172 }
1173
1174 ssl->in_left = 0;
1175 ssl->state++;
1176
1177 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1178
1179 return( 0 );
1180}
1181#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1182
Paul Bakker5121ce52009-01-03 21:22:43 +00001183static int ssl_parse_client_hello( ssl_context *ssl )
1184{
Paul Bakker23986e52011-04-24 08:57:21 +00001185 int ret;
1186 unsigned int i, j;
1187 size_t n;
1188 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001189 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001190 unsigned int ext_len = 0;
1191 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001192 int renegotiation_info_seen = 0;
1193 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001194 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001195 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001196
1197 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1198
Paul Bakker48916f92012-09-16 19:57:18 +00001199 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1200 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001201 {
1202 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1203 return( ret );
1204 }
1205
1206 buf = ssl->in_hdr;
1207
Paul Bakker78a8c712013-03-06 17:01:52 +01001208#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1209 if( ( buf[0] & 0x80 ) != 0 )
1210 return ssl_parse_client_hello_v2( ssl );
1211#endif
1212
Paul Bakkerec636f32012-09-09 19:17:02 +00001213 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1214
1215 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1216 buf[0] ) );
1217 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1218 ( buf[3] << 8 ) | buf[4] ) );
1219 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
1220 buf[1], buf[2] ) );
1221
1222 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001223 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001224 *
1225 * Record layer:
1226 * 0 . 0 message type
1227 * 1 . 2 protocol version
1228 * 3 . 4 message length
1229 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001230
1231 /* According to RFC 5246 Appendix E.1, the version here is typically
1232 * "{03,00}, the lowest version number supported by the client, [or] the
1233 * value of ClientHello.client_version", so the only meaningful check here
1234 * is the major version shouldn't be less than 3 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001235 if( buf[0] != SSL_MSG_HANDSHAKE ||
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001236 buf[1] < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001238 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1239 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1240 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001241
Paul Bakkerec636f32012-09-09 19:17:02 +00001242 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +00001243
Paul Bakker4f42c112014-04-17 14:48:23 +02001244 if( n < 45 || n > SSL_MAX_CONTENT_LEN )
Paul Bakkerec636f32012-09-09 19:17:02 +00001245 {
1246 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1247 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1248 }
1249
Paul Bakker48916f92012-09-16 19:57:18 +00001250 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1251 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001252 {
1253 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1254 return( ret );
1255 }
1256
1257 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +00001258 if( !ssl->renegotiation )
1259 n = ssl->in_left - 5;
1260 else
1261 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +00001262
Paul Bakker48916f92012-09-16 19:57:18 +00001263 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +00001264
1265 /*
1266 * SSL layer:
1267 * 0 . 0 handshake type
1268 * 1 . 3 handshake length
1269 * 4 . 5 protocol version
1270 * 6 . 9 UNIX time()
1271 * 10 . 37 random bytes
1272 * 38 . 38 session id length
1273 * 39 . 38+x session id
1274 * 39+x . 40+x ciphersuitelist length
Paul Bakker0f651c72014-05-22 15:12:19 +02001275 * 41+x . 40+y ciphersuitelist
1276 * 41+y . 41+y compression alg length
1277 * 42+y . 41+z compression algs
Paul Bakkerec636f32012-09-09 19:17:02 +00001278 * .. . .. extensions
1279 */
1280 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1281
1282 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1283 buf[0] ) );
1284 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1285 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1286 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1287 buf[4], buf[5] ) );
1288
1289 /*
1290 * Check the handshake type and protocol version
1291 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001292 if( buf[0] != SSL_HS_CLIENT_HELLO )
Paul Bakkerec636f32012-09-09 19:17:02 +00001293 {
1294 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1295 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1296 }
1297
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001298 ssl->major_ver = buf[4];
1299 ssl->minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001300
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001301 ssl->handshake->max_major_ver = ssl->major_ver;
1302 ssl->handshake->max_minor_ver = ssl->minor_ver;
1303
1304 if( ssl->major_ver < ssl->min_major_ver ||
1305 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001306 {
1307 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001308 " [%d:%d] < [%d:%d]",
1309 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001310 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001311
1312 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1313 SSL_ALERT_MSG_PROTOCOL_VERSION );
1314
1315 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1316 }
1317
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001318 if( ssl->major_ver > ssl->max_major_ver )
1319 {
1320 ssl->major_ver = ssl->max_major_ver;
1321 ssl->minor_ver = ssl->max_minor_ver;
1322 }
1323 else if( ssl->minor_ver > ssl->max_minor_ver )
1324 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001325
Paul Bakker48916f92012-09-16 19:57:18 +00001326 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001327
1328 /*
1329 * Check the handshake message length
1330 */
1331 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1332 {
1333 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1334 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1335 }
1336
1337 /*
1338 * Check the session length
1339 */
1340 sess_len = buf[38];
1341
Paul Bakker0f651c72014-05-22 15:12:19 +02001342 if( sess_len > 32 || sess_len > n - 42 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001343 {
1344 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1345 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1346 }
1347
Paul Bakker48916f92012-09-16 19:57:18 +00001348 ssl->session_negotiate->length = sess_len;
1349 memset( ssl->session_negotiate->id, 0,
1350 sizeof( ssl->session_negotiate->id ) );
1351 memcpy( ssl->session_negotiate->id, buf + 39,
1352 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001353
1354 /*
1355 * Check the ciphersuitelist length
1356 */
1357 ciph_len = ( buf[39 + sess_len] << 8 )
1358 | ( buf[40 + sess_len] );
1359
Paul Bakker0f651c72014-05-22 15:12:19 +02001360 if( ciph_len < 2 || ( ciph_len % 2 ) != 0 || ciph_len > n - 42 - sess_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001361 {
1362 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1363 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1364 }
1365
1366 /*
1367 * Check the compression algorithms length
1368 */
1369 comp_len = buf[41 + sess_len + ciph_len];
1370
Paul Bakker0f651c72014-05-22 15:12:19 +02001371 if( comp_len < 1 || comp_len > 16 ||
1372 comp_len > n - 42 - sess_len - ciph_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001373 {
1374 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1375 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1376 }
1377
Paul Bakker48916f92012-09-16 19:57:18 +00001378 /*
1379 * Check the extension length
1380 */
1381 if( n > 42 + sess_len + ciph_len + comp_len )
1382 {
1383 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1384 | ( buf[43 + sess_len + ciph_len + comp_len] );
1385
1386 if( ( ext_len > 0 && ext_len < 4 ) ||
1387 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1388 {
1389 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1390 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1391 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1392 }
1393 }
1394
1395 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001396#if defined(POLARSSL_ZLIB_SUPPORT)
1397 for( i = 0; i < comp_len; ++i )
1398 {
Paul Bakker48916f92012-09-16 19:57:18 +00001399 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001400 {
Paul Bakker48916f92012-09-16 19:57:18 +00001401 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001402 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001403 }
1404 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001405#endif
1406
Paul Bakkerec636f32012-09-09 19:17:02 +00001407 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1408 buf + 6, 32 );
1409 SSL_DEBUG_BUF( 3, "client hello, session id",
1410 buf + 38, sess_len );
1411 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1412 buf + 41 + sess_len, ciph_len );
1413 SSL_DEBUG_BUF( 3, "client hello, compression",
1414 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001415
Paul Bakkerec636f32012-09-09 19:17:02 +00001416 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001417 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1418 */
1419 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1420 {
1421 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1422 {
1423 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1424 if( ssl->renegotiation == SSL_RENEGOTIATION )
1425 {
1426 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001427
1428 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1429 return( ret );
1430
Paul Bakker48916f92012-09-16 19:57:18 +00001431 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1432 }
1433 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1434 break;
1435 }
1436 }
1437
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001438#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1439 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1440 {
1441 if( p[0] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1442 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1443 {
1444 SSL_DEBUG_MSG( 0, ( "received FALLBACK_SCSV" ) );
1445
1446 if( ssl->minor_ver < ssl->max_minor_ver )
1447 {
1448 SSL_DEBUG_MSG( 0, ( "inapropriate fallback" ) );
1449
1450 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1451 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1452
1453 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1454 }
1455
1456 break;
1457 }
1458 }
1459#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1460
Paul Bakker48916f92012-09-16 19:57:18 +00001461 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001462
1463 while( ext_len )
1464 {
1465 unsigned int ext_id = ( ( ext[0] << 8 )
1466 | ( ext[1] ) );
1467 unsigned int ext_size = ( ( ext[2] << 8 )
1468 | ( ext[3] ) );
1469
1470 if( ext_size + 4 > ext_len )
1471 {
1472 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1473 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1474 }
1475 switch( ext_id )
1476 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001477#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001478 case TLS_EXT_SERVERNAME:
1479 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1480 if( ssl->f_sni == NULL )
1481 break;
1482
1483 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1484 if( ret != 0 )
1485 return( ret );
1486 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001487#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001488
Paul Bakker48916f92012-09-16 19:57:18 +00001489 case TLS_EXT_RENEGOTIATION_INFO:
1490 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1491 renegotiation_info_seen = 1;
1492
Paul Bakker23f36802012-09-28 14:15:14 +00001493 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1494 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001495 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001496 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001497
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001498#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001499 case TLS_EXT_SIG_ALG:
1500 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1501 if( ssl->renegotiation == SSL_RENEGOTIATION )
1502 break;
1503
1504 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1505 if( ret != 0 )
1506 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001507 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001508#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001509
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001510#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001511 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1512 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1513
1514 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1515 if( ret != 0 )
1516 return( ret );
1517 break;
1518
1519 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1520 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001521 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001522
1523 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1524 if( ret != 0 )
1525 return( ret );
1526 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001527#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001528
Paul Bakker05decb22013-08-15 13:33:48 +02001529#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001530 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1531 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1532
1533 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1534 if( ret != 0 )
1535 return( ret );
1536 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001537#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001538
Paul Bakker1f2bc622013-08-15 13:45:55 +02001539#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001540 case TLS_EXT_TRUNCATED_HMAC:
1541 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1542
1543 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1544 if( ret != 0 )
1545 return( ret );
1546 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001547#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001548
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001549#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1550 case TLS_EXT_ENCRYPT_THEN_MAC:
1551 SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
1552
1553 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1554 if( ret != 0 )
1555 return( ret );
1556 break;
1557#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1558
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001559#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1560 case TLS_EXT_EXTENDED_MASTER_SECRET:
1561 SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
1562
1563 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1564 if( ret != 0 )
1565 return( ret );
1566 break;
1567#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1568
Paul Bakkera503a632013-08-14 13:48:06 +02001569#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001570 case TLS_EXT_SESSION_TICKET:
1571 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1572
1573 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1574 if( ret != 0 )
1575 return( ret );
1576 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001577#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001578
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001579#if defined(POLARSSL_SSL_ALPN)
1580 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001581 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001582
1583 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1584 if( ret != 0 )
1585 return( ret );
1586 break;
1587#endif /* POLARSSL_SSL_SESSION_TICKETS */
1588
Paul Bakker48916f92012-09-16 19:57:18 +00001589 default:
1590 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1591 ext_id ) );
1592 }
1593
1594 ext_len -= 4 + ext_size;
1595 ext += 4 + ext_size;
1596
1597 if( ext_len > 0 && ext_len < 4 )
1598 {
1599 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1600 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1601 }
1602 }
1603
1604 /*
1605 * Renegotiation security checks
1606 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001607 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1608 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1609 {
1610 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1611 handshake_failure = 1;
1612 }
1613 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1614 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1615 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001616 {
1617 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001618 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001619 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001620 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1621 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1622 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001623 {
1624 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001625 handshake_failure = 1;
1626 }
1627 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1628 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1629 renegotiation_info_seen == 1 )
1630 {
1631 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1632 handshake_failure = 1;
1633 }
1634
1635 if( handshake_failure == 1 )
1636 {
1637 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1638 return( ret );
1639
Paul Bakker48916f92012-09-16 19:57:18 +00001640 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1641 }
Paul Bakker380da532012-04-18 16:10:25 +00001642
Paul Bakker41c83d32013-03-20 14:39:14 +01001643 /*
1644 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001645 * (At the end because we need information from the EC-based extensions
1646 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001647 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001648 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001649 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001650#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1651 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
1652 {
1653 for( i = 0; ciphersuites[i] != 0; i++ )
1654#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001655 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001656 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001657 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001658#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001659 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001660 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1661 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1662 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001663
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001664 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1665 &ciphersuite_info ) ) != 0 )
1666 return( ret );
1667
1668 if( ciphersuite_info != NULL )
1669 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001670 }
1671 }
1672
1673 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1674
1675 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1676 return( ret );
1677
1678 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1679
1680have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001681 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001682 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1683 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1684
Paul Bakker5121ce52009-01-03 21:22:43 +00001685 ssl->in_left = 0;
1686 ssl->state++;
1687
1688 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1689
1690 return( 0 );
1691}
1692
Paul Bakker1f2bc622013-08-15 13:45:55 +02001693#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001694static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1695 unsigned char *buf,
1696 size_t *olen )
1697{
1698 unsigned char *p = buf;
1699
1700 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1701 {
1702 *olen = 0;
1703 return;
1704 }
1705
1706 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1707
1708 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1709 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1710
1711 *p++ = 0x00;
1712 *p++ = 0x00;
1713
1714 *olen = 4;
1715}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001716#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001717
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001718#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1719static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
1720 unsigned char *buf,
1721 size_t *olen )
1722{
1723 unsigned char *p = buf;
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01001724 const ssl_ciphersuite_t *suite = NULL;
1725 const cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001726
1727 if( ssl->session_negotiate->encrypt_then_mac == SSL_EXTENDED_MS_DISABLED ||
1728 ssl->minor_ver == SSL_MINOR_VERSION_0 )
1729 {
1730 *olen = 0;
1731 return;
1732 }
1733
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01001734 /*
1735 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
1736 * from a client and then selects a stream or Authenticated Encryption
1737 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
1738 * encrypt-then-MAC response extension back to the client."
1739 */
1740 if( ( suite = ssl_ciphersuite_from_id(
1741 ssl->session_negotiate->ciphersuite ) ) == NULL ||
1742 ( cipher = cipher_info_from_type( suite->cipher ) ) == NULL ||
1743 cipher->mode != POLARSSL_MODE_CBC )
1744 {
1745 *olen = 0;
1746 return;
1747 }
1748
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001749 SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
1750
1751 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
1752 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
1753
1754 *p++ = 0x00;
1755 *p++ = 0x00;
1756
1757 *olen = 4;
1758}
1759#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1760
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001761#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1762static void ssl_write_extended_ms_ext( ssl_context *ssl,
1763 unsigned char *buf,
1764 size_t *olen )
1765{
1766 unsigned char *p = buf;
1767
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +02001768 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_DISABLED ||
1769 ssl->minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001770 {
1771 *olen = 0;
1772 return;
1773 }
1774
1775 SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
1776 "extension" ) );
1777
1778 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
1779 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
1780
1781 *p++ = 0x00;
1782 *p++ = 0x00;
1783
1784 *olen = 4;
1785}
1786#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1787
Paul Bakkera503a632013-08-14 13:48:06 +02001788#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001789static void ssl_write_session_ticket_ext( ssl_context *ssl,
1790 unsigned char *buf,
1791 size_t *olen )
1792{
1793 unsigned char *p = buf;
1794
1795 if( ssl->handshake->new_session_ticket == 0 )
1796 {
1797 *olen = 0;
1798 return;
1799 }
1800
1801 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1802
1803 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1804 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1805
1806 *p++ = 0x00;
1807 *p++ = 0x00;
1808
1809 *olen = 4;
1810}
Paul Bakkera503a632013-08-14 13:48:06 +02001811#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001812
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001813static void ssl_write_renegotiation_ext( ssl_context *ssl,
1814 unsigned char *buf,
1815 size_t *olen )
1816{
1817 unsigned char *p = buf;
1818
1819 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1820 {
1821 *olen = 0;
1822 return;
1823 }
1824
1825 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1826
1827 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1828 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1829
1830 *p++ = 0x00;
1831 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1832 *p++ = ssl->verify_data_len * 2 & 0xFF;
1833
1834 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1835 p += ssl->verify_data_len;
1836 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1837 p += ssl->verify_data_len;
1838
1839 *olen = 5 + ssl->verify_data_len * 2;
1840}
1841
Paul Bakker05decb22013-08-15 13:33:48 +02001842#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001843static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1844 unsigned char *buf,
1845 size_t *olen )
1846{
1847 unsigned char *p = buf;
1848
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001849 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1850 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001851 *olen = 0;
1852 return;
1853 }
1854
1855 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1856
1857 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1858 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1859
1860 *p++ = 0x00;
1861 *p++ = 1;
1862
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001863 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001864
1865 *olen = 5;
1866}
Paul Bakker05decb22013-08-15 13:33:48 +02001867#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001868
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001869#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001870static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1871 unsigned char *buf,
1872 size_t *olen )
1873{
1874 unsigned char *p = buf;
1875 ((void) ssl);
1876
Paul Bakker677377f2013-10-28 12:54:26 +01001877 if( ( ssl->handshake->cli_exts &
1878 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
1879 {
1880 *olen = 0;
1881 return;
1882 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001883
1884 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1885
1886 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1887 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1888
1889 *p++ = 0x00;
1890 *p++ = 2;
1891
1892 *p++ = 1;
1893 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1894
1895 *olen = 6;
1896}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001897#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001898
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001899#if defined(POLARSSL_SSL_ALPN )
1900static void ssl_write_alpn_ext( ssl_context *ssl,
1901 unsigned char *buf, size_t *olen )
1902{
1903 if( ssl->alpn_chosen == NULL )
1904 {
1905 *olen = 0;
1906 return;
1907 }
1908
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001909 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001910
1911 /*
1912 * 0 . 1 ext identifier
1913 * 2 . 3 ext length
1914 * 4 . 5 protocol list length
1915 * 6 . 6 protocol name length
1916 * 7 . 7+n protocol name
1917 */
1918 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
1919 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
1920
1921 *olen = 7 + strlen( ssl->alpn_chosen );
1922
1923 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
1924 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
1925
1926 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
1927 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
1928
1929 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
1930
1931 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
1932}
1933#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
1934
Paul Bakker5121ce52009-01-03 21:22:43 +00001935static int ssl_write_server_hello( ssl_context *ssl )
1936{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001937#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001938 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001939#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001940 int ret;
1941 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001942 unsigned char *buf, *p;
1943
1944 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1945
Paul Bakkera9a028e2013-11-21 17:31:06 +01001946 if( ssl->f_rng == NULL )
1947 {
1948 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
1949 return( POLARSSL_ERR_SSL_NO_RNG );
1950 }
1951
Paul Bakker5121ce52009-01-03 21:22:43 +00001952 /*
1953 * 0 . 0 handshake type
1954 * 1 . 3 handshake length
1955 * 4 . 5 protocol version
1956 * 6 . 9 UNIX time()
1957 * 10 . 37 random bytes
1958 */
1959 buf = ssl->out_msg;
1960 p = buf + 4;
1961
1962 *p++ = (unsigned char) ssl->major_ver;
1963 *p++ = (unsigned char) ssl->minor_ver;
1964
1965 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1966 buf[4], buf[5] ) );
1967
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001968#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001969 t = time( NULL );
1970 *p++ = (unsigned char)( t >> 24 );
1971 *p++ = (unsigned char)( t >> 16 );
1972 *p++ = (unsigned char)( t >> 8 );
1973 *p++ = (unsigned char)( t );
1974
1975 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001976#else
1977 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1978 return( ret );
1979
1980 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02001981#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001982
Paul Bakkera3d195c2011-11-27 21:07:34 +00001983 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1984 return( ret );
1985
1986 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001987
Paul Bakker48916f92012-09-16 19:57:18 +00001988 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001989
1990 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1991
1992 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001993 * Resume is 0 by default, see ssl_handshake_init().
1994 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1995 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001996 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001997 if( ssl->handshake->resume == 0 &&
1998 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001999 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002000 ssl->f_get_cache != NULL &&
2001 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
2002 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01002003 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002004 ssl->handshake->resume = 1;
2005 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002006
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002007 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002008 {
2009 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002010 * New session, create a new session id,
2011 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002012 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002013 ssl->state++;
2014
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002015#if defined(POLARSSL_HAVE_TIME)
2016 ssl->session_negotiate->start = time( NULL );
2017#endif
2018
Paul Bakkera503a632013-08-14 13:48:06 +02002019#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002020 if( ssl->handshake->new_session_ticket != 0 )
2021 {
2022 ssl->session_negotiate->length = n = 0;
2023 memset( ssl->session_negotiate->id, 0, 32 );
2024 }
2025 else
2026#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002027 {
2028 ssl->session_negotiate->length = n = 32;
2029 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002030 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002031 return( ret );
2032 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002033 }
2034 else
2035 {
2036 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002037 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002038 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02002039 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002040 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002041
2042 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2043 {
2044 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2045 return( ret );
2046 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002047 }
2048
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002049 /*
2050 * 38 . 38 session id length
2051 * 39 . 38+n session id
2052 * 39+n . 40+n chosen ciphersuite
2053 * 41+n . 41+n chosen compression alg.
2054 * 42+n . 43+n extensions length
2055 * 44+n . 43+n+m extensions
2056 */
2057 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00002058 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
2059 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002060
2061 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2062 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2063 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002064 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002065
Paul Bakker48916f92012-09-16 19:57:18 +00002066 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2067 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2068 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002069
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02002070 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2071 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02002072 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002073 ssl->session_negotiate->compression ) );
2074
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002075 /*
2076 * First write extensions, then the total length
2077 */
2078 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2079 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002080
Paul Bakker05decb22013-08-15 13:33:48 +02002081#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002082 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2083 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002084#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002085
Paul Bakker1f2bc622013-08-15 13:45:55 +02002086#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002087 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2088 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002089#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002090
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002091#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2092 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2093 ext_len += olen;
2094#endif
2095
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002096#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2097 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2098 ext_len += olen;
2099#endif
2100
Paul Bakkera503a632013-08-14 13:48:06 +02002101#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002102 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2103 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002104#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002105
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002106#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002107 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2108 ext_len += olen;
2109#endif
2110
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002111#if defined(POLARSSL_SSL_ALPN)
2112 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2113 ext_len += olen;
2114#endif
2115
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002116 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002117
Paul Bakkera7036632014-04-30 10:15:38 +02002118 if( ext_len > 0 )
2119 {
2120 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2121 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2122 p += ext_len;
2123 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002124
2125 ssl->out_msglen = p - buf;
2126 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2127 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
2128
2129 ret = ssl_write_record( ssl );
2130
2131 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2132
2133 return( ret );
2134}
2135
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002136#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2137 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002138 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2139 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002140static int ssl_write_certificate_request( ssl_context *ssl )
2141{
Paul Bakkered27a042013-04-18 22:46:23 +02002142 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002143
2144 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2145
2146 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002147 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002148 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2149 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002150 {
2151 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2152 ssl->state++;
2153 return( 0 );
2154 }
2155
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002156 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2157 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002158}
2159#else
2160static int ssl_write_certificate_request( ssl_context *ssl )
2161{
2162 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2163 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002164 size_t dn_size, total_dn_size; /* excluding length bytes */
2165 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002166 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002167 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00002168
2169 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2170
2171 ssl->state++;
2172
Paul Bakkerfbb17802013-04-17 19:10:21 +02002173 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002174 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002175 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002176 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002177 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002178 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002179 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002180 return( 0 );
2181 }
2182
2183 /*
2184 * 0 . 0 handshake type
2185 * 1 . 3 handshake length
2186 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002187 * 5 .. m-1 cert types
2188 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002189 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002190 * n .. n+1 length of all DNs
2191 * n+2 .. n+3 length of DN 1
2192 * n+4 .. ... Distinguished Name #1
2193 * ... .. ... length of DN 2, etc.
2194 */
2195 buf = ssl->out_msg;
2196 p = buf + 4;
2197
2198 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002199 * Supported certificate types
2200 *
2201 * ClientCertificateType certificate_types<1..2^8-1>;
2202 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002203 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002204 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002205
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002206#if defined(POLARSSL_RSA_C)
2207 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2208#endif
2209#if defined(POLARSSL_ECDSA_C)
2210 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2211#endif
2212
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002213 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002214 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002215
Paul Bakker577e0062013-08-28 11:57:20 +02002216 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002217#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002218 /*
2219 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002220 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002221 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2222 *
2223 * struct {
2224 * HashAlgorithm hash;
2225 * SignatureAlgorithm signature;
2226 * } SignatureAndHashAlgorithm;
2227 *
2228 * enum { (255) } HashAlgorithm;
2229 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002230 */
Paul Bakker21dca692013-01-03 11:41:08 +01002231 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002232 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002233 /*
2234 * Only use current running hash algorithm that is already required
2235 * for requested ciphersuite.
2236 */
Paul Bakker926af752012-11-23 13:38:07 +01002237 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2238
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002239 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2240 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002241 {
2242 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2243 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002244
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002245 /*
2246 * Supported signature algorithms
2247 */
2248#if defined(POLARSSL_RSA_C)
2249 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2250 p[2 + sa_len++] = SSL_SIG_RSA;
2251#endif
2252#if defined(POLARSSL_ECDSA_C)
2253 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2254 p[2 + sa_len++] = SSL_SIG_ECDSA;
2255#endif
Paul Bakker926af752012-11-23 13:38:07 +01002256
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002257 p[0] = (unsigned char)( sa_len >> 8 );
2258 p[1] = (unsigned char)( sa_len );
2259 sa_len += 2;
2260 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002261 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002262#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002263
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002264 /*
2265 * DistinguishedName certificate_authorities<0..2^16-1>;
2266 * opaque DistinguishedName<1..2^16-1>;
2267 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002268 p += 2;
2269 crt = ssl->ca_chain;
2270
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002271 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002272 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002273 {
2274 if( p - buf > 4096 )
2275 break;
2276
Paul Bakker926af752012-11-23 13:38:07 +01002277 dn_size = crt->subject_raw.len;
2278 *p++ = (unsigned char)( dn_size >> 8 );
2279 *p++ = (unsigned char)( dn_size );
2280 memcpy( p, crt->subject_raw.p, dn_size );
2281 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002282
Paul Bakker926af752012-11-23 13:38:07 +01002283 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2284
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002285 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002286 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002287 }
2288
Paul Bakker926af752012-11-23 13:38:07 +01002289 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002290 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2291 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002292 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2293 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002294
2295 ret = ssl_write_record( ssl );
2296
2297 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2298
2299 return( ret );
2300}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002301#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2302 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002303 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2304 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002305
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002306#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2307 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2308static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2309{
2310 int ret;
2311
2312 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2313 {
2314 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2315 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2316 }
2317
2318 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2319 pk_ec( *ssl_own_key( ssl ) ),
2320 POLARSSL_ECDH_OURS ) ) != 0 )
2321 {
2322 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2323 return( ret );
2324 }
2325
2326 return( 0 );
2327}
2328#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2329 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2330
Paul Bakker41c83d32013-03-20 14:39:14 +01002331static int ssl_write_server_key_exchange( ssl_context *ssl )
2332{
Paul Bakker23986e52011-04-24 08:57:21 +00002333 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002334 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002335 const ssl_ciphersuite_t *ciphersuite_info =
2336 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002337
2338#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2339 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2340 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002341 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002342 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002343 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002344 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002345 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002346 ((void) dig_signed);
2347 ((void) dig_signed_len);
2348#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002349
Paul Bakker5121ce52009-01-03 21:22:43 +00002350 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2351
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002352#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2353 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2354 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002355 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2356 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2357 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002358 {
2359 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2360 ssl->state++;
2361 return( 0 );
2362 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002363#endif
2364
2365#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2366 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2367 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2368 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2369 {
2370 ssl_get_ecdh_params_from_cert( ssl );
2371
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002372 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002373 ssl->state++;
2374 return( 0 );
2375 }
2376#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002377
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002378#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2379 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2380 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2381 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002382 {
2383 /* TODO: Support identity hints */
2384 *(p++) = 0x00;
2385 *(p++) = 0x00;
2386
2387 n += 2;
2388 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002389#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2390 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002391
2392#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2393 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2394 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2395 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002396 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002397 /*
2398 * Ephemeral DH parameters:
2399 *
2400 * struct {
2401 * opaque dh_p<1..2^16-1>;
2402 * opaque dh_g<1..2^16-1>;
2403 * opaque dh_Ys<1..2^16-1>;
2404 * } ServerDHParams;
2405 */
2406 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2407 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2408 {
2409 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2410 return( ret );
2411 }
Paul Bakker48916f92012-09-16 19:57:18 +00002412
Paul Bakker41c83d32013-03-20 14:39:14 +01002413 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002414 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2415 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002416 {
2417 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2418 return( ret );
2419 }
2420
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002421 dig_signed = p;
2422 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002423
2424 p += len;
2425 n += len;
2426
Paul Bakker41c83d32013-03-20 14:39:14 +01002427 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2428 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2429 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2430 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2431 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002432#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2433 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002434
Gergely Budai987bfb52014-01-19 21:48:42 +01002435#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002436 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002437 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2438 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002439 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002440 /*
2441 * Ephemeral ECDH parameters:
2442 *
2443 * struct {
2444 * ECParameters curve_params;
2445 * ECPoint public;
2446 * } ServerECDHParams;
2447 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002448 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002449#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002450 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002451
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002452 /* Match our preference list against the offered curves */
2453 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2454 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2455 if( (*curve)->grp_id == *gid )
2456 goto curve_matching_done;
2457
2458curve_matching_done:
2459#else
2460 curve = ssl->handshake->curves;
2461#endif
2462
2463 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002464 {
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002465 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2466 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002467 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002468
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002469 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002470
Paul Bakker41c83d32013-03-20 14:39:14 +01002471 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002472 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002473 {
2474 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2475 return( ret );
2476 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002477
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002478 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2479 p, SSL_MAX_CONTENT_LEN - n,
2480 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002481 {
2482 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2483 return( ret );
2484 }
2485
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002486 dig_signed = p;
2487 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002488
2489 p += len;
2490 n += len;
2491
Paul Bakker41c83d32013-03-20 14:39:14 +01002492 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2493 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002494#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002495
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002496#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002497 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2498 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002499 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002500 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2501 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002502 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002503 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002504 unsigned int hashlen = 0;
2505 unsigned char hash[64];
2506 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002507
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002508 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002509 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2510 */
Paul Bakker577e0062013-08-28 11:57:20 +02002511#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002512 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2513 {
2514 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2515
2516 if( md_alg == POLARSSL_MD_NONE )
2517 {
2518 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002519 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002520 }
2521 }
Paul Bakker577e0062013-08-28 11:57:20 +02002522 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002523#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002524#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2525 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002526 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002527 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2528 {
2529 md_alg = POLARSSL_MD_SHA1;
2530 }
2531 else
Paul Bakker577e0062013-08-28 11:57:20 +02002532#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002533 {
2534 md_alg = POLARSSL_MD_NONE;
2535 }
2536
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002537 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002538 * Compute the hash to be signed
2539 */
Paul Bakker577e0062013-08-28 11:57:20 +02002540#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2541 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002542 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002543 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002544 md5_context md5;
2545 sha1_context sha1;
2546
Paul Bakker5b4af392014-06-26 12:09:34 +02002547 md5_init( &md5 );
2548 sha1_init( &sha1 );
2549
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002550 /*
2551 * digitally-signed struct {
2552 * opaque md5_hash[16];
2553 * opaque sha_hash[20];
2554 * };
2555 *
2556 * md5_hash
2557 * MD5(ClientHello.random + ServerHello.random
2558 * + ServerParams);
2559 * sha_hash
2560 * SHA(ClientHello.random + ServerHello.random
2561 * + ServerParams);
2562 */
2563 md5_starts( &md5 );
2564 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002565 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002566 md5_finish( &md5, hash );
2567
2568 sha1_starts( &sha1 );
2569 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002570 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002571 sha1_finish( &sha1, hash + 16 );
2572
2573 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02002574
2575 md5_free( &md5 );
2576 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002577 }
2578 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002579#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2580 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002581#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2582 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002583 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002584 {
2585 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02002586 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002587
Paul Bakker84bbeb52014-07-01 14:53:22 +02002588 md_init( &ctx );
2589
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002590 /* Info from md_alg will be used instead */
2591 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002592
2593 /*
2594 * digitally-signed struct {
2595 * opaque client_random[32];
2596 * opaque server_random[32];
2597 * ServerDHParams params;
2598 * };
2599 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002600 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002601 {
2602 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2603 return( ret );
2604 }
2605
2606 md_starts( &ctx );
2607 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002608 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002609 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002610 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00002611 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002612 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002613#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2614 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002615 {
2616 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002617 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002618 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002619
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002620 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2621 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002622
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002623 /*
2624 * Make the signature
2625 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002626 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002627 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002628 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2629 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002630 }
Paul Bakker23f36802012-09-28 14:15:14 +00002631
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002632#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002633 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2634 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002635 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002636 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002637
2638 n += 2;
2639 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002640#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002641
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002642 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002643 p + 2 , &signature_len,
2644 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002645 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002646 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002647 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002648 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002649
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002650 *(p++) = (unsigned char)( signature_len >> 8 );
2651 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002652 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002653
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002654 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002655
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002656 p += signature_len;
2657 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002658 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002659#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002660 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2661 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002662
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002663 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002664 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2665 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2666
2667 ssl->state++;
2668
2669 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2670 {
2671 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2672 return( ret );
2673 }
2674
2675 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2676
2677 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002678}
2679
2680static int ssl_write_server_hello_done( ssl_context *ssl )
2681{
2682 int ret;
2683
2684 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2685
2686 ssl->out_msglen = 4;
2687 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2688 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2689
2690 ssl->state++;
2691
2692 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2693 {
2694 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2695 return( ret );
2696 }
2697
2698 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2699
2700 return( 0 );
2701}
2702
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002703#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2704 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2705static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2706 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002707{
2708 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002709 size_t n;
2710
2711 /*
2712 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2713 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002714 if( *p + 2 > end )
2715 {
2716 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2717 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2718 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002719
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002720 n = ( (*p)[0] << 8 ) | (*p)[1];
2721 *p += 2;
2722
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002723 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002724 {
2725 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2726 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2727 }
2728
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002729 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002730 {
2731 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2732 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2733 }
2734
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002735 *p += n;
2736
Paul Bakker70df2fb2013-04-17 17:19:09 +02002737 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2738
Paul Bakker70df2fb2013-04-17 17:19:09 +02002739 return( ret );
2740}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002741#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2742 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002743
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002744#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2745 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002746static int ssl_parse_encrypted_pms( ssl_context *ssl,
2747 const unsigned char *p,
2748 const unsigned char *end,
2749 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002750{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002751 int ret;
2752 size_t len = pk_get_len( ssl_own_key( ssl ) );
2753 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002754
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002755 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002756 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002757 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002758 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2759 }
2760
2761 /*
2762 * Decrypt the premaster using own private RSA key
2763 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002764#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2765 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002766 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2767 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002768 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2769 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002770 {
2771 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2772 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2773 }
2774 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002775#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002776
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002777 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002778 {
2779 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2780 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2781 }
2782
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002783 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2784 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002785 sizeof( ssl->handshake->premaster ) - pms_offset,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002786 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002787
2788 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002789 pms[0] != ssl->handshake->max_major_ver ||
2790 pms[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002791 {
2792 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2793
2794 /*
2795 * Protection against Bleichenbacher's attack:
2796 * invalid PKCS#1 v1.5 padding must not cause
2797 * the connection to end immediately; instead,
2798 * send a bad_record_mac later in the handshake.
2799 */
2800 ssl->handshake->pmslen = 48;
2801
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002802 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002803 if( ret != 0 )
2804 return( ret );
2805 }
2806
2807 return( ret );
2808}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002809#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2810 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002811
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002812#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002813static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2814 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002815{
Paul Bakker6db455e2013-09-18 17:29:31 +02002816 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002817 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002818
Paul Bakker6db455e2013-09-18 17:29:31 +02002819 if( ssl->f_psk == NULL &&
2820 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2821 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002822 {
2823 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2824 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2825 }
2826
2827 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002828 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002829 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002830 if( *p + 2 > end )
2831 {
2832 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2833 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2834 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002835
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002836 n = ( (*p)[0] << 8 ) | (*p)[1];
2837 *p += 2;
2838
2839 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002840 {
2841 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2842 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2843 }
2844
Paul Bakker6db455e2013-09-18 17:29:31 +02002845 if( ssl->f_psk != NULL )
2846 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002847 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002848 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2849 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002850 else
Paul Bakker6db455e2013-09-18 17:29:31 +02002851 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002852 /* Identity is not a big secret since clients send it in the clear,
2853 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02002854 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002855 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002856 {
2857 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2858 }
2859 }
2860
2861 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002862 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002863 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002864 if( ( ret = ssl_send_alert_message( ssl,
2865 SSL_ALERT_LEVEL_FATAL,
2866 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2867 {
2868 return( ret );
2869 }
2870
2871 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002872 }
2873
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002874 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002875
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002876 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002877}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002878#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002879
Paul Bakker5121ce52009-01-03 21:22:43 +00002880static int ssl_parse_client_key_exchange( ssl_context *ssl )
2881{
Paul Bakker23986e52011-04-24 08:57:21 +00002882 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002883 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002884
Paul Bakker41c83d32013-03-20 14:39:14 +01002885 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002886
2887 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2888
2889 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2890 {
2891 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2892 return( ret );
2893 }
2894
2895 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2896 {
2897 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002898 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002899 }
2900
2901 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2902 {
2903 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002904 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002905 }
2906
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002907#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002908 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002909 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002910 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002911 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002912
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002913 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002914 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002915 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2916 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002917 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002918
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002919 if( p != end )
2920 {
2921 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2922 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2923 }
2924
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002925 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002926
2927 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2928 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002929 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002930 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002931 {
2932 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2933 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2934 }
2935
2936 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002937 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002938 else
2939#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002940#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002941 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2942 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2943 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002944 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002945 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2946 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2947 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002948 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002949 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002950 ssl->in_msg + 4, ssl->in_hslen - 4 ) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002951 {
2952 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2953 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2954 }
2955
2956 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2957
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002958 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2959 &ssl->handshake->pmslen,
2960 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002961 POLARSSL_MPI_MAX_SIZE,
2962 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002963 {
2964 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2965 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2966 }
2967
2968 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002969 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002970 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002971#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002972 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2973 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2974 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002975#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2976 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002977 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002978 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002979 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002980
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002981 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002982 {
2983 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2984 return( ret );
2985 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002986
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002987 if( p != end )
2988 {
2989 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2990 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2991 }
2992
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002993 if( ( ret = ssl_psk_derive_premaster( ssl,
2994 ciphersuite_info->key_exchange ) ) != 0 )
2995 {
2996 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2997 return( ret );
2998 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002999 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003000 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003001#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003002#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
3003 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
3004 {
3005 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003006 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003007
3008 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3009 {
3010 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3011 return( ret );
3012 }
3013
3014 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3015 {
3016 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
3017 return( ret );
3018 }
3019
3020 if( ( ret = ssl_psk_derive_premaster( ssl,
3021 ciphersuite_info->key_exchange ) ) != 0 )
3022 {
3023 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3024 return( ret );
3025 }
3026 }
3027 else
3028#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003029#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3030 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3031 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003032 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003033 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003034
3035 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3036 {
3037 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3038 return( ret );
3039 }
3040 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3041 {
3042 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3043 return( ret );
3044 }
3045
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003046 if( p != end )
3047 {
3048 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3049 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3050 }
3051
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003052 if( ( ret = ssl_psk_derive_premaster( ssl,
3053 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003054 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003055 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3056 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003057 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003058 }
3059 else
3060#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003061#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3062 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
3063 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003064 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003065 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003066
3067 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3068 {
3069 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3070 return( ret );
3071 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003072
3073 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
3074 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003075 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003076 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3077 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003078 }
3079
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003080 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3081
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003082 if( ( ret = ssl_psk_derive_premaster( ssl,
3083 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003084 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003085 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003086 return( ret );
3087 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003088 }
3089 else
3090#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003091#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
3092 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003093 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003094 if( ( ret = ssl_parse_encrypted_pms( ssl,
3095 ssl->in_msg + 4,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003096 ssl->in_msg + ssl->in_hslen,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003097 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003098 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003099 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003100 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003101 }
3102 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003103 else
3104#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
3105 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003106 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003107 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003108 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003109
Paul Bakkerff60ee62010-03-16 21:09:09 +00003110 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
3111 {
3112 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
3113 return( ret );
3114 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003115
Paul Bakker5121ce52009-01-03 21:22:43 +00003116 ssl->state++;
3117
3118 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3119
3120 return( 0 );
3121}
3122
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003123#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3124 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003125 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3126 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003127static int ssl_parse_certificate_verify( ssl_context *ssl )
3128{
Paul Bakkerfbb17802013-04-17 19:10:21 +02003129 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003130
3131 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3132
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003133 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003134 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003135 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003136 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02003137 {
3138 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3139 ssl->state++;
3140 return( 0 );
3141 }
3142
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003143 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3144 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003145}
3146#else
3147static int ssl_parse_certificate_verify( ssl_context *ssl )
3148{
3149 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003150 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003151 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003152 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003153 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003154#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003155 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003156#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003157 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003158 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3159
3160 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3161
3162 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003163 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003164 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003165 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3166 {
3167 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3168 ssl->state++;
3169 return( 0 );
3170 }
3171
Paul Bakkered27a042013-04-18 22:46:23 +02003172 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003173 {
3174 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3175 ssl->state++;
3176 return( 0 );
3177 }
3178
Paul Bakker48916f92012-09-16 19:57:18 +00003179 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003180
3181 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3182 {
3183 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3184 return( ret );
3185 }
3186
3187 ssl->state++;
3188
3189 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3190 {
3191 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003192 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003193 }
3194
3195 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
3196 {
3197 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003198 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003199 }
3200
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003201 /*
3202 * 0 . 0 handshake type
3203 * 1 . 3 handshake length
3204 * 4 . 5 sig alg (TLS 1.2 only)
3205 * 4+n . 5+n signature length (n = sa_len)
3206 * 6+n . 6+n+m signature (m = sig_len)
3207 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003208
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003209#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3210 defined(POLARSSL_SSL_PROTO_TLS1_1)
3211 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003212 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003213 sa_len = 0;
3214
Paul Bakkerc70b9822013-04-07 22:00:46 +02003215 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003216 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003217
3218 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3219 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3220 POLARSSL_PK_ECDSA ) )
3221 {
3222 hash_start += 16;
3223 hashlen -= 16;
3224 md_alg = POLARSSL_MD_SHA1;
3225 }
Paul Bakker926af752012-11-23 13:38:07 +01003226 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003227 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003228#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3229 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003230#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3231 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003232 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003233 sa_len = 2;
3234
Paul Bakker5121ce52009-01-03 21:22:43 +00003235 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003236 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003237 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003238 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003239 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003240 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3241 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003242 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3243 }
3244
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003245 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003246
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003247 /* Info from md_alg will be used instead */
3248 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003249
3250 /*
3251 * Signature
3252 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003253 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
3254 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003255 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003256 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3257 " for verify message" ) );
3258 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003259 }
3260
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003261 /*
3262 * Check the certificate's key type matches the signature alg
3263 */
3264 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3265 {
3266 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3267 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3268 }
Paul Bakker577e0062013-08-28 11:57:20 +02003269 }
3270 else
3271#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3272 {
3273 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003274 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003275 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003276
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003277 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01003278
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003279 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003280 {
3281 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003282 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003283 }
3284
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003285 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003286 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003287 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003288 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003289 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003290 return( ret );
3291 }
3292
3293 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3294
Paul Bakkered27a042013-04-18 22:46:23 +02003295 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003296}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003297#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3298 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3299 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003300
Paul Bakkera503a632013-08-14 13:48:06 +02003301#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003302static int ssl_write_new_session_ticket( ssl_context *ssl )
3303{
3304 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003305 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003306 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003307
3308 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3309
3310 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3311 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3312
3313 /*
3314 * struct {
3315 * uint32 ticket_lifetime_hint;
3316 * opaque ticket<0..2^16-1>;
3317 * } NewSessionTicket;
3318 *
3319 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3320 * 8 . 9 ticket_len (n)
3321 * 10 . 9+n ticket content
3322 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003323
3324 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3325 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3326 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3327 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003328
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003329 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3330 {
3331 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3332 tlen = 0;
3333 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003334
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003335 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3336 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003337
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003338 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003339
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003340 /*
3341 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3342 * ChangeCipherSpec share the same state.
3343 */
3344 ssl->handshake->new_session_ticket = 0;
3345
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003346 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3347 {
3348 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3349 return( ret );
3350 }
3351
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003352 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3353
3354 return( 0 );
3355}
Paul Bakkera503a632013-08-14 13:48:06 +02003356#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003357
Paul Bakker5121ce52009-01-03 21:22:43 +00003358/*
Paul Bakker1961b702013-01-25 14:49:24 +01003359 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003360 */
Paul Bakker1961b702013-01-25 14:49:24 +01003361int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003362{
3363 int ret = 0;
3364
Paul Bakker1961b702013-01-25 14:49:24 +01003365 if( ssl->state == SSL_HANDSHAKE_OVER )
3366 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003367
Paul Bakker1961b702013-01-25 14:49:24 +01003368 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3369
3370 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3371 return( ret );
3372
3373 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003374 {
Paul Bakker1961b702013-01-25 14:49:24 +01003375 case SSL_HELLO_REQUEST:
3376 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003377 break;
3378
Paul Bakker1961b702013-01-25 14:49:24 +01003379 /*
3380 * <== ClientHello
3381 */
3382 case SSL_CLIENT_HELLO:
3383 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003384 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003385
3386 /*
3387 * ==> ServerHello
3388 * Certificate
3389 * ( ServerKeyExchange )
3390 * ( CertificateRequest )
3391 * ServerHelloDone
3392 */
3393 case SSL_SERVER_HELLO:
3394 ret = ssl_write_server_hello( ssl );
3395 break;
3396
3397 case SSL_SERVER_CERTIFICATE:
3398 ret = ssl_write_certificate( ssl );
3399 break;
3400
3401 case SSL_SERVER_KEY_EXCHANGE:
3402 ret = ssl_write_server_key_exchange( ssl );
3403 break;
3404
3405 case SSL_CERTIFICATE_REQUEST:
3406 ret = ssl_write_certificate_request( ssl );
3407 break;
3408
3409 case SSL_SERVER_HELLO_DONE:
3410 ret = ssl_write_server_hello_done( ssl );
3411 break;
3412
3413 /*
3414 * <== ( Certificate/Alert )
3415 * ClientKeyExchange
3416 * ( CertificateVerify )
3417 * ChangeCipherSpec
3418 * Finished
3419 */
3420 case SSL_CLIENT_CERTIFICATE:
3421 ret = ssl_parse_certificate( ssl );
3422 break;
3423
3424 case SSL_CLIENT_KEY_EXCHANGE:
3425 ret = ssl_parse_client_key_exchange( ssl );
3426 break;
3427
3428 case SSL_CERTIFICATE_VERIFY:
3429 ret = ssl_parse_certificate_verify( ssl );
3430 break;
3431
3432 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3433 ret = ssl_parse_change_cipher_spec( ssl );
3434 break;
3435
3436 case SSL_CLIENT_FINISHED:
3437 ret = ssl_parse_finished( ssl );
3438 break;
3439
3440 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003441 * ==> ( NewSessionTicket )
3442 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003443 * Finished
3444 */
3445 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003446#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003447 if( ssl->handshake->new_session_ticket != 0 )
3448 ret = ssl_write_new_session_ticket( ssl );
3449 else
Paul Bakkera503a632013-08-14 13:48:06 +02003450#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003451 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003452 break;
3453
3454 case SSL_SERVER_FINISHED:
3455 ret = ssl_write_finished( ssl );
3456 break;
3457
3458 case SSL_FLUSH_BUFFERS:
3459 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3460 ssl->state = SSL_HANDSHAKE_WRAPUP;
3461 break;
3462
3463 case SSL_HANDSHAKE_WRAPUP:
3464 ssl_handshake_wrapup( ssl );
3465 break;
3466
3467 default:
3468 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3469 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003470 }
3471
Paul Bakker5121ce52009-01-03 21:22:43 +00003472 return( ret );
3473}
Paul Bakker9af723c2014-05-01 13:03:14 +02003474#endif /* POLARSSL_SSL_SRV_C */