blob: cdbb028b085ed504113ff9610710bcda681894f6 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 server-side functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakker40e46942009-01-03 21:51:57 +000032#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/debug.h"
35#include "polarssl/ssl.h"
Paul Bakker41c83d32013-03-20 14:39:14 +010036#if defined(POLARSSL_ECP_C)
37#include "polarssl/ecp.h"
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker7dc4c442014-02-01 22:50:26 +010040#if defined(POLARSSL_PLATFORM_C)
41#include "polarssl/platform.h"
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020042#else
43#define polarssl_malloc malloc
44#define polarssl_free free
45#endif
46
Paul Bakker5121ce52009-01-03 21:22:43 +000047#include <stdlib.h>
48#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020049
50#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000051#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020052#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Paul Bakkera503a632013-08-14 13:48:06 +020054#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker34617722014-06-13 17:20:13 +020055/* Implementation that should never be optimized out by the compiler */
56static void polarssl_zeroize( void *v, size_t n ) {
57 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
58}
59
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020060/*
61 * Serialize a session in the following format:
62 * 0 . n-1 session structure, n = sizeof(ssl_session)
63 * n . n+2 peer_cert length = m (0 if no certificate)
64 * n+3 . n+2+m peer cert ASN.1
65 *
66 * Assumes ticket is NULL (always true on server side).
67 */
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020068static int ssl_save_session( const ssl_session *session,
69 unsigned char *buf, size_t buf_len,
70 size_t *olen )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020071{
72 unsigned char *p = buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020073 size_t left = buf_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020075 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020077
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020078 if( left < sizeof( ssl_session ) )
79 return( -1 );
80
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020081 memcpy( p, session, sizeof( ssl_session ) );
82 p += sizeof( ssl_session );
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020083 left -= sizeof( ssl_session );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020084
Paul Bakker7c6b2c32013-09-16 13:49:26 +020085#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020086 if( session->peer_cert == NULL )
87 cert_len = 0;
88 else
89 cert_len = session->peer_cert->raw.len;
90
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020091 if( left < 3 + cert_len )
92 return( -1 );
93
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020094 *p++ = (unsigned char)( cert_len >> 16 & 0xFF );
95 *p++ = (unsigned char)( cert_len >> 8 & 0xFF );
96 *p++ = (unsigned char)( cert_len & 0xFF );
97
98 if( session->peer_cert != NULL )
99 memcpy( p, session->peer_cert->raw.p, cert_len );
100
101 p += cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200102#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200103
104 *olen = p - buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200105
106 return( 0 );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200107}
108
109/*
110 * Unserialise session, see ssl_save_session()
111 */
112static int ssl_load_session( ssl_session *session,
113 const unsigned char *buf, size_t len )
114{
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200115 const unsigned char *p = buf;
116 const unsigned char * const end = buf + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200117#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200118 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200119#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200120
121 if( p + sizeof( ssl_session ) > end )
122 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
123
124 memcpy( session, p, sizeof( ssl_session ) );
125 p += sizeof( ssl_session );
126
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200127#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200128 if( p + 3 > end )
129 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
130
131 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
132 p += 3;
133
134 if( cert_len == 0 )
135 {
136 session->peer_cert = NULL;
137 }
138 else
139 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200140 int ret;
141
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200142 if( p + cert_len > end )
143 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
144
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200145 session->peer_cert = polarssl_malloc( sizeof( x509_crt ) );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200146
147 if( session->peer_cert == NULL )
148 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
149
Paul Bakkerb6b09562013-09-18 14:17:41 +0200150 x509_crt_init( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200151
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200152 if( ( ret = x509_crt_parse_der( session->peer_cert,
153 p, cert_len ) ) != 0 )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200154 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200155 x509_crt_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200156 polarssl_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200157 session->peer_cert = NULL;
158 return( ret );
159 }
160
161 p += cert_len;
162 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200163#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200164
165 if( p != end )
166 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
167
168 return( 0 );
169}
170
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200171/*
172 * Create session ticket, secured as recommended in RFC 5077 section 4:
173 *
174 * struct {
175 * opaque key_name[16];
176 * opaque iv[16];
177 * opaque encrypted_state<0..2^16-1>;
178 * opaque mac[32];
179 * } ticket;
180 *
181 * (the internal state structure differs, however).
182 */
183static int ssl_write_ticket( ssl_context *ssl, size_t *tlen )
184{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200185 int ret;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200186 unsigned char * const start = ssl->out_msg + 10;
187 unsigned char *p = start;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200188 unsigned char *state;
189 unsigned char iv[16];
190 size_t clear_len, enc_len, pad_len, i;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200191
Manuel Pégourié-Gonnard0a201712013-08-23 16:25:16 +0200192 *tlen = 0;
193
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200194 if( ssl->ticket_keys == NULL )
195 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
196
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200197 /* Write key name */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200198 memcpy( p, ssl->ticket_keys->key_name, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200199 p += 16;
200
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200201 /* Generate and write IV (with a copy for aes_crypt) */
202 if( ( ret = ssl->f_rng( ssl->p_rng, p, 16 ) ) != 0 )
203 return( ret );
204 memcpy( iv, p, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200205 p += 16;
206
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200207 /*
208 * Dump session state
209 *
210 * After the session state itself, we still need room for 16 bytes of
211 * padding and 32 bytes of MAC, so there's only so much room left
212 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200213 state = p + 2;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200214 if( ssl_save_session( ssl->session_negotiate, state,
Manuel Pégourié-Gonnard5d53cbe2014-07-14 13:51:41 +0200215 SSL_MAX_CONTENT_LEN - ( state - ssl->out_msg ) - 48,
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200216 &clear_len ) != 0 )
217 {
218 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
219 }
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200220 SSL_DEBUG_BUF( 3, "session ticket cleartext", state, clear_len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200221
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200222 /* Apply PKCS padding */
223 pad_len = 16 - clear_len % 16;
224 enc_len = clear_len + pad_len;
225 for( i = clear_len; i < enc_len; i++ )
226 state[i] = (unsigned char) pad_len;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200227
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200228 /* Encrypt */
229 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->enc, AES_ENCRYPT,
230 enc_len, iv, state, state ) ) != 0 )
231 {
232 return( ret );
233 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200234
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200235 /* Write length */
236 *p++ = (unsigned char)( ( enc_len >> 8 ) & 0xFF );
237 *p++ = (unsigned char)( ( enc_len ) & 0xFF );
238 p = state + enc_len;
239
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200240 /* Compute and write MAC( key_name + iv + enc_state_len + enc_state ) */
241 sha256_hmac( ssl->ticket_keys->mac_key, 16, start, p - start, p, 0 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200242 p += 32;
243
244 *tlen = p - start;
245
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200246 SSL_DEBUG_BUF( 3, "session ticket structure", start, *tlen );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200247
248 return( 0 );
249}
250
251/*
252 * Load session ticket (see ssl_write_ticket for structure)
253 */
254static int ssl_parse_ticket( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200255 unsigned char *buf,
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200256 size_t len )
257{
258 int ret;
259 ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200260 unsigned char *key_name = buf;
261 unsigned char *iv = buf + 16;
262 unsigned char *enc_len_p = iv + 16;
263 unsigned char *ticket = enc_len_p + 2;
264 unsigned char *mac;
Manuel Pégourié-Gonnard34ced2d2013-09-20 11:37:39 +0200265 unsigned char computed_mac[32];
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200266 size_t enc_len, clear_len, i;
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100267 unsigned char pad_len, diff;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200268
269 SSL_DEBUG_BUF( 3, "session ticket structure", buf, len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200270
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200271 if( len < 34 || ssl->ticket_keys == NULL )
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200272 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
273
274 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
275 mac = ticket + enc_len;
276
277 if( len != enc_len + 66 )
278 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
279
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100280 /* Check name, in constant time though it's not a big secret */
281 diff = 0;
282 for( i = 0; i < 16; i++ )
283 diff |= key_name[i] ^ ssl->ticket_keys->key_name[i];
284 /* don't return yet, check the MAC anyway */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200285
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100286 /* Check mac, with constant-time buffer comparison */
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200287 sha256_hmac( ssl->ticket_keys->mac_key, 16, buf, len - 32,
288 computed_mac, 0 );
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100289
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200290 for( i = 0; i < 32; i++ )
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100291 diff |= mac[i] ^ computed_mac[i];
292
293 /* Now return if ticket is not authentic, since we want to avoid
294 * decrypting arbitrary attacker-chosen data */
295 if( diff != 0 )
296 return( POLARSSL_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200297
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200298 /* Decrypt */
299 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->dec, AES_DECRYPT,
300 enc_len, iv, ticket, ticket ) ) != 0 )
301 {
302 return( ret );
303 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200304
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200305 /* Check PKCS padding */
306 pad_len = ticket[enc_len - 1];
307
308 ret = 0;
309 for( i = 2; i < pad_len; i++ )
310 if( ticket[enc_len - i] != pad_len )
311 ret = POLARSSL_ERR_SSL_BAD_INPUT_DATA;
312 if( ret != 0 )
313 return( ret );
314
315 clear_len = enc_len - pad_len;
316
317 SSL_DEBUG_BUF( 3, "session ticket cleartext", ticket, clear_len );
318
319 /* Actually load session */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200320 if( ( ret = ssl_load_session( &session, ticket, clear_len ) ) != 0 )
321 {
322 SSL_DEBUG_MSG( 1, ( "failed to parse ticket content" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100323 ssl_session_free( &session );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200324 return( ret );
325 }
326
Paul Bakker606b4ba2013-08-14 16:52:14 +0200327#if defined(POLARSSL_HAVE_TIME)
328 /* Check if still valid */
329 if( (int) ( time( NULL) - session.start ) > ssl->ticket_lifetime )
330 {
331 SSL_DEBUG_MSG( 1, ( "session ticket expired" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100332 ssl_session_free( &session );
Paul Bakker606b4ba2013-08-14 16:52:14 +0200333 return( POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED );
334 }
335#endif
336
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200337 /*
338 * Keep the session ID sent by the client, since we MUST send it back to
339 * inform him we're accepting the ticket (RFC 5077 section 3.4)
340 */
341 session.length = ssl->session_negotiate->length;
342 memcpy( &session.id, ssl->session_negotiate->id, session.length );
343
344 ssl_session_free( ssl->session_negotiate );
345 memcpy( ssl->session_negotiate, &session, sizeof( ssl_session ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200346
347 /* Zeroize instead of free as we copied the content */
Paul Bakker34617722014-06-13 17:20:13 +0200348 polarssl_zeroize( &session, sizeof( ssl_session ) );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200349
350 return( 0 );
351}
Paul Bakkera503a632013-08-14 13:48:06 +0200352#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200353
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +0200354#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +0200355int ssl_set_client_transport_id( ssl_context *ssl,
356 const unsigned char *info,
357 size_t ilen )
358{
359 if( ssl->endpoint != SSL_IS_SERVER )
360 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
361
362 polarssl_free( ssl->cli_id );
363
364 if( ( ssl->cli_id = polarssl_malloc( ilen ) ) == NULL )
365 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
366
367 memcpy( ssl->cli_id, info, ilen );
368 ssl->cli_id_len = ilen;
369
370 return( 0 );
371}
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +0200372
373void ssl_set_dtls_cookies( ssl_context *ssl,
374 ssl_cookie_write_t *f_cookie_write,
375 ssl_cookie_check_t *f_cookie_check,
376 void *p_cookie )
377{
378 ssl->f_cookie_write = f_cookie_write;
379 ssl->f_cookie_check = f_cookie_check;
380 ssl->p_cookie = p_cookie;
381}
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +0200382#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +0200383
Paul Bakker0be444a2013-08-27 21:55:01 +0200384#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200385/*
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200386 * Wrapper around f_sni, allowing use of ssl_set_own_cert() but
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +0100387 * making it act on ssl->handshake->sni_key_cert instead.
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200388 */
389static int ssl_sni_wrapper( ssl_context *ssl,
390 const unsigned char* name, size_t len )
391{
392 int ret;
393 ssl_key_cert *key_cert_ori = ssl->key_cert;
394
395 ssl->key_cert = NULL;
396 ret = ssl->f_sni( ssl->p_sni, ssl, name, len );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200397 ssl->handshake->sni_key_cert = ssl->key_cert;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200398
399 ssl->key_cert = key_cert_ori;
400
401 return( ret );
402}
403
Paul Bakker5701cdc2012-09-27 21:49:42 +0000404static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000405 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000406 size_t len )
407{
408 int ret;
409 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000410 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000411
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100412 SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
413
Paul Bakker5701cdc2012-09-27 21:49:42 +0000414 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
415 if( servername_list_size + 2 != len )
416 {
417 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
418 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
419 }
420
421 p = buf + 2;
422 while( servername_list_size > 0 )
423 {
424 hostname_len = ( ( p[1] << 8 ) | p[2] );
425 if( hostname_len + 3 > servername_list_size )
426 {
427 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
428 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
429 }
430
431 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
432 {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200433 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000434 if( ret != 0 )
435 {
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100436 SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000437 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
438 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
439 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
440 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000441 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000442 }
443
444 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000445 p += hostname_len + 3;
446 }
447
448 if( servername_list_size != 0 )
449 {
450 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
451 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000452 }
453
454 return( 0 );
455}
Paul Bakker0be444a2013-08-27 21:55:01 +0200456#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000457
Paul Bakker48916f92012-09-16 19:57:18 +0000458static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000459 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000460 size_t len )
461{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000462 int ret;
463
Paul Bakker48916f92012-09-16 19:57:18 +0000464 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
465 {
466 if( len != 1 || buf[0] != 0x0 )
467 {
468 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000469
470 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
471 return( ret );
472
Paul Bakker48916f92012-09-16 19:57:18 +0000473 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
474 }
475
476 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
477 }
478 else
479 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100480 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000481 if( len != 1 + ssl->verify_data_len ||
482 buf[0] != ssl->verify_data_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100483 safer_memcmp( buf + 1, ssl->peer_verify_data,
484 ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000485 {
486 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000487
488 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
489 return( ret );
490
Paul Bakker48916f92012-09-16 19:57:18 +0000491 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
492 }
493 }
494
495 return( 0 );
496}
497
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200498#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +0000499static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
500 const unsigned char *buf,
501 size_t len )
502{
503 size_t sig_alg_list_size;
504 const unsigned char *p;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200505 const unsigned char *end = buf + len;
506 const int *md_cur;
507
Paul Bakker23f36802012-09-28 14:15:14 +0000508
509 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
510 if( sig_alg_list_size + 2 != len ||
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200511 sig_alg_list_size % 2 != 0 )
Paul Bakker23f36802012-09-28 14:15:14 +0000512 {
513 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
514 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
515 }
516
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200517 /*
518 * For now, ignore the SignatureAlgorithm part and rely on offered
519 * ciphersuites only for that part. To be fixed later.
520 *
521 * So, just look at the HashAlgorithm part.
522 */
523 for( md_cur = md_list(); *md_cur != POLARSSL_MD_NONE; md_cur++ ) {
524 for( p = buf + 2; p < end; p += 2 ) {
525 if( *md_cur == (int) ssl_md_alg_from_hash( p[0] ) ) {
526 ssl->handshake->sig_alg = p[0];
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200527 goto have_sig_alg;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200528 }
Paul Bakker23f36802012-09-28 14:15:14 +0000529 }
Paul Bakker23f36802012-09-28 14:15:14 +0000530 }
531
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200532 /* Some key echanges do not need signatures at all */
533 SSL_DEBUG_MSG( 3, ( "no signature_algorithm in common" ) );
534 return( 0 );
535
536have_sig_alg:
Paul Bakker23f36802012-09-28 14:15:14 +0000537 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
538 ssl->handshake->sig_alg ) );
539
540 return( 0 );
541}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200542#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker23f36802012-09-28 14:15:14 +0000543
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200544#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200545static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
546 const unsigned char *buf,
547 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100548{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200549 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100550 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200551 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100552
553 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
554 if( list_size + 2 != len ||
555 list_size % 2 != 0 )
556 {
557 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
558 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
559 }
560
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200561 /* Should never happen unless client duplicates the extension */
562 if( ssl->handshake->curves != NULL )
563 {
564 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
565 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
566 }
567
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +0100568 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200569 * and leave room for a final 0 */
570 our_size = list_size / 2 + 1;
571 if( our_size > POLARSSL_ECP_DP_MAX )
572 our_size = POLARSSL_ECP_DP_MAX;
573
574 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
575 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
576
Paul Bakker9af723c2014-05-01 13:03:14 +0200577 /* explicit void pointer cast for buggy MS compiler */
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200578 memset( (void *) curves, 0, our_size * sizeof( *curves ) );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200579 ssl->handshake->curves = curves;
580
Paul Bakker41c83d32013-03-20 14:39:14 +0100581 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200582 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100583 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200584 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200585
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200586 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100587 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200588 *curves++ = curve_info;
589 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100590 }
591
592 list_size -= 2;
593 p += 2;
594 }
595
596 return( 0 );
597}
598
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200599static int ssl_parse_supported_point_formats( ssl_context *ssl,
600 const unsigned char *buf,
601 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100602{
603 size_t list_size;
604 const unsigned char *p;
605
606 list_size = buf[0];
607 if( list_size + 1 != len )
608 {
609 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
610 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
611 }
612
613 p = buf + 2;
614 while( list_size > 0 )
615 {
616 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
617 p[0] == POLARSSL_ECP_PF_COMPRESSED )
618 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200619 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200620 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100621 return( 0 );
622 }
623
624 list_size--;
625 p++;
626 }
627
628 return( 0 );
629}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200630#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100631
Paul Bakker05decb22013-08-15 13:33:48 +0200632#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200633static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
634 const unsigned char *buf,
635 size_t len )
636{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200637 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200638 {
639 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
640 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
641 }
642
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200643 ssl->session_negotiate->mfl_code = buf[0];
644
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200645 return( 0 );
646}
Paul Bakker05decb22013-08-15 13:33:48 +0200647#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200648
Paul Bakker1f2bc622013-08-15 13:45:55 +0200649#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200650static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
651 const unsigned char *buf,
652 size_t len )
653{
654 if( len != 0 )
655 {
656 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
657 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
658 }
659
660 ((void) buf);
661
662 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
663
664 return( 0 );
665}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200666#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200667
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100668#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
669static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
670 const unsigned char *buf,
671 size_t len )
672{
673 if( len != 0 )
674 {
675 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
676 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
677 }
678
679 ((void) buf);
680
681 if( ssl->encrypt_then_mac == SSL_ETM_ENABLED &&
682 ssl->minor_ver != SSL_MINOR_VERSION_0 )
683 {
684 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
685 }
686
687 return( 0 );
688}
689#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
690
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200691#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
692static int ssl_parse_extended_ms_ext( ssl_context *ssl,
693 const unsigned char *buf,
694 size_t len )
695{
696 if( len != 0 )
697 {
698 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
699 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
700 }
701
702 ((void) buf);
703
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200704 if( ssl->extended_ms == SSL_EXTENDED_MS_ENABLED &&
705 ssl->minor_ver != SSL_MINOR_VERSION_0 )
706 {
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200707 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200708 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200709
710 return( 0 );
711}
712#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
713
Paul Bakkera503a632013-08-14 13:48:06 +0200714#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200715static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200716 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200717 size_t len )
718{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200719 int ret;
720
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200721 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
722 return( 0 );
723
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200724 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200725 ssl->handshake->new_session_ticket = 1;
726
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200727 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
728
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200729 if( len == 0 )
730 return( 0 );
731
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200732 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
733 {
734 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
735 return( 0 );
736 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200737
738 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200739 * Failures are ok: just ignore the ticket and proceed.
740 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200741 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
742 {
743 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200744 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200745 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200746
747 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
748
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200749 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200750
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200751 /* Don't send a new ticket after all, this one is OK */
752 ssl->handshake->new_session_ticket = 0;
753
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200754 return( 0 );
755}
Paul Bakkera503a632013-08-14 13:48:06 +0200756#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200757
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200758#if defined(POLARSSL_SSL_ALPN)
759static int ssl_parse_alpn_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200760 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200761{
Paul Bakker14b16c62014-05-28 11:33:54 +0200762 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200763 const unsigned char *theirs, *start, *end;
764 const char **ours;
765
766 /* If ALPN not configured, just ignore the extension */
767 if( ssl->alpn_list == NULL )
768 return( 0 );
769
770 /*
771 * opaque ProtocolName<1..2^8-1>;
772 *
773 * struct {
774 * ProtocolName protocol_name_list<2..2^16-1>
775 * } ProtocolNameList;
776 */
777
778 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
779 if( len < 4 )
780 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
781
782 list_len = ( buf[0] << 8 ) | buf[1];
783 if( list_len != len - 2 )
784 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
785
786 /*
787 * Use our order of preference
788 */
789 start = buf + 2;
790 end = buf + len;
791 for( ours = ssl->alpn_list; *ours != NULL; ours++ )
792 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200793 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200794 for( theirs = start; theirs != end; theirs += cur_len )
795 {
796 /* If the list is well formed, we should get equality first */
797 if( theirs > end )
798 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
799
800 cur_len = *theirs++;
801
802 /* Empty strings MUST NOT be included */
803 if( cur_len == 0 )
804 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
805
Paul Bakker14b16c62014-05-28 11:33:54 +0200806 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200807 memcmp( theirs, *ours, cur_len ) == 0 )
808 {
809 ssl->alpn_chosen = *ours;
810 return( 0 );
811 }
812 }
813 }
814
815 /* If we get there, no match was found */
816 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
817 SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
818 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
819}
820#endif /* POLARSSL_SSL_ALPN */
821
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100822/*
823 * Auxiliary functions for ServerHello parsing and related actions
824 */
825
826#if defined(POLARSSL_X509_CRT_PARSE_C)
827/*
828 * Return 1 if the given EC key uses the given curve, 0 otherwise
829 */
830#if defined(POLARSSL_ECDSA_C)
831static int ssl_key_matches_curves( pk_context *pk,
832 const ecp_curve_info **curves )
833{
834 const ecp_curve_info **crv = curves;
835 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
836
837 while( *crv != NULL )
838 {
839 if( (*crv)->grp_id == grp_id )
840 return( 1 );
841 crv++;
842 }
843
844 return( 0 );
845}
846#endif /* POLARSSL_ECDSA_C */
847
848/*
849 * Try picking a certificate for this ciphersuite,
850 * return 0 on success and -1 on failure.
851 */
852static int ssl_pick_cert( ssl_context *ssl,
853 const ssl_ciphersuite_t * ciphersuite_info )
854{
855 ssl_key_cert *cur, *list;
856 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
857
858#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
859 if( ssl->handshake->sni_key_cert != NULL )
860 list = ssl->handshake->sni_key_cert;
861 else
862#endif
863 list = ssl->handshake->key_cert;
864
865 if( pk_alg == POLARSSL_PK_NONE )
866 return( 0 );
867
868 for( cur = list; cur != NULL; cur = cur->next )
869 {
870 if( ! pk_can_do( cur->key, pk_alg ) )
871 continue;
872
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200873 /*
874 * This avoids sending the client a cert it'll reject based on
875 * keyUsage or other extensions.
876 *
877 * It also allows the user to provision different certificates for
878 * different uses based on keyUsage, eg if they want to avoid signing
879 * and decrypting with the same RSA key.
880 */
881 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
882 SSL_IS_SERVER ) != 0 )
883 {
884 continue;
885 }
886
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100887#if defined(POLARSSL_ECDSA_C)
888 if( pk_alg == POLARSSL_PK_ECDSA )
889 {
890 if( ssl_key_matches_curves( cur->key, ssl->handshake->curves ) )
891 break;
892 }
893 else
894#endif
895 break;
896 }
897
898 if( cur == NULL )
899 return( -1 );
900
901 ssl->handshake->key_cert = cur;
902 return( 0 );
903}
904#endif /* POLARSSL_X509_CRT_PARSE_C */
905
906/*
907 * Check if a given ciphersuite is suitable for use with our config/keys/etc
908 * Sets ciphersuite_info only if the suite matches.
909 */
910static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
911 const ssl_ciphersuite_t **ciphersuite_info )
912{
913 const ssl_ciphersuite_t *suite_info;
914
915 suite_info = ssl_ciphersuite_from_id( suite_id );
916 if( suite_info == NULL )
917 {
918 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", suite_id ) );
919 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
920 }
921
922 if( suite_info->min_minor_ver > ssl->minor_ver ||
923 suite_info->max_minor_ver < ssl->minor_ver )
924 return( 0 );
925
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +0100926#if defined(POLARSSL_SSL_PROTO_DTLS)
927 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
928 ( suite_info->flags & POLARSSL_CIPHERSUITE_NODTLS ) )
929 return( 0 );
930#endif
931
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100932#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
933 if( ssl_ciphersuite_uses_ec( suite_info ) &&
934 ( ssl->handshake->curves == NULL ||
935 ssl->handshake->curves[0] == NULL ) )
936 return( 0 );
937#endif
938
939#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
940 /* If the ciphersuite requires a pre-shared key and we don't
941 * have one, skip it now rather than failing later */
942 if( ssl_ciphersuite_uses_psk( suite_info ) &&
943 ssl->f_psk == NULL &&
944 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
945 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
946 return( 0 );
947#endif
948
949#if defined(POLARSSL_X509_CRT_PARSE_C)
950 /*
951 * Final check: if ciphersuite requires us to have a
952 * certificate/key of a particular type:
953 * - select the appropriate certificate if we have one, or
954 * - try the next ciphersuite if we don't
955 * This must be done last since we modify the key_cert list.
956 */
957 if( ssl_pick_cert( ssl, suite_info ) != 0 )
958 return( 0 );
959#endif
960
961 *ciphersuite_info = suite_info;
962 return( 0 );
963}
964
Paul Bakker78a8c712013-03-06 17:01:52 +0100965#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
966static int ssl_parse_client_hello_v2( ssl_context *ssl )
967{
968 int ret;
969 unsigned int i, j;
970 size_t n;
971 unsigned int ciph_len, sess_len, chal_len;
972 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200973 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200974 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100975
976 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
977
978 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
979 {
980 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
981
982 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
983 return( ret );
984
985 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
986 }
987
988 buf = ssl->in_hdr;
989
990 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
991
992 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
993 buf[2] ) );
994 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
995 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
996 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
997 buf[3], buf[4] ) );
998
999 /*
1000 * SSLv2 Client Hello
1001 *
1002 * Record layer:
1003 * 0 . 1 message length
1004 *
1005 * SSL layer:
1006 * 2 . 2 message type
1007 * 3 . 4 protocol version
1008 */
1009 if( buf[2] != SSL_HS_CLIENT_HELLO ||
1010 buf[3] != SSL_MAJOR_VERSION_3 )
1011 {
1012 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1013 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1014 }
1015
1016 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
1017
1018 if( n < 17 || n > 512 )
1019 {
1020 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1021 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1022 }
1023
1024 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +02001025 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
1026 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +01001027
1028 if( ssl->minor_ver < ssl->min_minor_ver )
1029 {
1030 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001031 " [%d:%d] < [%d:%d]",
1032 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +01001033 ssl->min_major_ver, ssl->min_minor_ver ) );
1034
1035 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1036 SSL_ALERT_MSG_PROTOCOL_VERSION );
1037 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1038 }
1039
Paul Bakker2fbefde2013-06-29 16:01:15 +02001040 ssl->handshake->max_major_ver = buf[3];
1041 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +01001042
1043 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
1044 {
1045 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1046 return( ret );
1047 }
1048
1049 ssl->handshake->update_checksum( ssl, buf + 2, n );
1050
1051 buf = ssl->in_msg;
1052 n = ssl->in_left - 5;
1053
1054 /*
1055 * 0 . 1 ciphersuitelist length
1056 * 2 . 3 session id length
1057 * 4 . 5 challenge length
1058 * 6 . .. ciphersuitelist
1059 * .. . .. session id
1060 * .. . .. challenge
1061 */
1062 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1063
1064 ciph_len = ( buf[0] << 8 ) | buf[1];
1065 sess_len = ( buf[2] << 8 ) | buf[3];
1066 chal_len = ( buf[4] << 8 ) | buf[5];
1067
1068 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
1069 ciph_len, sess_len, chal_len ) );
1070
1071 /*
1072 * Make sure each parameter length is valid
1073 */
1074 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1075 {
1076 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1077 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1078 }
1079
1080 if( sess_len > 32 )
1081 {
1082 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1083 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1084 }
1085
1086 if( chal_len < 8 || chal_len > 32 )
1087 {
1088 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1089 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1090 }
1091
1092 if( n != 6 + ciph_len + sess_len + chal_len )
1093 {
1094 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1095 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1096 }
1097
1098 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1099 buf + 6, ciph_len );
1100 SSL_DEBUG_BUF( 3, "client hello, session id",
1101 buf + 6 + ciph_len, sess_len );
1102 SSL_DEBUG_BUF( 3, "client hello, challenge",
1103 buf + 6 + ciph_len + sess_len, chal_len );
1104
1105 p = buf + 6 + ciph_len;
1106 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001107 memset( ssl->session_negotiate->id, 0,
1108 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001109 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1110
1111 p += sess_len;
1112 memset( ssl->handshake->randbytes, 0, 64 );
1113 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1114
1115 /*
1116 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1117 */
1118 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1119 {
1120 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1121 {
1122 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1123 if( ssl->renegotiation == SSL_RENEGOTIATION )
1124 {
1125 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1126
1127 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1128 return( ret );
1129
1130 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1131 }
1132 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1133 break;
1134 }
1135 }
1136
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001137#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1138 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1139 {
1140 if( p[0] == 0 &&
1141 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1142 p[2] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1143 {
1144 SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
1145
1146 if( ssl->minor_ver < ssl->max_minor_ver )
1147 {
1148 SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
1149
1150 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1151 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1152
1153 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1154 }
1155
1156 break;
1157 }
1158 }
1159#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1160
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001161 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001162 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001163#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1164 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1165 {
1166 for( i = 0; ciphersuites[i] != 0; i++ )
1167#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001168 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001169 {
1170 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001171#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001172 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001173 if( p[0] != 0 ||
1174 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1175 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1176 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001177
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001178 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1179 &ciphersuite_info ) ) != 0 )
1180 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001181
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001182 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001183 goto have_ciphersuite_v2;
1184 }
1185 }
1186
1187 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1188
1189 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1190
1191have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001192 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001193 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001194 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001195
1196 /*
1197 * SSLv2 Client Hello relevant renegotiation security checks
1198 */
1199 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1200 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1201 {
1202 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1203
1204 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1205 return( ret );
1206
1207 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1208 }
1209
1210 ssl->in_left = 0;
1211 ssl->state++;
1212
1213 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1214
1215 return( 0 );
1216}
1217#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1218
Paul Bakker5121ce52009-01-03 21:22:43 +00001219static int ssl_parse_client_hello( ssl_context *ssl )
1220{
Paul Bakker23986e52011-04-24 08:57:21 +00001221 int ret;
1222 unsigned int i, j;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001223 unsigned int ciph_offset, comp_offset, ext_offset;
1224 unsigned int msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001225#if defined(POLARSSL_SSL_PROTO_DTLS)
1226 unsigned int cookie_offset, cookie_len;
1227#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001228 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001229 int renegotiation_info_seen = 0;
1230 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001231 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001232 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001233 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001234
1235 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1236
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001237#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1238read_record_header:
1239#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001240 /*
1241 * If renegotiating, then the input was read with ssl_read_record(),
1242 * otherwise read it ourselves manually in order to support SSLv2
1243 * ClientHello, which doesn't use the same record layer format.
1244 */
Paul Bakker48916f92012-09-16 19:57:18 +00001245 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001246 ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001247 {
1248 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1249 return( ret );
1250 }
1251
1252 buf = ssl->in_hdr;
1253
Paul Bakker78a8c712013-03-06 17:01:52 +01001254#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02001255#if defined(POLARSSL_SSL_PROTO_DTLS)
1256 if( ssl->transport == SSL_TRANSPORT_STREAM )
1257#endif
1258 if( ( buf[0] & 0x80 ) != 0 )
1259 return ssl_parse_client_hello_v2( ssl );
Paul Bakker78a8c712013-03-06 17:01:52 +01001260#endif
1261
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001262 SSL_DEBUG_BUF( 4, "record header", buf, ssl_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001263
Paul Bakkerec636f32012-09-09 19:17:02 +00001264 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001265 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001266 *
1267 * Record layer:
1268 * 0 . 0 message type
1269 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001270 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001271 * 3 . 4 message length
1272 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001273 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1274 buf[0] ) );
1275
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001276 if( buf[0] != SSL_MSG_HANDSHAKE )
1277 {
1278 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1279 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1280 }
1281
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001282 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1283 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1284
1285 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol version: [%d:%d]",
1286 buf[1], buf[2] ) );
1287
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001288 ssl_read_version( &major, &minor, ssl->transport, buf + 1 );
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001289
1290 /* According to RFC 5246 Appendix E.1, the version here is typically
1291 * "{03,00}, the lowest version number supported by the client, [or] the
1292 * value of ClientHello.client_version", so the only meaningful check here
1293 * is the major version shouldn't be less than 3 */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001294 if( major < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001295 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001296 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1297 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1298 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001299
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001300 /* For DTLS if this is the initial handshake, remember the client sequence
1301 * number to use it in our next message (RFC 6347 4.2.1) */
1302#if defined(POLARSSL_SSL_PROTO_DTLS)
1303 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
1304 ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1305 {
1306 /* Epoch should be 0 for initial handshakes */
1307 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1308 {
1309 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1310 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1311 }
1312
1313 memcpy( ssl->out_ctr + 2, ssl->in_ctr + 2, 6 );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001314
1315#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1316 if( ssl_dtls_replay_check( ssl ) != 0 )
1317 {
1318 SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
1319 ssl->next_record_offset = 0;
1320 ssl->in_left = 0;
1321 goto read_record_header;
1322 }
1323
1324 /* No MAC to check yet, so we can update right now */
1325 ssl_dtls_replay_update( ssl );
1326#endif
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001327 }
1328#endif /* POLARSSL_SSL_PROTO_DTLS */
1329
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001330 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001331
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001332 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Paul Bakkerec636f32012-09-09 19:17:02 +00001333 {
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001334 if( msg_len > SSL_MAX_CONTENT_LEN )
1335 {
1336 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1337 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1338 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001339
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001340 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) + msg_len ) ) != 0 )
1341 {
1342 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1343 return( ret );
1344 }
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001345
1346 /* Done reading this record, get ready for the next one */
1347#if defined(POLARSSL_SSL_PROTO_DTLS)
1348 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1349 ssl->next_record_offset = msg_len + ssl_hdr_len( ssl );
1350 else
1351#endif
1352 ssl->in_left = 0;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001353 }
1354 else
Paul Bakkerec636f32012-09-09 19:17:02 +00001355 {
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001356 /* Set by ssl_read_record() */
1357 msg_len = ssl->in_hslen;
Paul Bakkerec636f32012-09-09 19:17:02 +00001358 }
1359
1360 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001361
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001362 SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001363
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001364 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001365
1366 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001367 * Handshake layer:
1368 * 0 . 0 handshake type
1369 * 1 . 3 handshake length
1370 * 4 . 5 DTLS only: message seqence number
1371 * 6 . 8 DTLS only: fragment offset
1372 * 9 . 11 DTLS only: fragment length
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001373 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001374 if( msg_len < ssl_hs_hdr_len( ssl ) )
1375 {
1376 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1377 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1378 }
1379
1380 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
1381
1382 if( buf[0] != SSL_HS_CLIENT_HELLO )
1383 {
1384 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1385 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1386 }
1387
1388 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1389 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1390
1391 /* We don't support fragmentation of ClientHello (yet?) */
1392 if( buf[1] != 0 ||
1393 msg_len != ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) )
1394 {
1395 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1396 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1397 }
1398
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001399#if defined(POLARSSL_SSL_PROTO_DTLS)
1400 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1401 {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001402 /*
1403 * Copy the client's handshake message_seq on initial handshakes
1404 */
1405 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02001406 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001407 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1408 ssl->in_msg[5];
1409 ssl->handshake->out_msg_seq = cli_msg_seq;
1410 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02001411 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001412 else
1413 {
1414 /* This couldn't be done in ssl_prepare_handshake_record() */
1415 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1416 ssl->in_msg[5];
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001417
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001418 if( cli_msg_seq != ssl->handshake->in_msg_seq )
1419 {
1420 SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
1421 "%d (expected %d)", cli_msg_seq,
1422 ssl->handshake->in_msg_seq ) );
1423 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1424 }
1425
1426 ssl->handshake->in_msg_seq++;
1427 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001428
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001429 /*
1430 * For now we don't support fragmentation, so make sure
1431 * fragment_offset == 0 and fragment_length == length
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001432 */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001433 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1434 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1435 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001436 SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001437 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1438 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001439 }
1440#endif /* POLARSSL_SSL_PROTO_DTLS */
1441
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001442 buf += ssl_hs_hdr_len( ssl );
1443 msg_len -= ssl_hs_hdr_len( ssl );
1444
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001445 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001446 * ClientHello layer:
1447 * 0 . 1 protocol version
1448 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1449 * 34 . 35 session id length (1 byte)
1450 * 35 . 34+x session id
1451 * 35+x . 35+x DTLS only: cookie length (1 byte)
1452 * 36+x . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001453 * .. . .. ciphersuite list length (2 bytes)
1454 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001455 * .. . .. compression alg. list length (1 byte)
1456 * .. . .. compression alg. list
1457 * .. . .. extensions length (2 bytes, optional)
1458 * .. . .. extensions (optional)
Paul Bakkerec636f32012-09-09 19:17:02 +00001459 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001460
1461 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001462 * Minimal length (with everything empty and extensions ommitted) is
1463 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1464 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001465 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001466 if( msg_len < 38 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001467 {
1468 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1469 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1470 }
1471
1472 /*
1473 * Check and save the protocol version
1474 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001475 SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001476
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001477 ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001478 ssl->transport, buf );
Paul Bakkerec636f32012-09-09 19:17:02 +00001479
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001480 ssl->handshake->max_major_ver = ssl->major_ver;
1481 ssl->handshake->max_minor_ver = ssl->minor_ver;
1482
1483 if( ssl->major_ver < ssl->min_major_ver ||
1484 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001485 {
1486 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001487 " [%d:%d] < [%d:%d]",
1488 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001489 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001490
1491 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1492 SSL_ALERT_MSG_PROTOCOL_VERSION );
1493
1494 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1495 }
1496
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001497 if( ssl->major_ver > ssl->max_major_ver )
1498 {
1499 ssl->major_ver = ssl->max_major_ver;
1500 ssl->minor_ver = ssl->max_minor_ver;
1501 }
1502 else if( ssl->minor_ver > ssl->max_minor_ver )
1503 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001504
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001505 /*
1506 * Save client random (inc. Unix time)
1507 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001508 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001509
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001510 memcpy( ssl->handshake->randbytes, buf + 2, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001511
1512 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001513 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001514 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001515 sess_len = buf[34];
Paul Bakkerec636f32012-09-09 19:17:02 +00001516
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001517 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001518 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001519 {
1520 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1521 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1522 }
1523
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001524 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001525
Paul Bakker48916f92012-09-16 19:57:18 +00001526 ssl->session_negotiate->length = sess_len;
1527 memset( ssl->session_negotiate->id, 0,
1528 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001529 memcpy( ssl->session_negotiate->id, buf + 35,
Paul Bakker48916f92012-09-16 19:57:18 +00001530 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001531
1532 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001533 * Check the cookie length and content
1534 */
1535#if defined(POLARSSL_SSL_PROTO_DTLS)
1536 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1537 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001538 cookie_offset = 35 + sess_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001539 cookie_len = buf[cookie_offset];
1540
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001541 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001542 {
1543 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1544 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1545 }
1546
1547 SSL_DEBUG_BUF( 3, "client hello, cookie",
1548 buf + cookie_offset + 1, cookie_len );
1549
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001550#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001551 if( ssl->f_cookie_check != NULL &&
1552 ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001553 {
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001554 if( ssl->f_cookie_check( ssl->p_cookie,
1555 buf + cookie_offset + 1, cookie_len,
1556 ssl->cli_id, ssl->cli_id_len ) != 0 )
1557 {
1558 SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
1559 ssl->handshake->verify_cookie_len = 1;
1560 }
1561 else
1562 {
1563 SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
1564 ssl->handshake->verify_cookie_len = 0;
1565 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001566 }
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001567 else
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +02001568#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001569 {
1570 /* We know we didn't send a cookie, so it should be empty */
1571 if( cookie_len != 0 )
1572 {
1573 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1574 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1575 }
1576
1577 SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
1578 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001579 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001580#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001581
1582 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001583 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001584 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001585#if defined(POLARSSL_SSL_PROTO_DTLS)
1586 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1587 ciph_offset = cookie_offset + 1 + cookie_len;
1588 else
1589#endif
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001590 ciph_offset = 35 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001591
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001592 ciph_len = ( buf[ciph_offset + 0] << 8 )
1593 | ( buf[ciph_offset + 1] );
1594
1595 if( ciph_len < 2 ||
1596 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1597 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001598 {
1599 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1600 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1601 }
1602
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001603 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1604 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001605
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001606 /*
1607 * Check the compression algorithms length and pick one
1608 */
1609 comp_offset = ciph_offset + 2 + ciph_len;
1610
1611 comp_len = buf[comp_offset];
1612
1613 if( comp_len < 1 ||
1614 comp_len > 16 ||
1615 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001616 {
1617 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1618 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1619 }
1620
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001621 SSL_DEBUG_BUF( 3, "client hello, compression",
1622 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001623
1624 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001625#if defined(POLARSSL_ZLIB_SUPPORT)
1626 for( i = 0; i < comp_len; ++i )
1627 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001628 if( buf[comp_offset + 1 + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001629 {
Paul Bakker48916f92012-09-16 19:57:18 +00001630 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001631 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001632 }
1633 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001634#endif
1635
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001636 /* See comments in ssl_write_client_hello() */
1637#if defined(POLARSSL_SSL_PROTO_DTLS)
1638 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1639 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
1640#endif
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001641
Paul Bakkerec636f32012-09-09 19:17:02 +00001642 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001643 * Check the extension length
Paul Bakker48916f92012-09-16 19:57:18 +00001644 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001645 ext_offset = comp_offset + 1 + comp_len;
1646 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001647 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001648 if( msg_len < ext_offset + 2 )
Paul Bakker48916f92012-09-16 19:57:18 +00001649 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001650 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1651 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1652 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001653
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001654 ext_len = ( buf[ext_offset + 0] << 8 )
1655 | ( buf[ext_offset + 1] );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001656
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001657 if( ( ext_len > 0 && ext_len < 4 ) ||
1658 msg_len != ext_offset + 2 + ext_len )
1659 {
1660 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1661 SSL_DEBUG_BUF( 3, "client hello extensions",
1662 buf + ext_offset + 2, ext_len );
1663 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +00001664 }
1665 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001666 else
1667 ext_len = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001668
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001669 ext = buf + ext_offset + 2;
Paul Bakker48916f92012-09-16 19:57:18 +00001670
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001671 while( ext_len != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001672 {
1673 unsigned int ext_id = ( ( ext[0] << 8 )
1674 | ( ext[1] ) );
1675 unsigned int ext_size = ( ( ext[2] << 8 )
1676 | ( ext[3] ) );
1677
1678 if( ext_size + 4 > ext_len )
1679 {
1680 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1681 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1682 }
1683 switch( ext_id )
1684 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001685#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001686 case TLS_EXT_SERVERNAME:
1687 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1688 if( ssl->f_sni == NULL )
1689 break;
1690
1691 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1692 if( ret != 0 )
1693 return( ret );
1694 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001695#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001696
Paul Bakker48916f92012-09-16 19:57:18 +00001697 case TLS_EXT_RENEGOTIATION_INFO:
1698 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1699 renegotiation_info_seen = 1;
1700
Paul Bakker23f36802012-09-28 14:15:14 +00001701 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1702 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001703 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001704 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001705
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001706#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001707 case TLS_EXT_SIG_ALG:
1708 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1709 if( ssl->renegotiation == SSL_RENEGOTIATION )
1710 break;
1711
1712 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1713 if( ret != 0 )
1714 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001715 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001716#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001717
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001718#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001719 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1720 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1721
1722 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1723 if( ret != 0 )
1724 return( ret );
1725 break;
1726
1727 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1728 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001729 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001730
1731 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1732 if( ret != 0 )
1733 return( ret );
1734 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001735#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001736
Paul Bakker05decb22013-08-15 13:33:48 +02001737#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001738 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1739 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1740
1741 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1742 if( ret != 0 )
1743 return( ret );
1744 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001745#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001746
Paul Bakker1f2bc622013-08-15 13:45:55 +02001747#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001748 case TLS_EXT_TRUNCATED_HMAC:
1749 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1750
1751 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1752 if( ret != 0 )
1753 return( ret );
1754 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001755#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001756
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001757#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1758 case TLS_EXT_ENCRYPT_THEN_MAC:
1759 SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
1760
1761 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1762 if( ret != 0 )
1763 return( ret );
1764 break;
1765#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1766
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001767#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1768 case TLS_EXT_EXTENDED_MASTER_SECRET:
1769 SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
1770
1771 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1772 if( ret != 0 )
1773 return( ret );
1774 break;
1775#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1776
Paul Bakkera503a632013-08-14 13:48:06 +02001777#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001778 case TLS_EXT_SESSION_TICKET:
1779 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1780
1781 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1782 if( ret != 0 )
1783 return( ret );
1784 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001785#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001786
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001787#if defined(POLARSSL_SSL_ALPN)
1788 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001789 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001790
1791 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1792 if( ret != 0 )
1793 return( ret );
1794 break;
1795#endif /* POLARSSL_SSL_SESSION_TICKETS */
1796
Paul Bakker48916f92012-09-16 19:57:18 +00001797 default:
1798 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1799 ext_id ) );
1800 }
1801
1802 ext_len -= 4 + ext_size;
1803 ext += 4 + ext_size;
1804
1805 if( ext_len > 0 && ext_len < 4 )
1806 {
1807 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1808 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1809 }
1810 }
1811
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01001812#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1813 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1814 {
1815 if( p[0] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1816 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1817 {
1818 SSL_DEBUG_MSG( 0, ( "received FALLBACK_SCSV" ) );
1819
1820 if( ssl->minor_ver < ssl->max_minor_ver )
1821 {
1822 SSL_DEBUG_MSG( 0, ( "inapropriate fallback" ) );
1823
1824 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1825 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1826
1827 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1828 }
1829
1830 break;
1831 }
1832 }
1833#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1834
Paul Bakker48916f92012-09-16 19:57:18 +00001835 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001836 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1837 */
1838 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
1839 {
1840 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1841 {
1842 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1843 if( ssl->renegotiation == SSL_RENEGOTIATION )
1844 {
1845 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1846
1847 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1848 return( ret );
1849
1850 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1851 }
1852 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1853 break;
1854 }
1855 }
1856
1857 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001858 * Renegotiation security checks
1859 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001860 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1861 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1862 {
1863 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1864 handshake_failure = 1;
1865 }
1866 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1867 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1868 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001869 {
1870 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001871 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001872 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001873 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1874 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1875 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001876 {
1877 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001878 handshake_failure = 1;
1879 }
1880 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1881 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1882 renegotiation_info_seen == 1 )
1883 {
1884 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1885 handshake_failure = 1;
1886 }
1887
1888 if( handshake_failure == 1 )
1889 {
1890 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1891 return( ret );
1892
Paul Bakker48916f92012-09-16 19:57:18 +00001893 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1894 }
Paul Bakker380da532012-04-18 16:10:25 +00001895
Paul Bakker41c83d32013-03-20 14:39:14 +01001896 /*
1897 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001898 * (At the end because we need information from the EC-based extensions
1899 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001900 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001901 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001902 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001903#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001904 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001905 {
1906 for( i = 0; ciphersuites[i] != 0; i++ )
1907#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001908 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001909 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001910 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001911#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001912 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001913 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1914 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1915 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001916
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001917 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1918 &ciphersuite_info ) ) != 0 )
1919 return( ret );
1920
1921 if( ciphersuite_info != NULL )
1922 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001923 }
1924 }
1925
1926 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1927
1928 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1929 return( ret );
1930
1931 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1932
1933have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001934 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001935 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1936 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1937
Paul Bakker5121ce52009-01-03 21:22:43 +00001938 ssl->state++;
1939
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001940#if defined(POLARSSL_SSL_PROTO_DTLS)
1941 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1942 ssl_recv_flight_completed( ssl );
1943#endif
1944
Paul Bakker5121ce52009-01-03 21:22:43 +00001945 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1946
1947 return( 0 );
1948}
1949
Paul Bakker1f2bc622013-08-15 13:45:55 +02001950#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001951static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1952 unsigned char *buf,
1953 size_t *olen )
1954{
1955 unsigned char *p = buf;
1956
1957 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1958 {
1959 *olen = 0;
1960 return;
1961 }
1962
1963 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1964
1965 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1966 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1967
1968 *p++ = 0x00;
1969 *p++ = 0x00;
1970
1971 *olen = 4;
1972}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001973#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001974
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001975#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1976static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
1977 unsigned char *buf,
1978 size_t *olen )
1979{
1980 unsigned char *p = buf;
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01001981 const ssl_ciphersuite_t *suite = NULL;
1982 const cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001983
1984 if( ssl->session_negotiate->encrypt_then_mac == SSL_EXTENDED_MS_DISABLED ||
1985 ssl->minor_ver == SSL_MINOR_VERSION_0 )
1986 {
1987 *olen = 0;
1988 return;
1989 }
1990
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01001991 /*
1992 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
1993 * from a client and then selects a stream or Authenticated Encryption
1994 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
1995 * encrypt-then-MAC response extension back to the client."
1996 */
1997 if( ( suite = ssl_ciphersuite_from_id(
1998 ssl->session_negotiate->ciphersuite ) ) == NULL ||
1999 ( cipher = cipher_info_from_type( suite->cipher ) ) == NULL ||
2000 cipher->mode != POLARSSL_MODE_CBC )
2001 {
2002 *olen = 0;
2003 return;
2004 }
2005
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002006 SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
2007
2008 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
2009 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
2010
2011 *p++ = 0x00;
2012 *p++ = 0x00;
2013
2014 *olen = 4;
2015}
2016#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
2017
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002018#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2019static void ssl_write_extended_ms_ext( ssl_context *ssl,
2020 unsigned char *buf,
2021 size_t *olen )
2022{
2023 unsigned char *p = buf;
2024
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +02002025 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_DISABLED ||
2026 ssl->minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002027 {
2028 *olen = 0;
2029 return;
2030 }
2031
2032 SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
2033 "extension" ) );
2034
2035 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
2036 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
2037
2038 *p++ = 0x00;
2039 *p++ = 0x00;
2040
2041 *olen = 4;
2042}
2043#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
2044
Paul Bakkera503a632013-08-14 13:48:06 +02002045#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002046static void ssl_write_session_ticket_ext( ssl_context *ssl,
2047 unsigned char *buf,
2048 size_t *olen )
2049{
2050 unsigned char *p = buf;
2051
2052 if( ssl->handshake->new_session_ticket == 0 )
2053 {
2054 *olen = 0;
2055 return;
2056 }
2057
2058 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
2059
2060 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
2061 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
2062
2063 *p++ = 0x00;
2064 *p++ = 0x00;
2065
2066 *olen = 4;
2067}
Paul Bakkera503a632013-08-14 13:48:06 +02002068#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002069
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002070static void ssl_write_renegotiation_ext( ssl_context *ssl,
2071 unsigned char *buf,
2072 size_t *olen )
2073{
2074 unsigned char *p = buf;
2075
2076 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
2077 {
2078 *olen = 0;
2079 return;
2080 }
2081
2082 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
2083
2084 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
2085 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
2086
2087 *p++ = 0x00;
2088 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2089 *p++ = ssl->verify_data_len * 2 & 0xFF;
2090
2091 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2092 p += ssl->verify_data_len;
2093 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2094 p += ssl->verify_data_len;
2095
2096 *olen = 5 + ssl->verify_data_len * 2;
2097}
2098
Paul Bakker05decb22013-08-15 13:33:48 +02002099#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002100static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
2101 unsigned char *buf,
2102 size_t *olen )
2103{
2104 unsigned char *p = buf;
2105
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002106 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
2107 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002108 *olen = 0;
2109 return;
2110 }
2111
2112 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
2113
2114 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
2115 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
2116
2117 *p++ = 0x00;
2118 *p++ = 1;
2119
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002120 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002121
2122 *olen = 5;
2123}
Paul Bakker05decb22013-08-15 13:33:48 +02002124#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002125
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002126#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002127static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
2128 unsigned char *buf,
2129 size_t *olen )
2130{
2131 unsigned char *p = buf;
2132 ((void) ssl);
2133
Paul Bakker677377f2013-10-28 12:54:26 +01002134 if( ( ssl->handshake->cli_exts &
2135 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
2136 {
2137 *olen = 0;
2138 return;
2139 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002140
2141 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
2142
2143 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2144 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
2145
2146 *p++ = 0x00;
2147 *p++ = 2;
2148
2149 *p++ = 1;
2150 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
2151
2152 *olen = 6;
2153}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002154#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002155
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002156#if defined(POLARSSL_SSL_ALPN )
2157static void ssl_write_alpn_ext( ssl_context *ssl,
2158 unsigned char *buf, size_t *olen )
2159{
2160 if( ssl->alpn_chosen == NULL )
2161 {
2162 *olen = 0;
2163 return;
2164 }
2165
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02002166 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002167
2168 /*
2169 * 0 . 1 ext identifier
2170 * 2 . 3 ext length
2171 * 4 . 5 protocol list length
2172 * 6 . 6 protocol name length
2173 * 7 . 7+n protocol name
2174 */
2175 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
2176 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
2177
2178 *olen = 7 + strlen( ssl->alpn_chosen );
2179
2180 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2181 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2182
2183 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2184 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2185
2186 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2187
2188 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2189}
2190#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
2191
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002192#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002193static int ssl_write_hello_verify_request( ssl_context *ssl )
2194{
2195 int ret;
2196 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002197 unsigned char *cookie_len_byte;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002198
2199 SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
2200
2201 /*
2202 * struct {
2203 * ProtocolVersion server_version;
2204 * opaque cookie<0..2^8-1>;
2205 * } HelloVerifyRequest;
2206 */
2207
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02002208 /* The RFC is not clear on this point, but sending the actual negotiated
2209 * version looks like the most interoperable thing to do. */
2210 ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002211 ssl->transport, p );
2212 SSL_DEBUG_BUF( 3, "server version", (unsigned char *) p, 2 );
2213 p += 2;
2214
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002215 /* If we get here, f_cookie_check is not null */
2216 if( ssl->f_cookie_write == NULL )
2217 {
2218 SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
2219 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2220 }
2221
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002222 /* Skip length byte until we know the length */
2223 cookie_len_byte = p++;
2224
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002225 if( ( ret = ssl->f_cookie_write( ssl->p_cookie,
2226 &p, ssl->out_buf + SSL_BUFFER_LEN,
2227 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002228 {
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002229 SSL_DEBUG_RET( 1, "f_cookie_write", ret );
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002230 return( ret );
2231 }
2232
2233 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2234
2235 SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002236
2237 ssl->out_msglen = p - ssl->out_msg;
2238 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2239 ssl->out_msg[0] = SSL_HS_HELLO_VERIFY_REQUEST;
2240
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002241 ssl->state = SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002242
2243 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2244 {
2245 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2246 return( ret );
2247 }
2248
2249 SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
2250
2251 return( 0 );
2252}
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002253#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002254
Paul Bakker5121ce52009-01-03 21:22:43 +00002255static int ssl_write_server_hello( ssl_context *ssl )
2256{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002257#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002258 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002259#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002260 int ret;
2261 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002262 unsigned char *buf, *p;
2263
2264 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
2265
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002266#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002267 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002268 ssl->handshake->verify_cookie_len != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002269 {
2270 SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2271 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2272
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002273 return( ssl_write_hello_verify_request( ssl ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002274 }
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002275#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002276
Paul Bakkera9a028e2013-11-21 17:31:06 +01002277 if( ssl->f_rng == NULL )
2278 {
2279 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2280 return( POLARSSL_ERR_SSL_NO_RNG );
2281 }
2282
Paul Bakker5121ce52009-01-03 21:22:43 +00002283 /*
2284 * 0 . 0 handshake type
2285 * 1 . 3 handshake length
2286 * 4 . 5 protocol version
2287 * 6 . 9 UNIX time()
2288 * 10 . 37 random bytes
2289 */
2290 buf = ssl->out_msg;
2291 p = buf + 4;
2292
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002293 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2294 ssl->transport, p );
2295 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002296
2297 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002298 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002299
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002300#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002301 t = time( NULL );
2302 *p++ = (unsigned char)( t >> 24 );
2303 *p++ = (unsigned char)( t >> 16 );
2304 *p++ = (unsigned char)( t >> 8 );
2305 *p++ = (unsigned char)( t );
2306
2307 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002308#else
2309 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
2310 return( ret );
2311
2312 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02002313#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002314
Paul Bakkera3d195c2011-11-27 21:07:34 +00002315 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
2316 return( ret );
2317
2318 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002319
Paul Bakker48916f92012-09-16 19:57:18 +00002320 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002321
2322 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
2323
2324 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002325 * Resume is 0 by default, see ssl_handshake_init().
2326 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2327 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002328 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002329 if( ssl->handshake->resume == 0 &&
2330 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002331 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002332 ssl->f_get_cache != NULL &&
2333 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
2334 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01002335 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002336 ssl->handshake->resume = 1;
2337 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002338
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002339 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002340 {
2341 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002342 * New session, create a new session id,
2343 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002344 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002345 ssl->state++;
2346
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002347#if defined(POLARSSL_HAVE_TIME)
2348 ssl->session_negotiate->start = time( NULL );
2349#endif
2350
Paul Bakkera503a632013-08-14 13:48:06 +02002351#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002352 if( ssl->handshake->new_session_ticket != 0 )
2353 {
2354 ssl->session_negotiate->length = n = 0;
2355 memset( ssl->session_negotiate->id, 0, 32 );
2356 }
2357 else
2358#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002359 {
2360 ssl->session_negotiate->length = n = 32;
2361 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002362 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002363 return( ret );
2364 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002365 }
2366 else
2367 {
2368 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002369 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002370 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02002371 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002372 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002373
2374 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2375 {
2376 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2377 return( ret );
2378 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002379 }
2380
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002381 /*
2382 * 38 . 38 session id length
2383 * 39 . 38+n session id
2384 * 39+n . 40+n chosen ciphersuite
2385 * 41+n . 41+n chosen compression alg.
2386 * 42+n . 43+n extensions length
2387 * 44+n . 43+n+m extensions
2388 */
2389 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00002390 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
2391 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002392
2393 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2394 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2395 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002396 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002397
Paul Bakker48916f92012-09-16 19:57:18 +00002398 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2399 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2400 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002401
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02002402 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2403 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02002404 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002405 ssl->session_negotiate->compression ) );
2406
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002407 /*
2408 * First write extensions, then the total length
2409 */
2410 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2411 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002412
Paul Bakker05decb22013-08-15 13:33:48 +02002413#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002414 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2415 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002416#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002417
Paul Bakker1f2bc622013-08-15 13:45:55 +02002418#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002419 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2420 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002421#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002422
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002423#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2424 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2425 ext_len += olen;
2426#endif
2427
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002428#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2429 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2430 ext_len += olen;
2431#endif
2432
Paul Bakkera503a632013-08-14 13:48:06 +02002433#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002434 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2435 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002436#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002437
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002438#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002439 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2440 ext_len += olen;
2441#endif
2442
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002443#if defined(POLARSSL_SSL_ALPN)
2444 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2445 ext_len += olen;
2446#endif
2447
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002448 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002449
Paul Bakkera7036632014-04-30 10:15:38 +02002450 if( ext_len > 0 )
2451 {
2452 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2453 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2454 p += ext_len;
2455 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002456
2457 ssl->out_msglen = p - buf;
2458 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2459 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
2460
2461 ret = ssl_write_record( ssl );
2462
2463 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2464
2465 return( ret );
2466}
2467
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002468#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2469 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002470 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2471 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002472static int ssl_write_certificate_request( ssl_context *ssl )
2473{
Paul Bakkered27a042013-04-18 22:46:23 +02002474 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002475
2476 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2477
2478 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002479 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002480 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2481 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002482 {
2483 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2484 ssl->state++;
2485 return( 0 );
2486 }
2487
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002488 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2489 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002490}
2491#else
2492static int ssl_write_certificate_request( ssl_context *ssl )
2493{
2494 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2495 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002496 size_t dn_size, total_dn_size; /* excluding length bytes */
2497 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002498 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002499 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00002500
2501 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2502
2503 ssl->state++;
2504
Paul Bakkerfbb17802013-04-17 19:10:21 +02002505 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002506 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002507 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002508 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002509 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002510 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002511 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002512 return( 0 );
2513 }
2514
2515 /*
2516 * 0 . 0 handshake type
2517 * 1 . 3 handshake length
2518 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002519 * 5 .. m-1 cert types
2520 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002521 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002522 * n .. n+1 length of all DNs
2523 * n+2 .. n+3 length of DN 1
2524 * n+4 .. ... Distinguished Name #1
2525 * ... .. ... length of DN 2, etc.
2526 */
2527 buf = ssl->out_msg;
2528 p = buf + 4;
2529
2530 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002531 * Supported certificate types
2532 *
2533 * ClientCertificateType certificate_types<1..2^8-1>;
2534 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002535 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002536 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002537
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002538#if defined(POLARSSL_RSA_C)
2539 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2540#endif
2541#if defined(POLARSSL_ECDSA_C)
2542 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2543#endif
2544
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002545 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002546 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002547
Paul Bakker577e0062013-08-28 11:57:20 +02002548 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002549#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002550 /*
2551 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002552 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002553 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2554 *
2555 * struct {
2556 * HashAlgorithm hash;
2557 * SignatureAlgorithm signature;
2558 * } SignatureAndHashAlgorithm;
2559 *
2560 * enum { (255) } HashAlgorithm;
2561 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002562 */
Paul Bakker21dca692013-01-03 11:41:08 +01002563 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002564 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002565 /*
2566 * Only use current running hash algorithm that is already required
2567 * for requested ciphersuite.
2568 */
Paul Bakker926af752012-11-23 13:38:07 +01002569 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2570
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002571 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2572 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002573 {
2574 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2575 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002576
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002577 /*
2578 * Supported signature algorithms
2579 */
2580#if defined(POLARSSL_RSA_C)
2581 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2582 p[2 + sa_len++] = SSL_SIG_RSA;
2583#endif
2584#if defined(POLARSSL_ECDSA_C)
2585 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2586 p[2 + sa_len++] = SSL_SIG_ECDSA;
2587#endif
Paul Bakker926af752012-11-23 13:38:07 +01002588
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002589 p[0] = (unsigned char)( sa_len >> 8 );
2590 p[1] = (unsigned char)( sa_len );
2591 sa_len += 2;
2592 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002593 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002594#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002595
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002596 /*
2597 * DistinguishedName certificate_authorities<0..2^16-1>;
2598 * opaque DistinguishedName<1..2^16-1>;
2599 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002600 p += 2;
2601 crt = ssl->ca_chain;
2602
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002603 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002604 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002605 {
2606 if( p - buf > 4096 )
2607 break;
2608
Paul Bakker926af752012-11-23 13:38:07 +01002609 dn_size = crt->subject_raw.len;
2610 *p++ = (unsigned char)( dn_size >> 8 );
2611 *p++ = (unsigned char)( dn_size );
2612 memcpy( p, crt->subject_raw.p, dn_size );
2613 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002614
Paul Bakker926af752012-11-23 13:38:07 +01002615 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2616
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002617 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002618 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 }
2620
Paul Bakker926af752012-11-23 13:38:07 +01002621 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002622 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2623 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002624 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2625 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002626
2627 ret = ssl_write_record( ssl );
2628
2629 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2630
2631 return( ret );
2632}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002633#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2634 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002635 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2636 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002637
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002638#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2639 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2640static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2641{
2642 int ret;
2643
2644 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2645 {
2646 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2647 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2648 }
2649
2650 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2651 pk_ec( *ssl_own_key( ssl ) ),
2652 POLARSSL_ECDH_OURS ) ) != 0 )
2653 {
2654 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2655 return( ret );
2656 }
2657
2658 return( 0 );
2659}
2660#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2661 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2662
Paul Bakker41c83d32013-03-20 14:39:14 +01002663static int ssl_write_server_key_exchange( ssl_context *ssl )
2664{
Paul Bakker23986e52011-04-24 08:57:21 +00002665 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002666 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002667 const ssl_ciphersuite_t *ciphersuite_info =
2668 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002669
2670#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2671 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2672 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002673 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002674 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002675 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002676 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002677 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002678 ((void) dig_signed);
2679 ((void) dig_signed_len);
2680#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002681
Paul Bakker5121ce52009-01-03 21:22:43 +00002682 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2683
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002684#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2685 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2686 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002687 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2688 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2689 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002690 {
2691 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2692 ssl->state++;
2693 return( 0 );
2694 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002695#endif
2696
2697#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2698 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2699 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2700 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2701 {
2702 ssl_get_ecdh_params_from_cert( ssl );
2703
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002704 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002705 ssl->state++;
2706 return( 0 );
2707 }
2708#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002709
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002710#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2711 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2712 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2713 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002714 {
2715 /* TODO: Support identity hints */
2716 *(p++) = 0x00;
2717 *(p++) = 0x00;
2718
2719 n += 2;
2720 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002721#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2722 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002723
2724#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2725 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2726 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2727 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002728 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002729 /*
2730 * Ephemeral DH parameters:
2731 *
2732 * struct {
2733 * opaque dh_p<1..2^16-1>;
2734 * opaque dh_g<1..2^16-1>;
2735 * opaque dh_Ys<1..2^16-1>;
2736 * } ServerDHParams;
2737 */
2738 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2739 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2740 {
2741 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2742 return( ret );
2743 }
Paul Bakker48916f92012-09-16 19:57:18 +00002744
Paul Bakker41c83d32013-03-20 14:39:14 +01002745 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002746 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2747 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002748 {
2749 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2750 return( ret );
2751 }
2752
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002753 dig_signed = p;
2754 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002755
2756 p += len;
2757 n += len;
2758
Paul Bakker41c83d32013-03-20 14:39:14 +01002759 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2760 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2761 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2762 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2763 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002764#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2765 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002766
Gergely Budai987bfb52014-01-19 21:48:42 +01002767#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002768 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002769 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2770 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002771 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002772 /*
2773 * Ephemeral ECDH parameters:
2774 *
2775 * struct {
2776 * ECParameters curve_params;
2777 * ECPoint public;
2778 * } ServerECDHParams;
2779 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002780 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002781#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002782 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002783
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002784 /* Match our preference list against the offered curves */
2785 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2786 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2787 if( (*curve)->grp_id == *gid )
2788 goto curve_matching_done;
2789
2790curve_matching_done:
2791#else
2792 curve = ssl->handshake->curves;
2793#endif
2794
2795 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002796 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002797 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2798 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002799 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002800
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002801 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002802
Paul Bakker41c83d32013-03-20 14:39:14 +01002803 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002804 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002805 {
2806 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2807 return( ret );
2808 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002809
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002810 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2811 p, SSL_MAX_CONTENT_LEN - n,
2812 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002813 {
2814 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2815 return( ret );
2816 }
2817
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002818 dig_signed = p;
2819 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002820
2821 p += len;
2822 n += len;
2823
Paul Bakker41c83d32013-03-20 14:39:14 +01002824 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2825 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002826#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002827
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002828#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002829 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2830 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002831 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002832 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2833 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002834 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002835 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002836 unsigned int hashlen = 0;
2837 unsigned char hash[64];
2838 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002839
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002840 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002841 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2842 */
Paul Bakker577e0062013-08-28 11:57:20 +02002843#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002844 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2845 {
2846 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2847
2848 if( md_alg == POLARSSL_MD_NONE )
2849 {
2850 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002851 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002852 }
2853 }
Paul Bakker577e0062013-08-28 11:57:20 +02002854 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002855#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002856#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2857 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002858 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002859 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2860 {
2861 md_alg = POLARSSL_MD_SHA1;
2862 }
2863 else
Paul Bakker577e0062013-08-28 11:57:20 +02002864#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002865 {
2866 md_alg = POLARSSL_MD_NONE;
2867 }
2868
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002869 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002870 * Compute the hash to be signed
2871 */
Paul Bakker577e0062013-08-28 11:57:20 +02002872#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2873 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002874 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002875 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002876 md5_context md5;
2877 sha1_context sha1;
2878
Paul Bakker5b4af392014-06-26 12:09:34 +02002879 md5_init( &md5 );
2880 sha1_init( &sha1 );
2881
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002882 /*
2883 * digitally-signed struct {
2884 * opaque md5_hash[16];
2885 * opaque sha_hash[20];
2886 * };
2887 *
2888 * md5_hash
2889 * MD5(ClientHello.random + ServerHello.random
2890 * + ServerParams);
2891 * sha_hash
2892 * SHA(ClientHello.random + ServerHello.random
2893 * + ServerParams);
2894 */
2895 md5_starts( &md5 );
2896 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002897 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002898 md5_finish( &md5, hash );
2899
2900 sha1_starts( &sha1 );
2901 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002902 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002903 sha1_finish( &sha1, hash + 16 );
2904
2905 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02002906
2907 md5_free( &md5 );
2908 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002909 }
2910 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002911#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2912 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002913#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2914 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002915 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002916 {
2917 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02002918 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002919
Paul Bakker84bbeb52014-07-01 14:53:22 +02002920 md_init( &ctx );
2921
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002922 /* Info from md_alg will be used instead */
2923 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002924
2925 /*
2926 * digitally-signed struct {
2927 * opaque client_random[32];
2928 * opaque server_random[32];
2929 * ServerDHParams params;
2930 * };
2931 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002932 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002933 {
2934 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2935 return( ret );
2936 }
2937
2938 md_starts( &ctx );
2939 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002940 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002941 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002942 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00002943 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002944 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002945#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2946 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002947 {
2948 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002949 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002950 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002951
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002952 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2953 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002954
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002955 /*
2956 * Make the signature
2957 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002958 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002959 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002960 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2961 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002962 }
Paul Bakker23f36802012-09-28 14:15:14 +00002963
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002964#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002965 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2966 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002967 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002968 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002969
2970 n += 2;
2971 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002972#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002973
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002974 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002975 p + 2 , &signature_len,
2976 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002977 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002978 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002979 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002980 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002981
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002982 *(p++) = (unsigned char)( signature_len >> 8 );
2983 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002984 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002985
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002986 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002987
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002988 p += signature_len;
2989 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002990 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002991#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002992 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2993 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002994
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002995 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002996 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2997 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2998
2999 ssl->state++;
3000
3001 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3002 {
3003 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3004 return( ret );
3005 }
3006
3007 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
3008
3009 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003010}
3011
3012static int ssl_write_server_hello_done( ssl_context *ssl )
3013{
3014 int ret;
3015
3016 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
3017
3018 ssl->out_msglen = 4;
3019 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3020 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
3021
3022 ssl->state++;
3023
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003024#if defined(POLARSSL_SSL_PROTO_DTLS)
3025 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3026 ssl_send_flight_completed( ssl );
3027#endif
3028
Paul Bakker5121ce52009-01-03 21:22:43 +00003029 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3030 {
3031 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3032 return( ret );
3033 }
3034
3035 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
3036
3037 return( 0 );
3038}
3039
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003040#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3041 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3042static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
3043 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003044{
3045 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003046 size_t n;
3047
3048 /*
3049 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3050 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003051 if( *p + 2 > end )
3052 {
3053 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3054 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3055 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003056
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003057 n = ( (*p)[0] << 8 ) | (*p)[1];
3058 *p += 2;
3059
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003060 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003061 {
3062 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3063 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3064 }
3065
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003066 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003067 {
3068 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
3069 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3070 }
3071
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003072 *p += n;
3073
Paul Bakker70df2fb2013-04-17 17:19:09 +02003074 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
3075
Paul Bakker70df2fb2013-04-17 17:19:09 +02003076 return( ret );
3077}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003078#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3079 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003080
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003081#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
3082 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003083static int ssl_parse_encrypted_pms( ssl_context *ssl,
3084 const unsigned char *p,
3085 const unsigned char *end,
3086 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003087{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003088 int ret;
3089 size_t len = pk_get_len( ssl_own_key( ssl ) );
3090 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003091 unsigned char ver[2];
Paul Bakker70df2fb2013-04-17 17:19:09 +02003092
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003093 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003094 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02003095 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003096 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3097 }
3098
3099 /*
3100 * Decrypt the premaster using own private RSA key
3101 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003102#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3103 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02003104 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
3105 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003106 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
3107 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003108 {
3109 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3110 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3111 }
3112 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003113#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003114
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003115 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003116 {
3117 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3118 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3119 }
3120
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003121 ssl_write_version( ssl->handshake->max_major_ver,
3122 ssl->handshake->max_minor_ver,
3123 ssl->transport, ver );
3124
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003125 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
3126 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003127 sizeof( ssl->handshake->premaster ) - pms_offset,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003128 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003129
3130 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003131 pms[0] != ver[0] ||
3132 pms[1] != ver[1] )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003133 {
3134 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3135
3136 /*
3137 * Protection against Bleichenbacher's attack:
3138 * invalid PKCS#1 v1.5 padding must not cause
3139 * the connection to end immediately; instead,
3140 * send a bad_record_mac later in the handshake.
3141 */
3142 ssl->handshake->pmslen = 48;
3143
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003144 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003145 if( ret != 0 )
3146 return( ret );
3147 }
3148
3149 return( ret );
3150}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003151#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
3152 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003153
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003154#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003155static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
3156 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003157{
Paul Bakker6db455e2013-09-18 17:29:31 +02003158 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003159 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003160
Paul Bakker6db455e2013-09-18 17:29:31 +02003161 if( ssl->f_psk == NULL &&
3162 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
3163 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003164 {
3165 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
3166 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3167 }
3168
3169 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003170 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003171 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003172 if( *p + 2 > end )
3173 {
3174 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3175 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3176 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003177
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003178 n = ( (*p)[0] << 8 ) | (*p)[1];
3179 *p += 2;
3180
3181 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003182 {
3183 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3184 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3185 }
3186
Paul Bakker6db455e2013-09-18 17:29:31 +02003187 if( ssl->f_psk != NULL )
3188 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003189 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003190 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3191 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003192 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003193 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003194 /* Identity is not a big secret since clients send it in the clear,
3195 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02003196 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003197 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003198 {
3199 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3200 }
3201 }
3202
3203 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003204 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003205 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02003206 if( ( ret = ssl_send_alert_message( ssl,
3207 SSL_ALERT_LEVEL_FATAL,
3208 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
3209 {
3210 return( ret );
3211 }
3212
3213 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003214 }
3215
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003216 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003217
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003218 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003219}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003220#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003221
Paul Bakker5121ce52009-01-03 21:22:43 +00003222static int ssl_parse_client_key_exchange( ssl_context *ssl )
3223{
Paul Bakker23986e52011-04-24 08:57:21 +00003224 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003225 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003226 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003227
Paul Bakker41c83d32013-03-20 14:39:14 +01003228 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003229
3230 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
3231
3232 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3233 {
3234 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3235 return( ret );
3236 }
3237
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003238 p = ssl->in_msg + ssl_hs_hdr_len( ssl );
3239 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00003240
Paul Bakker5121ce52009-01-03 21:22:43 +00003241 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3242 {
3243 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003244 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003245 }
3246
3247 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
3248 {
3249 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003250 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003251 }
3252
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003253#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01003254 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003255 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003256 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003257 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02003258 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3259 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003260 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003261
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003262 if( p != end )
3263 {
3264 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3265 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3266 }
3267
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02003268 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003269
3270 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
3271 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003272 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02003273 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003274 {
3275 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
3276 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3277 }
3278
3279 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003280 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003281 else
3282#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003283#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003284 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3285 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3286 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003287 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003288 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
3289 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
3290 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003291 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003292 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003293 p, end - p) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003294 {
3295 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3296 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3297 }
3298
3299 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3300
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003301 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
3302 &ssl->handshake->pmslen,
3303 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02003304 POLARSSL_MPI_MAX_SIZE,
3305 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003306 {
3307 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
3308 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3309 }
3310
3311 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003312 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003313 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003314#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003315 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3316 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3317 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003318#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
3319 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003320 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003321 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003322 {
3323 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3324 return( ret );
3325 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003326
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003327 if( p != end )
3328 {
3329 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3330 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3331 }
3332
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003333 if( ( ret = ssl_psk_derive_premaster( ssl,
3334 ciphersuite_info->key_exchange ) ) != 0 )
3335 {
3336 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3337 return( ret );
3338 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003339 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003340 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003341#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003342#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
3343 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
3344 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003345 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3346 {
3347 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3348 return( ret );
3349 }
3350
3351 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3352 {
3353 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
3354 return( ret );
3355 }
3356
3357 if( ( ret = ssl_psk_derive_premaster( ssl,
3358 ciphersuite_info->key_exchange ) ) != 0 )
3359 {
3360 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3361 return( ret );
3362 }
3363 }
3364 else
3365#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003366#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3367 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3368 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003369 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3370 {
3371 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3372 return( ret );
3373 }
3374 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3375 {
3376 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3377 return( ret );
3378 }
3379
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003380 if( p != end )
3381 {
3382 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3383 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3384 }
3385
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003386 if( ( ret = ssl_psk_derive_premaster( ssl,
3387 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003388 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003389 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3390 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003391 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003392 }
3393 else
3394#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003395#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3396 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
3397 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003398 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3399 {
3400 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3401 return( ret );
3402 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003403
3404 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
3405 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003406 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003407 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3408 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003409 }
3410
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003411 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3412
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003413 if( ( ret = ssl_psk_derive_premaster( ssl,
3414 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003415 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003416 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003417 return( ret );
3418 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003419 }
3420 else
3421#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003422#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
3423 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003424 {
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003425 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003426 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003427 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003428 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003429 }
3430 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003431 else
3432#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
3433 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003434 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003435 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003436 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003437
Paul Bakkerff60ee62010-03-16 21:09:09 +00003438 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
3439 {
3440 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
3441 return( ret );
3442 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003443
Paul Bakker5121ce52009-01-03 21:22:43 +00003444 ssl->state++;
3445
3446 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3447
3448 return( 0 );
3449}
3450
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003451#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3452 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003453 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3454 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003455static int ssl_parse_certificate_verify( ssl_context *ssl )
3456{
Paul Bakkerfbb17802013-04-17 19:10:21 +02003457 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003458
3459 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3460
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003461 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003462 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003463 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003464 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02003465 {
3466 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3467 ssl->state++;
3468 return( 0 );
3469 }
3470
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003471 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3472 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003473}
3474#else
3475static int ssl_parse_certificate_verify( ssl_context *ssl )
3476{
3477 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003478 size_t i, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003479 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003480 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003481 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003482#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003483 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003484#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003485 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003486 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3487
3488 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3489
3490 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003491 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003492 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003493 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3494 ssl->session_negotiate->peer_cert == NULL )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003495 {
3496 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3497 ssl->state++;
3498 return( 0 );
3499 }
3500
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003501 /* Needs to be done before read_record() to exclude current message */
Paul Bakker48916f92012-09-16 19:57:18 +00003502 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003503
3504 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3505 {
3506 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3507 return( ret );
3508 }
3509
3510 ssl->state++;
3511
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003512 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE ||
3513 ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003514 {
3515 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003516 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003517 }
3518
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003519 i = ssl_hs_hdr_len( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003520
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003521 /*
3522 * struct {
3523 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
3524 * opaque signature<0..2^16-1>;
3525 * } DigitallySigned;
3526 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003527#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3528 defined(POLARSSL_SSL_PROTO_TLS1_1)
3529 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003530 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003531 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003532 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003533
3534 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3535 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3536 POLARSSL_PK_ECDSA ) )
3537 {
3538 hash_start += 16;
3539 hashlen -= 16;
3540 md_alg = POLARSSL_MD_SHA1;
3541 }
Paul Bakker926af752012-11-23 13:38:07 +01003542 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003543 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003544#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3545 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003546#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3547 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003548 {
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00003549 if( i + 2 > ssl->in_hslen )
3550 {
3551 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3552 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3553 }
3554
Paul Bakker5121ce52009-01-03 21:22:43 +00003555 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003556 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003557 */
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003558 if( ssl->in_msg[i] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003559 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003560 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3561 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003562 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3563 }
3564
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003565 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003566
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003567 /* Info from md_alg will be used instead */
3568 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003569
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003570 i++;
3571
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003572 /*
3573 * Signature
3574 */
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003575 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003576 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003577 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003578 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3579 " for verify message" ) );
3580 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003581 }
3582
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003583 /*
3584 * Check the certificate's key type matches the signature alg
3585 */
3586 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3587 {
3588 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3589 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3590 }
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003591
3592 i++;
Paul Bakker577e0062013-08-28 11:57:20 +02003593 }
3594 else
3595#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3596 {
3597 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003598 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003599 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003600
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00003601 if( i + 2 > ssl->in_hslen )
3602 {
3603 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3604 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3605 }
3606
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003607 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
3608 i += 2;
Paul Bakker926af752012-11-23 13:38:07 +01003609
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003610 if( i + sig_len != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003611 {
3612 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003613 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003614 }
3615
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003616 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003617 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003618 ssl->in_msg + i, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003619 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003620 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003621 return( ret );
3622 }
3623
3624 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3625
Paul Bakkered27a042013-04-18 22:46:23 +02003626 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003627}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003628#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3629 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3630 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003631
Paul Bakkera503a632013-08-14 13:48:06 +02003632#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003633static int ssl_write_new_session_ticket( ssl_context *ssl )
3634{
3635 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003636 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003637 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003638
3639 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3640
3641 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3642 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3643
3644 /*
3645 * struct {
3646 * uint32 ticket_lifetime_hint;
3647 * opaque ticket<0..2^16-1>;
3648 * } NewSessionTicket;
3649 *
3650 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3651 * 8 . 9 ticket_len (n)
3652 * 10 . 9+n ticket content
3653 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003654
3655 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3656 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3657 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3658 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003659
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003660 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3661 {
3662 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3663 tlen = 0;
3664 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003665
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003666 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3667 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003668
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003669 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003670
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003671 /*
3672 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3673 * ChangeCipherSpec share the same state.
3674 */
3675 ssl->handshake->new_session_ticket = 0;
3676
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003677 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3678 {
3679 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3680 return( ret );
3681 }
3682
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003683 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3684
3685 return( 0 );
3686}
Paul Bakkera503a632013-08-14 13:48:06 +02003687#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003688
Paul Bakker5121ce52009-01-03 21:22:43 +00003689/*
Paul Bakker1961b702013-01-25 14:49:24 +01003690 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003691 */
Paul Bakker1961b702013-01-25 14:49:24 +01003692int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003693{
3694 int ret = 0;
3695
Paul Bakker1961b702013-01-25 14:49:24 +01003696 if( ssl->state == SSL_HANDSHAKE_OVER )
3697 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003698
Paul Bakker1961b702013-01-25 14:49:24 +01003699 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3700
3701 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3702 return( ret );
3703
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003704#if defined(POLARSSL_SSL_PROTO_DTLS)
3705 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3706 ssl->handshake != NULL &&
3707 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
3708 {
3709 if( ( ret = ssl_resend( ssl ) ) != 0 )
3710 return( ret );
3711 }
3712#endif
3713
Paul Bakker1961b702013-01-25 14:49:24 +01003714 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003715 {
Paul Bakker1961b702013-01-25 14:49:24 +01003716 case SSL_HELLO_REQUEST:
3717 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003718 break;
3719
Paul Bakker1961b702013-01-25 14:49:24 +01003720 /*
3721 * <== ClientHello
3722 */
3723 case SSL_CLIENT_HELLO:
3724 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003725 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003726
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02003727#if defined(POLARSSL_SSL_PROTO_DTLS)
3728 case SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
3729 return( POLARSSL_ERR_SSL_HELLO_VERIFY_REQUIRED );
3730#endif
3731
Paul Bakker1961b702013-01-25 14:49:24 +01003732 /*
3733 * ==> ServerHello
3734 * Certificate
3735 * ( ServerKeyExchange )
3736 * ( CertificateRequest )
3737 * ServerHelloDone
3738 */
3739 case SSL_SERVER_HELLO:
3740 ret = ssl_write_server_hello( ssl );
3741 break;
3742
3743 case SSL_SERVER_CERTIFICATE:
3744 ret = ssl_write_certificate( ssl );
3745 break;
3746
3747 case SSL_SERVER_KEY_EXCHANGE:
3748 ret = ssl_write_server_key_exchange( ssl );
3749 break;
3750
3751 case SSL_CERTIFICATE_REQUEST:
3752 ret = ssl_write_certificate_request( ssl );
3753 break;
3754
3755 case SSL_SERVER_HELLO_DONE:
3756 ret = ssl_write_server_hello_done( ssl );
3757 break;
3758
3759 /*
3760 * <== ( Certificate/Alert )
3761 * ClientKeyExchange
3762 * ( CertificateVerify )
3763 * ChangeCipherSpec
3764 * Finished
3765 */
3766 case SSL_CLIENT_CERTIFICATE:
3767 ret = ssl_parse_certificate( ssl );
3768 break;
3769
3770 case SSL_CLIENT_KEY_EXCHANGE:
3771 ret = ssl_parse_client_key_exchange( ssl );
3772 break;
3773
3774 case SSL_CERTIFICATE_VERIFY:
3775 ret = ssl_parse_certificate_verify( ssl );
3776 break;
3777
3778 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3779 ret = ssl_parse_change_cipher_spec( ssl );
3780 break;
3781
3782 case SSL_CLIENT_FINISHED:
3783 ret = ssl_parse_finished( ssl );
3784 break;
3785
3786 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003787 * ==> ( NewSessionTicket )
3788 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003789 * Finished
3790 */
3791 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003792#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003793 if( ssl->handshake->new_session_ticket != 0 )
3794 ret = ssl_write_new_session_ticket( ssl );
3795 else
Paul Bakkera503a632013-08-14 13:48:06 +02003796#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003797 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003798 break;
3799
3800 case SSL_SERVER_FINISHED:
3801 ret = ssl_write_finished( ssl );
3802 break;
3803
3804 case SSL_FLUSH_BUFFERS:
3805 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3806 ssl->state = SSL_HANDSHAKE_WRAPUP;
3807 break;
3808
3809 case SSL_HANDSHAKE_WRAPUP:
3810 ssl_handshake_wrapup( ssl );
3811 break;
3812
3813 default:
3814 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3815 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003816 }
3817
Paul Bakker5121ce52009-01-03 21:22:43 +00003818 return( ret );
3819}
Paul Bakker9af723c2014-05-01 13:03:14 +02003820#endif /* POLARSSL_SSL_SRV_C */