blob: 8ccd5ae7f13f63927a352d2320541f21d0e338ad [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 server-side functions
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Paul Bakker40e46942009-01-03 21:51:57 +000029#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Paul Bakker40e46942009-01-03 21:51:57 +000031#include "polarssl/debug.h"
32#include "polarssl/ssl.h"
Paul Bakker41c83d32013-03-20 14:39:14 +010033#if defined(POLARSSL_ECP_C)
34#include "polarssl/ecp.h"
35#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker7dc4c442014-02-01 22:50:26 +010037#if defined(POLARSSL_PLATFORM_C)
38#include "polarssl/platform.h"
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020039#else
40#define polarssl_malloc malloc
41#define polarssl_free free
42#endif
43
Paul Bakker5121ce52009-01-03 21:22:43 +000044#include <stdlib.h>
45#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020046
47#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000048#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020049#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000050
Paul Bakkera503a632013-08-14 13:48:06 +020051#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker34617722014-06-13 17:20:13 +020052/* Implementation that should never be optimized out by the compiler */
53static void polarssl_zeroize( void *v, size_t n ) {
54 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
55}
56
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020057/*
58 * Serialize a session in the following format:
59 * 0 . n-1 session structure, n = sizeof(ssl_session)
60 * n . n+2 peer_cert length = m (0 if no certificate)
61 * n+3 . n+2+m peer cert ASN.1
62 *
63 * Assumes ticket is NULL (always true on server side).
64 */
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020065static int ssl_save_session( const ssl_session *session,
66 unsigned char *buf, size_t buf_len,
67 size_t *olen )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020068{
69 unsigned char *p = buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020070 size_t left = buf_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020071#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020072 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020074
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020075 if( left < sizeof( ssl_session ) )
76 return( -1 );
77
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020078 memcpy( p, session, sizeof( ssl_session ) );
79 p += sizeof( ssl_session );
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020080 left -= sizeof( ssl_session );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020081
Paul Bakker7c6b2c32013-09-16 13:49:26 +020082#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020083 if( session->peer_cert == NULL )
84 cert_len = 0;
85 else
86 cert_len = session->peer_cert->raw.len;
87
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020088 if( left < 3 + cert_len )
89 return( -1 );
90
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020091 *p++ = (unsigned char)( cert_len >> 16 & 0xFF );
92 *p++ = (unsigned char)( cert_len >> 8 & 0xFF );
93 *p++ = (unsigned char)( cert_len & 0xFF );
94
95 if( session->peer_cert != NULL )
96 memcpy( p, session->peer_cert->raw.p, cert_len );
97
98 p += cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020099#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200100
101 *olen = p - buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200102
103 return( 0 );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200104}
105
106/*
107 * Unserialise session, see ssl_save_session()
108 */
109static int ssl_load_session( ssl_session *session,
110 const unsigned char *buf, size_t len )
111{
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200112 const unsigned char *p = buf;
113 const unsigned char * const end = buf + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200114#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200115 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200116#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200117
118 if( p + sizeof( ssl_session ) > end )
119 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
120
121 memcpy( session, p, sizeof( ssl_session ) );
122 p += sizeof( ssl_session );
123
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200124#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200125 if( p + 3 > end )
126 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
127
128 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
129 p += 3;
130
131 if( cert_len == 0 )
132 {
133 session->peer_cert = NULL;
134 }
135 else
136 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200137 int ret;
138
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200139 if( p + cert_len > end )
140 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
141
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200142 session->peer_cert = polarssl_malloc( sizeof( x509_crt ) );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200143
144 if( session->peer_cert == NULL )
145 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
146
Paul Bakkerb6b09562013-09-18 14:17:41 +0200147 x509_crt_init( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200148
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200149 if( ( ret = x509_crt_parse_der( session->peer_cert,
150 p, cert_len ) ) != 0 )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200151 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200152 x509_crt_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200153 polarssl_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200154 session->peer_cert = NULL;
155 return( ret );
156 }
157
158 p += cert_len;
159 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200160#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200161
162 if( p != end )
163 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
164
165 return( 0 );
166}
167
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200168/*
169 * Create session ticket, secured as recommended in RFC 5077 section 4:
170 *
171 * struct {
172 * opaque key_name[16];
173 * opaque iv[16];
174 * opaque encrypted_state<0..2^16-1>;
175 * opaque mac[32];
176 * } ticket;
177 *
178 * (the internal state structure differs, however).
179 */
180static int ssl_write_ticket( ssl_context *ssl, size_t *tlen )
181{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200182 int ret;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200183 unsigned char * const start = ssl->out_msg + 10;
184 unsigned char *p = start;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200185 unsigned char *state;
186 unsigned char iv[16];
187 size_t clear_len, enc_len, pad_len, i;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200188
Manuel Pégourié-Gonnard0a201712013-08-23 16:25:16 +0200189 *tlen = 0;
190
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200191 if( ssl->ticket_keys == NULL )
192 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
193
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200194 /* Write key name */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200195 memcpy( p, ssl->ticket_keys->key_name, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200196 p += 16;
197
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200198 /* Generate and write IV (with a copy for aes_crypt) */
199 if( ( ret = ssl->f_rng( ssl->p_rng, p, 16 ) ) != 0 )
200 return( ret );
201 memcpy( iv, p, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200202 p += 16;
203
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200204 /*
205 * Dump session state
206 *
207 * After the session state itself, we still need room for 16 bytes of
208 * padding and 32 bytes of MAC, so there's only so much room left
209 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200210 state = p + 2;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200211 if( ssl_save_session( ssl->session_negotiate, state,
Manuel Pégourié-Gonnard5d53cbe2014-07-14 13:51:41 +0200212 SSL_MAX_CONTENT_LEN - ( state - ssl->out_msg ) - 48,
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200213 &clear_len ) != 0 )
214 {
215 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
216 }
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200217 SSL_DEBUG_BUF( 3, "session ticket cleartext", state, clear_len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200218
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200219 /* Apply PKCS padding */
220 pad_len = 16 - clear_len % 16;
221 enc_len = clear_len + pad_len;
222 for( i = clear_len; i < enc_len; i++ )
223 state[i] = (unsigned char) pad_len;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200224
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200225 /* Encrypt */
226 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->enc, AES_ENCRYPT,
227 enc_len, iv, state, state ) ) != 0 )
228 {
229 return( ret );
230 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200231
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200232 /* Write length */
233 *p++ = (unsigned char)( ( enc_len >> 8 ) & 0xFF );
234 *p++ = (unsigned char)( ( enc_len ) & 0xFF );
235 p = state + enc_len;
236
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200237 /* Compute and write MAC( key_name + iv + enc_state_len + enc_state ) */
238 sha256_hmac( ssl->ticket_keys->mac_key, 16, start, p - start, p, 0 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200239 p += 32;
240
241 *tlen = p - start;
242
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200243 SSL_DEBUG_BUF( 3, "session ticket structure", start, *tlen );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200244
245 return( 0 );
246}
247
248/*
249 * Load session ticket (see ssl_write_ticket for structure)
250 */
251static int ssl_parse_ticket( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200252 unsigned char *buf,
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200253 size_t len )
254{
255 int ret;
256 ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200257 unsigned char *key_name = buf;
258 unsigned char *iv = buf + 16;
259 unsigned char *enc_len_p = iv + 16;
260 unsigned char *ticket = enc_len_p + 2;
261 unsigned char *mac;
Manuel Pégourié-Gonnard34ced2d2013-09-20 11:37:39 +0200262 unsigned char computed_mac[32];
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200263 size_t enc_len, clear_len, i;
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100264 unsigned char pad_len, diff;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200265
266 SSL_DEBUG_BUF( 3, "session ticket structure", buf, len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200267
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200268 if( len < 34 || ssl->ticket_keys == NULL )
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200269 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
270
271 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
272 mac = ticket + enc_len;
273
274 if( len != enc_len + 66 )
275 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
276
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100277 /* Check name, in constant time though it's not a big secret */
278 diff = 0;
279 for( i = 0; i < 16; i++ )
280 diff |= key_name[i] ^ ssl->ticket_keys->key_name[i];
281 /* don't return yet, check the MAC anyway */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200282
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100283 /* Check mac, with constant-time buffer comparison */
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200284 sha256_hmac( ssl->ticket_keys->mac_key, 16, buf, len - 32,
285 computed_mac, 0 );
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100286
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200287 for( i = 0; i < 32; i++ )
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100288 diff |= mac[i] ^ computed_mac[i];
289
290 /* Now return if ticket is not authentic, since we want to avoid
291 * decrypting arbitrary attacker-chosen data */
292 if( diff != 0 )
293 return( POLARSSL_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200294
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200295 /* Decrypt */
296 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->dec, AES_DECRYPT,
297 enc_len, iv, ticket, ticket ) ) != 0 )
298 {
299 return( ret );
300 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200301
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200302 /* Check PKCS padding */
303 pad_len = ticket[enc_len - 1];
304
305 ret = 0;
306 for( i = 2; i < pad_len; i++ )
307 if( ticket[enc_len - i] != pad_len )
308 ret = POLARSSL_ERR_SSL_BAD_INPUT_DATA;
309 if( ret != 0 )
310 return( ret );
311
312 clear_len = enc_len - pad_len;
313
314 SSL_DEBUG_BUF( 3, "session ticket cleartext", ticket, clear_len );
315
316 /* Actually load session */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200317 if( ( ret = ssl_load_session( &session, ticket, clear_len ) ) != 0 )
318 {
319 SSL_DEBUG_MSG( 1, ( "failed to parse ticket content" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100320 ssl_session_free( &session );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200321 return( ret );
322 }
323
Paul Bakker606b4ba2013-08-14 16:52:14 +0200324#if defined(POLARSSL_HAVE_TIME)
325 /* Check if still valid */
326 if( (int) ( time( NULL) - session.start ) > ssl->ticket_lifetime )
327 {
328 SSL_DEBUG_MSG( 1, ( "session ticket expired" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100329 ssl_session_free( &session );
Paul Bakker606b4ba2013-08-14 16:52:14 +0200330 return( POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED );
331 }
332#endif
333
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200334 /*
335 * Keep the session ID sent by the client, since we MUST send it back to
336 * inform him we're accepting the ticket (RFC 5077 section 3.4)
337 */
338 session.length = ssl->session_negotiate->length;
339 memcpy( &session.id, ssl->session_negotiate->id, session.length );
340
341 ssl_session_free( ssl->session_negotiate );
342 memcpy( ssl->session_negotiate, &session, sizeof( ssl_session ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200343
344 /* Zeroize instead of free as we copied the content */
Paul Bakker34617722014-06-13 17:20:13 +0200345 polarssl_zeroize( &session, sizeof( ssl_session ) );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200346
347 return( 0 );
348}
Paul Bakkera503a632013-08-14 13:48:06 +0200349#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200350
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +0200351#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +0200352int ssl_set_client_transport_id( ssl_context *ssl,
353 const unsigned char *info,
354 size_t ilen )
355{
356 if( ssl->endpoint != SSL_IS_SERVER )
357 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
358
359 polarssl_free( ssl->cli_id );
360
361 if( ( ssl->cli_id = polarssl_malloc( ilen ) ) == NULL )
362 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
363
364 memcpy( ssl->cli_id, info, ilen );
365 ssl->cli_id_len = ilen;
366
367 return( 0 );
368}
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +0200369
370void ssl_set_dtls_cookies( ssl_context *ssl,
371 ssl_cookie_write_t *f_cookie_write,
372 ssl_cookie_check_t *f_cookie_check,
373 void *p_cookie )
374{
375 ssl->f_cookie_write = f_cookie_write;
376 ssl->f_cookie_check = f_cookie_check;
377 ssl->p_cookie = p_cookie;
378}
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +0200379#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +0200380
Paul Bakker0be444a2013-08-27 21:55:01 +0200381#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200382/*
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200383 * Wrapper around f_sni, allowing use of ssl_set_own_cert() but
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +0100384 * making it act on ssl->handshake->sni_key_cert instead.
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200385 */
386static int ssl_sni_wrapper( ssl_context *ssl,
387 const unsigned char* name, size_t len )
388{
389 int ret;
390 ssl_key_cert *key_cert_ori = ssl->key_cert;
391
392 ssl->key_cert = NULL;
393 ret = ssl->f_sni( ssl->p_sni, ssl, name, len );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200394 ssl->handshake->sni_key_cert = ssl->key_cert;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200395
396 ssl->key_cert = key_cert_ori;
397
398 return( ret );
399}
400
Paul Bakker5701cdc2012-09-27 21:49:42 +0000401static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000402 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000403 size_t len )
404{
405 int ret;
406 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000407 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000408
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100409 SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
410
Paul Bakker5701cdc2012-09-27 21:49:42 +0000411 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
412 if( servername_list_size + 2 != len )
413 {
414 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
415 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
416 }
417
418 p = buf + 2;
419 while( servername_list_size > 0 )
420 {
421 hostname_len = ( ( p[1] << 8 ) | p[2] );
422 if( hostname_len + 3 > servername_list_size )
423 {
424 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
425 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
426 }
427
428 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
429 {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200430 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000431 if( ret != 0 )
432 {
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100433 SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000434 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
435 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
436 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
437 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000438 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000439 }
440
441 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000442 p += hostname_len + 3;
443 }
444
445 if( servername_list_size != 0 )
446 {
447 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
448 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000449 }
450
451 return( 0 );
452}
Paul Bakker0be444a2013-08-27 21:55:01 +0200453#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000454
Paul Bakker48916f92012-09-16 19:57:18 +0000455static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000456 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000457 size_t len )
458{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000459 int ret;
460
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100461#if defined(POLARSSL_SSL_RENEGOTIATION)
462 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
463 {
464 /* Check verify-data in constant-time. The length OTOH is no secret */
465 if( len != 1 + ssl->verify_data_len ||
466 buf[0] != ssl->verify_data_len ||
467 safer_memcmp( buf + 1, ssl->peer_verify_data,
468 ssl->verify_data_len ) != 0 )
469 {
470 SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
471
472 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
473 return( ret );
474
475 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
476 }
477 }
478 else
479#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +0000480 {
481 if( len != 1 || buf[0] != 0x0 )
482 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100483 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000484
485 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
486 return( ret );
487
Paul Bakker48916f92012-09-16 19:57:18 +0000488 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
489 }
490
491 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
492 }
Paul Bakker48916f92012-09-16 19:57:18 +0000493
494 return( 0 );
495}
496
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100497#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
498 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
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}
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100542#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
543 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker23f36802012-09-28 14:15:14 +0000544
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200545#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200546static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
547 const unsigned char *buf,
548 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100549{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200550 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100551 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200552 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100553
554 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
555 if( list_size + 2 != len ||
556 list_size % 2 != 0 )
557 {
558 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
559 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
560 }
561
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200562 /* Should never happen unless client duplicates the extension */
563 if( ssl->handshake->curves != NULL )
564 {
565 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
566 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
567 }
568
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +0100569 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200570 * and leave room for a final 0 */
571 our_size = list_size / 2 + 1;
572 if( our_size > POLARSSL_ECP_DP_MAX )
573 our_size = POLARSSL_ECP_DP_MAX;
574
575 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
576 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
577
Paul Bakker9af723c2014-05-01 13:03:14 +0200578 /* explicit void pointer cast for buggy MS compiler */
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200579 memset( (void *) curves, 0, our_size * sizeof( *curves ) );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200580 ssl->handshake->curves = curves;
581
Paul Bakker41c83d32013-03-20 14:39:14 +0100582 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200583 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100584 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200585 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200586
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200587 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100588 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200589 *curves++ = curve_info;
590 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100591 }
592
593 list_size -= 2;
594 p += 2;
595 }
596
597 return( 0 );
598}
599
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200600static int ssl_parse_supported_point_formats( ssl_context *ssl,
601 const unsigned char *buf,
602 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100603{
604 size_t list_size;
605 const unsigned char *p;
606
607 list_size = buf[0];
608 if( list_size + 1 != len )
609 {
610 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
611 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
612 }
613
614 p = buf + 2;
615 while( list_size > 0 )
616 {
617 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
618 p[0] == POLARSSL_ECP_PF_COMPRESSED )
619 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200620 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200621 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100622 return( 0 );
623 }
624
625 list_size--;
626 p++;
627 }
628
629 return( 0 );
630}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200631#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100632
Paul Bakker05decb22013-08-15 13:33:48 +0200633#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200634static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
635 const unsigned char *buf,
636 size_t len )
637{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200638 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200639 {
640 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
641 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
642 }
643
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200644 ssl->session_negotiate->mfl_code = buf[0];
645
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200646 return( 0 );
647}
Paul Bakker05decb22013-08-15 13:33:48 +0200648#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200649
Paul Bakker1f2bc622013-08-15 13:45:55 +0200650#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200651static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
652 const unsigned char *buf,
653 size_t len )
654{
655 if( len != 0 )
656 {
657 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
658 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
659 }
660
661 ((void) buf);
662
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100663 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
664 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200665
666 return( 0 );
667}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200668#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200669
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100670#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
671static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
672 const unsigned char *buf,
673 size_t len )
674{
675 if( len != 0 )
676 {
677 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
678 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
679 }
680
681 ((void) buf);
682
683 if( ssl->encrypt_then_mac == SSL_ETM_ENABLED &&
684 ssl->minor_ver != SSL_MINOR_VERSION_0 )
685 {
686 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
687 }
688
689 return( 0 );
690}
691#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
692
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200693#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
694static int ssl_parse_extended_ms_ext( ssl_context *ssl,
695 const unsigned char *buf,
696 size_t len )
697{
698 if( len != 0 )
699 {
700 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
701 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
702 }
703
704 ((void) buf);
705
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200706 if( ssl->extended_ms == SSL_EXTENDED_MS_ENABLED &&
707 ssl->minor_ver != SSL_MINOR_VERSION_0 )
708 {
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200709 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200710 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200711
712 return( 0 );
713}
714#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
715
Paul Bakkera503a632013-08-14 13:48:06 +0200716#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200717static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200718 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200719 size_t len )
720{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200721 int ret;
722
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200723 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
724 return( 0 );
725
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200726 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200727 ssl->handshake->new_session_ticket = 1;
728
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200729 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
730
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200731 if( len == 0 )
732 return( 0 );
733
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100734#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200735 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
736 {
737 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
738 return( 0 );
739 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100740#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200741
742 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200743 * Failures are ok: just ignore the ticket and proceed.
744 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200745 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
746 {
747 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200748 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200749 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200750
751 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
752
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200753 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200754
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200755 /* Don't send a new ticket after all, this one is OK */
756 ssl->handshake->new_session_ticket = 0;
757
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200758 return( 0 );
759}
Paul Bakkera503a632013-08-14 13:48:06 +0200760#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200761
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200762#if defined(POLARSSL_SSL_ALPN)
763static int ssl_parse_alpn_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200764 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200765{
Paul Bakker14b16c62014-05-28 11:33:54 +0200766 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200767 const unsigned char *theirs, *start, *end;
768 const char **ours;
769
770 /* If ALPN not configured, just ignore the extension */
771 if( ssl->alpn_list == NULL )
772 return( 0 );
773
774 /*
775 * opaque ProtocolName<1..2^8-1>;
776 *
777 * struct {
778 * ProtocolName protocol_name_list<2..2^16-1>
779 * } ProtocolNameList;
780 */
781
782 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
783 if( len < 4 )
784 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
785
786 list_len = ( buf[0] << 8 ) | buf[1];
787 if( list_len != len - 2 )
788 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
789
790 /*
791 * Use our order of preference
792 */
793 start = buf + 2;
794 end = buf + len;
795 for( ours = ssl->alpn_list; *ours != NULL; ours++ )
796 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200797 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200798 for( theirs = start; theirs != end; theirs += cur_len )
799 {
800 /* If the list is well formed, we should get equality first */
801 if( theirs > end )
802 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
803
804 cur_len = *theirs++;
805
806 /* Empty strings MUST NOT be included */
807 if( cur_len == 0 )
808 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
809
Paul Bakker14b16c62014-05-28 11:33:54 +0200810 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200811 memcmp( theirs, *ours, cur_len ) == 0 )
812 {
813 ssl->alpn_chosen = *ours;
814 return( 0 );
815 }
816 }
817 }
818
819 /* If we get there, no match was found */
820 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
821 SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
822 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
823}
824#endif /* POLARSSL_SSL_ALPN */
825
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100826/*
827 * Auxiliary functions for ServerHello parsing and related actions
828 */
829
830#if defined(POLARSSL_X509_CRT_PARSE_C)
831/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100832 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100833 */
834#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100835static int ssl_check_key_curve( pk_context *pk,
836 const ecp_curve_info **curves )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100837{
838 const ecp_curve_info **crv = curves;
839 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
840
841 while( *crv != NULL )
842 {
843 if( (*crv)->grp_id == grp_id )
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100844 return( 0 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100845 crv++;
846 }
847
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100848 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100849}
850#endif /* POLARSSL_ECDSA_C */
851
852/*
853 * Try picking a certificate for this ciphersuite,
854 * return 0 on success and -1 on failure.
855 */
856static int ssl_pick_cert( ssl_context *ssl,
857 const ssl_ciphersuite_t * ciphersuite_info )
858{
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100859 ssl_key_cert *cur, *list, *fallback = NULL;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100860 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
861
862#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
863 if( ssl->handshake->sni_key_cert != NULL )
864 list = ssl->handshake->sni_key_cert;
865 else
866#endif
867 list = ssl->handshake->key_cert;
868
869 if( pk_alg == POLARSSL_PK_NONE )
870 return( 0 );
871
872 for( cur = list; cur != NULL; cur = cur->next )
873 {
874 if( ! pk_can_do( cur->key, pk_alg ) )
875 continue;
876
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200877 /*
878 * This avoids sending the client a cert it'll reject based on
879 * keyUsage or other extensions.
880 *
881 * It also allows the user to provision different certificates for
882 * different uses based on keyUsage, eg if they want to avoid signing
883 * and decrypting with the same RSA key.
884 */
885 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
886 SSL_IS_SERVER ) != 0 )
887 {
888 continue;
889 }
890
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100891#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100892 if( pk_alg == POLARSSL_PK_ECDSA &&
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100893 ssl_check_key_curve( cur->key, ssl->handshake->curves ) != 0 )
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100894 continue;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100895#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100896
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100897 /*
898 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
899 * present them a SHA-higher cert rather than failing if it's the only
900 * one we got that satisfies the other conditions.
901 */
902 if( ssl->minor_ver < SSL_MINOR_VERSION_3 &&
903 cur->cert->sig_md != POLARSSL_MD_SHA1 )
904 {
905 if( fallback == NULL )
906 fallback = cur;
907 continue;
908 }
909
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100910 /* If we get there, we got a winner */
911 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100912 }
913
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100914 if( cur != NULL )
915 {
916 ssl->handshake->key_cert = cur;
917 return( 0 );
918 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100919
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100920 if( fallback != NULL )
921 {
922 ssl->handshake->key_cert = fallback;
923 return( 0 );
924 }
925
926 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100927}
928#endif /* POLARSSL_X509_CRT_PARSE_C */
929
930/*
931 * Check if a given ciphersuite is suitable for use with our config/keys/etc
932 * Sets ciphersuite_info only if the suite matches.
933 */
934static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
935 const ssl_ciphersuite_t **ciphersuite_info )
936{
937 const ssl_ciphersuite_t *suite_info;
938
939 suite_info = ssl_ciphersuite_from_id( suite_id );
940 if( suite_info == NULL )
941 {
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100942 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
943 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100944 }
945
946 if( suite_info->min_minor_ver > ssl->minor_ver ||
947 suite_info->max_minor_ver < ssl->minor_ver )
948 return( 0 );
949
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +0100950#if defined(POLARSSL_SSL_PROTO_DTLS)
951 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
952 ( suite_info->flags & POLARSSL_CIPHERSUITE_NODTLS ) )
953 return( 0 );
954#endif
955
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100956 if( ssl->arc4_disabled == SSL_ARC4_DISABLED &&
957 suite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
958 return( 0 );
959
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100960#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
961 if( ssl_ciphersuite_uses_ec( suite_info ) &&
962 ( ssl->handshake->curves == NULL ||
963 ssl->handshake->curves[0] == NULL ) )
964 return( 0 );
965#endif
966
967#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
968 /* If the ciphersuite requires a pre-shared key and we don't
969 * have one, skip it now rather than failing later */
970 if( ssl_ciphersuite_uses_psk( suite_info ) &&
971 ssl->f_psk == NULL &&
972 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
973 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
974 return( 0 );
975#endif
976
977#if defined(POLARSSL_X509_CRT_PARSE_C)
978 /*
979 * Final check: if ciphersuite requires us to have a
980 * certificate/key of a particular type:
981 * - select the appropriate certificate if we have one, or
982 * - try the next ciphersuite if we don't
983 * This must be done last since we modify the key_cert list.
984 */
985 if( ssl_pick_cert( ssl, suite_info ) != 0 )
986 return( 0 );
987#endif
988
989 *ciphersuite_info = suite_info;
990 return( 0 );
991}
992
Paul Bakker78a8c712013-03-06 17:01:52 +0100993#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
994static int ssl_parse_client_hello_v2( ssl_context *ssl )
995{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +0100996 int ret, got_common_suite;
Paul Bakker78a8c712013-03-06 17:01:52 +0100997 unsigned int i, j;
998 size_t n;
999 unsigned int ciph_len, sess_len, chal_len;
1000 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001001 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +02001002 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001003
1004 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
1005
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001006#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker78a8c712013-03-06 17:01:52 +01001007 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
1008 {
1009 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
1010
1011 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1012 return( ret );
1013
1014 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1015 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001016#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001017
1018 buf = ssl->in_hdr;
1019
1020 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1021
1022 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
1023 buf[2] ) );
1024 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
1025 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
1026 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
1027 buf[3], buf[4] ) );
1028
1029 /*
1030 * SSLv2 Client Hello
1031 *
1032 * Record layer:
1033 * 0 . 1 message length
1034 *
1035 * SSL layer:
1036 * 2 . 2 message type
1037 * 3 . 4 protocol version
1038 */
1039 if( buf[2] != SSL_HS_CLIENT_HELLO ||
1040 buf[3] != SSL_MAJOR_VERSION_3 )
1041 {
1042 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1043 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1044 }
1045
1046 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
1047
1048 if( n < 17 || n > 512 )
1049 {
1050 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1051 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1052 }
1053
1054 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +02001055 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
1056 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +01001057
1058 if( ssl->minor_ver < ssl->min_minor_ver )
1059 {
1060 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001061 " [%d:%d] < [%d:%d]",
1062 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +01001063 ssl->min_major_ver, ssl->min_minor_ver ) );
1064
1065 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1066 SSL_ALERT_MSG_PROTOCOL_VERSION );
1067 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1068 }
1069
Paul Bakker2fbefde2013-06-29 16:01:15 +02001070 ssl->handshake->max_major_ver = buf[3];
1071 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +01001072
1073 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
1074 {
1075 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1076 return( ret );
1077 }
1078
1079 ssl->handshake->update_checksum( ssl, buf + 2, n );
1080
1081 buf = ssl->in_msg;
1082 n = ssl->in_left - 5;
1083
1084 /*
1085 * 0 . 1 ciphersuitelist length
1086 * 2 . 3 session id length
1087 * 4 . 5 challenge length
1088 * 6 . .. ciphersuitelist
1089 * .. . .. session id
1090 * .. . .. challenge
1091 */
1092 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1093
1094 ciph_len = ( buf[0] << 8 ) | buf[1];
1095 sess_len = ( buf[2] << 8 ) | buf[3];
1096 chal_len = ( buf[4] << 8 ) | buf[5];
1097
1098 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
1099 ciph_len, sess_len, chal_len ) );
1100
1101 /*
1102 * Make sure each parameter length is valid
1103 */
1104 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1105 {
1106 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1107 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1108 }
1109
1110 if( sess_len > 32 )
1111 {
1112 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1113 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1114 }
1115
1116 if( chal_len < 8 || chal_len > 32 )
1117 {
1118 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1119 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1120 }
1121
1122 if( n != 6 + ciph_len + sess_len + chal_len )
1123 {
1124 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1125 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1126 }
1127
1128 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1129 buf + 6, ciph_len );
1130 SSL_DEBUG_BUF( 3, "client hello, session id",
1131 buf + 6 + ciph_len, sess_len );
1132 SSL_DEBUG_BUF( 3, "client hello, challenge",
1133 buf + 6 + ciph_len + sess_len, chal_len );
1134
1135 p = buf + 6 + ciph_len;
1136 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001137 memset( ssl->session_negotiate->id, 0,
1138 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001139 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1140
1141 p += sess_len;
1142 memset( ssl->handshake->randbytes, 0, 64 );
1143 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1144
1145 /*
1146 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1147 */
1148 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1149 {
1150 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1151 {
1152 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001153#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker78a8c712013-03-06 17:01:52 +01001154 if( ssl->renegotiation == SSL_RENEGOTIATION )
1155 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001156 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
1157 "during renegotiation" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001158
1159 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1160 return( ret );
1161
1162 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1163 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001164#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001165 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1166 break;
1167 }
1168 }
1169
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001170#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1171 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1172 {
1173 if( p[0] == 0 &&
1174 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1175 p[2] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1176 {
1177 SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
1178
1179 if( ssl->minor_ver < ssl->max_minor_ver )
1180 {
1181 SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
1182
1183 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1184 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1185
1186 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1187 }
1188
1189 break;
1190 }
1191 }
1192#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1193
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001194 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001195 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001196 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001197#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1198 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1199 {
1200 for( i = 0; ciphersuites[i] != 0; i++ )
1201#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001202 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001203 {
1204 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001205#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001206 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001207 if( p[0] != 0 ||
1208 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1209 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1210 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001211
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001212 got_common_suite = 1;
1213
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001214 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1215 &ciphersuite_info ) ) != 0 )
1216 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001217
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001218 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001219 goto have_ciphersuite_v2;
1220 }
1221 }
1222
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001223 if( got_common_suite )
1224 {
1225 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1226 "but none of them usable" ) );
1227 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
1228 }
1229 else
1230 {
1231 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1232 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1233 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001234
1235have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001236 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001237 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001238 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001239
1240 /*
1241 * SSLv2 Client Hello relevant renegotiation security checks
1242 */
1243 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1244 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1245 {
1246 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1247
1248 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1249 return( ret );
1250
1251 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1252 }
1253
1254 ssl->in_left = 0;
1255 ssl->state++;
1256
1257 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1258
1259 return( 0 );
1260}
1261#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1262
Paul Bakker5121ce52009-01-03 21:22:43 +00001263static int ssl_parse_client_hello( ssl_context *ssl )
1264{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001265 int ret, got_common_suite;
Paul Bakker23986e52011-04-24 08:57:21 +00001266 unsigned int i, j;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001267 unsigned int ciph_offset, comp_offset, ext_offset;
1268 unsigned int msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001269#if defined(POLARSSL_SSL_PROTO_DTLS)
1270 unsigned int cookie_offset, cookie_len;
1271#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001272 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001273#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001274 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001275#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001276 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001277 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001278 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001279 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001280
1281 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1282
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001283#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1284read_record_header:
1285#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001286 /*
1287 * If renegotiating, then the input was read with ssl_read_record(),
1288 * otherwise read it ourselves manually in order to support SSLv2
1289 * ClientHello, which doesn't use the same record layer format.
1290 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001291#if defined(POLARSSL_SSL_RENEGOTIATION)
1292 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1293#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001294 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001295 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
1296 {
1297 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1298 return( ret );
1299 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001300 }
1301
1302 buf = ssl->in_hdr;
1303
Paul Bakker78a8c712013-03-06 17:01:52 +01001304#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02001305#if defined(POLARSSL_SSL_PROTO_DTLS)
1306 if( ssl->transport == SSL_TRANSPORT_STREAM )
1307#endif
1308 if( ( buf[0] & 0x80 ) != 0 )
1309 return ssl_parse_client_hello_v2( ssl );
Paul Bakker78a8c712013-03-06 17:01:52 +01001310#endif
1311
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001312 SSL_DEBUG_BUF( 4, "record header", buf, ssl_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001313
Paul Bakkerec636f32012-09-09 19:17:02 +00001314 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001315 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001316 *
1317 * Record layer:
1318 * 0 . 0 message type
1319 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001320 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001321 * 3 . 4 message length
1322 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001323 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1324 buf[0] ) );
1325
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001326 if( buf[0] != SSL_MSG_HANDSHAKE )
1327 {
1328 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1329 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1330 }
1331
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001332 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1333 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1334
1335 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol version: [%d:%d]",
1336 buf[1], buf[2] ) );
1337
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001338 ssl_read_version( &major, &minor, ssl->transport, buf + 1 );
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001339
1340 /* According to RFC 5246 Appendix E.1, the version here is typically
1341 * "{03,00}, the lowest version number supported by the client, [or] the
1342 * value of ClientHello.client_version", so the only meaningful check here
1343 * is the major version shouldn't be less than 3 */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001344 if( major < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001345 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001346 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1347 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1348 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001349
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001350 /* For DTLS if this is the initial handshake, remember the client sequence
1351 * number to use it in our next message (RFC 6347 4.2.1) */
1352#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00001353 if( ssl->transport == SSL_TRANSPORT_DATAGRAM
1354#if defined(POLARSSL_SSL_RENEGOTIATION)
1355 && ssl->renegotiation == SSL_INITIAL_HANDSHAKE
1356#endif
1357 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001358 {
1359 /* Epoch should be 0 for initial handshakes */
1360 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1361 {
1362 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1363 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1364 }
1365
1366 memcpy( ssl->out_ctr + 2, ssl->in_ctr + 2, 6 );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001367
1368#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1369 if( ssl_dtls_replay_check( ssl ) != 0 )
1370 {
1371 SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
1372 ssl->next_record_offset = 0;
1373 ssl->in_left = 0;
1374 goto read_record_header;
1375 }
1376
1377 /* No MAC to check yet, so we can update right now */
1378 ssl_dtls_replay_update( ssl );
1379#endif
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001380 }
1381#endif /* POLARSSL_SSL_PROTO_DTLS */
1382
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001383 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001384
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001385#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001386 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
1387 {
1388 /* Set by ssl_read_record() */
1389 msg_len = ssl->in_hslen;
1390 }
1391 else
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001392#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001393 {
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001394 if( msg_len > SSL_MAX_CONTENT_LEN )
1395 {
1396 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1397 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1398 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001399
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001400 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) + msg_len ) ) != 0 )
1401 {
1402 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1403 return( ret );
1404 }
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001405
1406 /* Done reading this record, get ready for the next one */
1407#if defined(POLARSSL_SSL_PROTO_DTLS)
1408 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1409 ssl->next_record_offset = msg_len + ssl_hdr_len( ssl );
1410 else
1411#endif
1412 ssl->in_left = 0;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001413 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001414
1415 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001416
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001417 SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001418
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001419 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001420
1421 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001422 * Handshake layer:
1423 * 0 . 0 handshake type
1424 * 1 . 3 handshake length
1425 * 4 . 5 DTLS only: message seqence number
1426 * 6 . 8 DTLS only: fragment offset
1427 * 9 . 11 DTLS only: fragment length
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001428 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001429 if( msg_len < ssl_hs_hdr_len( ssl ) )
1430 {
1431 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1432 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1433 }
1434
1435 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
1436
1437 if( buf[0] != SSL_HS_CLIENT_HELLO )
1438 {
1439 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1440 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1441 }
1442
1443 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1444 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1445
1446 /* We don't support fragmentation of ClientHello (yet?) */
1447 if( buf[1] != 0 ||
1448 msg_len != ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) )
1449 {
1450 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1451 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1452 }
1453
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001454#if defined(POLARSSL_SSL_PROTO_DTLS)
1455 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1456 {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001457 /*
1458 * Copy the client's handshake message_seq on initial handshakes
1459 */
1460 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02001461 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001462 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1463 ssl->in_msg[5];
1464 ssl->handshake->out_msg_seq = cli_msg_seq;
1465 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02001466 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001467 else
1468 {
1469 /* This couldn't be done in ssl_prepare_handshake_record() */
1470 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1471 ssl->in_msg[5];
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001472
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001473 if( cli_msg_seq != ssl->handshake->in_msg_seq )
1474 {
1475 SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
1476 "%d (expected %d)", cli_msg_seq,
1477 ssl->handshake->in_msg_seq ) );
1478 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1479 }
1480
1481 ssl->handshake->in_msg_seq++;
1482 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001483
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001484 /*
1485 * For now we don't support fragmentation, so make sure
1486 * fragment_offset == 0 and fragment_length == length
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001487 */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001488 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1489 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1490 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001491 SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001492 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1493 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001494 }
1495#endif /* POLARSSL_SSL_PROTO_DTLS */
1496
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001497 buf += ssl_hs_hdr_len( ssl );
1498 msg_len -= ssl_hs_hdr_len( ssl );
1499
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001500 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001501 * ClientHello layer:
1502 * 0 . 1 protocol version
1503 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1504 * 34 . 35 session id length (1 byte)
1505 * 35 . 34+x session id
1506 * 35+x . 35+x DTLS only: cookie length (1 byte)
1507 * 36+x . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001508 * .. . .. ciphersuite list length (2 bytes)
1509 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001510 * .. . .. compression alg. list length (1 byte)
1511 * .. . .. compression alg. list
1512 * .. . .. extensions length (2 bytes, optional)
1513 * .. . .. extensions (optional)
Paul Bakkerec636f32012-09-09 19:17:02 +00001514 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001515
1516 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001517 * Minimal length (with everything empty and extensions ommitted) is
1518 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1519 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001520 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001521 if( msg_len < 38 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001522 {
1523 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1524 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1525 }
1526
1527 /*
1528 * Check and save the protocol version
1529 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001530 SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001531
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001532 ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001533 ssl->transport, buf );
Paul Bakkerec636f32012-09-09 19:17:02 +00001534
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001535 ssl->handshake->max_major_ver = ssl->major_ver;
1536 ssl->handshake->max_minor_ver = ssl->minor_ver;
1537
1538 if( ssl->major_ver < ssl->min_major_ver ||
1539 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001540 {
1541 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001542 " [%d:%d] < [%d:%d]",
1543 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001544 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001545
1546 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1547 SSL_ALERT_MSG_PROTOCOL_VERSION );
1548
1549 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1550 }
1551
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001552 if( ssl->major_ver > ssl->max_major_ver )
1553 {
1554 ssl->major_ver = ssl->max_major_ver;
1555 ssl->minor_ver = ssl->max_minor_ver;
1556 }
1557 else if( ssl->minor_ver > ssl->max_minor_ver )
1558 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001559
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001560 /*
1561 * Save client random (inc. Unix time)
1562 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001563 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001564
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001565 memcpy( ssl->handshake->randbytes, buf + 2, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001566
1567 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001568 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001569 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001570 sess_len = buf[34];
Paul Bakkerec636f32012-09-09 19:17:02 +00001571
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001572 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001573 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001574 {
1575 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1576 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1577 }
1578
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001579 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001580
Paul Bakker48916f92012-09-16 19:57:18 +00001581 ssl->session_negotiate->length = sess_len;
1582 memset( ssl->session_negotiate->id, 0,
1583 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001584 memcpy( ssl->session_negotiate->id, buf + 35,
Paul Bakker48916f92012-09-16 19:57:18 +00001585 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001586
1587 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001588 * Check the cookie length and content
1589 */
1590#if defined(POLARSSL_SSL_PROTO_DTLS)
1591 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1592 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001593 cookie_offset = 35 + sess_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001594 cookie_len = buf[cookie_offset];
1595
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001596 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001597 {
1598 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1599 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1600 }
1601
1602 SSL_DEBUG_BUF( 3, "client hello, cookie",
1603 buf + cookie_offset + 1, cookie_len );
1604
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001605#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001606 if( ssl->f_cookie_check != NULL &&
1607 ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001608 {
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001609 if( ssl->f_cookie_check( ssl->p_cookie,
1610 buf + cookie_offset + 1, cookie_len,
1611 ssl->cli_id, ssl->cli_id_len ) != 0 )
1612 {
1613 SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
1614 ssl->handshake->verify_cookie_len = 1;
1615 }
1616 else
1617 {
1618 SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
1619 ssl->handshake->verify_cookie_len = 0;
1620 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001621 }
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001622 else
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +02001623#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001624 {
1625 /* We know we didn't send a cookie, so it should be empty */
1626 if( cookie_len != 0 )
1627 {
1628 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1629 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1630 }
1631
1632 SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
1633 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001634 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001635#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001636
1637 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001638 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001639 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001640#if defined(POLARSSL_SSL_PROTO_DTLS)
1641 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1642 ciph_offset = cookie_offset + 1 + cookie_len;
1643 else
1644#endif
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001645 ciph_offset = 35 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001646
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001647 ciph_len = ( buf[ciph_offset + 0] << 8 )
1648 | ( buf[ciph_offset + 1] );
1649
1650 if( ciph_len < 2 ||
1651 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1652 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001653 {
1654 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1655 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1656 }
1657
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001658 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1659 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001660
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001661 /*
1662 * Check the compression algorithms length and pick one
1663 */
1664 comp_offset = ciph_offset + 2 + ciph_len;
1665
1666 comp_len = buf[comp_offset];
1667
1668 if( comp_len < 1 ||
1669 comp_len > 16 ||
1670 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001671 {
1672 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1673 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1674 }
1675
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001676 SSL_DEBUG_BUF( 3, "client hello, compression",
1677 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001678
1679 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001680#if defined(POLARSSL_ZLIB_SUPPORT)
1681 for( i = 0; i < comp_len; ++i )
1682 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001683 if( buf[comp_offset + 1 + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001684 {
Paul Bakker48916f92012-09-16 19:57:18 +00001685 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001686 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001687 }
1688 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001689#endif
1690
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001691 /* See comments in ssl_write_client_hello() */
1692#if defined(POLARSSL_SSL_PROTO_DTLS)
1693 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1694 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
1695#endif
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001696
Paul Bakkerec636f32012-09-09 19:17:02 +00001697 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001698 * Check the extension length
Paul Bakker48916f92012-09-16 19:57:18 +00001699 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001700 ext_offset = comp_offset + 1 + comp_len;
1701 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001702 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001703 if( msg_len < ext_offset + 2 )
Paul Bakker48916f92012-09-16 19:57:18 +00001704 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001705 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1706 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1707 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001708
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001709 ext_len = ( buf[ext_offset + 0] << 8 )
1710 | ( buf[ext_offset + 1] );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001711
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001712 if( ( ext_len > 0 && ext_len < 4 ) ||
1713 msg_len != ext_offset + 2 + ext_len )
1714 {
1715 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1716 SSL_DEBUG_BUF( 3, "client hello extensions",
1717 buf + ext_offset + 2, ext_len );
1718 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +00001719 }
1720 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001721 else
1722 ext_len = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001723
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001724 ext = buf + ext_offset + 2;
Paul Bakker48916f92012-09-16 19:57:18 +00001725
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001726 while( ext_len != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001727 {
1728 unsigned int ext_id = ( ( ext[0] << 8 )
1729 | ( ext[1] ) );
1730 unsigned int ext_size = ( ( ext[2] << 8 )
1731 | ( ext[3] ) );
1732
1733 if( ext_size + 4 > ext_len )
1734 {
1735 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1736 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1737 }
1738 switch( ext_id )
1739 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001740#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001741 case TLS_EXT_SERVERNAME:
1742 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1743 if( ssl->f_sni == NULL )
1744 break;
1745
1746 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1747 if( ret != 0 )
1748 return( ret );
1749 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001750#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001751
Paul Bakker48916f92012-09-16 19:57:18 +00001752 case TLS_EXT_RENEGOTIATION_INFO:
1753 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001754#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001755 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001756#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001757
Paul Bakker23f36802012-09-28 14:15:14 +00001758 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1759 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001760 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001761 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001762
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001763#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1764 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakker23f36802012-09-28 14:15:14 +00001765 case TLS_EXT_SIG_ALG:
1766 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001767#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker23f36802012-09-28 14:15:14 +00001768 if( ssl->renegotiation == SSL_RENEGOTIATION )
1769 break;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001770#endif
Paul Bakker23f36802012-09-28 14:15:14 +00001771
1772 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1773 if( ret != 0 )
1774 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001775 break;
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001776#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
1777 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001778
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001779#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001780 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1781 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1782
1783 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1784 if( ret != 0 )
1785 return( ret );
1786 break;
1787
1788 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1789 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001790 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001791
1792 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1793 if( ret != 0 )
1794 return( ret );
1795 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001796#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001797
Paul Bakker05decb22013-08-15 13:33:48 +02001798#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001799 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1800 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1801
1802 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1803 if( ret != 0 )
1804 return( ret );
1805 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001806#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001807
Paul Bakker1f2bc622013-08-15 13:45:55 +02001808#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001809 case TLS_EXT_TRUNCATED_HMAC:
1810 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1811
1812 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1813 if( ret != 0 )
1814 return( ret );
1815 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001816#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001817
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001818#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1819 case TLS_EXT_ENCRYPT_THEN_MAC:
1820 SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
1821
1822 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1823 if( ret != 0 )
1824 return( ret );
1825 break;
1826#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1827
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001828#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1829 case TLS_EXT_EXTENDED_MASTER_SECRET:
1830 SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
1831
1832 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1833 if( ret != 0 )
1834 return( ret );
1835 break;
1836#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1837
Paul Bakkera503a632013-08-14 13:48:06 +02001838#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001839 case TLS_EXT_SESSION_TICKET:
1840 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1841
1842 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1843 if( ret != 0 )
1844 return( ret );
1845 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001846#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001847
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001848#if defined(POLARSSL_SSL_ALPN)
1849 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001850 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001851
1852 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1853 if( ret != 0 )
1854 return( ret );
1855 break;
1856#endif /* POLARSSL_SSL_SESSION_TICKETS */
1857
Paul Bakker48916f92012-09-16 19:57:18 +00001858 default:
1859 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1860 ext_id ) );
1861 }
1862
1863 ext_len -= 4 + ext_size;
1864 ext += 4 + ext_size;
1865
1866 if( ext_len > 0 && ext_len < 4 )
1867 {
1868 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1869 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1870 }
1871 }
1872
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01001873#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1874 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1875 {
1876 if( p[0] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1877 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1878 {
1879 SSL_DEBUG_MSG( 0, ( "received FALLBACK_SCSV" ) );
1880
1881 if( ssl->minor_ver < ssl->max_minor_ver )
1882 {
1883 SSL_DEBUG_MSG( 0, ( "inapropriate fallback" ) );
1884
1885 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1886 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1887
1888 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1889 }
1890
1891 break;
1892 }
1893 }
1894#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1895
Paul Bakker48916f92012-09-16 19:57:18 +00001896 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001897 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1898 */
1899 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
1900 {
1901 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1902 {
1903 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1904 if( ssl->renegotiation == SSL_RENEGOTIATION )
1905 {
1906 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1907
1908 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1909 return( ret );
1910
1911 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1912 }
1913 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1914 break;
1915 }
1916 }
1917
1918 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001919 * Renegotiation security checks
1920 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001921 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001922 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1923 {
1924 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1925 handshake_failure = 1;
1926 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001927#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001928 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1929 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1930 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001931 {
1932 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001933 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001934 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001935 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1936 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1937 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001938 {
1939 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001940 handshake_failure = 1;
1941 }
1942 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1943 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1944 renegotiation_info_seen == 1 )
1945 {
1946 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1947 handshake_failure = 1;
1948 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001949#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001950
1951 if( handshake_failure == 1 )
1952 {
1953 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1954 return( ret );
1955
Paul Bakker48916f92012-09-16 19:57:18 +00001956 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1957 }
Paul Bakker380da532012-04-18 16:10:25 +00001958
Paul Bakker41c83d32013-03-20 14:39:14 +01001959 /*
1960 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001961 * (At the end because we need information from the EC-based extensions
1962 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001963 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001964 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001965 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001966 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001967#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001968 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001969 {
1970 for( i = 0; ciphersuites[i] != 0; i++ )
1971#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001972 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001973 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001974 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001975#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001976 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001977 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1978 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1979 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001980
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001981 got_common_suite = 1;
1982
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001983 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1984 &ciphersuite_info ) ) != 0 )
1985 return( ret );
1986
1987 if( ciphersuite_info != NULL )
1988 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001989 }
1990 }
1991
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001992 if( got_common_suite )
1993 {
1994 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1995 "but none of them usable" ) );
1996 ssl_send_fatal_handshake_failure( ssl );
1997 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
1998 }
1999 else
2000 {
2001 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
2002 ssl_send_fatal_handshake_failure( ssl );
2003 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
2004 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002005
2006have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002007 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01002008 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
2009 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
2010
Paul Bakker5121ce52009-01-03 21:22:43 +00002011 ssl->state++;
2012
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002013#if defined(POLARSSL_SSL_PROTO_DTLS)
2014 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2015 ssl_recv_flight_completed( ssl );
2016#endif
2017
Paul Bakker5121ce52009-01-03 21:22:43 +00002018 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
2019
2020 return( 0 );
2021}
2022
Paul Bakker1f2bc622013-08-15 13:45:55 +02002023#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002024static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
2025 unsigned char *buf,
2026 size_t *olen )
2027{
2028 unsigned char *p = buf;
2029
2030 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
2031 {
2032 *olen = 0;
2033 return;
2034 }
2035
2036 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
2037
2038 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
2039 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
2040
2041 *p++ = 0x00;
2042 *p++ = 0x00;
2043
2044 *olen = 4;
2045}
Paul Bakker1f2bc622013-08-15 13:45:55 +02002046#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002047
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002048#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2049static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
2050 unsigned char *buf,
2051 size_t *olen )
2052{
2053 unsigned char *p = buf;
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002054 const ssl_ciphersuite_t *suite = NULL;
2055 const cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002056
2057 if( ssl->session_negotiate->encrypt_then_mac == SSL_EXTENDED_MS_DISABLED ||
2058 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2059 {
2060 *olen = 0;
2061 return;
2062 }
2063
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002064 /*
2065 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
2066 * from a client and then selects a stream or Authenticated Encryption
2067 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
2068 * encrypt-then-MAC response extension back to the client."
2069 */
2070 if( ( suite = ssl_ciphersuite_from_id(
2071 ssl->session_negotiate->ciphersuite ) ) == NULL ||
2072 ( cipher = cipher_info_from_type( suite->cipher ) ) == NULL ||
2073 cipher->mode != POLARSSL_MODE_CBC )
2074 {
2075 *olen = 0;
2076 return;
2077 }
2078
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002079 SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
2080
2081 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
2082 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
2083
2084 *p++ = 0x00;
2085 *p++ = 0x00;
2086
2087 *olen = 4;
2088}
2089#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
2090
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002091#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2092static void ssl_write_extended_ms_ext( ssl_context *ssl,
2093 unsigned char *buf,
2094 size_t *olen )
2095{
2096 unsigned char *p = buf;
2097
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +02002098 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_DISABLED ||
2099 ssl->minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002100 {
2101 *olen = 0;
2102 return;
2103 }
2104
2105 SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
2106 "extension" ) );
2107
2108 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
2109 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
2110
2111 *p++ = 0x00;
2112 *p++ = 0x00;
2113
2114 *olen = 4;
2115}
2116#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
2117
Paul Bakkera503a632013-08-14 13:48:06 +02002118#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002119static void ssl_write_session_ticket_ext( ssl_context *ssl,
2120 unsigned char *buf,
2121 size_t *olen )
2122{
2123 unsigned char *p = buf;
2124
2125 if( ssl->handshake->new_session_ticket == 0 )
2126 {
2127 *olen = 0;
2128 return;
2129 }
2130
2131 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
2132
2133 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
2134 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
2135
2136 *p++ = 0x00;
2137 *p++ = 0x00;
2138
2139 *olen = 4;
2140}
Paul Bakkera503a632013-08-14 13:48:06 +02002141#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002142
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002143static void ssl_write_renegotiation_ext( ssl_context *ssl,
2144 unsigned char *buf,
2145 size_t *olen )
2146{
2147 unsigned char *p = buf;
2148
2149 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
2150 {
2151 *olen = 0;
2152 return;
2153 }
2154
2155 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
2156
2157 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
2158 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
2159
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002160#if defined(POLARSSL_SSL_RENEGOTIATION)
2161 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
2162 {
2163 *p++ = 0x00;
2164 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2165 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002166
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002167 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2168 p += ssl->verify_data_len;
2169 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2170 p += ssl->verify_data_len;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002171
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002172 *olen = 5 + ssl->verify_data_len * 2;
2173 }
2174 else
2175#endif /* POLARSSL_SSL_RENEGOTIATION */
2176 {
2177 *p++ = 0x00;
2178 *p++ = 0x01;
2179 *p++ = 0x00;
2180
2181 *olen = 5;
2182 }
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002183}
2184
Paul Bakker05decb22013-08-15 13:33:48 +02002185#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002186static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
2187 unsigned char *buf,
2188 size_t *olen )
2189{
2190 unsigned char *p = buf;
2191
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002192 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
2193 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002194 *olen = 0;
2195 return;
2196 }
2197
2198 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
2199
2200 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
2201 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
2202
2203 *p++ = 0x00;
2204 *p++ = 1;
2205
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002206 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002207
2208 *olen = 5;
2209}
Paul Bakker05decb22013-08-15 13:33:48 +02002210#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002211
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002212#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002213static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
2214 unsigned char *buf,
2215 size_t *olen )
2216{
2217 unsigned char *p = buf;
2218 ((void) ssl);
2219
Paul Bakker677377f2013-10-28 12:54:26 +01002220 if( ( ssl->handshake->cli_exts &
2221 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
2222 {
2223 *olen = 0;
2224 return;
2225 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002226
2227 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
2228
2229 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2230 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
2231
2232 *p++ = 0x00;
2233 *p++ = 2;
2234
2235 *p++ = 1;
2236 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
2237
2238 *olen = 6;
2239}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002240#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002241
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002242#if defined(POLARSSL_SSL_ALPN )
2243static void ssl_write_alpn_ext( ssl_context *ssl,
2244 unsigned char *buf, size_t *olen )
2245{
2246 if( ssl->alpn_chosen == NULL )
2247 {
2248 *olen = 0;
2249 return;
2250 }
2251
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02002252 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002253
2254 /*
2255 * 0 . 1 ext identifier
2256 * 2 . 3 ext length
2257 * 4 . 5 protocol list length
2258 * 6 . 6 protocol name length
2259 * 7 . 7+n protocol name
2260 */
2261 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
2262 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
2263
2264 *olen = 7 + strlen( ssl->alpn_chosen );
2265
2266 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2267 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2268
2269 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2270 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2271
2272 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2273
2274 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2275}
2276#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
2277
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002278#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002279static int ssl_write_hello_verify_request( ssl_context *ssl )
2280{
2281 int ret;
2282 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002283 unsigned char *cookie_len_byte;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002284
2285 SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
2286
2287 /*
2288 * struct {
2289 * ProtocolVersion server_version;
2290 * opaque cookie<0..2^8-1>;
2291 * } HelloVerifyRequest;
2292 */
2293
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02002294 /* The RFC is not clear on this point, but sending the actual negotiated
2295 * version looks like the most interoperable thing to do. */
2296 ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002297 ssl->transport, p );
2298 SSL_DEBUG_BUF( 3, "server version", (unsigned char *) p, 2 );
2299 p += 2;
2300
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002301 /* If we get here, f_cookie_check is not null */
2302 if( ssl->f_cookie_write == NULL )
2303 {
2304 SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
2305 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2306 }
2307
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002308 /* Skip length byte until we know the length */
2309 cookie_len_byte = p++;
2310
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002311 if( ( ret = ssl->f_cookie_write( ssl->p_cookie,
2312 &p, ssl->out_buf + SSL_BUFFER_LEN,
2313 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002314 {
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002315 SSL_DEBUG_RET( 1, "f_cookie_write", ret );
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002316 return( ret );
2317 }
2318
2319 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2320
2321 SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002322
2323 ssl->out_msglen = p - ssl->out_msg;
2324 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2325 ssl->out_msg[0] = SSL_HS_HELLO_VERIFY_REQUEST;
2326
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002327 ssl->state = SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002328
2329 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2330 {
2331 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2332 return( ret );
2333 }
2334
2335 SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
2336
2337 return( 0 );
2338}
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002339#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002340
Paul Bakker5121ce52009-01-03 21:22:43 +00002341static int ssl_write_server_hello( ssl_context *ssl )
2342{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002343#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002344 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002345#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002346 int ret;
2347 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002348 unsigned char *buf, *p;
2349
2350 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
2351
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002352#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002353 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002354 ssl->handshake->verify_cookie_len != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002355 {
2356 SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2357 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2358
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002359 return( ssl_write_hello_verify_request( ssl ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002360 }
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002361#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002362
Paul Bakkera9a028e2013-11-21 17:31:06 +01002363 if( ssl->f_rng == NULL )
2364 {
2365 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2366 return( POLARSSL_ERR_SSL_NO_RNG );
2367 }
2368
Paul Bakker5121ce52009-01-03 21:22:43 +00002369 /*
2370 * 0 . 0 handshake type
2371 * 1 . 3 handshake length
2372 * 4 . 5 protocol version
2373 * 6 . 9 UNIX time()
2374 * 10 . 37 random bytes
2375 */
2376 buf = ssl->out_msg;
2377 p = buf + 4;
2378
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002379 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2380 ssl->transport, p );
2381 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002382
2383 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002384 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002385
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002386#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002387 t = time( NULL );
2388 *p++ = (unsigned char)( t >> 24 );
2389 *p++ = (unsigned char)( t >> 16 );
2390 *p++ = (unsigned char)( t >> 8 );
2391 *p++ = (unsigned char)( t );
2392
2393 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002394#else
2395 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
2396 return( ret );
2397
2398 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02002399#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002400
Paul Bakkera3d195c2011-11-27 21:07:34 +00002401 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
2402 return( ret );
2403
2404 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002405
Paul Bakker48916f92012-09-16 19:57:18 +00002406 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002407
2408 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
2409
2410 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002411 * Resume is 0 by default, see ssl_handshake_init().
2412 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2413 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002414 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002415 if( ssl->handshake->resume == 0 &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002416#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002417 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002418#endif
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002419 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002420 ssl->f_get_cache != NULL &&
2421 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
2422 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01002423 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002424 ssl->handshake->resume = 1;
2425 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002426
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002427 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002428 {
2429 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002430 * New session, create a new session id,
2431 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002432 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002433 ssl->state++;
2434
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002435#if defined(POLARSSL_HAVE_TIME)
2436 ssl->session_negotiate->start = time( NULL );
2437#endif
2438
Paul Bakkera503a632013-08-14 13:48:06 +02002439#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002440 if( ssl->handshake->new_session_ticket != 0 )
2441 {
2442 ssl->session_negotiate->length = n = 0;
2443 memset( ssl->session_negotiate->id, 0, 32 );
2444 }
2445 else
2446#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002447 {
2448 ssl->session_negotiate->length = n = 32;
2449 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002450 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002451 return( ret );
2452 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002453 }
2454 else
2455 {
2456 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002457 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002458 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02002459 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002460 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002461
2462 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2463 {
2464 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2465 return( ret );
2466 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002467 }
2468
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002469 /*
2470 * 38 . 38 session id length
2471 * 39 . 38+n session id
2472 * 39+n . 40+n chosen ciphersuite
2473 * 41+n . 41+n chosen compression alg.
2474 * 42+n . 43+n extensions length
2475 * 44+n . 43+n+m extensions
2476 */
2477 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00002478 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
2479 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002480
2481 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2482 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2483 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002484 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002485
Paul Bakker48916f92012-09-16 19:57:18 +00002486 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2487 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2488 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002489
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02002490 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2491 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02002492 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002493 ssl->session_negotiate->compression ) );
2494
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002495 /*
2496 * First write extensions, then the total length
2497 */
2498 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2499 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002500
Paul Bakker05decb22013-08-15 13:33:48 +02002501#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002502 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2503 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002504#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002505
Paul Bakker1f2bc622013-08-15 13:45:55 +02002506#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002507 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2508 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002509#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002510
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002511#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2512 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2513 ext_len += olen;
2514#endif
2515
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002516#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2517 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2518 ext_len += olen;
2519#endif
2520
Paul Bakkera503a632013-08-14 13:48:06 +02002521#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002522 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2523 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002524#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002525
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002526#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002527 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2528 ext_len += olen;
2529#endif
2530
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002531#if defined(POLARSSL_SSL_ALPN)
2532 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2533 ext_len += olen;
2534#endif
2535
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002536 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002537
Paul Bakkera7036632014-04-30 10:15:38 +02002538 if( ext_len > 0 )
2539 {
2540 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2541 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2542 p += ext_len;
2543 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002544
2545 ssl->out_msglen = p - buf;
2546 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2547 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
2548
2549 ret = ssl_write_record( ssl );
2550
2551 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2552
2553 return( ret );
2554}
2555
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002556#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2557 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002558 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2559 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002560static int ssl_write_certificate_request( ssl_context *ssl )
2561{
Paul Bakkered27a042013-04-18 22:46:23 +02002562 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002563
2564 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2565
2566 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002567 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002568 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2569 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002570 {
2571 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2572 ssl->state++;
2573 return( 0 );
2574 }
2575
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002576 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2577 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002578}
2579#else
2580static int ssl_write_certificate_request( ssl_context *ssl )
2581{
2582 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2583 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002584 size_t dn_size, total_dn_size; /* excluding length bytes */
2585 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002586 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002587 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00002588
2589 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2590
2591 ssl->state++;
2592
Paul Bakkerfbb17802013-04-17 19:10:21 +02002593 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002594 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002595 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002596 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002597 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002598 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002599 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002600 return( 0 );
2601 }
2602
2603 /*
2604 * 0 . 0 handshake type
2605 * 1 . 3 handshake length
2606 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002607 * 5 .. m-1 cert types
2608 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002609 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002610 * n .. n+1 length of all DNs
2611 * n+2 .. n+3 length of DN 1
2612 * n+4 .. ... Distinguished Name #1
2613 * ... .. ... length of DN 2, etc.
2614 */
2615 buf = ssl->out_msg;
2616 p = buf + 4;
2617
2618 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002619 * Supported certificate types
2620 *
2621 * ClientCertificateType certificate_types<1..2^8-1>;
2622 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002623 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002624 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002625
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002626#if defined(POLARSSL_RSA_C)
2627 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2628#endif
2629#if defined(POLARSSL_ECDSA_C)
2630 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2631#endif
2632
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002633 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002634 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002635
Paul Bakker577e0062013-08-28 11:57:20 +02002636 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002637#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002638 /*
2639 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002640 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002641 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2642 *
2643 * struct {
2644 * HashAlgorithm hash;
2645 * SignatureAlgorithm signature;
2646 * } SignatureAndHashAlgorithm;
2647 *
2648 * enum { (255) } HashAlgorithm;
2649 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002650 */
Paul Bakker21dca692013-01-03 11:41:08 +01002651 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002652 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002653 /*
2654 * Only use current running hash algorithm that is already required
2655 * for requested ciphersuite.
2656 */
Paul Bakker926af752012-11-23 13:38:07 +01002657 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2658
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002659 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2660 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002661 {
2662 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2663 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002664
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002665 /*
2666 * Supported signature algorithms
2667 */
2668#if defined(POLARSSL_RSA_C)
2669 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2670 p[2 + sa_len++] = SSL_SIG_RSA;
2671#endif
2672#if defined(POLARSSL_ECDSA_C)
2673 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2674 p[2 + sa_len++] = SSL_SIG_ECDSA;
2675#endif
Paul Bakker926af752012-11-23 13:38:07 +01002676
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002677 p[0] = (unsigned char)( sa_len >> 8 );
2678 p[1] = (unsigned char)( sa_len );
2679 sa_len += 2;
2680 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002681 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002682#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002683
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002684 /*
2685 * DistinguishedName certificate_authorities<0..2^16-1>;
2686 * opaque DistinguishedName<1..2^16-1>;
2687 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002688 p += 2;
2689 crt = ssl->ca_chain;
2690
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002691 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002692 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002693 {
2694 if( p - buf > 4096 )
2695 break;
2696
Paul Bakker926af752012-11-23 13:38:07 +01002697 dn_size = crt->subject_raw.len;
2698 *p++ = (unsigned char)( dn_size >> 8 );
2699 *p++ = (unsigned char)( dn_size );
2700 memcpy( p, crt->subject_raw.p, dn_size );
2701 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002702
Paul Bakker926af752012-11-23 13:38:07 +01002703 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2704
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002705 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002706 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002707 }
2708
Paul Bakker926af752012-11-23 13:38:07 +01002709 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002710 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2711 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002712 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2713 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002714
2715 ret = ssl_write_record( ssl );
2716
2717 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2718
2719 return( ret );
2720}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002721#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2722 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002723 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2724 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002725
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002726#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2727 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2728static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2729{
2730 int ret;
2731
2732 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2733 {
2734 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2735 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2736 }
2737
2738 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2739 pk_ec( *ssl_own_key( ssl ) ),
2740 POLARSSL_ECDH_OURS ) ) != 0 )
2741 {
2742 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2743 return( ret );
2744 }
2745
2746 return( 0 );
2747}
2748#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2749 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2750
Paul Bakker41c83d32013-03-20 14:39:14 +01002751static int ssl_write_server_key_exchange( ssl_context *ssl )
2752{
Paul Bakker23986e52011-04-24 08:57:21 +00002753 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002754 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002755 const ssl_ciphersuite_t *ciphersuite_info =
2756 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002757
2758#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2759 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2760 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002761 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002762 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002763 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002764 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002765 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002766 ((void) dig_signed);
2767 ((void) dig_signed_len);
2768#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002769
Paul Bakker5121ce52009-01-03 21:22:43 +00002770 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2771
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002772#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2773 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2774 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002775 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2776 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2777 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002778 {
2779 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2780 ssl->state++;
2781 return( 0 );
2782 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002783#endif
2784
2785#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2786 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2787 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2788 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2789 {
2790 ssl_get_ecdh_params_from_cert( ssl );
2791
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002792 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002793 ssl->state++;
2794 return( 0 );
2795 }
2796#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002797
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002798#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2799 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2800 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2801 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002802 {
2803 /* TODO: Support identity hints */
2804 *(p++) = 0x00;
2805 *(p++) = 0x00;
2806
2807 n += 2;
2808 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002809#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2810 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002811
2812#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2813 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2814 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2815 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002816 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002817 /*
2818 * Ephemeral DH parameters:
2819 *
2820 * struct {
2821 * opaque dh_p<1..2^16-1>;
2822 * opaque dh_g<1..2^16-1>;
2823 * opaque dh_Ys<1..2^16-1>;
2824 * } ServerDHParams;
2825 */
2826 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2827 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2828 {
2829 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2830 return( ret );
2831 }
Paul Bakker48916f92012-09-16 19:57:18 +00002832
Paul Bakker41c83d32013-03-20 14:39:14 +01002833 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002834 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2835 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002836 {
2837 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2838 return( ret );
2839 }
2840
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002841 dig_signed = p;
2842 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002843
2844 p += len;
2845 n += len;
2846
Paul Bakker41c83d32013-03-20 14:39:14 +01002847 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2848 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2849 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2850 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2851 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002852#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2853 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002854
Gergely Budai987bfb52014-01-19 21:48:42 +01002855#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002856 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002857 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2858 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002859 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002860 /*
2861 * Ephemeral ECDH parameters:
2862 *
2863 * struct {
2864 * ECParameters curve_params;
2865 * ECPoint public;
2866 * } ServerECDHParams;
2867 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002868 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002869#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002870 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002871
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002872 /* Match our preference list against the offered curves */
2873 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2874 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2875 if( (*curve)->grp_id == *gid )
2876 goto curve_matching_done;
2877
2878curve_matching_done:
2879#else
2880 curve = ssl->handshake->curves;
2881#endif
2882
2883 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002884 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002885 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2886 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002887 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002888
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002889 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002890
Paul Bakker41c83d32013-03-20 14:39:14 +01002891 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002892 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002893 {
2894 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2895 return( ret );
2896 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002897
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002898 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2899 p, SSL_MAX_CONTENT_LEN - n,
2900 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002901 {
2902 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2903 return( ret );
2904 }
2905
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002906 dig_signed = p;
2907 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002908
2909 p += len;
2910 n += len;
2911
Paul Bakker41c83d32013-03-20 14:39:14 +01002912 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2913 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002914#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002915
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002916#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002917 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2918 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002919 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002920 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2921 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002922 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002923 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002924 unsigned int hashlen = 0;
2925 unsigned char hash[64];
2926 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002927
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002928 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002929 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2930 */
Paul Bakker577e0062013-08-28 11:57:20 +02002931#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002932 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2933 {
2934 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2935
2936 if( md_alg == POLARSSL_MD_NONE )
2937 {
2938 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002939 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002940 }
2941 }
Paul Bakker577e0062013-08-28 11:57:20 +02002942 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002943#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002944#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2945 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002946 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002947 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2948 {
2949 md_alg = POLARSSL_MD_SHA1;
2950 }
2951 else
Paul Bakker577e0062013-08-28 11:57:20 +02002952#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002953 {
2954 md_alg = POLARSSL_MD_NONE;
2955 }
2956
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002957 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002958 * Compute the hash to be signed
2959 */
Paul Bakker577e0062013-08-28 11:57:20 +02002960#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2961 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002962 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002963 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002964 md5_context md5;
2965 sha1_context sha1;
2966
Paul Bakker5b4af392014-06-26 12:09:34 +02002967 md5_init( &md5 );
2968 sha1_init( &sha1 );
2969
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002970 /*
2971 * digitally-signed struct {
2972 * opaque md5_hash[16];
2973 * opaque sha_hash[20];
2974 * };
2975 *
2976 * md5_hash
2977 * MD5(ClientHello.random + ServerHello.random
2978 * + ServerParams);
2979 * sha_hash
2980 * SHA(ClientHello.random + ServerHello.random
2981 * + ServerParams);
2982 */
2983 md5_starts( &md5 );
2984 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002985 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002986 md5_finish( &md5, hash );
2987
2988 sha1_starts( &sha1 );
2989 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002990 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002991 sha1_finish( &sha1, hash + 16 );
2992
2993 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02002994
2995 md5_free( &md5 );
2996 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002997 }
2998 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002999#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
3000 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02003001#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3002 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02003003 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003004 {
3005 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02003006 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003007
Paul Bakker84bbeb52014-07-01 14:53:22 +02003008 md_init( &ctx );
3009
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003010 /* Info from md_alg will be used instead */
3011 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003012
3013 /*
3014 * digitally-signed struct {
3015 * opaque client_random[32];
3016 * opaque server_random[32];
3017 * ServerDHParams params;
3018 * };
3019 */
Paul Bakker66d5d072014-06-17 16:39:18 +02003020 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003021 {
3022 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
3023 return( ret );
3024 }
3025
3026 md_starts( &ctx );
3027 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003028 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003029 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003030 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00003031 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003032 else
Paul Bakker9659dae2013-08-28 16:21:34 +02003033#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3034 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003035 {
3036 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003037 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003038 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003039
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02003040 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
3041 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003042
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003043 /*
3044 * Make the signature
3045 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003046 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00003047 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003048 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
3049 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003050 }
Paul Bakker23f36802012-09-28 14:15:14 +00003051
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003052#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00003053 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
3054 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003055 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003056 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003057
3058 n += 2;
3059 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003060#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003061
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003062 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003063 p + 2 , &signature_len,
3064 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003065 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003066 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003067 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003068 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003069
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003070 *(p++) = (unsigned char)( signature_len >> 8 );
3071 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00003072 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003073
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003074 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00003075
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003076 p += signature_len;
3077 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003078 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003079#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003080 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3081 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003082
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003083 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003084 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3085 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
3086
3087 ssl->state++;
3088
3089 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3090 {
3091 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3092 return( ret );
3093 }
3094
3095 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
3096
3097 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003098}
3099
3100static int ssl_write_server_hello_done( ssl_context *ssl )
3101{
3102 int ret;
3103
3104 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
3105
3106 ssl->out_msglen = 4;
3107 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3108 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
3109
3110 ssl->state++;
3111
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003112#if defined(POLARSSL_SSL_PROTO_DTLS)
3113 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3114 ssl_send_flight_completed( ssl );
3115#endif
3116
Paul Bakker5121ce52009-01-03 21:22:43 +00003117 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3118 {
3119 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3120 return( ret );
3121 }
3122
3123 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
3124
3125 return( 0 );
3126}
3127
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003128#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3129 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3130static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
3131 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003132{
3133 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003134 size_t n;
3135
3136 /*
3137 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3138 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003139 if( *p + 2 > end )
3140 {
3141 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3142 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3143 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003144
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003145 n = ( (*p)[0] << 8 ) | (*p)[1];
3146 *p += 2;
3147
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003148 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003149 {
3150 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3151 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3152 }
3153
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003154 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003155 {
3156 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
3157 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3158 }
3159
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003160 *p += n;
3161
Paul Bakker70df2fb2013-04-17 17:19:09 +02003162 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
3163
Paul Bakker70df2fb2013-04-17 17:19:09 +02003164 return( ret );
3165}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003166#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3167 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003168
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003169#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
3170 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003171static int ssl_parse_encrypted_pms( ssl_context *ssl,
3172 const unsigned char *p,
3173 const unsigned char *end,
3174 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003175{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003176 int ret;
3177 size_t len = pk_get_len( ssl_own_key( ssl ) );
3178 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003179 unsigned char ver[2];
Paul Bakker70df2fb2013-04-17 17:19:09 +02003180
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003181 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003182 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02003183 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003184 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3185 }
3186
3187 /*
3188 * Decrypt the premaster using own private RSA key
3189 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003190#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3191 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02003192 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
3193 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003194 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
3195 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003196 {
3197 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3198 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3199 }
3200 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003201#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003202
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003203 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003204 {
3205 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3206 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3207 }
3208
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003209 ssl_write_version( ssl->handshake->max_major_ver,
3210 ssl->handshake->max_minor_ver,
3211 ssl->transport, ver );
3212
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003213 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
3214 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003215 sizeof( ssl->handshake->premaster ) - pms_offset,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003216 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003217
3218 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003219 pms[0] != ver[0] ||
3220 pms[1] != ver[1] )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003221 {
3222 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3223
3224 /*
3225 * Protection against Bleichenbacher's attack:
3226 * invalid PKCS#1 v1.5 padding must not cause
3227 * the connection to end immediately; instead,
3228 * send a bad_record_mac later in the handshake.
3229 */
3230 ssl->handshake->pmslen = 48;
3231
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003232 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003233 if( ret != 0 )
3234 return( ret );
3235 }
3236
3237 return( ret );
3238}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003239#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
3240 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003241
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003242#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003243static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
3244 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003245{
Paul Bakker6db455e2013-09-18 17:29:31 +02003246 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003247 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003248
Paul Bakker6db455e2013-09-18 17:29:31 +02003249 if( ssl->f_psk == NULL &&
3250 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
3251 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003252 {
3253 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
3254 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3255 }
3256
3257 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003258 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003259 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003260 if( *p + 2 > end )
3261 {
3262 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3263 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3264 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003265
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003266 n = ( (*p)[0] << 8 ) | (*p)[1];
3267 *p += 2;
3268
3269 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003270 {
3271 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3272 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3273 }
3274
Paul Bakker6db455e2013-09-18 17:29:31 +02003275 if( ssl->f_psk != NULL )
3276 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003277 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003278 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3279 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003280 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003281 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003282 /* Identity is not a big secret since clients send it in the clear,
3283 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02003284 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003285 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003286 {
3287 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3288 }
3289 }
3290
3291 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003292 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003293 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02003294 if( ( ret = ssl_send_alert_message( ssl,
3295 SSL_ALERT_LEVEL_FATAL,
3296 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
3297 {
3298 return( ret );
3299 }
3300
3301 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003302 }
3303
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003304 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003305
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003306 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003307}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003308#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003309
Paul Bakker5121ce52009-01-03 21:22:43 +00003310static int ssl_parse_client_key_exchange( ssl_context *ssl )
3311{
Paul Bakker23986e52011-04-24 08:57:21 +00003312 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003313 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003314 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003315
Paul Bakker41c83d32013-03-20 14:39:14 +01003316 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003317
3318 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
3319
3320 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3321 {
3322 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3323 return( ret );
3324 }
3325
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003326 p = ssl->in_msg + ssl_hs_hdr_len( ssl );
3327 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00003328
Paul Bakker5121ce52009-01-03 21:22:43 +00003329 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3330 {
3331 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003332 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003333 }
3334
3335 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
3336 {
3337 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003338 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003339 }
3340
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003341#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01003342 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003343 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003344 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003345 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02003346 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3347 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003348 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003349
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003350 if( p != end )
3351 {
3352 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3353 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3354 }
3355
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02003356 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003357
3358 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
3359 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003360 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02003361 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003362 {
3363 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
3364 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3365 }
3366
3367 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003368 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003369 else
3370#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003371#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003372 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3373 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3374 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003375 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003376 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
3377 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
3378 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003379 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003380 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003381 p, end - p) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003382 {
3383 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3384 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3385 }
3386
3387 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3388
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003389 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
3390 &ssl->handshake->pmslen,
3391 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02003392 POLARSSL_MPI_MAX_SIZE,
3393 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003394 {
3395 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
3396 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3397 }
3398
3399 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003400 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003401 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003402#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003403 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3404 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3405 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003406#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
3407 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003408 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003409 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003410 {
3411 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3412 return( ret );
3413 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003414
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003415 if( p != end )
3416 {
3417 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3418 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3419 }
3420
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003421 if( ( ret = ssl_psk_derive_premaster( ssl,
3422 ciphersuite_info->key_exchange ) ) != 0 )
3423 {
3424 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3425 return( ret );
3426 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003427 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003428 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003429#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003430#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
3431 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
3432 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003433 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3434 {
3435 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3436 return( ret );
3437 }
3438
3439 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3440 {
3441 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
3442 return( ret );
3443 }
3444
3445 if( ( ret = ssl_psk_derive_premaster( ssl,
3446 ciphersuite_info->key_exchange ) ) != 0 )
3447 {
3448 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3449 return( ret );
3450 }
3451 }
3452 else
3453#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003454#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3455 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3456 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003457 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3458 {
3459 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3460 return( ret );
3461 }
3462 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3463 {
3464 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3465 return( ret );
3466 }
3467
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003468 if( p != end )
3469 {
3470 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3471 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3472 }
3473
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003474 if( ( ret = ssl_psk_derive_premaster( ssl,
3475 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003476 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003477 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3478 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003479 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003480 }
3481 else
3482#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003483#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3484 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
3485 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003486 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3487 {
3488 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3489 return( ret );
3490 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003491
3492 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
3493 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003494 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003495 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3496 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003497 }
3498
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003499 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3500
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003501 if( ( ret = ssl_psk_derive_premaster( ssl,
3502 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003503 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003504 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003505 return( ret );
3506 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003507 }
3508 else
3509#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003510#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
3511 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003512 {
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003513 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003514 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003515 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003516 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003517 }
3518 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003519 else
3520#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
3521 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003522 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003523 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003524 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003525
Paul Bakkerff60ee62010-03-16 21:09:09 +00003526 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
3527 {
3528 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
3529 return( ret );
3530 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003531
Paul Bakker5121ce52009-01-03 21:22:43 +00003532 ssl->state++;
3533
3534 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3535
3536 return( 0 );
3537}
3538
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003539#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3540 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003541 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3542 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003543static int ssl_parse_certificate_verify( ssl_context *ssl )
3544{
Paul Bakkerfbb17802013-04-17 19:10:21 +02003545 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003546
3547 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3548
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003549 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003550 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003551 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003552 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02003553 {
3554 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3555 ssl->state++;
3556 return( 0 );
3557 }
3558
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003559 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3560 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003561}
3562#else
3563static int ssl_parse_certificate_verify( ssl_context *ssl )
3564{
3565 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003566 size_t i, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003567 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003568 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003569 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003570#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003571 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003572#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003573 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003574 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3575
3576 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3577
3578 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003579 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003580 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003581 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3582 ssl->session_negotiate->peer_cert == NULL )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003583 {
3584 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3585 ssl->state++;
3586 return( 0 );
3587 }
3588
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003589 /* Needs to be done before read_record() to exclude current message */
Paul Bakker48916f92012-09-16 19:57:18 +00003590 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003591
3592 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3593 {
3594 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3595 return( ret );
3596 }
3597
3598 ssl->state++;
3599
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003600 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE ||
3601 ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003602 {
3603 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003604 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003605 }
3606
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003607 i = ssl_hs_hdr_len( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003608
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003609 /*
3610 * struct {
3611 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
3612 * opaque signature<0..2^16-1>;
3613 * } DigitallySigned;
3614 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003615#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3616 defined(POLARSSL_SSL_PROTO_TLS1_1)
3617 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003618 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003619 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003620 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003621
3622 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3623 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3624 POLARSSL_PK_ECDSA ) )
3625 {
3626 hash_start += 16;
3627 hashlen -= 16;
3628 md_alg = POLARSSL_MD_SHA1;
3629 }
Paul Bakker926af752012-11-23 13:38:07 +01003630 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003631 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003632#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3633 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003634#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3635 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003636 {
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00003637 if( i + 2 > ssl->in_hslen )
3638 {
3639 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3640 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3641 }
3642
Paul Bakker5121ce52009-01-03 21:22:43 +00003643 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003644 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003645 */
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003646 if( ssl->in_msg[i] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003647 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003648 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3649 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003650 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3651 }
3652
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003653 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003654
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003655 /* Info from md_alg will be used instead */
3656 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003657
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003658 i++;
3659
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003660 /*
3661 * Signature
3662 */
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003663 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003664 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003665 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003666 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3667 " for verify message" ) );
3668 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003669 }
3670
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003671 /*
3672 * Check the certificate's key type matches the signature alg
3673 */
3674 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3675 {
3676 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3677 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3678 }
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003679
3680 i++;
Paul Bakker577e0062013-08-28 11:57:20 +02003681 }
3682 else
3683#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3684 {
3685 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003686 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003687 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003688
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00003689 if( i + 2 > ssl->in_hslen )
3690 {
3691 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3692 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3693 }
3694
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003695 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
3696 i += 2;
Paul Bakker926af752012-11-23 13:38:07 +01003697
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003698 if( i + sig_len != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003699 {
3700 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003701 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003702 }
3703
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003704 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003705 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003706 ssl->in_msg + i, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003707 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003708 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003709 return( ret );
3710 }
3711
3712 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3713
Paul Bakkered27a042013-04-18 22:46:23 +02003714 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003715}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003716#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3717 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3718 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003719
Paul Bakkera503a632013-08-14 13:48:06 +02003720#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003721static int ssl_write_new_session_ticket( ssl_context *ssl )
3722{
3723 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003724 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003725 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003726
3727 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3728
3729 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3730 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3731
3732 /*
3733 * struct {
3734 * uint32 ticket_lifetime_hint;
3735 * opaque ticket<0..2^16-1>;
3736 * } NewSessionTicket;
3737 *
3738 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3739 * 8 . 9 ticket_len (n)
3740 * 10 . 9+n ticket content
3741 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003742
3743 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3744 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3745 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3746 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003747
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003748 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3749 {
3750 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3751 tlen = 0;
3752 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003753
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003754 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3755 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003756
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003757 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003758
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003759 /*
3760 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3761 * ChangeCipherSpec share the same state.
3762 */
3763 ssl->handshake->new_session_ticket = 0;
3764
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003765 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3766 {
3767 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3768 return( ret );
3769 }
3770
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003771 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3772
3773 return( 0 );
3774}
Paul Bakkera503a632013-08-14 13:48:06 +02003775#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003776
Paul Bakker5121ce52009-01-03 21:22:43 +00003777/*
Paul Bakker1961b702013-01-25 14:49:24 +01003778 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003779 */
Paul Bakker1961b702013-01-25 14:49:24 +01003780int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003781{
3782 int ret = 0;
3783
Paul Bakker1961b702013-01-25 14:49:24 +01003784 if( ssl->state == SSL_HANDSHAKE_OVER )
3785 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003786
Paul Bakker1961b702013-01-25 14:49:24 +01003787 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3788
3789 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3790 return( ret );
3791
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003792#if defined(POLARSSL_SSL_PROTO_DTLS)
3793 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3794 ssl->handshake != NULL &&
3795 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
3796 {
3797 if( ( ret = ssl_resend( ssl ) ) != 0 )
3798 return( ret );
3799 }
3800#endif
3801
Paul Bakker1961b702013-01-25 14:49:24 +01003802 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003803 {
Paul Bakker1961b702013-01-25 14:49:24 +01003804 case SSL_HELLO_REQUEST:
3805 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003806 break;
3807
Paul Bakker1961b702013-01-25 14:49:24 +01003808 /*
3809 * <== ClientHello
3810 */
3811 case SSL_CLIENT_HELLO:
3812 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003813 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003814
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02003815#if defined(POLARSSL_SSL_PROTO_DTLS)
3816 case SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
3817 return( POLARSSL_ERR_SSL_HELLO_VERIFY_REQUIRED );
3818#endif
3819
Paul Bakker1961b702013-01-25 14:49:24 +01003820 /*
3821 * ==> ServerHello
3822 * Certificate
3823 * ( ServerKeyExchange )
3824 * ( CertificateRequest )
3825 * ServerHelloDone
3826 */
3827 case SSL_SERVER_HELLO:
3828 ret = ssl_write_server_hello( ssl );
3829 break;
3830
3831 case SSL_SERVER_CERTIFICATE:
3832 ret = ssl_write_certificate( ssl );
3833 break;
3834
3835 case SSL_SERVER_KEY_EXCHANGE:
3836 ret = ssl_write_server_key_exchange( ssl );
3837 break;
3838
3839 case SSL_CERTIFICATE_REQUEST:
3840 ret = ssl_write_certificate_request( ssl );
3841 break;
3842
3843 case SSL_SERVER_HELLO_DONE:
3844 ret = ssl_write_server_hello_done( ssl );
3845 break;
3846
3847 /*
3848 * <== ( Certificate/Alert )
3849 * ClientKeyExchange
3850 * ( CertificateVerify )
3851 * ChangeCipherSpec
3852 * Finished
3853 */
3854 case SSL_CLIENT_CERTIFICATE:
3855 ret = ssl_parse_certificate( ssl );
3856 break;
3857
3858 case SSL_CLIENT_KEY_EXCHANGE:
3859 ret = ssl_parse_client_key_exchange( ssl );
3860 break;
3861
3862 case SSL_CERTIFICATE_VERIFY:
3863 ret = ssl_parse_certificate_verify( ssl );
3864 break;
3865
3866 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3867 ret = ssl_parse_change_cipher_spec( ssl );
3868 break;
3869
3870 case SSL_CLIENT_FINISHED:
3871 ret = ssl_parse_finished( ssl );
3872 break;
3873
3874 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003875 * ==> ( NewSessionTicket )
3876 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003877 * Finished
3878 */
3879 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003880#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003881 if( ssl->handshake->new_session_ticket != 0 )
3882 ret = ssl_write_new_session_ticket( ssl );
3883 else
Paul Bakkera503a632013-08-14 13:48:06 +02003884#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003885 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003886 break;
3887
3888 case SSL_SERVER_FINISHED:
3889 ret = ssl_write_finished( ssl );
3890 break;
3891
3892 case SSL_FLUSH_BUFFERS:
3893 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3894 ssl->state = SSL_HANDSHAKE_WRAPUP;
3895 break;
3896
3897 case SSL_HANDSHAKE_WRAPUP:
3898 ssl_handshake_wrapup( ssl );
3899 break;
3900
3901 default:
3902 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3903 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003904 }
3905
Paul Bakker5121ce52009-01-03 21:22:43 +00003906 return( ret );
3907}
Paul Bakker9af723c2014-05-01 13:03:14 +02003908#endif /* POLARSSL_SSL_SRV_C */