blob: 669523f4d5319e779ca02ba8404c60a6da3ec35f [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é-Gonnard967a2a52015-01-22 14:28:16 +00006 * This file is part of mbed TLS (http://www.polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +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,
Paul Bakker66d5d072014-06-17 16:39:18 +0200212 SSL_MAX_CONTENT_LEN - ( state - ssl->out_ctr ) - 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
Paul Bakker0be444a2013-08-27 21:55:01 +0200351#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200352/*
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200353 * Wrapper around f_sni, allowing use of ssl_set_own_cert() but
354 * making it act on ssl->hanshake->sni_key_cert instead.
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200355 */
356static int ssl_sni_wrapper( ssl_context *ssl,
357 const unsigned char* name, size_t len )
358{
359 int ret;
360 ssl_key_cert *key_cert_ori = ssl->key_cert;
361
362 ssl->key_cert = NULL;
363 ret = ssl->f_sni( ssl->p_sni, ssl, name, len );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200364 ssl->handshake->sni_key_cert = ssl->key_cert;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200365
366 ssl->key_cert = key_cert_ori;
367
368 return( ret );
369}
370
Paul Bakker5701cdc2012-09-27 21:49:42 +0000371static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000372 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000373 size_t len )
374{
375 int ret;
376 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000377 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000378
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100379 SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
380
Paul Bakker5701cdc2012-09-27 21:49:42 +0000381 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
382 if( servername_list_size + 2 != len )
383 {
384 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
385 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
386 }
387
388 p = buf + 2;
389 while( servername_list_size > 0 )
390 {
391 hostname_len = ( ( p[1] << 8 ) | p[2] );
392 if( hostname_len + 3 > servername_list_size )
393 {
394 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
395 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
396 }
397
398 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
399 {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200400 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000401 if( ret != 0 )
402 {
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100403 SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000404 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
405 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
406 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
407 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000408 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000409 }
410
411 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000412 p += hostname_len + 3;
413 }
414
415 if( servername_list_size != 0 )
416 {
417 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
418 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000419 }
420
421 return( 0 );
422}
Paul Bakker0be444a2013-08-27 21:55:01 +0200423#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000424
Paul Bakker48916f92012-09-16 19:57:18 +0000425static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000426 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000427 size_t len )
428{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000429 int ret;
430
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100431#if defined(POLARSSL_SSL_RENEGOTIATION)
432 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
433 {
434 /* Check verify-data in constant-time. The length OTOH is no secret */
435 if( len != 1 + ssl->verify_data_len ||
436 buf[0] != ssl->verify_data_len ||
437 safer_memcmp( buf + 1, ssl->peer_verify_data,
438 ssl->verify_data_len ) != 0 )
439 {
440 SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
441
442 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
443 return( ret );
444
445 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
446 }
447 }
448 else
449#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +0000450 {
451 if( len != 1 || buf[0] != 0x0 )
452 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100453 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000454
455 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
456 return( ret );
457
Paul Bakker48916f92012-09-16 19:57:18 +0000458 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
459 }
460
461 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
462 }
Paul Bakker48916f92012-09-16 19:57:18 +0000463
464 return( 0 );
465}
466
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100467#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
468 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakker23f36802012-09-28 14:15:14 +0000469static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
470 const unsigned char *buf,
471 size_t len )
472{
473 size_t sig_alg_list_size;
474 const unsigned char *p;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200475 const unsigned char *end = buf + len;
476 const int *md_cur;
477
Paul Bakker23f36802012-09-28 14:15:14 +0000478
479 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
480 if( sig_alg_list_size + 2 != len ||
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200481 sig_alg_list_size % 2 != 0 )
Paul Bakker23f36802012-09-28 14:15:14 +0000482 {
483 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
484 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
485 }
486
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200487 /*
488 * For now, ignore the SignatureAlgorithm part and rely on offered
489 * ciphersuites only for that part. To be fixed later.
490 *
491 * So, just look at the HashAlgorithm part.
492 */
493 for( md_cur = md_list(); *md_cur != POLARSSL_MD_NONE; md_cur++ ) {
494 for( p = buf + 2; p < end; p += 2 ) {
495 if( *md_cur == (int) ssl_md_alg_from_hash( p[0] ) ) {
496 ssl->handshake->sig_alg = p[0];
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200497 goto have_sig_alg;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200498 }
Paul Bakker23f36802012-09-28 14:15:14 +0000499 }
Paul Bakker23f36802012-09-28 14:15:14 +0000500 }
501
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200502 /* Some key echanges do not need signatures at all */
503 SSL_DEBUG_MSG( 3, ( "no signature_algorithm in common" ) );
504 return( 0 );
505
506have_sig_alg:
Paul Bakker23f36802012-09-28 14:15:14 +0000507 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
508 ssl->handshake->sig_alg ) );
509
510 return( 0 );
511}
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100512#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
513 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker23f36802012-09-28 14:15:14 +0000514
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200515#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200516static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
517 const unsigned char *buf,
518 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100519{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200520 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100521 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200522 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100523
524 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
525 if( list_size + 2 != len ||
526 list_size % 2 != 0 )
527 {
528 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
529 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
530 }
531
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200532 /* Should never happen unless client duplicates the extension */
533 if( ssl->handshake->curves != NULL )
534 {
535 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
536 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
537 }
538
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +0100539 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200540 * and leave room for a final 0 */
541 our_size = list_size / 2 + 1;
542 if( our_size > POLARSSL_ECP_DP_MAX )
543 our_size = POLARSSL_ECP_DP_MAX;
544
545 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
546 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
547
Paul Bakker9af723c2014-05-01 13:03:14 +0200548 /* explicit void pointer cast for buggy MS compiler */
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200549 memset( (void *) curves, 0, our_size * sizeof( *curves ) );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200550 ssl->handshake->curves = curves;
551
Paul Bakker41c83d32013-03-20 14:39:14 +0100552 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200553 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100554 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200555 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200556
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200557 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100558 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200559 *curves++ = curve_info;
560 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100561 }
562
563 list_size -= 2;
564 p += 2;
565 }
566
567 return( 0 );
568}
569
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200570static int ssl_parse_supported_point_formats( ssl_context *ssl,
571 const unsigned char *buf,
572 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100573{
574 size_t list_size;
575 const unsigned char *p;
576
577 list_size = buf[0];
578 if( list_size + 1 != len )
579 {
580 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
581 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
582 }
583
584 p = buf + 2;
585 while( list_size > 0 )
586 {
587 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
588 p[0] == POLARSSL_ECP_PF_COMPRESSED )
589 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200590 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200591 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100592 return( 0 );
593 }
594
595 list_size--;
596 p++;
597 }
598
599 return( 0 );
600}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200601#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100602
Paul Bakker05decb22013-08-15 13:33:48 +0200603#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200604static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
605 const unsigned char *buf,
606 size_t len )
607{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200608 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200609 {
610 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
611 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
612 }
613
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200614 ssl->session_negotiate->mfl_code = buf[0];
615
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200616 return( 0 );
617}
Paul Bakker05decb22013-08-15 13:33:48 +0200618#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200619
Paul Bakker1f2bc622013-08-15 13:45:55 +0200620#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200621static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
622 const unsigned char *buf,
623 size_t len )
624{
625 if( len != 0 )
626 {
627 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
628 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
629 }
630
631 ((void) buf);
632
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100633 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
634 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200635
636 return( 0 );
637}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200638#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200639
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100640#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
641static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
642 const unsigned char *buf,
643 size_t len )
644{
645 if( len != 0 )
646 {
647 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
648 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
649 }
650
651 ((void) buf);
652
653 if( ssl->encrypt_then_mac == SSL_ETM_ENABLED &&
654 ssl->minor_ver != SSL_MINOR_VERSION_0 )
655 {
656 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
657 }
658
659 return( 0 );
660}
661#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
662
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200663#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
664static int ssl_parse_extended_ms_ext( ssl_context *ssl,
665 const unsigned char *buf,
666 size_t len )
667{
668 if( len != 0 )
669 {
670 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
671 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
672 }
673
674 ((void) buf);
675
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200676 if( ssl->extended_ms == SSL_EXTENDED_MS_ENABLED &&
677 ssl->minor_ver != SSL_MINOR_VERSION_0 )
678 {
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200679 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200680 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200681
682 return( 0 );
683}
684#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
685
Paul Bakkera503a632013-08-14 13:48:06 +0200686#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200687static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200688 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200689 size_t len )
690{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200691 int ret;
692
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200693 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
694 return( 0 );
695
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200696 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200697 ssl->handshake->new_session_ticket = 1;
698
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200699 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
700
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200701 if( len == 0 )
702 return( 0 );
703
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100704#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200705 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
706 {
707 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
708 return( 0 );
709 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100710#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200711
712 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200713 * Failures are ok: just ignore the ticket and proceed.
714 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200715 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
716 {
717 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200718 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200719 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200720
721 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
722
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200723 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200724
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200725 /* Don't send a new ticket after all, this one is OK */
726 ssl->handshake->new_session_ticket = 0;
727
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200728 return( 0 );
729}
Paul Bakkera503a632013-08-14 13:48:06 +0200730#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200731
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200732#if defined(POLARSSL_SSL_ALPN)
733static int ssl_parse_alpn_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200734 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200735{
Paul Bakker14b16c62014-05-28 11:33:54 +0200736 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200737 const unsigned char *theirs, *start, *end;
738 const char **ours;
739
740 /* If ALPN not configured, just ignore the extension */
741 if( ssl->alpn_list == NULL )
742 return( 0 );
743
744 /*
745 * opaque ProtocolName<1..2^8-1>;
746 *
747 * struct {
748 * ProtocolName protocol_name_list<2..2^16-1>
749 * } ProtocolNameList;
750 */
751
752 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
753 if( len < 4 )
754 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
755
756 list_len = ( buf[0] << 8 ) | buf[1];
757 if( list_len != len - 2 )
758 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
759
760 /*
761 * Use our order of preference
762 */
763 start = buf + 2;
764 end = buf + len;
765 for( ours = ssl->alpn_list; *ours != NULL; ours++ )
766 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200767 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200768 for( theirs = start; theirs != end; theirs += cur_len )
769 {
770 /* If the list is well formed, we should get equality first */
771 if( theirs > end )
772 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
773
774 cur_len = *theirs++;
775
776 /* Empty strings MUST NOT be included */
777 if( cur_len == 0 )
778 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
779
Paul Bakker14b16c62014-05-28 11:33:54 +0200780 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200781 memcmp( theirs, *ours, cur_len ) == 0 )
782 {
783 ssl->alpn_chosen = *ours;
784 return( 0 );
785 }
786 }
787 }
788
789 /* If we get there, no match was found */
790 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
791 SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
792 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
793}
794#endif /* POLARSSL_SSL_ALPN */
795
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100796/*
797 * Auxiliary functions for ServerHello parsing and related actions
798 */
799
800#if defined(POLARSSL_X509_CRT_PARSE_C)
801/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100802 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100803 */
804#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100805static int ssl_check_key_curve( pk_context *pk,
806 const ecp_curve_info **curves )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100807{
808 const ecp_curve_info **crv = curves;
809 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
810
811 while( *crv != NULL )
812 {
813 if( (*crv)->grp_id == grp_id )
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100814 return( 0 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100815 crv++;
816 }
817
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100818 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100819}
820#endif /* POLARSSL_ECDSA_C */
821
822/*
823 * Try picking a certificate for this ciphersuite,
824 * return 0 on success and -1 on failure.
825 */
826static int ssl_pick_cert( ssl_context *ssl,
827 const ssl_ciphersuite_t * ciphersuite_info )
828{
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100829 ssl_key_cert *cur, *list, *fallback = NULL;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100830 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
831
832#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
833 if( ssl->handshake->sni_key_cert != NULL )
834 list = ssl->handshake->sni_key_cert;
835 else
836#endif
837 list = ssl->handshake->key_cert;
838
839 if( pk_alg == POLARSSL_PK_NONE )
840 return( 0 );
841
842 for( cur = list; cur != NULL; cur = cur->next )
843 {
844 if( ! pk_can_do( cur->key, pk_alg ) )
845 continue;
846
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200847 /*
848 * This avoids sending the client a cert it'll reject based on
849 * keyUsage or other extensions.
850 *
851 * It also allows the user to provision different certificates for
852 * different uses based on keyUsage, eg if they want to avoid signing
853 * and decrypting with the same RSA key.
854 */
855 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
856 SSL_IS_SERVER ) != 0 )
857 {
858 continue;
859 }
860
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100861#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100862 if( pk_alg == POLARSSL_PK_ECDSA &&
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100863 ssl_check_key_curve( cur->key, ssl->handshake->curves ) != 0 )
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100864 continue;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100865#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100866
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100867 /*
868 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
869 * present them a SHA-higher cert rather than failing if it's the only
870 * one we got that satisfies the other conditions.
871 */
872 if( ssl->minor_ver < SSL_MINOR_VERSION_3 &&
873 cur->cert->sig_md != POLARSSL_MD_SHA1 )
874 {
875 if( fallback == NULL )
876 fallback = cur;
877 continue;
878 }
879
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100880 /* If we get there, we got a winner */
881 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100882 }
883
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100884 if( cur != NULL )
885 {
886 ssl->handshake->key_cert = cur;
887 return( 0 );
888 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100889
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100890 if( fallback != NULL )
891 {
892 ssl->handshake->key_cert = fallback;
893 return( 0 );
894 }
895
896 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100897}
898#endif /* POLARSSL_X509_CRT_PARSE_C */
899
900/*
901 * Check if a given ciphersuite is suitable for use with our config/keys/etc
902 * Sets ciphersuite_info only if the suite matches.
903 */
904static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
905 const ssl_ciphersuite_t **ciphersuite_info )
906{
907 const ssl_ciphersuite_t *suite_info;
908
909 suite_info = ssl_ciphersuite_from_id( suite_id );
910 if( suite_info == NULL )
911 {
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100912 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
913 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100914 }
915
916 if( suite_info->min_minor_ver > ssl->minor_ver ||
917 suite_info->max_minor_ver < ssl->minor_ver )
918 return( 0 );
919
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100920 if( ssl->arc4_disabled == SSL_ARC4_DISABLED &&
921 suite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
922 return( 0 );
923
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100924#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
925 if( ssl_ciphersuite_uses_ec( suite_info ) &&
926 ( ssl->handshake->curves == NULL ||
927 ssl->handshake->curves[0] == NULL ) )
928 return( 0 );
929#endif
930
931#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
932 /* If the ciphersuite requires a pre-shared key and we don't
933 * have one, skip it now rather than failing later */
934 if( ssl_ciphersuite_uses_psk( suite_info ) &&
935 ssl->f_psk == NULL &&
936 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
937 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
938 return( 0 );
939#endif
940
941#if defined(POLARSSL_X509_CRT_PARSE_C)
942 /*
943 * Final check: if ciphersuite requires us to have a
944 * certificate/key of a particular type:
945 * - select the appropriate certificate if we have one, or
946 * - try the next ciphersuite if we don't
947 * This must be done last since we modify the key_cert list.
948 */
949 if( ssl_pick_cert( ssl, suite_info ) != 0 )
950 return( 0 );
951#endif
952
953 *ciphersuite_info = suite_info;
954 return( 0 );
955}
956
Paul Bakker78a8c712013-03-06 17:01:52 +0100957#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
958static int ssl_parse_client_hello_v2( ssl_context *ssl )
959{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +0100960 int ret, got_common_suite;
Paul Bakker78a8c712013-03-06 17:01:52 +0100961 unsigned int i, j;
962 size_t n;
963 unsigned int ciph_len, sess_len, chal_len;
964 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200965 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200966 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100967
968 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
969
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100970#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker78a8c712013-03-06 17:01:52 +0100971 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
972 {
973 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
974
975 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
976 return( ret );
977
978 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
979 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100980#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +0100981
982 buf = ssl->in_hdr;
983
984 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
985
986 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
987 buf[2] ) );
988 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
989 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
990 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
991 buf[3], buf[4] ) );
992
993 /*
994 * SSLv2 Client Hello
995 *
996 * Record layer:
997 * 0 . 1 message length
998 *
999 * SSL layer:
1000 * 2 . 2 message type
1001 * 3 . 4 protocol version
1002 */
1003 if( buf[2] != SSL_HS_CLIENT_HELLO ||
1004 buf[3] != SSL_MAJOR_VERSION_3 )
1005 {
1006 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1007 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1008 }
1009
1010 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
1011
1012 if( n < 17 || n > 512 )
1013 {
1014 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1015 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1016 }
1017
1018 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +02001019 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
1020 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +01001021
1022 if( ssl->minor_ver < ssl->min_minor_ver )
1023 {
1024 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001025 " [%d:%d] < [%d:%d]",
1026 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +01001027 ssl->min_major_ver, ssl->min_minor_ver ) );
1028
1029 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1030 SSL_ALERT_MSG_PROTOCOL_VERSION );
1031 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1032 }
1033
Paul Bakker2fbefde2013-06-29 16:01:15 +02001034 ssl->handshake->max_major_ver = buf[3];
1035 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +01001036
1037 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
1038 {
1039 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1040 return( ret );
1041 }
1042
1043 ssl->handshake->update_checksum( ssl, buf + 2, n );
1044
1045 buf = ssl->in_msg;
1046 n = ssl->in_left - 5;
1047
1048 /*
1049 * 0 . 1 ciphersuitelist length
1050 * 2 . 3 session id length
1051 * 4 . 5 challenge length
1052 * 6 . .. ciphersuitelist
1053 * .. . .. session id
1054 * .. . .. challenge
1055 */
1056 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1057
1058 ciph_len = ( buf[0] << 8 ) | buf[1];
1059 sess_len = ( buf[2] << 8 ) | buf[3];
1060 chal_len = ( buf[4] << 8 ) | buf[5];
1061
1062 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
1063 ciph_len, sess_len, chal_len ) );
1064
1065 /*
1066 * Make sure each parameter length is valid
1067 */
1068 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1069 {
1070 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1071 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1072 }
1073
1074 if( sess_len > 32 )
1075 {
1076 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1077 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1078 }
1079
1080 if( chal_len < 8 || chal_len > 32 )
1081 {
1082 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1083 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1084 }
1085
1086 if( n != 6 + ciph_len + sess_len + chal_len )
1087 {
1088 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1089 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1090 }
1091
1092 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1093 buf + 6, ciph_len );
1094 SSL_DEBUG_BUF( 3, "client hello, session id",
1095 buf + 6 + ciph_len, sess_len );
1096 SSL_DEBUG_BUF( 3, "client hello, challenge",
1097 buf + 6 + ciph_len + sess_len, chal_len );
1098
1099 p = buf + 6 + ciph_len;
1100 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001101 memset( ssl->session_negotiate->id, 0,
1102 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001103 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1104
1105 p += sess_len;
1106 memset( ssl->handshake->randbytes, 0, 64 );
1107 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1108
1109 /*
1110 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1111 */
1112 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1113 {
1114 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1115 {
1116 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001117#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker78a8c712013-03-06 17:01:52 +01001118 if( ssl->renegotiation == SSL_RENEGOTIATION )
1119 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001120 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
1121 "during renegotiation" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001122
1123 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1124 return( ret );
1125
1126 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1127 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001128#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001129 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1130 break;
1131 }
1132 }
1133
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001134#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1135 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1136 {
1137 if( p[0] == 0 &&
1138 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1139 p[2] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1140 {
1141 SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
1142
1143 if( ssl->minor_ver < ssl->max_minor_ver )
1144 {
1145 SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
1146
1147 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1148 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1149
1150 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1151 }
1152
1153 break;
1154 }
1155 }
1156#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1157
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001158 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001159 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001160 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001161#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1162 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1163 {
1164 for( i = 0; ciphersuites[i] != 0; i++ )
1165#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001166 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001167 {
1168 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001169#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001170 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001171 if( p[0] != 0 ||
1172 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1173 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1174 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001175
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001176 got_common_suite = 1;
1177
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001178 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1179 &ciphersuite_info ) ) != 0 )
1180 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001181
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001182 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001183 goto have_ciphersuite_v2;
1184 }
1185 }
1186
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001187 if( got_common_suite )
1188 {
1189 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1190 "but none of them usable" ) );
1191 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
1192 }
1193 else
1194 {
1195 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1196 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1197 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001198
1199have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001200 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001201 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001202 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001203
1204 /*
1205 * SSLv2 Client Hello relevant renegotiation security checks
1206 */
1207 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1208 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1209 {
1210 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1211
1212 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1213 return( ret );
1214
1215 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1216 }
1217
1218 ssl->in_left = 0;
1219 ssl->state++;
1220
1221 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1222
1223 return( 0 );
1224}
1225#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1226
Paul Bakker5121ce52009-01-03 21:22:43 +00001227static int ssl_parse_client_hello( ssl_context *ssl )
1228{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001229 int ret, got_common_suite;
Paul Bakker23986e52011-04-24 08:57:21 +00001230 unsigned int i, j;
1231 size_t n;
1232 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001233 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001234 unsigned int ext_len = 0;
1235 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001236#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001237 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001238#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001239 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001240 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001241 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001242
1243 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1244
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001245#if defined(POLARSSL_SSL_RENEGOTIATION)
1246 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1247#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001248 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001249 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
1250 {
1251 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1252 return( ret );
1253 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001254 }
1255
1256 buf = ssl->in_hdr;
1257
Paul Bakker78a8c712013-03-06 17:01:52 +01001258#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1259 if( ( buf[0] & 0x80 ) != 0 )
1260 return ssl_parse_client_hello_v2( ssl );
1261#endif
1262
Paul Bakkerec636f32012-09-09 19:17:02 +00001263 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1264
1265 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1266 buf[0] ) );
1267 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1268 ( buf[3] << 8 ) | buf[4] ) );
1269 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
1270 buf[1], buf[2] ) );
1271
1272 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001273 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001274 *
1275 * Record layer:
1276 * 0 . 0 message type
1277 * 1 . 2 protocol version
1278 * 3 . 4 message length
1279 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001280
1281 /* According to RFC 5246 Appendix E.1, the version here is typically
1282 * "{03,00}, the lowest version number supported by the client, [or] the
1283 * value of ClientHello.client_version", so the only meaningful check here
1284 * is the major version shouldn't be less than 3 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001285 if( buf[0] != SSL_MSG_HANDSHAKE ||
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001286 buf[1] < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001287 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001288 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1289 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1290 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001291
Paul Bakkerec636f32012-09-09 19:17:02 +00001292 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +00001293
Paul Bakker4f42c112014-04-17 14:48:23 +02001294 if( n < 45 || n > SSL_MAX_CONTENT_LEN )
Paul Bakkerec636f32012-09-09 19:17:02 +00001295 {
1296 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1297 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1298 }
1299
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001300#if defined(POLARSSL_SSL_RENEGOTIATION)
1301 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1302#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001303 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001304 if( ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
1305 {
1306 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1307 return( ret );
1308 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001309 }
1310
1311 buf = ssl->in_msg;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001312#if defined(POLARSSL_SSL_RENEGOTIATION)
1313 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001314 n = ssl->in_msglen;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001315 else
1316#endif
1317 n = ssl->in_left - 5;
Paul Bakkerec636f32012-09-09 19:17:02 +00001318
Paul Bakker48916f92012-09-16 19:57:18 +00001319 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +00001320
1321 /*
1322 * SSL layer:
1323 * 0 . 0 handshake type
1324 * 1 . 3 handshake length
1325 * 4 . 5 protocol version
1326 * 6 . 9 UNIX time()
1327 * 10 . 37 random bytes
1328 * 38 . 38 session id length
1329 * 39 . 38+x session id
1330 * 39+x . 40+x ciphersuitelist length
Paul Bakker0f651c72014-05-22 15:12:19 +02001331 * 41+x . 40+y ciphersuitelist
1332 * 41+y . 41+y compression alg length
1333 * 42+y . 41+z compression algs
Paul Bakkerec636f32012-09-09 19:17:02 +00001334 * .. . .. extensions
1335 */
1336 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1337
1338 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1339 buf[0] ) );
1340 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1341 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1342 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1343 buf[4], buf[5] ) );
1344
1345 /*
1346 * Check the handshake type and protocol version
1347 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001348 if( buf[0] != SSL_HS_CLIENT_HELLO )
Paul Bakkerec636f32012-09-09 19:17:02 +00001349 {
1350 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1351 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1352 }
1353
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001354 ssl->major_ver = buf[4];
1355 ssl->minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001356
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001357 ssl->handshake->max_major_ver = ssl->major_ver;
1358 ssl->handshake->max_minor_ver = ssl->minor_ver;
1359
1360 if( ssl->major_ver < ssl->min_major_ver ||
1361 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001362 {
1363 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001364 " [%d:%d] < [%d:%d]",
1365 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001366 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001367
1368 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1369 SSL_ALERT_MSG_PROTOCOL_VERSION );
1370
1371 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1372 }
1373
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001374 if( ssl->major_ver > ssl->max_major_ver )
1375 {
1376 ssl->major_ver = ssl->max_major_ver;
1377 ssl->minor_ver = ssl->max_minor_ver;
1378 }
1379 else if( ssl->minor_ver > ssl->max_minor_ver )
1380 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001381
Paul Bakker48916f92012-09-16 19:57:18 +00001382 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001383
1384 /*
1385 * Check the handshake message length
1386 */
1387 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1388 {
1389 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1390 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1391 }
1392
1393 /*
1394 * Check the session length
1395 */
1396 sess_len = buf[38];
1397
Paul Bakker0f651c72014-05-22 15:12:19 +02001398 if( sess_len > 32 || sess_len > n - 42 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001399 {
1400 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1401 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1402 }
1403
Paul Bakker48916f92012-09-16 19:57:18 +00001404 ssl->session_negotiate->length = sess_len;
1405 memset( ssl->session_negotiate->id, 0,
1406 sizeof( ssl->session_negotiate->id ) );
1407 memcpy( ssl->session_negotiate->id, buf + 39,
1408 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001409
1410 /*
1411 * Check the ciphersuitelist length
1412 */
1413 ciph_len = ( buf[39 + sess_len] << 8 )
1414 | ( buf[40 + sess_len] );
1415
Paul Bakker0f651c72014-05-22 15:12:19 +02001416 if( ciph_len < 2 || ( ciph_len % 2 ) != 0 || ciph_len > n - 42 - sess_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001417 {
1418 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1419 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1420 }
1421
1422 /*
1423 * Check the compression algorithms length
1424 */
1425 comp_len = buf[41 + sess_len + ciph_len];
1426
Paul Bakker0f651c72014-05-22 15:12:19 +02001427 if( comp_len < 1 || comp_len > 16 ||
1428 comp_len > n - 42 - sess_len - ciph_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001429 {
1430 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1431 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1432 }
1433
Paul Bakker48916f92012-09-16 19:57:18 +00001434 /*
1435 * Check the extension length
1436 */
1437 if( n > 42 + sess_len + ciph_len + comp_len )
1438 {
1439 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1440 | ( buf[43 + sess_len + ciph_len + comp_len] );
1441
1442 if( ( ext_len > 0 && ext_len < 4 ) ||
1443 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1444 {
1445 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1446 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1447 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1448 }
1449 }
1450
1451 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001452#if defined(POLARSSL_ZLIB_SUPPORT)
1453 for( i = 0; i < comp_len; ++i )
1454 {
Paul Bakker48916f92012-09-16 19:57:18 +00001455 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001456 {
Paul Bakker48916f92012-09-16 19:57:18 +00001457 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001458 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001459 }
1460 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001461#endif
1462
Paul Bakkerec636f32012-09-09 19:17:02 +00001463 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1464 buf + 6, 32 );
1465 SSL_DEBUG_BUF( 3, "client hello, session id",
1466 buf + 38, sess_len );
1467 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1468 buf + 41 + sess_len, ciph_len );
1469 SSL_DEBUG_BUF( 3, "client hello, compression",
1470 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001471
Paul Bakkerec636f32012-09-09 19:17:02 +00001472 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001473 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1474 */
1475 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1476 {
1477 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1478 {
1479 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001480#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001481 if( ssl->renegotiation == SSL_RENEGOTIATION )
1482 {
1483 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001484
1485 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1486 return( ret );
1487
Paul Bakker48916f92012-09-16 19:57:18 +00001488 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1489 }
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001490 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001491#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00001492 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1493 break;
1494 }
1495 }
1496
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001497#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1498 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1499 {
1500 if( p[0] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1501 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1502 {
1503 SSL_DEBUG_MSG( 0, ( "received FALLBACK_SCSV" ) );
1504
1505 if( ssl->minor_ver < ssl->max_minor_ver )
1506 {
1507 SSL_DEBUG_MSG( 0, ( "inapropriate fallback" ) );
1508
1509 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1510 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1511
1512 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1513 }
1514
1515 break;
1516 }
1517 }
1518#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1519
Paul Bakker48916f92012-09-16 19:57:18 +00001520 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001521
1522 while( ext_len )
1523 {
1524 unsigned int ext_id = ( ( ext[0] << 8 )
1525 | ( ext[1] ) );
1526 unsigned int ext_size = ( ( ext[2] << 8 )
1527 | ( ext[3] ) );
1528
1529 if( ext_size + 4 > ext_len )
1530 {
1531 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1532 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1533 }
1534 switch( ext_id )
1535 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001536#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001537 case TLS_EXT_SERVERNAME:
1538 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1539 if( ssl->f_sni == NULL )
1540 break;
1541
1542 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1543 if( ret != 0 )
1544 return( ret );
1545 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001546#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001547
Paul Bakker48916f92012-09-16 19:57:18 +00001548 case TLS_EXT_RENEGOTIATION_INFO:
1549 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001550#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001551 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001552#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001553
Paul Bakker23f36802012-09-28 14:15:14 +00001554 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1555 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001556 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001557 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001558
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001559#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1560 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakker23f36802012-09-28 14:15:14 +00001561 case TLS_EXT_SIG_ALG:
1562 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001563#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker23f36802012-09-28 14:15:14 +00001564 if( ssl->renegotiation == SSL_RENEGOTIATION )
1565 break;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001566#endif
Paul Bakker23f36802012-09-28 14:15:14 +00001567
1568 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1569 if( ret != 0 )
1570 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001571 break;
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001572#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
1573 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001574
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001575#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001576 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1577 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1578
1579 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1580 if( ret != 0 )
1581 return( ret );
1582 break;
1583
1584 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1585 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001586 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001587
1588 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1589 if( ret != 0 )
1590 return( ret );
1591 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001592#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001593
Paul Bakker05decb22013-08-15 13:33:48 +02001594#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001595 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1596 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1597
1598 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1599 if( ret != 0 )
1600 return( ret );
1601 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001602#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001603
Paul Bakker1f2bc622013-08-15 13:45:55 +02001604#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001605 case TLS_EXT_TRUNCATED_HMAC:
1606 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1607
1608 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1609 if( ret != 0 )
1610 return( ret );
1611 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001612#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001613
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001614#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1615 case TLS_EXT_ENCRYPT_THEN_MAC:
1616 SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
1617
1618 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1619 if( ret != 0 )
1620 return( ret );
1621 break;
1622#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1623
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001624#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1625 case TLS_EXT_EXTENDED_MASTER_SECRET:
1626 SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
1627
1628 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1629 if( ret != 0 )
1630 return( ret );
1631 break;
1632#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1633
Paul Bakkera503a632013-08-14 13:48:06 +02001634#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001635 case TLS_EXT_SESSION_TICKET:
1636 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1637
1638 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1639 if( ret != 0 )
1640 return( ret );
1641 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001642#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001643
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001644#if defined(POLARSSL_SSL_ALPN)
1645 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001646 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001647
1648 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1649 if( ret != 0 )
1650 return( ret );
1651 break;
1652#endif /* POLARSSL_SSL_SESSION_TICKETS */
1653
Paul Bakker48916f92012-09-16 19:57:18 +00001654 default:
1655 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1656 ext_id ) );
1657 }
1658
1659 ext_len -= 4 + ext_size;
1660 ext += 4 + ext_size;
1661
1662 if( ext_len > 0 && ext_len < 4 )
1663 {
1664 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1665 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1666 }
1667 }
1668
1669 /*
1670 * Renegotiation security checks
1671 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001672 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001673 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1674 {
1675 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1676 handshake_failure = 1;
1677 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001678#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001679 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1680 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1681 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001682 {
1683 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001684 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001685 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001686 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1687 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1688 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001689 {
1690 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001691 handshake_failure = 1;
1692 }
1693 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1694 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1695 renegotiation_info_seen == 1 )
1696 {
1697 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1698 handshake_failure = 1;
1699 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001700#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001701
1702 if( handshake_failure == 1 )
1703 {
1704 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1705 return( ret );
1706
Paul Bakker48916f92012-09-16 19:57:18 +00001707 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1708 }
Paul Bakker380da532012-04-18 16:10:25 +00001709
Paul Bakker41c83d32013-03-20 14:39:14 +01001710 /*
1711 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001712 * (At the end because we need information from the EC-based extensions
1713 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001714 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001715 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001716 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001717 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001718#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1719 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
1720 {
1721 for( i = 0; ciphersuites[i] != 0; i++ )
1722#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001723 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001724 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001725 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001726#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001727 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001728 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1729 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1730 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001731
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001732 got_common_suite = 1;
1733
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001734 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1735 &ciphersuite_info ) ) != 0 )
1736 return( ret );
1737
1738 if( ciphersuite_info != NULL )
1739 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001740 }
1741 }
1742
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001743 if( got_common_suite )
1744 {
1745 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1746 "but none of them usable" ) );
1747 ssl_send_fatal_handshake_failure( ssl );
1748 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
1749 }
1750 else
1751 {
1752 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1753 ssl_send_fatal_handshake_failure( ssl );
1754 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1755 }
Paul Bakker41c83d32013-03-20 14:39:14 +01001756
1757have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001758 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001759 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1760 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1761
Paul Bakker5121ce52009-01-03 21:22:43 +00001762 ssl->in_left = 0;
1763 ssl->state++;
1764
1765 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1766
1767 return( 0 );
1768}
1769
Paul Bakker1f2bc622013-08-15 13:45:55 +02001770#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001771static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1772 unsigned char *buf,
1773 size_t *olen )
1774{
1775 unsigned char *p = buf;
1776
1777 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1778 {
1779 *olen = 0;
1780 return;
1781 }
1782
1783 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1784
1785 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1786 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1787
1788 *p++ = 0x00;
1789 *p++ = 0x00;
1790
1791 *olen = 4;
1792}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001793#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001794
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001795#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1796static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
1797 unsigned char *buf,
1798 size_t *olen )
1799{
1800 unsigned char *p = buf;
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01001801 const ssl_ciphersuite_t *suite = NULL;
1802 const cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001803
1804 if( ssl->session_negotiate->encrypt_then_mac == SSL_EXTENDED_MS_DISABLED ||
1805 ssl->minor_ver == SSL_MINOR_VERSION_0 )
1806 {
1807 *olen = 0;
1808 return;
1809 }
1810
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01001811 /*
1812 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
1813 * from a client and then selects a stream or Authenticated Encryption
1814 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
1815 * encrypt-then-MAC response extension back to the client."
1816 */
1817 if( ( suite = ssl_ciphersuite_from_id(
1818 ssl->session_negotiate->ciphersuite ) ) == NULL ||
1819 ( cipher = cipher_info_from_type( suite->cipher ) ) == NULL ||
1820 cipher->mode != POLARSSL_MODE_CBC )
1821 {
1822 *olen = 0;
1823 return;
1824 }
1825
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001826 SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
1827
1828 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
1829 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
1830
1831 *p++ = 0x00;
1832 *p++ = 0x00;
1833
1834 *olen = 4;
1835}
1836#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1837
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001838#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1839static void ssl_write_extended_ms_ext( ssl_context *ssl,
1840 unsigned char *buf,
1841 size_t *olen )
1842{
1843 unsigned char *p = buf;
1844
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +02001845 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_DISABLED ||
1846 ssl->minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001847 {
1848 *olen = 0;
1849 return;
1850 }
1851
1852 SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
1853 "extension" ) );
1854
1855 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
1856 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
1857
1858 *p++ = 0x00;
1859 *p++ = 0x00;
1860
1861 *olen = 4;
1862}
1863#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1864
Paul Bakkera503a632013-08-14 13:48:06 +02001865#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001866static void ssl_write_session_ticket_ext( ssl_context *ssl,
1867 unsigned char *buf,
1868 size_t *olen )
1869{
1870 unsigned char *p = buf;
1871
1872 if( ssl->handshake->new_session_ticket == 0 )
1873 {
1874 *olen = 0;
1875 return;
1876 }
1877
1878 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1879
1880 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1881 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1882
1883 *p++ = 0x00;
1884 *p++ = 0x00;
1885
1886 *olen = 4;
1887}
Paul Bakkera503a632013-08-14 13:48:06 +02001888#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001889
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001890static void ssl_write_renegotiation_ext( ssl_context *ssl,
1891 unsigned char *buf,
1892 size_t *olen )
1893{
1894 unsigned char *p = buf;
1895
1896 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1897 {
1898 *olen = 0;
1899 return;
1900 }
1901
1902 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1903
1904 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1905 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1906
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001907#if defined(POLARSSL_SSL_RENEGOTIATION)
1908 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
1909 {
1910 *p++ = 0x00;
1911 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1912 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001913
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001914 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1915 p += ssl->verify_data_len;
1916 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1917 p += ssl->verify_data_len;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001918
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001919 *olen = 5 + ssl->verify_data_len * 2;
1920 }
1921 else
1922#endif /* POLARSSL_SSL_RENEGOTIATION */
1923 {
1924 *p++ = 0x00;
1925 *p++ = 0x01;
1926 *p++ = 0x00;
1927
1928 *olen = 5;
1929 }
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001930}
1931
Paul Bakker05decb22013-08-15 13:33:48 +02001932#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001933static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1934 unsigned char *buf,
1935 size_t *olen )
1936{
1937 unsigned char *p = buf;
1938
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001939 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1940 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001941 *olen = 0;
1942 return;
1943 }
1944
1945 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1946
1947 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1948 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1949
1950 *p++ = 0x00;
1951 *p++ = 1;
1952
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001953 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001954
1955 *olen = 5;
1956}
Paul Bakker05decb22013-08-15 13:33:48 +02001957#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001958
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001959#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001960static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1961 unsigned char *buf,
1962 size_t *olen )
1963{
1964 unsigned char *p = buf;
1965 ((void) ssl);
1966
Paul Bakker677377f2013-10-28 12:54:26 +01001967 if( ( ssl->handshake->cli_exts &
1968 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
1969 {
1970 *olen = 0;
1971 return;
1972 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001973
1974 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1975
1976 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1977 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1978
1979 *p++ = 0x00;
1980 *p++ = 2;
1981
1982 *p++ = 1;
1983 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1984
1985 *olen = 6;
1986}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001987#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001988
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001989#if defined(POLARSSL_SSL_ALPN )
1990static void ssl_write_alpn_ext( ssl_context *ssl,
1991 unsigned char *buf, size_t *olen )
1992{
1993 if( ssl->alpn_chosen == NULL )
1994 {
1995 *olen = 0;
1996 return;
1997 }
1998
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001999 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002000
2001 /*
2002 * 0 . 1 ext identifier
2003 * 2 . 3 ext length
2004 * 4 . 5 protocol list length
2005 * 6 . 6 protocol name length
2006 * 7 . 7+n protocol name
2007 */
2008 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
2009 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
2010
2011 *olen = 7 + strlen( ssl->alpn_chosen );
2012
2013 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2014 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2015
2016 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2017 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2018
2019 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2020
2021 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2022}
2023#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
2024
Paul Bakker5121ce52009-01-03 21:22:43 +00002025static int ssl_write_server_hello( ssl_context *ssl )
2026{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002027#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002028 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002029#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002030 int ret;
2031 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002032 unsigned char *buf, *p;
2033
2034 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
2035
Paul Bakkera9a028e2013-11-21 17:31:06 +01002036 if( ssl->f_rng == NULL )
2037 {
2038 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2039 return( POLARSSL_ERR_SSL_NO_RNG );
2040 }
2041
Paul Bakker5121ce52009-01-03 21:22:43 +00002042 /*
2043 * 0 . 0 handshake type
2044 * 1 . 3 handshake length
2045 * 4 . 5 protocol version
2046 * 6 . 9 UNIX time()
2047 * 10 . 37 random bytes
2048 */
2049 buf = ssl->out_msg;
2050 p = buf + 4;
2051
2052 *p++ = (unsigned char) ssl->major_ver;
2053 *p++ = (unsigned char) ssl->minor_ver;
2054
2055 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
2056 buf[4], buf[5] ) );
2057
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002058#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002059 t = time( NULL );
2060 *p++ = (unsigned char)( t >> 24 );
2061 *p++ = (unsigned char)( t >> 16 );
2062 *p++ = (unsigned char)( t >> 8 );
2063 *p++ = (unsigned char)( t );
2064
2065 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002066#else
2067 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
2068 return( ret );
2069
2070 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02002071#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002072
Paul Bakkera3d195c2011-11-27 21:07:34 +00002073 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
2074 return( ret );
2075
2076 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002077
Paul Bakker48916f92012-09-16 19:57:18 +00002078 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002079
2080 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
2081
2082 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002083 * Resume is 0 by default, see ssl_handshake_init().
2084 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2085 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002086 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002087 if( ssl->handshake->resume == 0 &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002088#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002089 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002090#endif
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002091 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002092 ssl->f_get_cache != NULL &&
2093 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
2094 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01002095 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002096 ssl->handshake->resume = 1;
2097 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002098
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002099 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002100 {
2101 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002102 * New session, create a new session id,
2103 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002104 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 ssl->state++;
2106
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002107#if defined(POLARSSL_HAVE_TIME)
2108 ssl->session_negotiate->start = time( NULL );
2109#endif
2110
Paul Bakkera503a632013-08-14 13:48:06 +02002111#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002112 if( ssl->handshake->new_session_ticket != 0 )
2113 {
2114 ssl->session_negotiate->length = n = 0;
2115 memset( ssl->session_negotiate->id, 0, 32 );
2116 }
2117 else
2118#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002119 {
2120 ssl->session_negotiate->length = n = 32;
2121 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002122 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002123 return( ret );
2124 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 }
2126 else
2127 {
2128 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002129 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002130 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02002131 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002132 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002133
2134 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2135 {
2136 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2137 return( ret );
2138 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002139 }
2140
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002141 /*
2142 * 38 . 38 session id length
2143 * 39 . 38+n session id
2144 * 39+n . 40+n chosen ciphersuite
2145 * 41+n . 41+n chosen compression alg.
2146 * 42+n . 43+n extensions length
2147 * 44+n . 43+n+m extensions
2148 */
2149 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00002150 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
2151 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002152
2153 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2154 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2155 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002156 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002157
Paul Bakker48916f92012-09-16 19:57:18 +00002158 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2159 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2160 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002161
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02002162 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2163 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02002164 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002165 ssl->session_negotiate->compression ) );
2166
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002167 /*
2168 * First write extensions, then the total length
2169 */
2170 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2171 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002172
Paul Bakker05decb22013-08-15 13:33:48 +02002173#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002174 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2175 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002176#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002177
Paul Bakker1f2bc622013-08-15 13:45:55 +02002178#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002179 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2180 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002181#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002182
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002183#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2184 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2185 ext_len += olen;
2186#endif
2187
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002188#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2189 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2190 ext_len += olen;
2191#endif
2192
Paul Bakkera503a632013-08-14 13:48:06 +02002193#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002194 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2195 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002196#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002197
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002198#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002199 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2200 ext_len += olen;
2201#endif
2202
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002203#if defined(POLARSSL_SSL_ALPN)
2204 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2205 ext_len += olen;
2206#endif
2207
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002208 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002209
Paul Bakkera7036632014-04-30 10:15:38 +02002210 if( ext_len > 0 )
2211 {
2212 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2213 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2214 p += ext_len;
2215 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002216
2217 ssl->out_msglen = p - buf;
2218 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2219 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
2220
2221 ret = ssl_write_record( ssl );
2222
2223 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2224
2225 return( ret );
2226}
2227
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002228#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2229 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002230 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2231 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002232static int ssl_write_certificate_request( ssl_context *ssl )
2233{
Paul Bakkered27a042013-04-18 22:46:23 +02002234 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002235
2236 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2237
2238 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002239 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002240 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2241 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002242 {
2243 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2244 ssl->state++;
2245 return( 0 );
2246 }
2247
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002248 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2249 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002250}
2251#else
2252static int ssl_write_certificate_request( ssl_context *ssl )
2253{
2254 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2255 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002256 size_t dn_size, total_dn_size; /* excluding length bytes */
2257 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002258 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002259 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00002260
2261 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2262
2263 ssl->state++;
2264
Paul Bakkerfbb17802013-04-17 19:10:21 +02002265 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002266 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002267 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002268 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002269 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002270 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002271 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002272 return( 0 );
2273 }
2274
2275 /*
2276 * 0 . 0 handshake type
2277 * 1 . 3 handshake length
2278 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002279 * 5 .. m-1 cert types
2280 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002281 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002282 * n .. n+1 length of all DNs
2283 * n+2 .. n+3 length of DN 1
2284 * n+4 .. ... Distinguished Name #1
2285 * ... .. ... length of DN 2, etc.
2286 */
2287 buf = ssl->out_msg;
2288 p = buf + 4;
2289
2290 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002291 * Supported certificate types
2292 *
2293 * ClientCertificateType certificate_types<1..2^8-1>;
2294 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002295 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002296 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002297
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002298#if defined(POLARSSL_RSA_C)
2299 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2300#endif
2301#if defined(POLARSSL_ECDSA_C)
2302 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2303#endif
2304
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002305 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002306 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002307
Paul Bakker577e0062013-08-28 11:57:20 +02002308 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002309#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002310 /*
2311 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002312 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002313 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2314 *
2315 * struct {
2316 * HashAlgorithm hash;
2317 * SignatureAlgorithm signature;
2318 * } SignatureAndHashAlgorithm;
2319 *
2320 * enum { (255) } HashAlgorithm;
2321 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002322 */
Paul Bakker21dca692013-01-03 11:41:08 +01002323 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002324 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002325 /*
2326 * Only use current running hash algorithm that is already required
2327 * for requested ciphersuite.
2328 */
Paul Bakker926af752012-11-23 13:38:07 +01002329 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2330
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002331 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2332 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002333 {
2334 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2335 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002336
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002337 /*
2338 * Supported signature algorithms
2339 */
2340#if defined(POLARSSL_RSA_C)
2341 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2342 p[2 + sa_len++] = SSL_SIG_RSA;
2343#endif
2344#if defined(POLARSSL_ECDSA_C)
2345 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2346 p[2 + sa_len++] = SSL_SIG_ECDSA;
2347#endif
Paul Bakker926af752012-11-23 13:38:07 +01002348
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002349 p[0] = (unsigned char)( sa_len >> 8 );
2350 p[1] = (unsigned char)( sa_len );
2351 sa_len += 2;
2352 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002353 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002354#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002355
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002356 /*
2357 * DistinguishedName certificate_authorities<0..2^16-1>;
2358 * opaque DistinguishedName<1..2^16-1>;
2359 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002360 p += 2;
2361 crt = ssl->ca_chain;
2362
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002363 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002364 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002365 {
2366 if( p - buf > 4096 )
2367 break;
2368
Paul Bakker926af752012-11-23 13:38:07 +01002369 dn_size = crt->subject_raw.len;
2370 *p++ = (unsigned char)( dn_size >> 8 );
2371 *p++ = (unsigned char)( dn_size );
2372 memcpy( p, crt->subject_raw.p, dn_size );
2373 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002374
Paul Bakker926af752012-11-23 13:38:07 +01002375 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2376
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002377 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002378 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002379 }
2380
Paul Bakker926af752012-11-23 13:38:07 +01002381 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002382 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2383 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002384 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2385 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002386
2387 ret = ssl_write_record( ssl );
2388
2389 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2390
2391 return( ret );
2392}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002393#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2394 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002395 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2396 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002397
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002398#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2399 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2400static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2401{
2402 int ret;
2403
2404 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2405 {
2406 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2407 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2408 }
2409
2410 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2411 pk_ec( *ssl_own_key( ssl ) ),
2412 POLARSSL_ECDH_OURS ) ) != 0 )
2413 {
2414 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2415 return( ret );
2416 }
2417
2418 return( 0 );
2419}
2420#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2421 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2422
Paul Bakker41c83d32013-03-20 14:39:14 +01002423static int ssl_write_server_key_exchange( ssl_context *ssl )
2424{
Paul Bakker23986e52011-04-24 08:57:21 +00002425 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002426 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002427 const ssl_ciphersuite_t *ciphersuite_info =
2428 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002429
2430#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2431 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2432 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002433 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002434 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002435 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002436 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002437 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002438 ((void) dig_signed);
2439 ((void) dig_signed_len);
2440#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002441
Paul Bakker5121ce52009-01-03 21:22:43 +00002442 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2443
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002444#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2445 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2446 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002447 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2448 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2449 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002450 {
2451 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2452 ssl->state++;
2453 return( 0 );
2454 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002455#endif
2456
2457#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2458 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2459 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2460 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2461 {
2462 ssl_get_ecdh_params_from_cert( ssl );
2463
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002464 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002465 ssl->state++;
2466 return( 0 );
2467 }
2468#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002469
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002470#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2471 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2472 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2473 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002474 {
2475 /* TODO: Support identity hints */
2476 *(p++) = 0x00;
2477 *(p++) = 0x00;
2478
2479 n += 2;
2480 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002481#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2482 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002483
2484#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2485 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2486 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2487 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002488 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002489 /*
2490 * Ephemeral DH parameters:
2491 *
2492 * struct {
2493 * opaque dh_p<1..2^16-1>;
2494 * opaque dh_g<1..2^16-1>;
2495 * opaque dh_Ys<1..2^16-1>;
2496 * } ServerDHParams;
2497 */
2498 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2499 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2500 {
2501 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2502 return( ret );
2503 }
Paul Bakker48916f92012-09-16 19:57:18 +00002504
Paul Bakker41c83d32013-03-20 14:39:14 +01002505 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002506 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2507 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002508 {
2509 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2510 return( ret );
2511 }
2512
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002513 dig_signed = p;
2514 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002515
2516 p += len;
2517 n += len;
2518
Paul Bakker41c83d32013-03-20 14:39:14 +01002519 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2520 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2521 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2522 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2523 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002524#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2525 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002526
Gergely Budai987bfb52014-01-19 21:48:42 +01002527#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002528 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002529 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2530 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002531 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002532 /*
2533 * Ephemeral ECDH parameters:
2534 *
2535 * struct {
2536 * ECParameters curve_params;
2537 * ECPoint public;
2538 * } ServerECDHParams;
2539 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002540 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002541#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002542 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002543
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002544 /* Match our preference list against the offered curves */
2545 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2546 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2547 if( (*curve)->grp_id == *gid )
2548 goto curve_matching_done;
2549
2550curve_matching_done:
2551#else
2552 curve = ssl->handshake->curves;
2553#endif
2554
2555 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002556 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002557 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2558 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002559 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002560
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002561 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002562
Paul Bakker41c83d32013-03-20 14:39:14 +01002563 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002564 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002565 {
2566 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2567 return( ret );
2568 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002569
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002570 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2571 p, SSL_MAX_CONTENT_LEN - n,
2572 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002573 {
2574 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2575 return( ret );
2576 }
2577
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002578 dig_signed = p;
2579 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002580
2581 p += len;
2582 n += len;
2583
Paul Bakker41c83d32013-03-20 14:39:14 +01002584 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2585 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002586#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002587
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002588#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002589 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2590 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002591 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002592 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2593 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002594 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002595 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002596 unsigned int hashlen = 0;
2597 unsigned char hash[64];
2598 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002599
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002600 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002601 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2602 */
Paul Bakker577e0062013-08-28 11:57:20 +02002603#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002604 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2605 {
2606 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2607
2608 if( md_alg == POLARSSL_MD_NONE )
2609 {
2610 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002611 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002612 }
2613 }
Paul Bakker577e0062013-08-28 11:57:20 +02002614 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002615#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002616#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2617 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002618 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002619 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2620 {
2621 md_alg = POLARSSL_MD_SHA1;
2622 }
2623 else
Paul Bakker577e0062013-08-28 11:57:20 +02002624#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002625 {
2626 md_alg = POLARSSL_MD_NONE;
2627 }
2628
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002629 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002630 * Compute the hash to be signed
2631 */
Paul Bakker577e0062013-08-28 11:57:20 +02002632#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2633 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002634 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002635 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002636 md5_context md5;
2637 sha1_context sha1;
2638
Paul Bakker5b4af392014-06-26 12:09:34 +02002639 md5_init( &md5 );
2640 sha1_init( &sha1 );
2641
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002642 /*
2643 * digitally-signed struct {
2644 * opaque md5_hash[16];
2645 * opaque sha_hash[20];
2646 * };
2647 *
2648 * md5_hash
2649 * MD5(ClientHello.random + ServerHello.random
2650 * + ServerParams);
2651 * sha_hash
2652 * SHA(ClientHello.random + ServerHello.random
2653 * + ServerParams);
2654 */
2655 md5_starts( &md5 );
2656 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002657 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002658 md5_finish( &md5, hash );
2659
2660 sha1_starts( &sha1 );
2661 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002662 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002663 sha1_finish( &sha1, hash + 16 );
2664
2665 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02002666
2667 md5_free( &md5 );
2668 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002669 }
2670 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002671#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2672 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002673#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2674 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002675 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002676 {
2677 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02002678 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002679
Paul Bakker84bbeb52014-07-01 14:53:22 +02002680 md_init( &ctx );
2681
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002682 /* Info from md_alg will be used instead */
2683 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002684
2685 /*
2686 * digitally-signed struct {
2687 * opaque client_random[32];
2688 * opaque server_random[32];
2689 * ServerDHParams params;
2690 * };
2691 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002692 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002693 {
2694 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2695 return( ret );
2696 }
2697
2698 md_starts( &ctx );
2699 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002700 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002701 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002702 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00002703 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002704 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002705#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2706 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002707 {
2708 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002709 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002710 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002711
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002712 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2713 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002714
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002715 /*
2716 * Make the signature
2717 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002718 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002719 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002720 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2721 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002722 }
Paul Bakker23f36802012-09-28 14:15:14 +00002723
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002724#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002725 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2726 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002727 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002728 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002729
2730 n += 2;
2731 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002732#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002733
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002734 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002735 p + 2 , &signature_len,
2736 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002737 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002738 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002739 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002740 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002741
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002742 *(p++) = (unsigned char)( signature_len >> 8 );
2743 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002744 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002745
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002746 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002747
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002748 p += signature_len;
2749 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002750 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002751#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002752 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2753 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002754
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002755 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002756 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2757 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2758
2759 ssl->state++;
2760
2761 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2762 {
2763 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2764 return( ret );
2765 }
2766
2767 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2768
2769 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002770}
2771
2772static int ssl_write_server_hello_done( ssl_context *ssl )
2773{
2774 int ret;
2775
2776 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2777
2778 ssl->out_msglen = 4;
2779 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2780 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2781
2782 ssl->state++;
2783
2784 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2785 {
2786 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2787 return( ret );
2788 }
2789
2790 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2791
2792 return( 0 );
2793}
2794
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002795#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2796 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2797static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2798 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002799{
2800 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002801 size_t n;
2802
2803 /*
2804 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2805 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002806 if( *p + 2 > end )
2807 {
2808 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2809 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2810 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002811
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002812 n = ( (*p)[0] << 8 ) | (*p)[1];
2813 *p += 2;
2814
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002815 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002816 {
2817 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2818 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2819 }
2820
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002821 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002822 {
2823 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2824 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2825 }
2826
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002827 *p += n;
2828
Paul Bakker70df2fb2013-04-17 17:19:09 +02002829 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2830
Paul Bakker70df2fb2013-04-17 17:19:09 +02002831 return( ret );
2832}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002833#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2834 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002835
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002836#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2837 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002838static int ssl_parse_encrypted_pms( ssl_context *ssl,
2839 const unsigned char *p,
2840 const unsigned char *end,
2841 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002842{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002843 int ret;
2844 size_t len = pk_get_len( ssl_own_key( ssl ) );
2845 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002846
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002847 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002848 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002849 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002850 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2851 }
2852
2853 /*
2854 * Decrypt the premaster using own private RSA key
2855 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002856#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2857 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002858 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2859 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002860 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2861 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002862 {
2863 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2864 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2865 }
2866 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002867#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002868
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002869 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002870 {
2871 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2872 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2873 }
2874
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002875 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2876 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002877 sizeof( ssl->handshake->premaster ) - pms_offset,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002878 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002879
2880 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002881 pms[0] != ssl->handshake->max_major_ver ||
2882 pms[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002883 {
2884 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2885
2886 /*
2887 * Protection against Bleichenbacher's attack:
2888 * invalid PKCS#1 v1.5 padding must not cause
2889 * the connection to end immediately; instead,
2890 * send a bad_record_mac later in the handshake.
2891 */
2892 ssl->handshake->pmslen = 48;
2893
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002894 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002895 if( ret != 0 )
2896 return( ret );
2897 }
2898
2899 return( ret );
2900}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002901#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2902 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002903
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002904#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002905static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2906 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002907{
Paul Bakker6db455e2013-09-18 17:29:31 +02002908 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002909 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002910
Paul Bakker6db455e2013-09-18 17:29:31 +02002911 if( ssl->f_psk == NULL &&
2912 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2913 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002914 {
2915 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2916 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2917 }
2918
2919 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002920 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002921 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002922 if( *p + 2 > end )
2923 {
2924 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2925 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2926 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002927
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002928 n = ( (*p)[0] << 8 ) | (*p)[1];
2929 *p += 2;
2930
2931 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002932 {
2933 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2934 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2935 }
2936
Paul Bakker6db455e2013-09-18 17:29:31 +02002937 if( ssl->f_psk != NULL )
2938 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002939 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002940 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2941 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002942 else
Paul Bakker6db455e2013-09-18 17:29:31 +02002943 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002944 /* Identity is not a big secret since clients send it in the clear,
2945 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02002946 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002947 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002948 {
2949 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2950 }
2951 }
2952
2953 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002954 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002955 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002956 if( ( ret = ssl_send_alert_message( ssl,
2957 SSL_ALERT_LEVEL_FATAL,
2958 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2959 {
2960 return( ret );
2961 }
2962
2963 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002964 }
2965
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002966 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002967
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002968 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002969}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002970#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002971
Paul Bakker5121ce52009-01-03 21:22:43 +00002972static int ssl_parse_client_key_exchange( ssl_context *ssl )
2973{
Paul Bakker23986e52011-04-24 08:57:21 +00002974 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002975 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002976
Paul Bakker41c83d32013-03-20 14:39:14 +01002977 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002978
2979 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2980
2981 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2982 {
2983 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2984 return( ret );
2985 }
2986
2987 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2988 {
2989 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002990 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002991 }
2992
2993 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2994 {
2995 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002996 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002997 }
2998
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002999#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01003000 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003001 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003002 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003003 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003004
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003005 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003006 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02003007 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3008 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003009 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003010
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003011 if( p != end )
3012 {
3013 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3014 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3015 }
3016
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02003017 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003018
3019 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
3020 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003021 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02003022 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003023 {
3024 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
3025 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3026 }
3027
3028 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003029 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003030 else
3031#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003032#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003033 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3034 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3035 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003036 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003037 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
3038 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
3039 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003040 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003041 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003042 ssl->in_msg + 4, ssl->in_hslen - 4 ) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003043 {
3044 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3045 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3046 }
3047
3048 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3049
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003050 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
3051 &ssl->handshake->pmslen,
3052 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02003053 POLARSSL_MPI_MAX_SIZE,
3054 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003055 {
3056 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
3057 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3058 }
3059
3060 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003061 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003062 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003063#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003064 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3065 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3066 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003067#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
3068 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003069 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003070 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003071 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003072
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003073 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003074 {
3075 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3076 return( ret );
3077 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003078
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003079 if( p != end )
3080 {
3081 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3082 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3083 }
3084
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003085 if( ( ret = ssl_psk_derive_premaster( ssl,
3086 ciphersuite_info->key_exchange ) ) != 0 )
3087 {
3088 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3089 return( ret );
3090 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003091 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003092 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003093#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003094#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
3095 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
3096 {
3097 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003098 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003099
3100 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3101 {
3102 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3103 return( ret );
3104 }
3105
3106 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3107 {
3108 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
3109 return( ret );
3110 }
3111
3112 if( ( ret = ssl_psk_derive_premaster( ssl,
3113 ciphersuite_info->key_exchange ) ) != 0 )
3114 {
3115 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3116 return( ret );
3117 }
3118 }
3119 else
3120#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003121#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3122 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3123 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003124 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003125 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003126
3127 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3128 {
3129 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3130 return( ret );
3131 }
3132 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3133 {
3134 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3135 return( ret );
3136 }
3137
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003138 if( p != end )
3139 {
3140 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3141 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3142 }
3143
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003144 if( ( ret = ssl_psk_derive_premaster( ssl,
3145 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003146 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003147 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3148 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003149 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003150 }
3151 else
3152#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003153#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3154 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
3155 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003156 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003157 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003158
3159 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3160 {
3161 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3162 return( ret );
3163 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003164
3165 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
3166 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003167 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003168 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3169 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003170 }
3171
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003172 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3173
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003174 if( ( ret = ssl_psk_derive_premaster( ssl,
3175 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003176 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003177 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003178 return( ret );
3179 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003180 }
3181 else
3182#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003183#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
3184 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003185 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003186 if( ( ret = ssl_parse_encrypted_pms( ssl,
3187 ssl->in_msg + 4,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003188 ssl->in_msg + ssl->in_hslen,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003189 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003190 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003191 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003192 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003193 }
3194 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003195 else
3196#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
3197 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003198 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003199 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003200 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003201
Paul Bakkerff60ee62010-03-16 21:09:09 +00003202 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
3203 {
3204 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
3205 return( ret );
3206 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003207
Paul Bakker5121ce52009-01-03 21:22:43 +00003208 ssl->state++;
3209
3210 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3211
3212 return( 0 );
3213}
3214
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003215#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3216 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003217 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3218 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003219static int ssl_parse_certificate_verify( ssl_context *ssl )
3220{
Paul Bakkerfbb17802013-04-17 19:10:21 +02003221 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003222
3223 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3224
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003225 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003226 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003227 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003228 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02003229 {
3230 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3231 ssl->state++;
3232 return( 0 );
3233 }
3234
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003235 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3236 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003237}
3238#else
3239static int ssl_parse_certificate_verify( ssl_context *ssl )
3240{
3241 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003242 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003243 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003244 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003245 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003246#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003247 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003248#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003249 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003250 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3251
3252 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3253
3254 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003255 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003256 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003257 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3258 {
3259 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3260 ssl->state++;
3261 return( 0 );
3262 }
3263
Paul Bakkered27a042013-04-18 22:46:23 +02003264 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003265 {
3266 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3267 ssl->state++;
3268 return( 0 );
3269 }
3270
Paul Bakker48916f92012-09-16 19:57:18 +00003271 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003272
3273 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3274 {
3275 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3276 return( ret );
3277 }
3278
3279 ssl->state++;
3280
3281 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3282 {
3283 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003284 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003285 }
3286
3287 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
3288 {
3289 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003290 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003291 }
3292
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003293 /*
3294 * 0 . 0 handshake type
3295 * 1 . 3 handshake length
3296 * 4 . 5 sig alg (TLS 1.2 only)
3297 * 4+n . 5+n signature length (n = sa_len)
3298 * 6+n . 6+n+m signature (m = sig_len)
3299 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003300
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003301#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3302 defined(POLARSSL_SSL_PROTO_TLS1_1)
3303 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003304 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003305 sa_len = 0;
3306
Paul Bakkerc70b9822013-04-07 22:00:46 +02003307 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003308 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003309
3310 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3311 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3312 POLARSSL_PK_ECDSA ) )
3313 {
3314 hash_start += 16;
3315 hashlen -= 16;
3316 md_alg = POLARSSL_MD_SHA1;
3317 }
Paul Bakker926af752012-11-23 13:38:07 +01003318 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003319 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003320#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3321 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003322#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3323 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003324 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003325 sa_len = 2;
3326
Paul Bakker5121ce52009-01-03 21:22:43 +00003327 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003328 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003329 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003330 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003331 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003332 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3333 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003334 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3335 }
3336
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003337 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003338
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003339 /* Info from md_alg will be used instead */
3340 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003341
3342 /*
3343 * Signature
3344 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003345 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
3346 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003347 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003348 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3349 " for verify message" ) );
3350 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003351 }
3352
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003353 /*
3354 * Check the certificate's key type matches the signature alg
3355 */
3356 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3357 {
3358 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3359 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3360 }
Paul Bakker577e0062013-08-28 11:57:20 +02003361 }
3362 else
3363#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3364 {
3365 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003366 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003367 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003368
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003369 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01003370
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003371 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003372 {
3373 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003374 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003375 }
3376
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003377 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003378 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003379 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003380 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003381 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003382 return( ret );
3383 }
3384
3385 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3386
Paul Bakkered27a042013-04-18 22:46:23 +02003387 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003388}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003389#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3390 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3391 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003392
Paul Bakkera503a632013-08-14 13:48:06 +02003393#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003394static int ssl_write_new_session_ticket( ssl_context *ssl )
3395{
3396 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003397 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003398 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003399
3400 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3401
3402 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3403 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3404
3405 /*
3406 * struct {
3407 * uint32 ticket_lifetime_hint;
3408 * opaque ticket<0..2^16-1>;
3409 * } NewSessionTicket;
3410 *
3411 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3412 * 8 . 9 ticket_len (n)
3413 * 10 . 9+n ticket content
3414 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003415
3416 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3417 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3418 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3419 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003420
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003421 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3422 {
3423 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3424 tlen = 0;
3425 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003426
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003427 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3428 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003429
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003430 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003431
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003432 /*
3433 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3434 * ChangeCipherSpec share the same state.
3435 */
3436 ssl->handshake->new_session_ticket = 0;
3437
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003438 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3439 {
3440 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3441 return( ret );
3442 }
3443
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003444 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3445
3446 return( 0 );
3447}
Paul Bakkera503a632013-08-14 13:48:06 +02003448#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003449
Paul Bakker5121ce52009-01-03 21:22:43 +00003450/*
Paul Bakker1961b702013-01-25 14:49:24 +01003451 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003452 */
Paul Bakker1961b702013-01-25 14:49:24 +01003453int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003454{
3455 int ret = 0;
3456
Paul Bakker1961b702013-01-25 14:49:24 +01003457 if( ssl->state == SSL_HANDSHAKE_OVER )
3458 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003459
Paul Bakker1961b702013-01-25 14:49:24 +01003460 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3461
3462 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3463 return( ret );
3464
3465 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003466 {
Paul Bakker1961b702013-01-25 14:49:24 +01003467 case SSL_HELLO_REQUEST:
3468 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003469 break;
3470
Paul Bakker1961b702013-01-25 14:49:24 +01003471 /*
3472 * <== ClientHello
3473 */
3474 case SSL_CLIENT_HELLO:
3475 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003476 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003477
3478 /*
3479 * ==> ServerHello
3480 * Certificate
3481 * ( ServerKeyExchange )
3482 * ( CertificateRequest )
3483 * ServerHelloDone
3484 */
3485 case SSL_SERVER_HELLO:
3486 ret = ssl_write_server_hello( ssl );
3487 break;
3488
3489 case SSL_SERVER_CERTIFICATE:
3490 ret = ssl_write_certificate( ssl );
3491 break;
3492
3493 case SSL_SERVER_KEY_EXCHANGE:
3494 ret = ssl_write_server_key_exchange( ssl );
3495 break;
3496
3497 case SSL_CERTIFICATE_REQUEST:
3498 ret = ssl_write_certificate_request( ssl );
3499 break;
3500
3501 case SSL_SERVER_HELLO_DONE:
3502 ret = ssl_write_server_hello_done( ssl );
3503 break;
3504
3505 /*
3506 * <== ( Certificate/Alert )
3507 * ClientKeyExchange
3508 * ( CertificateVerify )
3509 * ChangeCipherSpec
3510 * Finished
3511 */
3512 case SSL_CLIENT_CERTIFICATE:
3513 ret = ssl_parse_certificate( ssl );
3514 break;
3515
3516 case SSL_CLIENT_KEY_EXCHANGE:
3517 ret = ssl_parse_client_key_exchange( ssl );
3518 break;
3519
3520 case SSL_CERTIFICATE_VERIFY:
3521 ret = ssl_parse_certificate_verify( ssl );
3522 break;
3523
3524 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3525 ret = ssl_parse_change_cipher_spec( ssl );
3526 break;
3527
3528 case SSL_CLIENT_FINISHED:
3529 ret = ssl_parse_finished( ssl );
3530 break;
3531
3532 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003533 * ==> ( NewSessionTicket )
3534 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003535 * Finished
3536 */
3537 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003538#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003539 if( ssl->handshake->new_session_ticket != 0 )
3540 ret = ssl_write_new_session_ticket( ssl );
3541 else
Paul Bakkera503a632013-08-14 13:48:06 +02003542#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003543 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003544 break;
3545
3546 case SSL_SERVER_FINISHED:
3547 ret = ssl_write_finished( ssl );
3548 break;
3549
3550 case SSL_FLUSH_BUFFERS:
3551 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3552 ssl->state = SSL_HANDSHAKE_WRAPUP;
3553 break;
3554
3555 case SSL_HANDSHAKE_WRAPUP:
3556 ssl_handshake_wrapup( ssl );
3557 break;
3558
3559 default:
3560 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3561 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003562 }
3563
Paul Bakker5121ce52009-01-03 21:22:43 +00003564 return( ret );
3565}
Paul Bakker9af723c2014-05-01 13:03:14 +02003566#endif /* POLARSSL_SSL_SRV_C */