blob: 2fefdda9443159aa1b6c07442c24f2726e614642 [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é-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://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
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000872 SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) );
873
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100874 for( cur = list; cur != NULL; cur = cur->next )
875 {
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000876 SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate",
877 cur->cert );
878
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100879 if( ! pk_can_do( cur->key, pk_alg ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000880 {
881 SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100882 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000883 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100884
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200885 /*
886 * This avoids sending the client a cert it'll reject based on
887 * keyUsage or other extensions.
888 *
889 * It also allows the user to provision different certificates for
890 * different uses based on keyUsage, eg if they want to avoid signing
891 * and decrypting with the same RSA key.
892 */
893 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
894 SSL_IS_SERVER ) != 0 )
895 {
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000896 SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
897 "(extended) key usage extension" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200898 continue;
899 }
900
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100901#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100902 if( pk_alg == POLARSSL_PK_ECDSA &&
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100903 ssl_check_key_curve( cur->key, ssl->handshake->curves ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000904 {
905 SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) );
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100906 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000907 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100908#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100909
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100910 /*
911 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
912 * present them a SHA-higher cert rather than failing if it's the only
913 * one we got that satisfies the other conditions.
914 */
915 if( ssl->minor_ver < SSL_MINOR_VERSION_3 &&
916 cur->cert->sig_md != POLARSSL_MD_SHA1 )
917 {
918 if( fallback == NULL )
919 fallback = cur;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000920 {
921 SSL_DEBUG_MSG( 3, ( "certificate not preferred: "
922 "sha-2 with pre-TLS 1.2 client" ) );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100923 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000924 }
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100925 }
926
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100927 /* If we get there, we got a winner */
928 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100929 }
930
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000931 if( cur == NULL )
932 cur = fallback;
933
934
935 /* Do not update ssl->handshake->key_cert unless the is a match */
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100936 if( cur != NULL )
937 {
938 ssl->handshake->key_cert = cur;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000939 SSL_DEBUG_CRT( 3, "selected certificate chain, certificate",
940 ssl->handshake->key_cert->cert );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100941 return( 0 );
942 }
943
944 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100945}
946#endif /* POLARSSL_X509_CRT_PARSE_C */
947
948/*
949 * Check if a given ciphersuite is suitable for use with our config/keys/etc
950 * Sets ciphersuite_info only if the suite matches.
951 */
952static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
953 const ssl_ciphersuite_t **ciphersuite_info )
954{
955 const ssl_ciphersuite_t *suite_info;
956
957 suite_info = ssl_ciphersuite_from_id( suite_id );
958 if( suite_info == NULL )
959 {
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100960 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
961 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100962 }
963
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000964 SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %s", suite_info->name ) );
965
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100966 if( suite_info->min_minor_ver > ssl->minor_ver ||
967 suite_info->max_minor_ver < ssl->minor_ver )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000968 {
969 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100970 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000971 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100972
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +0100973#if defined(POLARSSL_SSL_PROTO_DTLS)
974 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
975 ( suite_info->flags & POLARSSL_CIPHERSUITE_NODTLS ) )
976 return( 0 );
977#endif
978
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100979 if( ssl->arc4_disabled == SSL_ARC4_DISABLED &&
980 suite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000981 {
982 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100983 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000984 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100985
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100986#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
987 if( ssl_ciphersuite_uses_ec( suite_info ) &&
988 ( ssl->handshake->curves == NULL ||
989 ssl->handshake->curves[0] == NULL ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000990 {
991 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
992 "no common elliptic curve" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100993 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000994 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100995#endif
996
997#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
998 /* If the ciphersuite requires a pre-shared key and we don't
999 * have one, skip it now rather than failing later */
1000 if( ssl_ciphersuite_uses_psk( suite_info ) &&
1001 ssl->f_psk == NULL &&
1002 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
1003 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001004 {
1005 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001006 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001007 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001008#endif
1009
1010#if defined(POLARSSL_X509_CRT_PARSE_C)
1011 /*
1012 * Final check: if ciphersuite requires us to have a
1013 * certificate/key of a particular type:
1014 * - select the appropriate certificate if we have one, or
1015 * - try the next ciphersuite if we don't
1016 * This must be done last since we modify the key_cert list.
1017 */
1018 if( ssl_pick_cert( ssl, suite_info ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001019 {
1020 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
1021 "no suitable certificate" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001022 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001023 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001024#endif
1025
1026 *ciphersuite_info = suite_info;
1027 return( 0 );
1028}
1029
Paul Bakker78a8c712013-03-06 17:01:52 +01001030#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1031static int ssl_parse_client_hello_v2( ssl_context *ssl )
1032{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001033 int ret, got_common_suite;
Paul Bakker78a8c712013-03-06 17:01:52 +01001034 unsigned int i, j;
1035 size_t n;
1036 unsigned int ciph_len, sess_len, chal_len;
1037 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001038 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +02001039 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001040
1041 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
1042
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001043#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker78a8c712013-03-06 17:01:52 +01001044 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
1045 {
1046 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
1047
1048 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1049 return( ret );
1050
1051 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1052 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001053#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001054
1055 buf = ssl->in_hdr;
1056
1057 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1058
1059 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
1060 buf[2] ) );
1061 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
1062 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
1063 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
1064 buf[3], buf[4] ) );
1065
1066 /*
1067 * SSLv2 Client Hello
1068 *
1069 * Record layer:
1070 * 0 . 1 message length
1071 *
1072 * SSL layer:
1073 * 2 . 2 message type
1074 * 3 . 4 protocol version
1075 */
1076 if( buf[2] != SSL_HS_CLIENT_HELLO ||
1077 buf[3] != SSL_MAJOR_VERSION_3 )
1078 {
1079 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1080 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1081 }
1082
1083 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
1084
1085 if( n < 17 || n > 512 )
1086 {
1087 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1088 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1089 }
1090
1091 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +02001092 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
1093 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +01001094
1095 if( ssl->minor_ver < ssl->min_minor_ver )
1096 {
1097 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001098 " [%d:%d] < [%d:%d]",
1099 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +01001100 ssl->min_major_ver, ssl->min_minor_ver ) );
1101
1102 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1103 SSL_ALERT_MSG_PROTOCOL_VERSION );
1104 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1105 }
1106
Paul Bakker2fbefde2013-06-29 16:01:15 +02001107 ssl->handshake->max_major_ver = buf[3];
1108 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +01001109
1110 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
1111 {
1112 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1113 return( ret );
1114 }
1115
1116 ssl->handshake->update_checksum( ssl, buf + 2, n );
1117
1118 buf = ssl->in_msg;
1119 n = ssl->in_left - 5;
1120
1121 /*
1122 * 0 . 1 ciphersuitelist length
1123 * 2 . 3 session id length
1124 * 4 . 5 challenge length
1125 * 6 . .. ciphersuitelist
1126 * .. . .. session id
1127 * .. . .. challenge
1128 */
1129 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1130
1131 ciph_len = ( buf[0] << 8 ) | buf[1];
1132 sess_len = ( buf[2] << 8 ) | buf[3];
1133 chal_len = ( buf[4] << 8 ) | buf[5];
1134
1135 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
1136 ciph_len, sess_len, chal_len ) );
1137
1138 /*
1139 * Make sure each parameter length is valid
1140 */
1141 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1142 {
1143 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1144 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1145 }
1146
1147 if( sess_len > 32 )
1148 {
1149 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1150 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1151 }
1152
1153 if( chal_len < 8 || chal_len > 32 )
1154 {
1155 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1156 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1157 }
1158
1159 if( n != 6 + ciph_len + sess_len + chal_len )
1160 {
1161 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1162 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1163 }
1164
1165 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1166 buf + 6, ciph_len );
1167 SSL_DEBUG_BUF( 3, "client hello, session id",
1168 buf + 6 + ciph_len, sess_len );
1169 SSL_DEBUG_BUF( 3, "client hello, challenge",
1170 buf + 6 + ciph_len + sess_len, chal_len );
1171
1172 p = buf + 6 + ciph_len;
1173 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001174 memset( ssl->session_negotiate->id, 0,
1175 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001176 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1177
1178 p += sess_len;
1179 memset( ssl->handshake->randbytes, 0, 64 );
1180 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1181
1182 /*
1183 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1184 */
1185 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1186 {
1187 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1188 {
1189 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001190#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker78a8c712013-03-06 17:01:52 +01001191 if( ssl->renegotiation == SSL_RENEGOTIATION )
1192 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001193 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
1194 "during renegotiation" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001195
1196 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1197 return( ret );
1198
1199 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1200 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001201#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001202 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1203 break;
1204 }
1205 }
1206
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001207#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1208 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1209 {
1210 if( p[0] == 0 &&
1211 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1212 p[2] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1213 {
1214 SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
1215
1216 if( ssl->minor_ver < ssl->max_minor_ver )
1217 {
1218 SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
1219
1220 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1221 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1222
1223 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1224 }
1225
1226 break;
1227 }
1228 }
1229#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1230
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001231 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001232 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001233 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001234#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1235 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1236 {
1237 for( i = 0; ciphersuites[i] != 0; i++ )
1238#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001239 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001240 {
1241 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001242#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001243 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001244 if( p[0] != 0 ||
1245 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1246 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1247 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001248
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001249 got_common_suite = 1;
1250
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001251 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1252 &ciphersuite_info ) ) != 0 )
1253 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001254
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001255 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001256 goto have_ciphersuite_v2;
1257 }
1258 }
1259
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001260 if( got_common_suite )
1261 {
1262 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1263 "but none of them usable" ) );
1264 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
1265 }
1266 else
1267 {
1268 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1269 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1270 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001271
1272have_ciphersuite_v2:
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001273 SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
1274
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001275 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001276 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001277 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001278
1279 /*
1280 * SSLv2 Client Hello relevant renegotiation security checks
1281 */
1282 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1283 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1284 {
1285 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1286
1287 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1288 return( ret );
1289
1290 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1291 }
1292
1293 ssl->in_left = 0;
1294 ssl->state++;
1295
1296 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1297
1298 return( 0 );
1299}
1300#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1301
Paul Bakker5121ce52009-01-03 21:22:43 +00001302static int ssl_parse_client_hello( ssl_context *ssl )
1303{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001304 int ret, got_common_suite;
Paul Bakker23986e52011-04-24 08:57:21 +00001305 unsigned int i, j;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001306 unsigned int ciph_offset, comp_offset, ext_offset;
1307 unsigned int msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001308#if defined(POLARSSL_SSL_PROTO_DTLS)
1309 unsigned int cookie_offset, cookie_len;
1310#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001311 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001312#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001313 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001314#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001315 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001316 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001317 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001318 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001319
1320 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1321
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001322#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1323read_record_header:
1324#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001325 /*
1326 * If renegotiating, then the input was read with ssl_read_record(),
1327 * otherwise read it ourselves manually in order to support SSLv2
1328 * ClientHello, which doesn't use the same record layer format.
1329 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001330#if defined(POLARSSL_SSL_RENEGOTIATION)
1331 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1332#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001333 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001334 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
1335 {
1336 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1337 return( ret );
1338 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 }
1340
1341 buf = ssl->in_hdr;
1342
Paul Bakker78a8c712013-03-06 17:01:52 +01001343#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02001344#if defined(POLARSSL_SSL_PROTO_DTLS)
1345 if( ssl->transport == SSL_TRANSPORT_STREAM )
1346#endif
1347 if( ( buf[0] & 0x80 ) != 0 )
1348 return ssl_parse_client_hello_v2( ssl );
Paul Bakker78a8c712013-03-06 17:01:52 +01001349#endif
1350
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001351 SSL_DEBUG_BUF( 4, "record header", buf, ssl_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001352
Paul Bakkerec636f32012-09-09 19:17:02 +00001353 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001354 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001355 *
1356 * Record layer:
1357 * 0 . 0 message type
1358 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001359 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001360 * 3 . 4 message length
1361 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001362 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1363 buf[0] ) );
1364
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001365 if( buf[0] != SSL_MSG_HANDSHAKE )
1366 {
1367 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1368 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1369 }
1370
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001371 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1372 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1373
1374 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol version: [%d:%d]",
1375 buf[1], buf[2] ) );
1376
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001377 ssl_read_version( &major, &minor, ssl->transport, buf + 1 );
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001378
1379 /* According to RFC 5246 Appendix E.1, the version here is typically
1380 * "{03,00}, the lowest version number supported by the client, [or] the
1381 * value of ClientHello.client_version", so the only meaningful check here
1382 * is the major version shouldn't be less than 3 */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001383 if( major < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001384 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001385 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1386 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1387 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001388
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001389 /* For DTLS if this is the initial handshake, remember the client sequence
1390 * number to use it in our next message (RFC 6347 4.2.1) */
1391#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00001392 if( ssl->transport == SSL_TRANSPORT_DATAGRAM
1393#if defined(POLARSSL_SSL_RENEGOTIATION)
1394 && ssl->renegotiation == SSL_INITIAL_HANDSHAKE
1395#endif
1396 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001397 {
1398 /* Epoch should be 0 for initial handshakes */
1399 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1400 {
1401 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1402 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1403 }
1404
1405 memcpy( ssl->out_ctr + 2, ssl->in_ctr + 2, 6 );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001406
1407#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1408 if( ssl_dtls_replay_check( ssl ) != 0 )
1409 {
1410 SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
1411 ssl->next_record_offset = 0;
1412 ssl->in_left = 0;
1413 goto read_record_header;
1414 }
1415
1416 /* No MAC to check yet, so we can update right now */
1417 ssl_dtls_replay_update( ssl );
1418#endif
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001419 }
1420#endif /* POLARSSL_SSL_PROTO_DTLS */
1421
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001422 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001423
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001424#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001425 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
1426 {
1427 /* Set by ssl_read_record() */
1428 msg_len = ssl->in_hslen;
1429 }
1430 else
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001431#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001432 {
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001433 if( msg_len > SSL_MAX_CONTENT_LEN )
1434 {
1435 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1436 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1437 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001438
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001439 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) + msg_len ) ) != 0 )
1440 {
1441 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1442 return( ret );
1443 }
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001444
1445 /* Done reading this record, get ready for the next one */
1446#if defined(POLARSSL_SSL_PROTO_DTLS)
1447 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1448 ssl->next_record_offset = msg_len + ssl_hdr_len( ssl );
1449 else
1450#endif
1451 ssl->in_left = 0;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001452 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001453
1454 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001455
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001456 SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001457
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001458 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001459
1460 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001461 * Handshake layer:
1462 * 0 . 0 handshake type
1463 * 1 . 3 handshake length
1464 * 4 . 5 DTLS only: message seqence number
1465 * 6 . 8 DTLS only: fragment offset
1466 * 9 . 11 DTLS only: fragment length
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001467 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001468 if( msg_len < ssl_hs_hdr_len( ssl ) )
1469 {
1470 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1471 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1472 }
1473
1474 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
1475
1476 if( buf[0] != SSL_HS_CLIENT_HELLO )
1477 {
1478 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1479 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1480 }
1481
1482 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1483 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1484
1485 /* We don't support fragmentation of ClientHello (yet?) */
1486 if( buf[1] != 0 ||
1487 msg_len != ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) )
1488 {
1489 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1490 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1491 }
1492
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001493#if defined(POLARSSL_SSL_PROTO_DTLS)
1494 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1495 {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001496 /*
1497 * Copy the client's handshake message_seq on initial handshakes
1498 */
1499 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02001500 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001501 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1502 ssl->in_msg[5];
1503 ssl->handshake->out_msg_seq = cli_msg_seq;
1504 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02001505 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001506 else
1507 {
1508 /* This couldn't be done in ssl_prepare_handshake_record() */
1509 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1510 ssl->in_msg[5];
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001511
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001512 if( cli_msg_seq != ssl->handshake->in_msg_seq )
1513 {
1514 SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
1515 "%d (expected %d)", cli_msg_seq,
1516 ssl->handshake->in_msg_seq ) );
1517 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1518 }
1519
1520 ssl->handshake->in_msg_seq++;
1521 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001522
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001523 /*
1524 * For now we don't support fragmentation, so make sure
1525 * fragment_offset == 0 and fragment_length == length
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001526 */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001527 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1528 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1529 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001530 SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001531 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1532 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001533 }
1534#endif /* POLARSSL_SSL_PROTO_DTLS */
1535
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001536 buf += ssl_hs_hdr_len( ssl );
1537 msg_len -= ssl_hs_hdr_len( ssl );
1538
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001539 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001540 * ClientHello layer:
1541 * 0 . 1 protocol version
1542 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1543 * 34 . 35 session id length (1 byte)
1544 * 35 . 34+x session id
1545 * 35+x . 35+x DTLS only: cookie length (1 byte)
1546 * 36+x . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001547 * .. . .. ciphersuite list length (2 bytes)
1548 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001549 * .. . .. compression alg. list length (1 byte)
1550 * .. . .. compression alg. list
1551 * .. . .. extensions length (2 bytes, optional)
1552 * .. . .. extensions (optional)
Paul Bakkerec636f32012-09-09 19:17:02 +00001553 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001554
1555 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001556 * Minimal length (with everything empty and extensions ommitted) is
1557 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1558 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001559 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001560 if( msg_len < 38 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001561 {
1562 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1563 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1564 }
1565
1566 /*
1567 * Check and save the protocol version
1568 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001569 SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001570
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001571 ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001572 ssl->transport, buf );
Paul Bakkerec636f32012-09-09 19:17:02 +00001573
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001574 ssl->handshake->max_major_ver = ssl->major_ver;
1575 ssl->handshake->max_minor_ver = ssl->minor_ver;
1576
1577 if( ssl->major_ver < ssl->min_major_ver ||
1578 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001579 {
1580 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001581 " [%d:%d] < [%d:%d]",
1582 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001583 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001584
1585 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1586 SSL_ALERT_MSG_PROTOCOL_VERSION );
1587
1588 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1589 }
1590
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001591 if( ssl->major_ver > ssl->max_major_ver )
1592 {
1593 ssl->major_ver = ssl->max_major_ver;
1594 ssl->minor_ver = ssl->max_minor_ver;
1595 }
1596 else if( ssl->minor_ver > ssl->max_minor_ver )
1597 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001598
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001599 /*
1600 * Save client random (inc. Unix time)
1601 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001602 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001603
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001604 memcpy( ssl->handshake->randbytes, buf + 2, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001605
1606 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001607 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001608 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001609 sess_len = buf[34];
Paul Bakkerec636f32012-09-09 19:17:02 +00001610
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001611 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001612 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001613 {
1614 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1615 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1616 }
1617
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001618 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001619
Paul Bakker48916f92012-09-16 19:57:18 +00001620 ssl->session_negotiate->length = sess_len;
1621 memset( ssl->session_negotiate->id, 0,
1622 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001623 memcpy( ssl->session_negotiate->id, buf + 35,
Paul Bakker48916f92012-09-16 19:57:18 +00001624 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001625
1626 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001627 * Check the cookie length and content
1628 */
1629#if defined(POLARSSL_SSL_PROTO_DTLS)
1630 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1631 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001632 cookie_offset = 35 + sess_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001633 cookie_len = buf[cookie_offset];
1634
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001635 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001636 {
1637 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1638 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1639 }
1640
1641 SSL_DEBUG_BUF( 3, "client hello, cookie",
1642 buf + cookie_offset + 1, cookie_len );
1643
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001644#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001645 if( ssl->f_cookie_check != NULL &&
1646 ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001647 {
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001648 if( ssl->f_cookie_check( ssl->p_cookie,
1649 buf + cookie_offset + 1, cookie_len,
1650 ssl->cli_id, ssl->cli_id_len ) != 0 )
1651 {
1652 SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
1653 ssl->handshake->verify_cookie_len = 1;
1654 }
1655 else
1656 {
1657 SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
1658 ssl->handshake->verify_cookie_len = 0;
1659 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001660 }
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001661 else
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +02001662#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001663 {
1664 /* We know we didn't send a cookie, so it should be empty */
1665 if( cookie_len != 0 )
1666 {
1667 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1668 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1669 }
1670
1671 SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
1672 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001673 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001674#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001675
1676 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001677 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001678 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001679#if defined(POLARSSL_SSL_PROTO_DTLS)
1680 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1681 ciph_offset = cookie_offset + 1 + cookie_len;
1682 else
1683#endif
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001684 ciph_offset = 35 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001685
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001686 ciph_len = ( buf[ciph_offset + 0] << 8 )
1687 | ( buf[ciph_offset + 1] );
1688
1689 if( ciph_len < 2 ||
1690 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1691 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001692 {
1693 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1694 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1695 }
1696
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001697 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1698 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001699
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001700 /*
1701 * Check the compression algorithms length and pick one
1702 */
1703 comp_offset = ciph_offset + 2 + ciph_len;
1704
1705 comp_len = buf[comp_offset];
1706
1707 if( comp_len < 1 ||
1708 comp_len > 16 ||
1709 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001710 {
1711 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1712 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1713 }
1714
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001715 SSL_DEBUG_BUF( 3, "client hello, compression",
1716 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001717
1718 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001719#if defined(POLARSSL_ZLIB_SUPPORT)
1720 for( i = 0; i < comp_len; ++i )
1721 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001722 if( buf[comp_offset + 1 + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001723 {
Paul Bakker48916f92012-09-16 19:57:18 +00001724 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001725 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001726 }
1727 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001728#endif
1729
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001730 /* See comments in ssl_write_client_hello() */
1731#if defined(POLARSSL_SSL_PROTO_DTLS)
1732 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1733 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
1734#endif
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001735
Paul Bakkerec636f32012-09-09 19:17:02 +00001736 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001737 * Check the extension length
Paul Bakker48916f92012-09-16 19:57:18 +00001738 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001739 ext_offset = comp_offset + 1 + comp_len;
1740 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001741 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001742 if( msg_len < ext_offset + 2 )
Paul Bakker48916f92012-09-16 19:57:18 +00001743 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001744 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1745 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1746 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001747
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001748 ext_len = ( buf[ext_offset + 0] << 8 )
1749 | ( buf[ext_offset + 1] );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001750
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001751 if( ( ext_len > 0 && ext_len < 4 ) ||
1752 msg_len != ext_offset + 2 + ext_len )
1753 {
1754 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1755 SSL_DEBUG_BUF( 3, "client hello extensions",
1756 buf + ext_offset + 2, ext_len );
1757 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +00001758 }
1759 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001760 else
1761 ext_len = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001762
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001763 ext = buf + ext_offset + 2;
Paul Bakker48916f92012-09-16 19:57:18 +00001764
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001765 while( ext_len != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001766 {
1767 unsigned int ext_id = ( ( ext[0] << 8 )
1768 | ( ext[1] ) );
1769 unsigned int ext_size = ( ( ext[2] << 8 )
1770 | ( ext[3] ) );
1771
1772 if( ext_size + 4 > ext_len )
1773 {
1774 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1775 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1776 }
1777 switch( ext_id )
1778 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001779#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001780 case TLS_EXT_SERVERNAME:
1781 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1782 if( ssl->f_sni == NULL )
1783 break;
1784
1785 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1786 if( ret != 0 )
1787 return( ret );
1788 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001789#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001790
Paul Bakker48916f92012-09-16 19:57:18 +00001791 case TLS_EXT_RENEGOTIATION_INFO:
1792 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001793#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001794 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001795#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001796
Paul Bakker23f36802012-09-28 14:15:14 +00001797 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1798 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001799 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001800 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001801
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001802#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1803 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakker23f36802012-09-28 14:15:14 +00001804 case TLS_EXT_SIG_ALG:
1805 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001806#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker23f36802012-09-28 14:15:14 +00001807 if( ssl->renegotiation == SSL_RENEGOTIATION )
1808 break;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001809#endif
Paul Bakker23f36802012-09-28 14:15:14 +00001810
1811 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1812 if( ret != 0 )
1813 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001814 break;
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001815#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
1816 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001817
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001818#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001819 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1820 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1821
1822 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1823 if( ret != 0 )
1824 return( ret );
1825 break;
1826
1827 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1828 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001829 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001830
1831 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1832 if( ret != 0 )
1833 return( ret );
1834 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001835#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001836
Paul Bakker05decb22013-08-15 13:33:48 +02001837#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001838 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1839 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1840
1841 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1842 if( ret != 0 )
1843 return( ret );
1844 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001845#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001846
Paul Bakker1f2bc622013-08-15 13:45:55 +02001847#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001848 case TLS_EXT_TRUNCATED_HMAC:
1849 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1850
1851 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1852 if( ret != 0 )
1853 return( ret );
1854 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001855#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001856
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001857#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1858 case TLS_EXT_ENCRYPT_THEN_MAC:
1859 SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
1860
1861 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1862 if( ret != 0 )
1863 return( ret );
1864 break;
1865#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1866
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001867#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1868 case TLS_EXT_EXTENDED_MASTER_SECRET:
1869 SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
1870
1871 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1872 if( ret != 0 )
1873 return( ret );
1874 break;
1875#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1876
Paul Bakkera503a632013-08-14 13:48:06 +02001877#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001878 case TLS_EXT_SESSION_TICKET:
1879 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1880
1881 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1882 if( ret != 0 )
1883 return( ret );
1884 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001885#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001886
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001887#if defined(POLARSSL_SSL_ALPN)
1888 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001889 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001890
1891 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1892 if( ret != 0 )
1893 return( ret );
1894 break;
1895#endif /* POLARSSL_SSL_SESSION_TICKETS */
1896
Paul Bakker48916f92012-09-16 19:57:18 +00001897 default:
1898 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1899 ext_id ) );
1900 }
1901
1902 ext_len -= 4 + ext_size;
1903 ext += 4 + ext_size;
1904
1905 if( ext_len > 0 && ext_len < 4 )
1906 {
1907 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1908 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1909 }
1910 }
1911
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01001912#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1913 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1914 {
1915 if( p[0] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1916 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1917 {
1918 SSL_DEBUG_MSG( 0, ( "received FALLBACK_SCSV" ) );
1919
1920 if( ssl->minor_ver < ssl->max_minor_ver )
1921 {
1922 SSL_DEBUG_MSG( 0, ( "inapropriate fallback" ) );
1923
1924 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1925 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1926
1927 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1928 }
1929
1930 break;
1931 }
1932 }
1933#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1934
Paul Bakker48916f92012-09-16 19:57:18 +00001935 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001936 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1937 */
1938 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
1939 {
1940 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1941 {
1942 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1943 if( ssl->renegotiation == SSL_RENEGOTIATION )
1944 {
1945 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1946
1947 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1948 return( ret );
1949
1950 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1951 }
1952 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1953 break;
1954 }
1955 }
1956
1957 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001958 * Renegotiation security checks
1959 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001960 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001961 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1962 {
1963 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1964 handshake_failure = 1;
1965 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001966#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001967 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1968 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1969 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001970 {
1971 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001972 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001973 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001974 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1975 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1976 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001977 {
1978 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001979 handshake_failure = 1;
1980 }
1981 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1982 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1983 renegotiation_info_seen == 1 )
1984 {
1985 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1986 handshake_failure = 1;
1987 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001988#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001989
1990 if( handshake_failure == 1 )
1991 {
1992 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1993 return( ret );
1994
Paul Bakker48916f92012-09-16 19:57:18 +00001995 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1996 }
Paul Bakker380da532012-04-18 16:10:25 +00001997
Paul Bakker41c83d32013-03-20 14:39:14 +01001998 /*
1999 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002000 * (At the end because we need information from the EC-based extensions
2001 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01002002 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002003 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002004 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01002005 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002006#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002007 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002008 {
2009 for( i = 0; ciphersuites[i] != 0; i++ )
2010#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002011 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01002012 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002013 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002014#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002015 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002016 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
2017 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
2018 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01002019
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002020 got_common_suite = 1;
2021
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002022 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
2023 &ciphersuite_info ) ) != 0 )
2024 return( ret );
2025
2026 if( ciphersuite_info != NULL )
2027 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01002028 }
2029 }
2030
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002031 if( got_common_suite )
2032 {
2033 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
2034 "but none of them usable" ) );
2035 ssl_send_fatal_handshake_failure( ssl );
2036 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
2037 }
2038 else
2039 {
2040 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
2041 ssl_send_fatal_handshake_failure( ssl );
2042 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
2043 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002044
2045have_ciphersuite:
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00002046 SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
2047
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002048 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01002049 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
2050 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
2051
Paul Bakker5121ce52009-01-03 21:22:43 +00002052 ssl->state++;
2053
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002054#if defined(POLARSSL_SSL_PROTO_DTLS)
2055 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2056 ssl_recv_flight_completed( ssl );
2057#endif
2058
Paul Bakker5121ce52009-01-03 21:22:43 +00002059 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
2060
2061 return( 0 );
2062}
2063
Paul Bakker1f2bc622013-08-15 13:45:55 +02002064#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002065static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
2066 unsigned char *buf,
2067 size_t *olen )
2068{
2069 unsigned char *p = buf;
2070
2071 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
2072 {
2073 *olen = 0;
2074 return;
2075 }
2076
2077 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
2078
2079 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
2080 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
2081
2082 *p++ = 0x00;
2083 *p++ = 0x00;
2084
2085 *olen = 4;
2086}
Paul Bakker1f2bc622013-08-15 13:45:55 +02002087#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002088
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002089#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2090static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
2091 unsigned char *buf,
2092 size_t *olen )
2093{
2094 unsigned char *p = buf;
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002095 const ssl_ciphersuite_t *suite = NULL;
2096 const cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002097
2098 if( ssl->session_negotiate->encrypt_then_mac == SSL_EXTENDED_MS_DISABLED ||
2099 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2100 {
2101 *olen = 0;
2102 return;
2103 }
2104
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002105 /*
2106 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
2107 * from a client and then selects a stream or Authenticated Encryption
2108 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
2109 * encrypt-then-MAC response extension back to the client."
2110 */
2111 if( ( suite = ssl_ciphersuite_from_id(
2112 ssl->session_negotiate->ciphersuite ) ) == NULL ||
2113 ( cipher = cipher_info_from_type( suite->cipher ) ) == NULL ||
2114 cipher->mode != POLARSSL_MODE_CBC )
2115 {
2116 *olen = 0;
2117 return;
2118 }
2119
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002120 SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
2121
2122 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
2123 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
2124
2125 *p++ = 0x00;
2126 *p++ = 0x00;
2127
2128 *olen = 4;
2129}
2130#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
2131
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002132#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2133static void ssl_write_extended_ms_ext( ssl_context *ssl,
2134 unsigned char *buf,
2135 size_t *olen )
2136{
2137 unsigned char *p = buf;
2138
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +02002139 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_DISABLED ||
2140 ssl->minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002141 {
2142 *olen = 0;
2143 return;
2144 }
2145
2146 SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
2147 "extension" ) );
2148
2149 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
2150 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
2151
2152 *p++ = 0x00;
2153 *p++ = 0x00;
2154
2155 *olen = 4;
2156}
2157#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
2158
Paul Bakkera503a632013-08-14 13:48:06 +02002159#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002160static void ssl_write_session_ticket_ext( ssl_context *ssl,
2161 unsigned char *buf,
2162 size_t *olen )
2163{
2164 unsigned char *p = buf;
2165
2166 if( ssl->handshake->new_session_ticket == 0 )
2167 {
2168 *olen = 0;
2169 return;
2170 }
2171
2172 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
2173
2174 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
2175 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
2176
2177 *p++ = 0x00;
2178 *p++ = 0x00;
2179
2180 *olen = 4;
2181}
Paul Bakkera503a632013-08-14 13:48:06 +02002182#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002183
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002184static void ssl_write_renegotiation_ext( ssl_context *ssl,
2185 unsigned char *buf,
2186 size_t *olen )
2187{
2188 unsigned char *p = buf;
2189
2190 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
2191 {
2192 *olen = 0;
2193 return;
2194 }
2195
2196 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
2197
2198 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
2199 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
2200
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002201#if defined(POLARSSL_SSL_RENEGOTIATION)
2202 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
2203 {
2204 *p++ = 0x00;
2205 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2206 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002207
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002208 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2209 p += ssl->verify_data_len;
2210 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2211 p += ssl->verify_data_len;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002212
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002213 *olen = 5 + ssl->verify_data_len * 2;
2214 }
2215 else
2216#endif /* POLARSSL_SSL_RENEGOTIATION */
2217 {
2218 *p++ = 0x00;
2219 *p++ = 0x01;
2220 *p++ = 0x00;
2221
2222 *olen = 5;
2223 }
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002224}
2225
Paul Bakker05decb22013-08-15 13:33:48 +02002226#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002227static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
2228 unsigned char *buf,
2229 size_t *olen )
2230{
2231 unsigned char *p = buf;
2232
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002233 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
2234 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002235 *olen = 0;
2236 return;
2237 }
2238
2239 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
2240
2241 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
2242 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
2243
2244 *p++ = 0x00;
2245 *p++ = 1;
2246
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002247 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002248
2249 *olen = 5;
2250}
Paul Bakker05decb22013-08-15 13:33:48 +02002251#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002252
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002253#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002254static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
2255 unsigned char *buf,
2256 size_t *olen )
2257{
2258 unsigned char *p = buf;
2259 ((void) ssl);
2260
Paul Bakker677377f2013-10-28 12:54:26 +01002261 if( ( ssl->handshake->cli_exts &
2262 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
2263 {
2264 *olen = 0;
2265 return;
2266 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002267
2268 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
2269
2270 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2271 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
2272
2273 *p++ = 0x00;
2274 *p++ = 2;
2275
2276 *p++ = 1;
2277 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
2278
2279 *olen = 6;
2280}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002281#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002282
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002283#if defined(POLARSSL_SSL_ALPN )
2284static void ssl_write_alpn_ext( ssl_context *ssl,
2285 unsigned char *buf, size_t *olen )
2286{
2287 if( ssl->alpn_chosen == NULL )
2288 {
2289 *olen = 0;
2290 return;
2291 }
2292
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02002293 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002294
2295 /*
2296 * 0 . 1 ext identifier
2297 * 2 . 3 ext length
2298 * 4 . 5 protocol list length
2299 * 6 . 6 protocol name length
2300 * 7 . 7+n protocol name
2301 */
2302 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
2303 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
2304
2305 *olen = 7 + strlen( ssl->alpn_chosen );
2306
2307 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2308 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2309
2310 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2311 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2312
2313 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2314
2315 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2316}
2317#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
2318
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002319#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002320static int ssl_write_hello_verify_request( ssl_context *ssl )
2321{
2322 int ret;
2323 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002324 unsigned char *cookie_len_byte;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002325
2326 SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
2327
2328 /*
2329 * struct {
2330 * ProtocolVersion server_version;
2331 * opaque cookie<0..2^8-1>;
2332 * } HelloVerifyRequest;
2333 */
2334
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02002335 /* The RFC is not clear on this point, but sending the actual negotiated
2336 * version looks like the most interoperable thing to do. */
2337 ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002338 ssl->transport, p );
2339 SSL_DEBUG_BUF( 3, "server version", (unsigned char *) p, 2 );
2340 p += 2;
2341
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002342 /* If we get here, f_cookie_check is not null */
2343 if( ssl->f_cookie_write == NULL )
2344 {
2345 SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
2346 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2347 }
2348
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002349 /* Skip length byte until we know the length */
2350 cookie_len_byte = p++;
2351
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002352 if( ( ret = ssl->f_cookie_write( ssl->p_cookie,
2353 &p, ssl->out_buf + SSL_BUFFER_LEN,
2354 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002355 {
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002356 SSL_DEBUG_RET( 1, "f_cookie_write", ret );
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002357 return( ret );
2358 }
2359
2360 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2361
2362 SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002363
2364 ssl->out_msglen = p - ssl->out_msg;
2365 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2366 ssl->out_msg[0] = SSL_HS_HELLO_VERIFY_REQUEST;
2367
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002368 ssl->state = SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002369
2370 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2371 {
2372 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2373 return( ret );
2374 }
2375
2376 SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
2377
2378 return( 0 );
2379}
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002380#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002381
Paul Bakker5121ce52009-01-03 21:22:43 +00002382static int ssl_write_server_hello( ssl_context *ssl )
2383{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002384#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002385 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002386#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002387 int ret;
2388 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002389 unsigned char *buf, *p;
2390
2391 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
2392
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002393#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002394 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002395 ssl->handshake->verify_cookie_len != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002396 {
2397 SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2398 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2399
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002400 return( ssl_write_hello_verify_request( ssl ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002401 }
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002402#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002403
Paul Bakkera9a028e2013-11-21 17:31:06 +01002404 if( ssl->f_rng == NULL )
2405 {
2406 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2407 return( POLARSSL_ERR_SSL_NO_RNG );
2408 }
2409
Paul Bakker5121ce52009-01-03 21:22:43 +00002410 /*
2411 * 0 . 0 handshake type
2412 * 1 . 3 handshake length
2413 * 4 . 5 protocol version
2414 * 6 . 9 UNIX time()
2415 * 10 . 37 random bytes
2416 */
2417 buf = ssl->out_msg;
2418 p = buf + 4;
2419
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002420 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2421 ssl->transport, p );
2422 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002423
2424 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002425 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002426
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002427#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002428 t = time( NULL );
2429 *p++ = (unsigned char)( t >> 24 );
2430 *p++ = (unsigned char)( t >> 16 );
2431 *p++ = (unsigned char)( t >> 8 );
2432 *p++ = (unsigned char)( t );
2433
2434 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002435#else
2436 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
2437 return( ret );
2438
2439 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02002440#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002441
Paul Bakkera3d195c2011-11-27 21:07:34 +00002442 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
2443 return( ret );
2444
2445 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002446
Paul Bakker48916f92012-09-16 19:57:18 +00002447 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002448
2449 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
2450
2451 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002452 * Resume is 0 by default, see ssl_handshake_init().
2453 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2454 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002455 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002456 if( ssl->handshake->resume == 0 &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002457#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002458 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002459#endif
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002460 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002461 ssl->f_get_cache != NULL &&
2462 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
2463 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01002464 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002465 ssl->handshake->resume = 1;
2466 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002467
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002468 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002469 {
2470 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002471 * New session, create a new session id,
2472 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002473 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002474 ssl->state++;
2475
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002476#if defined(POLARSSL_HAVE_TIME)
2477 ssl->session_negotiate->start = time( NULL );
2478#endif
2479
Paul Bakkera503a632013-08-14 13:48:06 +02002480#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002481 if( ssl->handshake->new_session_ticket != 0 )
2482 {
2483 ssl->session_negotiate->length = n = 0;
2484 memset( ssl->session_negotiate->id, 0, 32 );
2485 }
2486 else
2487#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002488 {
2489 ssl->session_negotiate->length = n = 32;
2490 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002491 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002492 return( ret );
2493 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002494 }
2495 else
2496 {
2497 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002498 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002499 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02002500 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002501 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002502
2503 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2504 {
2505 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2506 return( ret );
2507 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002508 }
2509
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002510 /*
2511 * 38 . 38 session id length
2512 * 39 . 38+n session id
2513 * 39+n . 40+n chosen ciphersuite
2514 * 41+n . 41+n chosen compression alg.
2515 * 42+n . 43+n extensions length
2516 * 44+n . 43+n+m extensions
2517 */
2518 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00002519 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
2520 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002521
2522 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2523 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2524 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002525 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002526
Paul Bakker48916f92012-09-16 19:57:18 +00002527 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2528 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2529 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002530
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02002531 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2532 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02002533 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002534 ssl->session_negotiate->compression ) );
2535
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002536 /*
2537 * First write extensions, then the total length
2538 */
2539 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2540 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002541
Paul Bakker05decb22013-08-15 13:33:48 +02002542#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002543 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2544 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002545#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002546
Paul Bakker1f2bc622013-08-15 13:45:55 +02002547#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002548 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2549 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002550#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002551
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002552#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2553 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2554 ext_len += olen;
2555#endif
2556
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002557#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2558 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2559 ext_len += olen;
2560#endif
2561
Paul Bakkera503a632013-08-14 13:48:06 +02002562#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002563 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2564 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002565#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002566
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002567#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002568 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2569 ext_len += olen;
2570#endif
2571
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002572#if defined(POLARSSL_SSL_ALPN)
2573 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2574 ext_len += olen;
2575#endif
2576
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002577 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002578
Paul Bakkera7036632014-04-30 10:15:38 +02002579 if( ext_len > 0 )
2580 {
2581 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2582 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2583 p += ext_len;
2584 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002585
2586 ssl->out_msglen = p - buf;
2587 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2588 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
2589
2590 ret = ssl_write_record( ssl );
2591
2592 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2593
2594 return( ret );
2595}
2596
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002597#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2598 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002599 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2600 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002601static int ssl_write_certificate_request( ssl_context *ssl )
2602{
Paul Bakkered27a042013-04-18 22:46:23 +02002603 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002604
2605 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2606
2607 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002608 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002609 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2610 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002611 {
2612 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2613 ssl->state++;
2614 return( 0 );
2615 }
2616
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002617 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2618 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002619}
2620#else
2621static int ssl_write_certificate_request( ssl_context *ssl )
2622{
2623 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2624 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002625 size_t dn_size, total_dn_size; /* excluding length bytes */
2626 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002627 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002628 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00002629
2630 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2631
2632 ssl->state++;
2633
Paul Bakkerfbb17802013-04-17 19:10:21 +02002634 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002635 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002636 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002637 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002638 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002639 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002640 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002641 return( 0 );
2642 }
2643
2644 /*
2645 * 0 . 0 handshake type
2646 * 1 . 3 handshake length
2647 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002648 * 5 .. m-1 cert types
2649 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002650 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002651 * n .. n+1 length of all DNs
2652 * n+2 .. n+3 length of DN 1
2653 * n+4 .. ... Distinguished Name #1
2654 * ... .. ... length of DN 2, etc.
2655 */
2656 buf = ssl->out_msg;
2657 p = buf + 4;
2658
2659 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002660 * Supported certificate types
2661 *
2662 * ClientCertificateType certificate_types<1..2^8-1>;
2663 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002664 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002665 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002666
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002667#if defined(POLARSSL_RSA_C)
2668 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2669#endif
2670#if defined(POLARSSL_ECDSA_C)
2671 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2672#endif
2673
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002674 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002675 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002676
Paul Bakker577e0062013-08-28 11:57:20 +02002677 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002678#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002679 /*
2680 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002681 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002682 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2683 *
2684 * struct {
2685 * HashAlgorithm hash;
2686 * SignatureAlgorithm signature;
2687 * } SignatureAndHashAlgorithm;
2688 *
2689 * enum { (255) } HashAlgorithm;
2690 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002691 */
Paul Bakker21dca692013-01-03 11:41:08 +01002692 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002693 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002694 /*
2695 * Only use current running hash algorithm that is already required
2696 * for requested ciphersuite.
2697 */
Paul Bakker926af752012-11-23 13:38:07 +01002698 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2699
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002700 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2701 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002702 {
2703 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2704 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002705
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002706 /*
2707 * Supported signature algorithms
2708 */
2709#if defined(POLARSSL_RSA_C)
2710 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2711 p[2 + sa_len++] = SSL_SIG_RSA;
2712#endif
2713#if defined(POLARSSL_ECDSA_C)
2714 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2715 p[2 + sa_len++] = SSL_SIG_ECDSA;
2716#endif
Paul Bakker926af752012-11-23 13:38:07 +01002717
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002718 p[0] = (unsigned char)( sa_len >> 8 );
2719 p[1] = (unsigned char)( sa_len );
2720 sa_len += 2;
2721 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002722 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002723#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002724
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002725 /*
2726 * DistinguishedName certificate_authorities<0..2^16-1>;
2727 * opaque DistinguishedName<1..2^16-1>;
2728 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002729 p += 2;
2730 crt = ssl->ca_chain;
2731
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002732 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002733 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002734 {
2735 if( p - buf > 4096 )
2736 break;
2737
Paul Bakker926af752012-11-23 13:38:07 +01002738 dn_size = crt->subject_raw.len;
2739 *p++ = (unsigned char)( dn_size >> 8 );
2740 *p++ = (unsigned char)( dn_size );
2741 memcpy( p, crt->subject_raw.p, dn_size );
2742 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002743
Paul Bakker926af752012-11-23 13:38:07 +01002744 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2745
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002746 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002747 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002748 }
2749
Paul Bakker926af752012-11-23 13:38:07 +01002750 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002751 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2752 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002753 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2754 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002755
2756 ret = ssl_write_record( ssl );
2757
2758 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2759
2760 return( ret );
2761}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002762#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2763 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002764 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2765 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002766
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002767#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2768 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2769static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2770{
2771 int ret;
2772
2773 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2774 {
2775 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2776 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2777 }
2778
2779 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2780 pk_ec( *ssl_own_key( ssl ) ),
2781 POLARSSL_ECDH_OURS ) ) != 0 )
2782 {
2783 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2784 return( ret );
2785 }
2786
2787 return( 0 );
2788}
2789#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2790 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2791
Paul Bakker41c83d32013-03-20 14:39:14 +01002792static int ssl_write_server_key_exchange( ssl_context *ssl )
2793{
Paul Bakker23986e52011-04-24 08:57:21 +00002794 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002795 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002796 const ssl_ciphersuite_t *ciphersuite_info =
2797 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002798
2799#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2800 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2801 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002802 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002803 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002804 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002805 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002806 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002807 ((void) dig_signed);
2808 ((void) dig_signed_len);
2809#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002810
Paul Bakker5121ce52009-01-03 21:22:43 +00002811 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2812
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002813#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2814 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2815 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002816 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2817 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2818 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002819 {
2820 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2821 ssl->state++;
2822 return( 0 );
2823 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002824#endif
2825
2826#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2827 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2828 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2829 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2830 {
2831 ssl_get_ecdh_params_from_cert( ssl );
2832
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002833 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002834 ssl->state++;
2835 return( 0 );
2836 }
2837#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002838
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002839#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2840 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2841 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2842 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002843 {
2844 /* TODO: Support identity hints */
2845 *(p++) = 0x00;
2846 *(p++) = 0x00;
2847
2848 n += 2;
2849 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002850#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2851 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002852
2853#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2854 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2855 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2856 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002857 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002858 /*
2859 * Ephemeral DH parameters:
2860 *
2861 * struct {
2862 * opaque dh_p<1..2^16-1>;
2863 * opaque dh_g<1..2^16-1>;
2864 * opaque dh_Ys<1..2^16-1>;
2865 * } ServerDHParams;
2866 */
2867 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2868 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2869 {
2870 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2871 return( ret );
2872 }
Paul Bakker48916f92012-09-16 19:57:18 +00002873
Paul Bakker41c83d32013-03-20 14:39:14 +01002874 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002875 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2876 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002877 {
2878 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2879 return( ret );
2880 }
2881
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002882 dig_signed = p;
2883 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002884
2885 p += len;
2886 n += len;
2887
Paul Bakker41c83d32013-03-20 14:39:14 +01002888 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2889 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2890 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2891 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2892 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002893#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2894 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002895
Gergely Budai987bfb52014-01-19 21:48:42 +01002896#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002897 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002898 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2899 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002900 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002901 /*
2902 * Ephemeral ECDH parameters:
2903 *
2904 * struct {
2905 * ECParameters curve_params;
2906 * ECPoint public;
2907 * } ServerECDHParams;
2908 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002909 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002910#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002911 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002912
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002913 /* Match our preference list against the offered curves */
2914 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2915 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2916 if( (*curve)->grp_id == *gid )
2917 goto curve_matching_done;
2918
2919curve_matching_done:
2920#else
2921 curve = ssl->handshake->curves;
2922#endif
2923
2924 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002925 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002926 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2927 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002928 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002929
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002930 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002931
Paul Bakker41c83d32013-03-20 14:39:14 +01002932 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002933 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002934 {
2935 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2936 return( ret );
2937 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002938
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002939 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2940 p, SSL_MAX_CONTENT_LEN - n,
2941 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002942 {
2943 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2944 return( ret );
2945 }
2946
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002947 dig_signed = p;
2948 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002949
2950 p += len;
2951 n += len;
2952
Paul Bakker41c83d32013-03-20 14:39:14 +01002953 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2954 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002955#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002956
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002957#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002958 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2959 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002960 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002961 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2962 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002963 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002964 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002965 unsigned int hashlen = 0;
2966 unsigned char hash[64];
2967 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002968
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002969 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002970 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2971 */
Paul Bakker577e0062013-08-28 11:57:20 +02002972#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002973 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2974 {
2975 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2976
2977 if( md_alg == POLARSSL_MD_NONE )
2978 {
2979 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002980 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002981 }
2982 }
Paul Bakker577e0062013-08-28 11:57:20 +02002983 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002984#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002985#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2986 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002987 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002988 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2989 {
2990 md_alg = POLARSSL_MD_SHA1;
2991 }
2992 else
Paul Bakker577e0062013-08-28 11:57:20 +02002993#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002994 {
2995 md_alg = POLARSSL_MD_NONE;
2996 }
2997
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002998 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002999 * Compute the hash to be signed
3000 */
Paul Bakker577e0062013-08-28 11:57:20 +02003001#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3002 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003003 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00003004 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003005 md5_context md5;
3006 sha1_context sha1;
3007
Paul Bakker5b4af392014-06-26 12:09:34 +02003008 md5_init( &md5 );
3009 sha1_init( &sha1 );
3010
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003011 /*
3012 * digitally-signed struct {
3013 * opaque md5_hash[16];
3014 * opaque sha_hash[20];
3015 * };
3016 *
3017 * md5_hash
3018 * MD5(ClientHello.random + ServerHello.random
3019 * + ServerParams);
3020 * sha_hash
3021 * SHA(ClientHello.random + ServerHello.random
3022 * + ServerParams);
3023 */
3024 md5_starts( &md5 );
3025 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003026 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003027 md5_finish( &md5, hash );
3028
3029 sha1_starts( &sha1 );
3030 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003031 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003032 sha1_finish( &sha1, hash + 16 );
3033
3034 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02003035
3036 md5_free( &md5 );
3037 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003038 }
3039 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003040#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
3041 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02003042#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3043 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02003044 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003045 {
3046 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02003047 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003048
Paul Bakker84bbeb52014-07-01 14:53:22 +02003049 md_init( &ctx );
3050
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003051 /* Info from md_alg will be used instead */
3052 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003053
3054 /*
3055 * digitally-signed struct {
3056 * opaque client_random[32];
3057 * opaque server_random[32];
3058 * ServerDHParams params;
3059 * };
3060 */
Paul Bakker66d5d072014-06-17 16:39:18 +02003061 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003062 {
3063 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
3064 return( ret );
3065 }
3066
3067 md_starts( &ctx );
3068 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003069 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003070 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003071 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00003072 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003073 else
Paul Bakker9659dae2013-08-28 16:21:34 +02003074#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3075 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003076 {
3077 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003078 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003079 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003080
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02003081 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
3082 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003083
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003084 /*
3085 * Make the signature
3086 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003087 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00003088 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003089 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
3090 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003091 }
Paul Bakker23f36802012-09-28 14:15:14 +00003092
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003093#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00003094 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
3095 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003096 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003097 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003098
3099 n += 2;
3100 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003101#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003102
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003103 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003104 p + 2 , &signature_len,
3105 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003106 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003107 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003108 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003109 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003110
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003111 *(p++) = (unsigned char)( signature_len >> 8 );
3112 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00003113 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003114
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003115 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00003116
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003117 p += signature_len;
3118 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003119 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003120#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003121 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3122 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003123
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003124 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003125 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3126 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
3127
3128 ssl->state++;
3129
3130 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3131 {
3132 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3133 return( ret );
3134 }
3135
3136 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
3137
3138 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003139}
3140
3141static int ssl_write_server_hello_done( ssl_context *ssl )
3142{
3143 int ret;
3144
3145 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
3146
3147 ssl->out_msglen = 4;
3148 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3149 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
3150
3151 ssl->state++;
3152
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003153#if defined(POLARSSL_SSL_PROTO_DTLS)
3154 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3155 ssl_send_flight_completed( ssl );
3156#endif
3157
Paul Bakker5121ce52009-01-03 21:22:43 +00003158 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3159 {
3160 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3161 return( ret );
3162 }
3163
3164 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
3165
3166 return( 0 );
3167}
3168
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003169#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3170 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3171static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
3172 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003173{
3174 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003175 size_t n;
3176
3177 /*
3178 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3179 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003180 if( *p + 2 > end )
3181 {
3182 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3183 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3184 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003185
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003186 n = ( (*p)[0] << 8 ) | (*p)[1];
3187 *p += 2;
3188
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003189 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003190 {
3191 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3192 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3193 }
3194
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003195 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003196 {
3197 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
3198 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3199 }
3200
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003201 *p += n;
3202
Paul Bakker70df2fb2013-04-17 17:19:09 +02003203 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
3204
Paul Bakker70df2fb2013-04-17 17:19:09 +02003205 return( ret );
3206}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003207#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3208 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003209
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003210#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
3211 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003212static int ssl_parse_encrypted_pms( ssl_context *ssl,
3213 const unsigned char *p,
3214 const unsigned char *end,
3215 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003216{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003217 int ret;
3218 size_t len = pk_get_len( ssl_own_key( ssl ) );
3219 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003220 unsigned char ver[2];
Paul Bakker70df2fb2013-04-17 17:19:09 +02003221
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003222 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003223 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02003224 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003225 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3226 }
3227
3228 /*
3229 * Decrypt the premaster using own private RSA key
3230 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003231#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3232 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02003233 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
3234 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003235 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
3236 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003237 {
3238 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3239 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3240 }
3241 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003242#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003243
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003244 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003245 {
3246 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3247 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3248 }
3249
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003250 ssl_write_version( ssl->handshake->max_major_ver,
3251 ssl->handshake->max_minor_ver,
3252 ssl->transport, ver );
3253
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003254 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
3255 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003256 sizeof( ssl->handshake->premaster ) - pms_offset,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003257 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003258
3259 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003260 pms[0] != ver[0] ||
3261 pms[1] != ver[1] )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003262 {
3263 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3264
3265 /*
3266 * Protection against Bleichenbacher's attack:
3267 * invalid PKCS#1 v1.5 padding must not cause
3268 * the connection to end immediately; instead,
3269 * send a bad_record_mac later in the handshake.
3270 */
3271 ssl->handshake->pmslen = 48;
3272
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003273 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003274 if( ret != 0 )
3275 return( ret );
3276 }
3277
3278 return( ret );
3279}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003280#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
3281 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003282
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003283#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003284static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
3285 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003286{
Paul Bakker6db455e2013-09-18 17:29:31 +02003287 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003288 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003289
Paul Bakker6db455e2013-09-18 17:29:31 +02003290 if( ssl->f_psk == NULL &&
3291 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
3292 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003293 {
3294 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
3295 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3296 }
3297
3298 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003299 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003300 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003301 if( *p + 2 > end )
3302 {
3303 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3304 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3305 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003306
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003307 n = ( (*p)[0] << 8 ) | (*p)[1];
3308 *p += 2;
3309
3310 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003311 {
3312 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3313 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3314 }
3315
Paul Bakker6db455e2013-09-18 17:29:31 +02003316 if( ssl->f_psk != NULL )
3317 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003318 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003319 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3320 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003321 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003322 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003323 /* Identity is not a big secret since clients send it in the clear,
3324 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02003325 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003326 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003327 {
3328 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3329 }
3330 }
3331
3332 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003333 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003334 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02003335 if( ( ret = ssl_send_alert_message( ssl,
3336 SSL_ALERT_LEVEL_FATAL,
3337 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
3338 {
3339 return( ret );
3340 }
3341
3342 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003343 }
3344
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003345 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003346
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003347 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003348}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003349#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003350
Paul Bakker5121ce52009-01-03 21:22:43 +00003351static int ssl_parse_client_key_exchange( ssl_context *ssl )
3352{
Paul Bakker23986e52011-04-24 08:57:21 +00003353 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003354 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003355 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003356
Paul Bakker41c83d32013-03-20 14:39:14 +01003357 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003358
3359 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
3360
3361 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3362 {
3363 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3364 return( ret );
3365 }
3366
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003367 p = ssl->in_msg + ssl_hs_hdr_len( ssl );
3368 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00003369
Paul Bakker5121ce52009-01-03 21:22:43 +00003370 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3371 {
3372 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003373 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003374 }
3375
3376 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
3377 {
3378 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003379 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003380 }
3381
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003382#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01003383 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003384 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003385 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003386 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02003387 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3388 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003389 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003390
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003391 if( p != end )
3392 {
3393 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3394 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3395 }
3396
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02003397 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003398
3399 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
3400 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003401 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02003402 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003403 {
3404 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
3405 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3406 }
3407
3408 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003409 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003410 else
3411#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003412#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003413 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3414 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3415 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003416 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003417 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
3418 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
3419 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003420 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003421 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003422 p, end - p) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003423 {
3424 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3425 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3426 }
3427
3428 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3429
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003430 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
3431 &ssl->handshake->pmslen,
3432 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02003433 POLARSSL_MPI_MAX_SIZE,
3434 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003435 {
3436 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
3437 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3438 }
3439
3440 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003441 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003442 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003443#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003444 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3445 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3446 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003447#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
3448 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003449 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003450 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003451 {
3452 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3453 return( ret );
3454 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003455
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003456 if( p != end )
3457 {
3458 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3459 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3460 }
3461
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003462 if( ( ret = ssl_psk_derive_premaster( ssl,
3463 ciphersuite_info->key_exchange ) ) != 0 )
3464 {
3465 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3466 return( ret );
3467 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003468 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003469 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003470#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003471#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
3472 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
3473 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003474 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3475 {
3476 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3477 return( ret );
3478 }
3479
3480 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3481 {
3482 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
3483 return( ret );
3484 }
3485
3486 if( ( ret = ssl_psk_derive_premaster( ssl,
3487 ciphersuite_info->key_exchange ) ) != 0 )
3488 {
3489 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3490 return( ret );
3491 }
3492 }
3493 else
3494#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003495#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3496 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3497 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003498 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3499 {
3500 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3501 return( ret );
3502 }
3503 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3504 {
3505 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3506 return( ret );
3507 }
3508
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003509 if( p != end )
3510 {
3511 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3512 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3513 }
3514
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003515 if( ( ret = ssl_psk_derive_premaster( ssl,
3516 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003517 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003518 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3519 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003520 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003521 }
3522 else
3523#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003524#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3525 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
3526 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003527 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3528 {
3529 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3530 return( ret );
3531 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003532
3533 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
3534 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003535 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003536 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3537 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003538 }
3539
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003540 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3541
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003542 if( ( ret = ssl_psk_derive_premaster( ssl,
3543 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003544 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003545 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003546 return( ret );
3547 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003548 }
3549 else
3550#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003551#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
3552 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003553 {
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003554 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003555 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003556 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003557 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003558 }
3559 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003560 else
3561#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
3562 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003563 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003564 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003565 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003566
Paul Bakkerff60ee62010-03-16 21:09:09 +00003567 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
3568 {
3569 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
3570 return( ret );
3571 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003572
Paul Bakker5121ce52009-01-03 21:22:43 +00003573 ssl->state++;
3574
3575 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3576
3577 return( 0 );
3578}
3579
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003580#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3581 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003582 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3583 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003584static int ssl_parse_certificate_verify( ssl_context *ssl )
3585{
Paul Bakkerfbb17802013-04-17 19:10:21 +02003586 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003587
3588 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3589
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003590 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003591 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003592 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003593 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02003594 {
3595 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3596 ssl->state++;
3597 return( 0 );
3598 }
3599
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003600 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3601 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003602}
3603#else
3604static int ssl_parse_certificate_verify( ssl_context *ssl )
3605{
3606 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003607 size_t i, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003608 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003609 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003610 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003611#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003612 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003613#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003614 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003615 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3616
3617 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3618
3619 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003620 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003621 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003622 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3623 ssl->session_negotiate->peer_cert == NULL )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003624 {
3625 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3626 ssl->state++;
3627 return( 0 );
3628 }
3629
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003630 /* Needs to be done before read_record() to exclude current message */
Paul Bakker48916f92012-09-16 19:57:18 +00003631 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003632
3633 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3634 {
3635 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3636 return( ret );
3637 }
3638
3639 ssl->state++;
3640
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003641 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE ||
3642 ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003643 {
3644 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003645 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003646 }
3647
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003648 i = ssl_hs_hdr_len( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003649
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003650 /*
3651 * struct {
3652 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
3653 * opaque signature<0..2^16-1>;
3654 * } DigitallySigned;
3655 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003656#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3657 defined(POLARSSL_SSL_PROTO_TLS1_1)
3658 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003659 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003660 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003661 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003662
3663 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3664 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3665 POLARSSL_PK_ECDSA ) )
3666 {
3667 hash_start += 16;
3668 hashlen -= 16;
3669 md_alg = POLARSSL_MD_SHA1;
3670 }
Paul Bakker926af752012-11-23 13:38:07 +01003671 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003672 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003673#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3674 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003675#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3676 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003677 {
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00003678 if( i + 2 > ssl->in_hslen )
3679 {
3680 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3681 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3682 }
3683
Paul Bakker5121ce52009-01-03 21:22:43 +00003684 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003685 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003686 */
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003687 if( ssl->in_msg[i] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003688 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003689 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3690 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003691 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3692 }
3693
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003694 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003695
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003696 /* Info from md_alg will be used instead */
3697 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003698
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003699 i++;
3700
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003701 /*
3702 * Signature
3703 */
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003704 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003705 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003706 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003707 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3708 " for verify message" ) );
3709 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003710 }
3711
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003712 /*
3713 * Check the certificate's key type matches the signature alg
3714 */
3715 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3716 {
3717 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3718 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3719 }
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003720
3721 i++;
Paul Bakker577e0062013-08-28 11:57:20 +02003722 }
3723 else
3724#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3725 {
3726 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003727 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003728 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003729
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00003730 if( i + 2 > ssl->in_hslen )
3731 {
3732 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3733 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3734 }
3735
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003736 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
3737 i += 2;
Paul Bakker926af752012-11-23 13:38:07 +01003738
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003739 if( i + sig_len != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003740 {
3741 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003742 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003743 }
3744
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003745 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003746 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003747 ssl->in_msg + i, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003748 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003749 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003750 return( ret );
3751 }
3752
3753 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3754
Paul Bakkered27a042013-04-18 22:46:23 +02003755 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003756}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003757#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3758 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3759 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003760
Paul Bakkera503a632013-08-14 13:48:06 +02003761#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003762static int ssl_write_new_session_ticket( ssl_context *ssl )
3763{
3764 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003765 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003766 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003767
3768 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3769
3770 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3771 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3772
3773 /*
3774 * struct {
3775 * uint32 ticket_lifetime_hint;
3776 * opaque ticket<0..2^16-1>;
3777 * } NewSessionTicket;
3778 *
3779 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3780 * 8 . 9 ticket_len (n)
3781 * 10 . 9+n ticket content
3782 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003783
3784 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3785 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3786 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3787 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003788
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003789 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3790 {
3791 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3792 tlen = 0;
3793 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003794
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003795 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3796 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003797
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003798 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003799
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003800 /*
3801 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3802 * ChangeCipherSpec share the same state.
3803 */
3804 ssl->handshake->new_session_ticket = 0;
3805
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003806 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3807 {
3808 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3809 return( ret );
3810 }
3811
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003812 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3813
3814 return( 0 );
3815}
Paul Bakkera503a632013-08-14 13:48:06 +02003816#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003817
Paul Bakker5121ce52009-01-03 21:22:43 +00003818/*
Paul Bakker1961b702013-01-25 14:49:24 +01003819 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003820 */
Paul Bakker1961b702013-01-25 14:49:24 +01003821int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003822{
3823 int ret = 0;
3824
Paul Bakker1961b702013-01-25 14:49:24 +01003825 if( ssl->state == SSL_HANDSHAKE_OVER )
3826 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003827
Paul Bakker1961b702013-01-25 14:49:24 +01003828 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3829
3830 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3831 return( ret );
3832
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003833#if defined(POLARSSL_SSL_PROTO_DTLS)
3834 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3835 ssl->handshake != NULL &&
3836 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
3837 {
3838 if( ( ret = ssl_resend( ssl ) ) != 0 )
3839 return( ret );
3840 }
3841#endif
3842
Paul Bakker1961b702013-01-25 14:49:24 +01003843 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003844 {
Paul Bakker1961b702013-01-25 14:49:24 +01003845 case SSL_HELLO_REQUEST:
3846 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003847 break;
3848
Paul Bakker1961b702013-01-25 14:49:24 +01003849 /*
3850 * <== ClientHello
3851 */
3852 case SSL_CLIENT_HELLO:
3853 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003854 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003855
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02003856#if defined(POLARSSL_SSL_PROTO_DTLS)
3857 case SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
3858 return( POLARSSL_ERR_SSL_HELLO_VERIFY_REQUIRED );
3859#endif
3860
Paul Bakker1961b702013-01-25 14:49:24 +01003861 /*
3862 * ==> ServerHello
3863 * Certificate
3864 * ( ServerKeyExchange )
3865 * ( CertificateRequest )
3866 * ServerHelloDone
3867 */
3868 case SSL_SERVER_HELLO:
3869 ret = ssl_write_server_hello( ssl );
3870 break;
3871
3872 case SSL_SERVER_CERTIFICATE:
3873 ret = ssl_write_certificate( ssl );
3874 break;
3875
3876 case SSL_SERVER_KEY_EXCHANGE:
3877 ret = ssl_write_server_key_exchange( ssl );
3878 break;
3879
3880 case SSL_CERTIFICATE_REQUEST:
3881 ret = ssl_write_certificate_request( ssl );
3882 break;
3883
3884 case SSL_SERVER_HELLO_DONE:
3885 ret = ssl_write_server_hello_done( ssl );
3886 break;
3887
3888 /*
3889 * <== ( Certificate/Alert )
3890 * ClientKeyExchange
3891 * ( CertificateVerify )
3892 * ChangeCipherSpec
3893 * Finished
3894 */
3895 case SSL_CLIENT_CERTIFICATE:
3896 ret = ssl_parse_certificate( ssl );
3897 break;
3898
3899 case SSL_CLIENT_KEY_EXCHANGE:
3900 ret = ssl_parse_client_key_exchange( ssl );
3901 break;
3902
3903 case SSL_CERTIFICATE_VERIFY:
3904 ret = ssl_parse_certificate_verify( ssl );
3905 break;
3906
3907 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3908 ret = ssl_parse_change_cipher_spec( ssl );
3909 break;
3910
3911 case SSL_CLIENT_FINISHED:
3912 ret = ssl_parse_finished( ssl );
3913 break;
3914
3915 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003916 * ==> ( NewSessionTicket )
3917 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003918 * Finished
3919 */
3920 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003921#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003922 if( ssl->handshake->new_session_ticket != 0 )
3923 ret = ssl_write_new_session_ticket( ssl );
3924 else
Paul Bakkera503a632013-08-14 13:48:06 +02003925#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003926 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003927 break;
3928
3929 case SSL_SERVER_FINISHED:
3930 ret = ssl_write_finished( ssl );
3931 break;
3932
3933 case SSL_FLUSH_BUFFERS:
3934 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3935 ssl->state = SSL_HANDSHAKE_WRAPUP;
3936 break;
3937
3938 case SSL_HANDSHAKE_WRAPUP:
3939 ssl_handshake_wrapup( ssl );
3940 break;
3941
3942 default:
3943 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3944 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003945 }
3946
Paul Bakker5121ce52009-01-03 21:22:43 +00003947 return( ret );
3948}
Paul Bakker9af723c2014-05-01 13:03:14 +02003949#endif /* POLARSSL_SSL_SRV_C */