blob: cd207c509f6b4248ab3aabf8145a9163167c1ddf [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 server-side functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakker40e46942009-01-03 21:51:57 +000032#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/debug.h"
35#include "polarssl/ssl.h"
Paul Bakker41c83d32013-03-20 14:39:14 +010036#if defined(POLARSSL_ECP_C)
37#include "polarssl/ecp.h"
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker7dc4c442014-02-01 22:50:26 +010040#if defined(POLARSSL_PLATFORM_C)
41#include "polarssl/platform.h"
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020042#else
43#define polarssl_malloc malloc
44#define polarssl_free free
45#endif
46
Paul Bakker5121ce52009-01-03 21:22:43 +000047#include <stdlib.h>
48#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020049
50#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000051#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020052#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Paul Bakkera503a632013-08-14 13:48:06 +020054#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker34617722014-06-13 17:20:13 +020055/* Implementation that should never be optimized out by the compiler */
56static void polarssl_zeroize( void *v, size_t n ) {
57 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
58}
59
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020060/*
61 * Serialize a session in the following format:
62 * 0 . n-1 session structure, n = sizeof(ssl_session)
63 * n . n+2 peer_cert length = m (0 if no certificate)
64 * n+3 . n+2+m peer cert ASN.1
65 *
66 * Assumes ticket is NULL (always true on server side).
67 */
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020068static int ssl_save_session( const ssl_session *session,
69 unsigned char *buf, size_t buf_len,
70 size_t *olen )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020071{
72 unsigned char *p = buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020073 size_t left = buf_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020075 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020077
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020078 if( left < sizeof( ssl_session ) )
79 return( -1 );
80
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020081 memcpy( p, session, sizeof( ssl_session ) );
82 p += sizeof( ssl_session );
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020083 left -= sizeof( ssl_session );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020084
Paul Bakker7c6b2c32013-09-16 13:49:26 +020085#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020086 if( session->peer_cert == NULL )
87 cert_len = 0;
88 else
89 cert_len = session->peer_cert->raw.len;
90
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020091 if( left < 3 + cert_len )
92 return( -1 );
93
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020094 *p++ = (unsigned char)( cert_len >> 16 & 0xFF );
95 *p++ = (unsigned char)( cert_len >> 8 & 0xFF );
96 *p++ = (unsigned char)( cert_len & 0xFF );
97
98 if( session->peer_cert != NULL )
99 memcpy( p, session->peer_cert->raw.p, cert_len );
100
101 p += cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200102#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200103
104 *olen = p - buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200105
106 return( 0 );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200107}
108
109/*
110 * Unserialise session, see ssl_save_session()
111 */
112static int ssl_load_session( ssl_session *session,
113 const unsigned char *buf, size_t len )
114{
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200115 const unsigned char *p = buf;
116 const unsigned char * const end = buf + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200117#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200118 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200119#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200120
121 if( p + sizeof( ssl_session ) > end )
122 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
123
124 memcpy( session, p, sizeof( ssl_session ) );
125 p += sizeof( ssl_session );
126
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200127#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200128 if( p + 3 > end )
129 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
130
131 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
132 p += 3;
133
134 if( cert_len == 0 )
135 {
136 session->peer_cert = NULL;
137 }
138 else
139 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200140 int ret;
141
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200142 if( p + cert_len > end )
143 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
144
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200145 session->peer_cert = polarssl_malloc( sizeof( x509_crt ) );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200146
147 if( session->peer_cert == NULL )
148 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
149
Paul Bakkerb6b09562013-09-18 14:17:41 +0200150 x509_crt_init( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200151
Paul Bakkerddf26b42013-09-18 13:46:23 +0200152 if( ( ret = x509_crt_parse( session->peer_cert, p, cert_len ) ) != 0 )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200153 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200154 x509_crt_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200155 polarssl_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200156 session->peer_cert = NULL;
157 return( ret );
158 }
159
160 p += cert_len;
161 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200162#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200163
164 if( p != end )
165 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
166
167 return( 0 );
168}
169
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200170/*
171 * Create session ticket, secured as recommended in RFC 5077 section 4:
172 *
173 * struct {
174 * opaque key_name[16];
175 * opaque iv[16];
176 * opaque encrypted_state<0..2^16-1>;
177 * opaque mac[32];
178 * } ticket;
179 *
180 * (the internal state structure differs, however).
181 */
182static int ssl_write_ticket( ssl_context *ssl, size_t *tlen )
183{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200184 int ret;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200185 unsigned char * const start = ssl->out_msg + 10;
186 unsigned char *p = start;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200187 unsigned char *state;
188 unsigned char iv[16];
189 size_t clear_len, enc_len, pad_len, i;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200190
Manuel Pégourié-Gonnard0a201712013-08-23 16:25:16 +0200191 *tlen = 0;
192
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200193 if( ssl->ticket_keys == NULL )
194 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
195
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200196 /* Write key name */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200197 memcpy( p, ssl->ticket_keys->key_name, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200198 p += 16;
199
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200200 /* Generate and write IV (with a copy for aes_crypt) */
201 if( ( ret = ssl->f_rng( ssl->p_rng, p, 16 ) ) != 0 )
202 return( ret );
203 memcpy( iv, p, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200204 p += 16;
205
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200206 /*
207 * Dump session state
208 *
209 * After the session state itself, we still need room for 16 bytes of
210 * padding and 32 bytes of MAC, so there's only so much room left
211 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200212 state = p + 2;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200213 if( ssl_save_session( ssl->session_negotiate, state,
214 SSL_MAX_CONTENT_LEN - (state - ssl->out_ctr) - 48,
215 &clear_len ) != 0 )
216 {
217 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
218 }
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200219 SSL_DEBUG_BUF( 3, "session ticket cleartext", state, clear_len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200220
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200221 /* Apply PKCS padding */
222 pad_len = 16 - clear_len % 16;
223 enc_len = clear_len + pad_len;
224 for( i = clear_len; i < enc_len; i++ )
225 state[i] = (unsigned char) pad_len;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200226
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200227 /* Encrypt */
228 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->enc, AES_ENCRYPT,
229 enc_len, iv, state, state ) ) != 0 )
230 {
231 return( ret );
232 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200233
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200234 /* Write length */
235 *p++ = (unsigned char)( ( enc_len >> 8 ) & 0xFF );
236 *p++ = (unsigned char)( ( enc_len ) & 0xFF );
237 p = state + enc_len;
238
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200239 /* Compute and write MAC( key_name + iv + enc_state_len + enc_state ) */
240 sha256_hmac( ssl->ticket_keys->mac_key, 16, start, p - start, p, 0 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200241 p += 32;
242
243 *tlen = p - start;
244
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200245 SSL_DEBUG_BUF( 3, "session ticket structure", start, *tlen );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200246
247 return( 0 );
248}
249
250/*
251 * Load session ticket (see ssl_write_ticket for structure)
252 */
253static int ssl_parse_ticket( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200254 unsigned char *buf,
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200255 size_t len )
256{
257 int ret;
258 ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200259 unsigned char *key_name = buf;
260 unsigned char *iv = buf + 16;
261 unsigned char *enc_len_p = iv + 16;
262 unsigned char *ticket = enc_len_p + 2;
263 unsigned char *mac;
Manuel Pégourié-Gonnard34ced2d2013-09-20 11:37:39 +0200264 unsigned char computed_mac[32];
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200265 size_t enc_len, clear_len, i;
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100266 unsigned char pad_len, diff;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200267
268 SSL_DEBUG_BUF( 3, "session ticket structure", buf, len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200269
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200270 if( len < 34 || ssl->ticket_keys == NULL )
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200271 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
272
273 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
274 mac = ticket + enc_len;
275
276 if( len != enc_len + 66 )
277 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
278
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100279 /* Check name, in constant time though it's not a big secret */
280 diff = 0;
281 for( i = 0; i < 16; i++ )
282 diff |= key_name[i] ^ ssl->ticket_keys->key_name[i];
283 /* don't return yet, check the MAC anyway */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200284
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100285 /* Check mac, with constant-time buffer comparison */
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200286 sha256_hmac( ssl->ticket_keys->mac_key, 16, buf, len - 32,
287 computed_mac, 0 );
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100288
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200289 for( i = 0; i < 32; i++ )
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100290 diff |= mac[i] ^ computed_mac[i];
291
292 /* Now return if ticket is not authentic, since we want to avoid
293 * decrypting arbitrary attacker-chosen data */
294 if( diff != 0 )
295 return( POLARSSL_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200296
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200297 /* Decrypt */
298 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->dec, AES_DECRYPT,
299 enc_len, iv, ticket, ticket ) ) != 0 )
300 {
301 return( ret );
302 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200303
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200304 /* Check PKCS padding */
305 pad_len = ticket[enc_len - 1];
306
307 ret = 0;
308 for( i = 2; i < pad_len; i++ )
309 if( ticket[enc_len - i] != pad_len )
310 ret = POLARSSL_ERR_SSL_BAD_INPUT_DATA;
311 if( ret != 0 )
312 return( ret );
313
314 clear_len = enc_len - pad_len;
315
316 SSL_DEBUG_BUF( 3, "session ticket cleartext", ticket, clear_len );
317
318 /* Actually load session */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200319 if( ( ret = ssl_load_session( &session, ticket, clear_len ) ) != 0 )
320 {
321 SSL_DEBUG_MSG( 1, ( "failed to parse ticket content" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100322 ssl_session_free( &session );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200323 return( ret );
324 }
325
Paul Bakker606b4ba2013-08-14 16:52:14 +0200326#if defined(POLARSSL_HAVE_TIME)
327 /* Check if still valid */
328 if( (int) ( time( NULL) - session.start ) > ssl->ticket_lifetime )
329 {
330 SSL_DEBUG_MSG( 1, ( "session ticket expired" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100331 ssl_session_free( &session );
Paul Bakker606b4ba2013-08-14 16:52:14 +0200332 return( POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED );
333 }
334#endif
335
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200336 /*
337 * Keep the session ID sent by the client, since we MUST send it back to
338 * inform him we're accepting the ticket (RFC 5077 section 3.4)
339 */
340 session.length = ssl->session_negotiate->length;
341 memcpy( &session.id, ssl->session_negotiate->id, session.length );
342
343 ssl_session_free( ssl->session_negotiate );
344 memcpy( ssl->session_negotiate, &session, sizeof( ssl_session ) );
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
Paul Bakker48916f92012-09-16 19:57:18 +0000431 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
432 {
433 if( len != 1 || buf[0] != 0x0 )
434 {
435 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000436
437 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
438 return( ret );
439
Paul Bakker48916f92012-09-16 19:57:18 +0000440 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
441 }
442
443 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
444 }
445 else
446 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100447 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000448 if( len != 1 + ssl->verify_data_len ||
449 buf[0] != ssl->verify_data_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100450 safer_memcmp( buf + 1, ssl->peer_verify_data,
451 ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000452 {
453 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
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
462 return( 0 );
463}
464
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200465#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +0000466static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
467 const unsigned char *buf,
468 size_t len )
469{
470 size_t sig_alg_list_size;
471 const unsigned char *p;
472
473 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
474 if( sig_alg_list_size + 2 != len ||
475 sig_alg_list_size %2 != 0 )
476 {
477 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
478 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
479 }
480
481 p = buf + 2;
482 while( sig_alg_list_size > 0 )
483 {
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200484 /*
485 * For now, just ignore signature algorithm and rely on offered
486 * ciphersuites only. To be fixed later.
487 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200488#if defined(POLARSSL_SHA512_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000489 if( p[0] == SSL_HASH_SHA512 )
490 {
491 ssl->handshake->sig_alg = SSL_HASH_SHA512;
492 break;
493 }
494 if( p[0] == SSL_HASH_SHA384 )
495 {
496 ssl->handshake->sig_alg = SSL_HASH_SHA384;
497 break;
498 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200499#endif /* POLARSSL_SHA512_C */
Paul Bakker9e36f042013-06-30 14:34:05 +0200500#if defined(POLARSSL_SHA256_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000501 if( p[0] == SSL_HASH_SHA256 )
502 {
503 ssl->handshake->sig_alg = SSL_HASH_SHA256;
504 break;
505 }
506 if( p[0] == SSL_HASH_SHA224 )
507 {
508 ssl->handshake->sig_alg = SSL_HASH_SHA224;
509 break;
510 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200511#endif /* POLARSSL_SHA256_C */
Paul Bakker23f36802012-09-28 14:15:14 +0000512 if( p[0] == SSL_HASH_SHA1 )
513 {
514 ssl->handshake->sig_alg = SSL_HASH_SHA1;
515 break;
516 }
517 if( p[0] == SSL_HASH_MD5 )
518 {
519 ssl->handshake->sig_alg = SSL_HASH_MD5;
520 break;
521 }
522
523 sig_alg_list_size -= 2;
524 p += 2;
525 }
526
527 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
528 ssl->handshake->sig_alg ) );
529
530 return( 0 );
531}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200532#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker23f36802012-09-28 14:15:14 +0000533
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200534#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200535static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
536 const unsigned char *buf,
537 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100538{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200539 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100540 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200541 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100542
543 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
544 if( list_size + 2 != len ||
545 list_size % 2 != 0 )
546 {
547 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
548 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
549 }
550
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +0100551 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200552 * and leave room for a final 0 */
553 our_size = list_size / 2 + 1;
554 if( our_size > POLARSSL_ECP_DP_MAX )
555 our_size = POLARSSL_ECP_DP_MAX;
556
557 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
558 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
559
Paul Bakker9af723c2014-05-01 13:03:14 +0200560 /* explicit void pointer cast for buggy MS compiler */
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200561 memset( (void *) curves, 0, our_size * sizeof( *curves ) );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200562 ssl->handshake->curves = curves;
563
Paul Bakker41c83d32013-03-20 14:39:14 +0100564 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200565 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100566 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200567 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200568
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200569 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100570 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200571 *curves++ = curve_info;
572 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100573 }
574
575 list_size -= 2;
576 p += 2;
577 }
578
579 return( 0 );
580}
581
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200582static int ssl_parse_supported_point_formats( ssl_context *ssl,
583 const unsigned char *buf,
584 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100585{
586 size_t list_size;
587 const unsigned char *p;
588
589 list_size = buf[0];
590 if( list_size + 1 != len )
591 {
592 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
593 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
594 }
595
596 p = buf + 2;
597 while( list_size > 0 )
598 {
599 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
600 p[0] == POLARSSL_ECP_PF_COMPRESSED )
601 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200602 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200603 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100604 return( 0 );
605 }
606
607 list_size--;
608 p++;
609 }
610
611 return( 0 );
612}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200613#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100614
Paul Bakker05decb22013-08-15 13:33:48 +0200615#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200616static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
617 const unsigned char *buf,
618 size_t len )
619{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200620 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200621 {
622 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
623 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
624 }
625
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200626 ssl->session_negotiate->mfl_code = buf[0];
627
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200628 return( 0 );
629}
Paul Bakker05decb22013-08-15 13:33:48 +0200630#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200631
Paul Bakker1f2bc622013-08-15 13:45:55 +0200632#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200633static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
634 const unsigned char *buf,
635 size_t len )
636{
637 if( len != 0 )
638 {
639 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
640 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
641 }
642
643 ((void) buf);
644
645 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
646
647 return( 0 );
648}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200649#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200650
Paul Bakkera503a632013-08-14 13:48:06 +0200651#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200652static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200653 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200654 size_t len )
655{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200656 int ret;
657
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200658 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
659 return( 0 );
660
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200661 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200662 ssl->handshake->new_session_ticket = 1;
663
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200664 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
665
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200666 if( len == 0 )
667 return( 0 );
668
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200669 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
670 {
671 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
672 return( 0 );
673 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200674
675 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200676 * Failures are ok: just ignore the ticket and proceed.
677 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200678 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
679 {
680 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200681 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200682 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200683
684 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
685
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200686 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200687
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200688 /* Don't send a new ticket after all, this one is OK */
689 ssl->handshake->new_session_ticket = 0;
690
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200691 return( 0 );
692}
Paul Bakkera503a632013-08-14 13:48:06 +0200693#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200694
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200695#if defined(POLARSSL_SSL_ALPN)
696static int ssl_parse_alpn_ext( ssl_context *ssl,
697 unsigned char *buf, size_t len )
698{
Paul Bakker14b16c62014-05-28 11:33:54 +0200699 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200700 const unsigned char *theirs, *start, *end;
701 const char **ours;
702
703 /* If ALPN not configured, just ignore the extension */
704 if( ssl->alpn_list == NULL )
705 return( 0 );
706
707 /*
708 * opaque ProtocolName<1..2^8-1>;
709 *
710 * struct {
711 * ProtocolName protocol_name_list<2..2^16-1>
712 * } ProtocolNameList;
713 */
714
715 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
716 if( len < 4 )
717 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
718
719 list_len = ( buf[0] << 8 ) | buf[1];
720 if( list_len != len - 2 )
721 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
722
723 /*
724 * Use our order of preference
725 */
726 start = buf + 2;
727 end = buf + len;
728 for( ours = ssl->alpn_list; *ours != NULL; ours++ )
729 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200730 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200731 for( theirs = start; theirs != end; theirs += cur_len )
732 {
733 /* If the list is well formed, we should get equality first */
734 if( theirs > end )
735 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
736
737 cur_len = *theirs++;
738
739 /* Empty strings MUST NOT be included */
740 if( cur_len == 0 )
741 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
742
Paul Bakker14b16c62014-05-28 11:33:54 +0200743 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200744 memcmp( theirs, *ours, cur_len ) == 0 )
745 {
746 ssl->alpn_chosen = *ours;
747 return( 0 );
748 }
749 }
750 }
751
752 /* If we get there, no match was found */
753 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
754 SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
755 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
756}
757#endif /* POLARSSL_SSL_ALPN */
758
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100759/*
760 * Auxiliary functions for ServerHello parsing and related actions
761 */
762
763#if defined(POLARSSL_X509_CRT_PARSE_C)
764/*
765 * Return 1 if the given EC key uses the given curve, 0 otherwise
766 */
767#if defined(POLARSSL_ECDSA_C)
768static int ssl_key_matches_curves( pk_context *pk,
769 const ecp_curve_info **curves )
770{
771 const ecp_curve_info **crv = curves;
772 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
773
774 while( *crv != NULL )
775 {
776 if( (*crv)->grp_id == grp_id )
777 return( 1 );
778 crv++;
779 }
780
781 return( 0 );
782}
783#endif /* POLARSSL_ECDSA_C */
784
785/*
786 * Try picking a certificate for this ciphersuite,
787 * return 0 on success and -1 on failure.
788 */
789static int ssl_pick_cert( ssl_context *ssl,
790 const ssl_ciphersuite_t * ciphersuite_info )
791{
792 ssl_key_cert *cur, *list;
793 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
794
795#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
796 if( ssl->handshake->sni_key_cert != NULL )
797 list = ssl->handshake->sni_key_cert;
798 else
799#endif
800 list = ssl->handshake->key_cert;
801
802 if( pk_alg == POLARSSL_PK_NONE )
803 return( 0 );
804
805 for( cur = list; cur != NULL; cur = cur->next )
806 {
807 if( ! pk_can_do( cur->key, pk_alg ) )
808 continue;
809
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200810 /*
811 * This avoids sending the client a cert it'll reject based on
812 * keyUsage or other extensions.
813 *
814 * It also allows the user to provision different certificates for
815 * different uses based on keyUsage, eg if they want to avoid signing
816 * and decrypting with the same RSA key.
817 */
818 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
819 SSL_IS_SERVER ) != 0 )
820 {
821 continue;
822 }
823
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100824#if defined(POLARSSL_ECDSA_C)
825 if( pk_alg == POLARSSL_PK_ECDSA )
826 {
827 if( ssl_key_matches_curves( cur->key, ssl->handshake->curves ) )
828 break;
829 }
830 else
831#endif
832 break;
833 }
834
835 if( cur == NULL )
836 return( -1 );
837
838 ssl->handshake->key_cert = cur;
839 return( 0 );
840}
841#endif /* POLARSSL_X509_CRT_PARSE_C */
842
843/*
844 * Check if a given ciphersuite is suitable for use with our config/keys/etc
845 * Sets ciphersuite_info only if the suite matches.
846 */
847static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
848 const ssl_ciphersuite_t **ciphersuite_info )
849{
850 const ssl_ciphersuite_t *suite_info;
851
852 suite_info = ssl_ciphersuite_from_id( suite_id );
853 if( suite_info == NULL )
854 {
855 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", suite_id ) );
856 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
857 }
858
859 if( suite_info->min_minor_ver > ssl->minor_ver ||
860 suite_info->max_minor_ver < ssl->minor_ver )
861 return( 0 );
862
863#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
864 if( ssl_ciphersuite_uses_ec( suite_info ) &&
865 ( ssl->handshake->curves == NULL ||
866 ssl->handshake->curves[0] == NULL ) )
867 return( 0 );
868#endif
869
870#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
871 /* If the ciphersuite requires a pre-shared key and we don't
872 * have one, skip it now rather than failing later */
873 if( ssl_ciphersuite_uses_psk( suite_info ) &&
874 ssl->f_psk == NULL &&
875 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
876 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
877 return( 0 );
878#endif
879
880#if defined(POLARSSL_X509_CRT_PARSE_C)
881 /*
882 * Final check: if ciphersuite requires us to have a
883 * certificate/key of a particular type:
884 * - select the appropriate certificate if we have one, or
885 * - try the next ciphersuite if we don't
886 * This must be done last since we modify the key_cert list.
887 */
888 if( ssl_pick_cert( ssl, suite_info ) != 0 )
889 return( 0 );
890#endif
891
892 *ciphersuite_info = suite_info;
893 return( 0 );
894}
895
Paul Bakker78a8c712013-03-06 17:01:52 +0100896#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
897static int ssl_parse_client_hello_v2( ssl_context *ssl )
898{
899 int ret;
900 unsigned int i, j;
901 size_t n;
902 unsigned int ciph_len, sess_len, chal_len;
903 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200904 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200905 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100906
907 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
908
909 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
910 {
911 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
912
913 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
914 return( ret );
915
916 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
917 }
918
919 buf = ssl->in_hdr;
920
921 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
922
923 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
924 buf[2] ) );
925 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
926 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
927 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
928 buf[3], buf[4] ) );
929
930 /*
931 * SSLv2 Client Hello
932 *
933 * Record layer:
934 * 0 . 1 message length
935 *
936 * SSL layer:
937 * 2 . 2 message type
938 * 3 . 4 protocol version
939 */
940 if( buf[2] != SSL_HS_CLIENT_HELLO ||
941 buf[3] != SSL_MAJOR_VERSION_3 )
942 {
943 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
944 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
945 }
946
947 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
948
949 if( n < 17 || n > 512 )
950 {
951 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
952 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
953 }
954
955 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200956 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
957 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100958
959 if( ssl->minor_ver < ssl->min_minor_ver )
960 {
961 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200962 " [%d:%d] < [%d:%d]",
963 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +0100964 ssl->min_major_ver, ssl->min_minor_ver ) );
965
966 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
967 SSL_ALERT_MSG_PROTOCOL_VERSION );
968 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
969 }
970
Paul Bakker2fbefde2013-06-29 16:01:15 +0200971 ssl->handshake->max_major_ver = buf[3];
972 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100973
974 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
975 {
976 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
977 return( ret );
978 }
979
980 ssl->handshake->update_checksum( ssl, buf + 2, n );
981
982 buf = ssl->in_msg;
983 n = ssl->in_left - 5;
984
985 /*
986 * 0 . 1 ciphersuitelist length
987 * 2 . 3 session id length
988 * 4 . 5 challenge length
989 * 6 . .. ciphersuitelist
990 * .. . .. session id
991 * .. . .. challenge
992 */
993 SSL_DEBUG_BUF( 4, "record contents", buf, n );
994
995 ciph_len = ( buf[0] << 8 ) | buf[1];
996 sess_len = ( buf[2] << 8 ) | buf[3];
997 chal_len = ( buf[4] << 8 ) | buf[5];
998
999 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
1000 ciph_len, sess_len, chal_len ) );
1001
1002 /*
1003 * Make sure each parameter length is valid
1004 */
1005 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1006 {
1007 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1008 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1009 }
1010
1011 if( sess_len > 32 )
1012 {
1013 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1014 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1015 }
1016
1017 if( chal_len < 8 || chal_len > 32 )
1018 {
1019 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1020 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1021 }
1022
1023 if( n != 6 + ciph_len + sess_len + chal_len )
1024 {
1025 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1026 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1027 }
1028
1029 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1030 buf + 6, ciph_len );
1031 SSL_DEBUG_BUF( 3, "client hello, session id",
1032 buf + 6 + ciph_len, sess_len );
1033 SSL_DEBUG_BUF( 3, "client hello, challenge",
1034 buf + 6 + ciph_len + sess_len, chal_len );
1035
1036 p = buf + 6 + ciph_len;
1037 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001038 memset( ssl->session_negotiate->id, 0,
1039 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001040 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1041
1042 p += sess_len;
1043 memset( ssl->handshake->randbytes, 0, 64 );
1044 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1045
1046 /*
1047 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1048 */
1049 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1050 {
1051 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1052 {
1053 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1054 if( ssl->renegotiation == SSL_RENEGOTIATION )
1055 {
1056 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1057
1058 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1059 return( ret );
1060
1061 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1062 }
1063 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1064 break;
1065 }
1066 }
1067
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001068 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001069 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001070#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1071 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1072 {
1073 for( i = 0; ciphersuites[i] != 0; i++ )
1074#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001075 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001076 {
1077 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001078#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001079 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001080 if( p[0] != 0 ||
1081 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1082 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1083 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001084
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001085 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1086 &ciphersuite_info ) ) != 0 )
1087 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001088
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001089 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001090 goto have_ciphersuite_v2;
1091 }
1092 }
1093
1094 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1095
1096 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1097
1098have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001099 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001100 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001101 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001102
1103 /*
1104 * SSLv2 Client Hello relevant renegotiation security checks
1105 */
1106 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1107 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1108 {
1109 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1110
1111 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1112 return( ret );
1113
1114 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1115 }
1116
1117 ssl->in_left = 0;
1118 ssl->state++;
1119
1120 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1121
1122 return( 0 );
1123}
1124#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1125
Paul Bakker5121ce52009-01-03 21:22:43 +00001126static int ssl_parse_client_hello( ssl_context *ssl )
1127{
Paul Bakker23986e52011-04-24 08:57:21 +00001128 int ret;
1129 unsigned int i, j;
1130 size_t n;
1131 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001132 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001133 unsigned int ext_len = 0;
1134 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001135 int renegotiation_info_seen = 0;
1136 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001137 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001138 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001139
1140 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1141
Paul Bakker48916f92012-09-16 19:57:18 +00001142 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1143 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001144 {
1145 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1146 return( ret );
1147 }
1148
1149 buf = ssl->in_hdr;
1150
Paul Bakker78a8c712013-03-06 17:01:52 +01001151#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1152 if( ( buf[0] & 0x80 ) != 0 )
1153 return ssl_parse_client_hello_v2( ssl );
1154#endif
1155
Paul Bakkerec636f32012-09-09 19:17:02 +00001156 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1157
1158 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1159 buf[0] ) );
1160 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1161 ( buf[3] << 8 ) | buf[4] ) );
1162 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
1163 buf[1], buf[2] ) );
1164
1165 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001166 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001167 *
1168 * Record layer:
1169 * 0 . 0 message type
1170 * 1 . 2 protocol version
1171 * 3 . 4 message length
1172 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001173
1174 /* According to RFC 5246 Appendix E.1, the version here is typically
1175 * "{03,00}, the lowest version number supported by the client, [or] the
1176 * value of ClientHello.client_version", so the only meaningful check here
1177 * is the major version shouldn't be less than 3 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001178 if( buf[0] != SSL_MSG_HANDSHAKE ||
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001179 buf[1] < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001180 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001181 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1182 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1183 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001184
Paul Bakkerec636f32012-09-09 19:17:02 +00001185 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +00001186
Paul Bakker4f42c112014-04-17 14:48:23 +02001187 if( n < 45 || n > SSL_MAX_CONTENT_LEN )
Paul Bakkerec636f32012-09-09 19:17:02 +00001188 {
1189 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1190 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1191 }
1192
Paul Bakker48916f92012-09-16 19:57:18 +00001193 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1194 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001195 {
1196 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1197 return( ret );
1198 }
1199
1200 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +00001201 if( !ssl->renegotiation )
1202 n = ssl->in_left - 5;
1203 else
1204 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +00001205
Paul Bakker48916f92012-09-16 19:57:18 +00001206 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +00001207
1208 /*
1209 * SSL layer:
1210 * 0 . 0 handshake type
1211 * 1 . 3 handshake length
1212 * 4 . 5 protocol version
1213 * 6 . 9 UNIX time()
1214 * 10 . 37 random bytes
1215 * 38 . 38 session id length
1216 * 39 . 38+x session id
1217 * 39+x . 40+x ciphersuitelist length
Paul Bakker0f651c72014-05-22 15:12:19 +02001218 * 41+x . 40+y ciphersuitelist
1219 * 41+y . 41+y compression alg length
1220 * 42+y . 41+z compression algs
Paul Bakkerec636f32012-09-09 19:17:02 +00001221 * .. . .. extensions
1222 */
1223 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1224
1225 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1226 buf[0] ) );
1227 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1228 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1229 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1230 buf[4], buf[5] ) );
1231
1232 /*
1233 * Check the handshake type and protocol version
1234 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001235 if( buf[0] != SSL_HS_CLIENT_HELLO )
Paul Bakkerec636f32012-09-09 19:17:02 +00001236 {
1237 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1238 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1239 }
1240
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001241 ssl->major_ver = buf[4];
1242 ssl->minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001243
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001244 ssl->handshake->max_major_ver = ssl->major_ver;
1245 ssl->handshake->max_minor_ver = ssl->minor_ver;
1246
1247 if( ssl->major_ver < ssl->min_major_ver ||
1248 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001249 {
1250 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001251 " [%d:%d] < [%d:%d]",
1252 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001253 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001254
1255 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1256 SSL_ALERT_MSG_PROTOCOL_VERSION );
1257
1258 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1259 }
1260
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001261 if( ssl->major_ver > ssl->max_major_ver )
1262 {
1263 ssl->major_ver = ssl->max_major_ver;
1264 ssl->minor_ver = ssl->max_minor_ver;
1265 }
1266 else if( ssl->minor_ver > ssl->max_minor_ver )
1267 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001268
Paul Bakker48916f92012-09-16 19:57:18 +00001269 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001270
1271 /*
1272 * Check the handshake message length
1273 */
1274 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1275 {
1276 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1277 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1278 }
1279
1280 /*
1281 * Check the session length
1282 */
1283 sess_len = buf[38];
1284
Paul Bakker0f651c72014-05-22 15:12:19 +02001285 if( sess_len > 32 || sess_len > n - 42 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001286 {
1287 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1288 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1289 }
1290
Paul Bakker48916f92012-09-16 19:57:18 +00001291 ssl->session_negotiate->length = sess_len;
1292 memset( ssl->session_negotiate->id, 0,
1293 sizeof( ssl->session_negotiate->id ) );
1294 memcpy( ssl->session_negotiate->id, buf + 39,
1295 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001296
1297 /*
1298 * Check the ciphersuitelist length
1299 */
1300 ciph_len = ( buf[39 + sess_len] << 8 )
1301 | ( buf[40 + sess_len] );
1302
Paul Bakker0f651c72014-05-22 15:12:19 +02001303 if( ciph_len < 2 || ( ciph_len % 2 ) != 0 || ciph_len > n - 42 - sess_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001304 {
1305 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1306 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1307 }
1308
1309 /*
1310 * Check the compression algorithms length
1311 */
1312 comp_len = buf[41 + sess_len + ciph_len];
1313
Paul Bakker0f651c72014-05-22 15:12:19 +02001314 if( comp_len < 1 || comp_len > 16 ||
1315 comp_len > n - 42 - sess_len - ciph_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001316 {
1317 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1318 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1319 }
1320
Paul Bakker48916f92012-09-16 19:57:18 +00001321 /*
1322 * Check the extension length
1323 */
1324 if( n > 42 + sess_len + ciph_len + comp_len )
1325 {
1326 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1327 | ( buf[43 + sess_len + ciph_len + comp_len] );
1328
1329 if( ( ext_len > 0 && ext_len < 4 ) ||
1330 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1331 {
1332 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1333 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1334 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1335 }
1336 }
1337
1338 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001339#if defined(POLARSSL_ZLIB_SUPPORT)
1340 for( i = 0; i < comp_len; ++i )
1341 {
Paul Bakker48916f92012-09-16 19:57:18 +00001342 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001343 {
Paul Bakker48916f92012-09-16 19:57:18 +00001344 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001345 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001346 }
1347 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001348#endif
1349
Paul Bakkerec636f32012-09-09 19:17:02 +00001350 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1351 buf + 6, 32 );
1352 SSL_DEBUG_BUF( 3, "client hello, session id",
1353 buf + 38, sess_len );
1354 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1355 buf + 41 + sess_len, ciph_len );
1356 SSL_DEBUG_BUF( 3, "client hello, compression",
1357 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001358
Paul Bakkerec636f32012-09-09 19:17:02 +00001359 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001360 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1361 */
1362 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1363 {
1364 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1365 {
1366 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1367 if( ssl->renegotiation == SSL_RENEGOTIATION )
1368 {
1369 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001370
1371 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1372 return( ret );
1373
Paul Bakker48916f92012-09-16 19:57:18 +00001374 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1375 }
1376 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1377 break;
1378 }
1379 }
1380
Paul Bakker48916f92012-09-16 19:57:18 +00001381 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001382
1383 while( ext_len )
1384 {
1385 unsigned int ext_id = ( ( ext[0] << 8 )
1386 | ( ext[1] ) );
1387 unsigned int ext_size = ( ( ext[2] << 8 )
1388 | ( ext[3] ) );
1389
1390 if( ext_size + 4 > ext_len )
1391 {
1392 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1393 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1394 }
1395 switch( ext_id )
1396 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001397#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001398 case TLS_EXT_SERVERNAME:
1399 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1400 if( ssl->f_sni == NULL )
1401 break;
1402
1403 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1404 if( ret != 0 )
1405 return( ret );
1406 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001407#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001408
Paul Bakker48916f92012-09-16 19:57:18 +00001409 case TLS_EXT_RENEGOTIATION_INFO:
1410 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1411 renegotiation_info_seen = 1;
1412
Paul Bakker23f36802012-09-28 14:15:14 +00001413 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1414 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001415 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001416 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001417
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001418#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001419 case TLS_EXT_SIG_ALG:
1420 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1421 if( ssl->renegotiation == SSL_RENEGOTIATION )
1422 break;
1423
1424 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1425 if( ret != 0 )
1426 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001427 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001428#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001429
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001430#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001431 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1432 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1433
1434 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1435 if( ret != 0 )
1436 return( ret );
1437 break;
1438
1439 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1440 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001441 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001442
1443 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1444 if( ret != 0 )
1445 return( ret );
1446 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001447#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001448
Paul Bakker05decb22013-08-15 13:33:48 +02001449#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001450 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1451 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1452
1453 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1454 if( ret != 0 )
1455 return( ret );
1456 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001457#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001458
Paul Bakker1f2bc622013-08-15 13:45:55 +02001459#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001460 case TLS_EXT_TRUNCATED_HMAC:
1461 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1462
1463 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1464 if( ret != 0 )
1465 return( ret );
1466 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001467#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001468
Paul Bakkera503a632013-08-14 13:48:06 +02001469#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001470 case TLS_EXT_SESSION_TICKET:
1471 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1472
1473 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1474 if( ret != 0 )
1475 return( ret );
1476 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001477#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001478
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001479#if defined(POLARSSL_SSL_ALPN)
1480 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001481 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001482
1483 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1484 if( ret != 0 )
1485 return( ret );
1486 break;
1487#endif /* POLARSSL_SSL_SESSION_TICKETS */
1488
Paul Bakker48916f92012-09-16 19:57:18 +00001489 default:
1490 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1491 ext_id ) );
1492 }
1493
1494 ext_len -= 4 + ext_size;
1495 ext += 4 + ext_size;
1496
1497 if( ext_len > 0 && ext_len < 4 )
1498 {
1499 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1500 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1501 }
1502 }
1503
1504 /*
1505 * Renegotiation security checks
1506 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001507 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1508 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1509 {
1510 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1511 handshake_failure = 1;
1512 }
1513 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1514 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1515 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001516 {
1517 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001518 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001519 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001520 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1521 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1522 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001523 {
1524 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001525 handshake_failure = 1;
1526 }
1527 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1528 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1529 renegotiation_info_seen == 1 )
1530 {
1531 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1532 handshake_failure = 1;
1533 }
1534
1535 if( handshake_failure == 1 )
1536 {
1537 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1538 return( ret );
1539
Paul Bakker48916f92012-09-16 19:57:18 +00001540 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1541 }
Paul Bakker380da532012-04-18 16:10:25 +00001542
Paul Bakker41c83d32013-03-20 14:39:14 +01001543 /*
1544 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001545 * (At the end because we need information from the EC-based extensions
1546 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001547 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001548 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001549 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001550#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1551 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
1552 {
1553 for( i = 0; ciphersuites[i] != 0; i++ )
1554#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001555 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001556 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001557 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001558#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001559 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001560 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1561 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1562 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001563
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001564 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1565 &ciphersuite_info ) ) != 0 )
1566 return( ret );
1567
1568 if( ciphersuite_info != NULL )
1569 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001570 }
1571 }
1572
1573 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1574
1575 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1576 return( ret );
1577
1578 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1579
1580have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001581 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001582 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1583 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1584
Paul Bakker5121ce52009-01-03 21:22:43 +00001585 ssl->in_left = 0;
1586 ssl->state++;
1587
1588 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1589
1590 return( 0 );
1591}
1592
Paul Bakker1f2bc622013-08-15 13:45:55 +02001593#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001594static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1595 unsigned char *buf,
1596 size_t *olen )
1597{
1598 unsigned char *p = buf;
1599
1600 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1601 {
1602 *olen = 0;
1603 return;
1604 }
1605
1606 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1607
1608 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1609 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1610
1611 *p++ = 0x00;
1612 *p++ = 0x00;
1613
1614 *olen = 4;
1615}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001616#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001617
Paul Bakkera503a632013-08-14 13:48:06 +02001618#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001619static void ssl_write_session_ticket_ext( ssl_context *ssl,
1620 unsigned char *buf,
1621 size_t *olen )
1622{
1623 unsigned char *p = buf;
1624
1625 if( ssl->handshake->new_session_ticket == 0 )
1626 {
1627 *olen = 0;
1628 return;
1629 }
1630
1631 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1632
1633 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1634 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1635
1636 *p++ = 0x00;
1637 *p++ = 0x00;
1638
1639 *olen = 4;
1640}
Paul Bakkera503a632013-08-14 13:48:06 +02001641#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001642
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001643static void ssl_write_renegotiation_ext( ssl_context *ssl,
1644 unsigned char *buf,
1645 size_t *olen )
1646{
1647 unsigned char *p = buf;
1648
1649 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1650 {
1651 *olen = 0;
1652 return;
1653 }
1654
1655 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1656
1657 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1658 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1659
1660 *p++ = 0x00;
1661 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1662 *p++ = ssl->verify_data_len * 2 & 0xFF;
1663
1664 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1665 p += ssl->verify_data_len;
1666 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1667 p += ssl->verify_data_len;
1668
1669 *olen = 5 + ssl->verify_data_len * 2;
1670}
1671
Paul Bakker05decb22013-08-15 13:33:48 +02001672#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001673static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1674 unsigned char *buf,
1675 size_t *olen )
1676{
1677 unsigned char *p = buf;
1678
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001679 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1680 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001681 *olen = 0;
1682 return;
1683 }
1684
1685 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1686
1687 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1688 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1689
1690 *p++ = 0x00;
1691 *p++ = 1;
1692
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001693 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001694
1695 *olen = 5;
1696}
Paul Bakker05decb22013-08-15 13:33:48 +02001697#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001698
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001699#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001700static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1701 unsigned char *buf,
1702 size_t *olen )
1703{
1704 unsigned char *p = buf;
1705 ((void) ssl);
1706
Paul Bakker677377f2013-10-28 12:54:26 +01001707 if( ( ssl->handshake->cli_exts &
1708 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
1709 {
1710 *olen = 0;
1711 return;
1712 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001713
1714 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1715
1716 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1717 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1718
1719 *p++ = 0x00;
1720 *p++ = 2;
1721
1722 *p++ = 1;
1723 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1724
1725 *olen = 6;
1726}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001727#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001728
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001729#if defined(POLARSSL_SSL_ALPN )
1730static void ssl_write_alpn_ext( ssl_context *ssl,
1731 unsigned char *buf, size_t *olen )
1732{
1733 if( ssl->alpn_chosen == NULL )
1734 {
1735 *olen = 0;
1736 return;
1737 }
1738
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001739 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001740
1741 /*
1742 * 0 . 1 ext identifier
1743 * 2 . 3 ext length
1744 * 4 . 5 protocol list length
1745 * 6 . 6 protocol name length
1746 * 7 . 7+n protocol name
1747 */
1748 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
1749 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
1750
1751 *olen = 7 + strlen( ssl->alpn_chosen );
1752
1753 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
1754 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
1755
1756 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
1757 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
1758
1759 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
1760
1761 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
1762}
1763#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
1764
Paul Bakker5121ce52009-01-03 21:22:43 +00001765static int ssl_write_server_hello( ssl_context *ssl )
1766{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001767#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001768 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001769#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001770 int ret;
1771 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001772 unsigned char *buf, *p;
1773
1774 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1775
Paul Bakkera9a028e2013-11-21 17:31:06 +01001776 if( ssl->f_rng == NULL )
1777 {
1778 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
1779 return( POLARSSL_ERR_SSL_NO_RNG );
1780 }
1781
Paul Bakker5121ce52009-01-03 21:22:43 +00001782 /*
1783 * 0 . 0 handshake type
1784 * 1 . 3 handshake length
1785 * 4 . 5 protocol version
1786 * 6 . 9 UNIX time()
1787 * 10 . 37 random bytes
1788 */
1789 buf = ssl->out_msg;
1790 p = buf + 4;
1791
1792 *p++ = (unsigned char) ssl->major_ver;
1793 *p++ = (unsigned char) ssl->minor_ver;
1794
1795 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1796 buf[4], buf[5] ) );
1797
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001798#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001799 t = time( NULL );
1800 *p++ = (unsigned char)( t >> 24 );
1801 *p++ = (unsigned char)( t >> 16 );
1802 *p++ = (unsigned char)( t >> 8 );
1803 *p++ = (unsigned char)( t );
1804
1805 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001806#else
1807 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1808 return( ret );
1809
1810 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02001811#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001812
Paul Bakkera3d195c2011-11-27 21:07:34 +00001813 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1814 return( ret );
1815
1816 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001817
Paul Bakker48916f92012-09-16 19:57:18 +00001818 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001819
1820 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1821
1822 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001823 * Resume is 0 by default, see ssl_handshake_init().
1824 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1825 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001826 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001827 if( ssl->handshake->resume == 0 &&
1828 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001829 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001830 ssl->f_get_cache != NULL &&
1831 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1832 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01001833 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001834 ssl->handshake->resume = 1;
1835 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001836
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001837 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001838 {
1839 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001840 * New session, create a new session id,
1841 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001842 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001843 ssl->state++;
1844
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001845#if defined(POLARSSL_HAVE_TIME)
1846 ssl->session_negotiate->start = time( NULL );
1847#endif
1848
Paul Bakkera503a632013-08-14 13:48:06 +02001849#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001850 if( ssl->handshake->new_session_ticket != 0 )
1851 {
1852 ssl->session_negotiate->length = n = 0;
1853 memset( ssl->session_negotiate->id, 0, 32 );
1854 }
1855 else
1856#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001857 {
1858 ssl->session_negotiate->length = n = 32;
1859 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001860 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001861 return( ret );
1862 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001863 }
1864 else
1865 {
1866 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001867 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001868 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001869 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001870 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001871
1872 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1873 {
1874 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1875 return( ret );
1876 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001877 }
1878
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001879 /*
1880 * 38 . 38 session id length
1881 * 39 . 38+n session id
1882 * 39+n . 40+n chosen ciphersuite
1883 * 41+n . 41+n chosen compression alg.
1884 * 42+n . 43+n extensions length
1885 * 44+n . 43+n+m extensions
1886 */
1887 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001888 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1889 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001890
1891 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1892 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1893 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001894 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001895
Paul Bakker48916f92012-09-16 19:57:18 +00001896 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1897 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1898 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001899
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001900 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
1901 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001902 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001903 ssl->session_negotiate->compression ) );
1904
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001905 /*
1906 * First write extensions, then the total length
1907 */
1908 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1909 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001910
Paul Bakker05decb22013-08-15 13:33:48 +02001911#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001912 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1913 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001914#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001915
Paul Bakker1f2bc622013-08-15 13:45:55 +02001916#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001917 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1918 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001919#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001920
Paul Bakkera503a632013-08-14 13:48:06 +02001921#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001922 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1923 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001924#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001925
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001926#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001927 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1928 ext_len += olen;
1929#endif
1930
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001931#if defined(POLARSSL_SSL_ALPN)
1932 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
1933 ext_len += olen;
1934#endif
1935
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001936 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001937
Paul Bakkera7036632014-04-30 10:15:38 +02001938 if( ext_len > 0 )
1939 {
1940 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1941 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1942 p += ext_len;
1943 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001944
1945 ssl->out_msglen = p - buf;
1946 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1947 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1948
1949 ret = ssl_write_record( ssl );
1950
1951 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1952
1953 return( ret );
1954}
1955
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001956#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1957 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001958 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1959 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001960static int ssl_write_certificate_request( ssl_context *ssl )
1961{
Paul Bakkered27a042013-04-18 22:46:23 +02001962 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001963
1964 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1965
1966 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01001967 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001968 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1969 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001970 {
1971 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1972 ssl->state++;
1973 return( 0 );
1974 }
1975
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001976 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1977 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001978}
1979#else
1980static int ssl_write_certificate_request( ssl_context *ssl )
1981{
1982 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1983 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001984 size_t dn_size, total_dn_size; /* excluding length bytes */
1985 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00001986 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001987 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001988
1989 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1990
1991 ssl->state++;
1992
Paul Bakkerfbb17802013-04-17 19:10:21 +02001993 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01001994 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001995 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001996 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001997 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001998 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001999 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002000 return( 0 );
2001 }
2002
2003 /*
2004 * 0 . 0 handshake type
2005 * 1 . 3 handshake length
2006 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002007 * 5 .. m-1 cert types
2008 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002009 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002010 * n .. n+1 length of all DNs
2011 * n+2 .. n+3 length of DN 1
2012 * n+4 .. ... Distinguished Name #1
2013 * ... .. ... length of DN 2, etc.
2014 */
2015 buf = ssl->out_msg;
2016 p = buf + 4;
2017
2018 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002019 * Supported certificate types
2020 *
2021 * ClientCertificateType certificate_types<1..2^8-1>;
2022 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002023 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002024 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002025
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002026#if defined(POLARSSL_RSA_C)
2027 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2028#endif
2029#if defined(POLARSSL_ECDSA_C)
2030 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2031#endif
2032
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002033 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002034 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002035
Paul Bakker577e0062013-08-28 11:57:20 +02002036 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002037#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002038 /*
2039 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002040 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002041 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2042 *
2043 * struct {
2044 * HashAlgorithm hash;
2045 * SignatureAlgorithm signature;
2046 * } SignatureAndHashAlgorithm;
2047 *
2048 * enum { (255) } HashAlgorithm;
2049 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002050 */
Paul Bakker21dca692013-01-03 11:41:08 +01002051 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002052 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002053 /*
2054 * Only use current running hash algorithm that is already required
2055 * for requested ciphersuite.
2056 */
Paul Bakker926af752012-11-23 13:38:07 +01002057 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2058
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002059 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2060 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002061 {
2062 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2063 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002064
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002065 /*
2066 * Supported signature algorithms
2067 */
2068#if defined(POLARSSL_RSA_C)
2069 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2070 p[2 + sa_len++] = SSL_SIG_RSA;
2071#endif
2072#if defined(POLARSSL_ECDSA_C)
2073 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2074 p[2 + sa_len++] = SSL_SIG_ECDSA;
2075#endif
Paul Bakker926af752012-11-23 13:38:07 +01002076
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002077 p[0] = (unsigned char)( sa_len >> 8 );
2078 p[1] = (unsigned char)( sa_len );
2079 sa_len += 2;
2080 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002081 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002082#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002083
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002084 /*
2085 * DistinguishedName certificate_authorities<0..2^16-1>;
2086 * opaque DistinguishedName<1..2^16-1>;
2087 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002088 p += 2;
2089 crt = ssl->ca_chain;
2090
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002091 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002092 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002093 {
2094 if( p - buf > 4096 )
2095 break;
2096
Paul Bakker926af752012-11-23 13:38:07 +01002097 dn_size = crt->subject_raw.len;
2098 *p++ = (unsigned char)( dn_size >> 8 );
2099 *p++ = (unsigned char)( dn_size );
2100 memcpy( p, crt->subject_raw.p, dn_size );
2101 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002102
Paul Bakker926af752012-11-23 13:38:07 +01002103 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2104
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002105 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002106 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002107 }
2108
Paul Bakker926af752012-11-23 13:38:07 +01002109 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2111 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002112 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2113 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002114
2115 ret = ssl_write_record( ssl );
2116
2117 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2118
2119 return( ret );
2120}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002121#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2122 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002123 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2124 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002125
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002126#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2127 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2128static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2129{
2130 int ret;
2131
2132 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2133 {
2134 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2135 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2136 }
2137
2138 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2139 pk_ec( *ssl_own_key( ssl ) ),
2140 POLARSSL_ECDH_OURS ) ) != 0 )
2141 {
2142 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2143 return( ret );
2144 }
2145
2146 return( 0 );
2147}
2148#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2149 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2150
Paul Bakker41c83d32013-03-20 14:39:14 +01002151static int ssl_write_server_key_exchange( ssl_context *ssl )
2152{
Paul Bakker23986e52011-04-24 08:57:21 +00002153 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002154 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002155 const ssl_ciphersuite_t *ciphersuite_info =
2156 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002157
2158#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2159 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2160 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002161 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002162 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002163 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002164 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002165 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002166 ((void) dig_signed);
2167 ((void) dig_signed_len);
2168#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002169
Paul Bakker5121ce52009-01-03 21:22:43 +00002170 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2171
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002172#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2173 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2174 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002175 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2176 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2177 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002178 {
2179 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2180 ssl->state++;
2181 return( 0 );
2182 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002183#endif
2184
2185#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2186 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2187 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2188 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2189 {
2190 ssl_get_ecdh_params_from_cert( ssl );
2191
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002192 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002193 ssl->state++;
2194 return( 0 );
2195 }
2196#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002197
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002198#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2199 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2200 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2201 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002202 {
2203 /* TODO: Support identity hints */
2204 *(p++) = 0x00;
2205 *(p++) = 0x00;
2206
2207 n += 2;
2208 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002209#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2210 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002211
2212#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2213 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2214 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2215 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002216 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002217 /*
2218 * Ephemeral DH parameters:
2219 *
2220 * struct {
2221 * opaque dh_p<1..2^16-1>;
2222 * opaque dh_g<1..2^16-1>;
2223 * opaque dh_Ys<1..2^16-1>;
2224 * } ServerDHParams;
2225 */
2226 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2227 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2228 {
2229 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2230 return( ret );
2231 }
Paul Bakker48916f92012-09-16 19:57:18 +00002232
Paul Bakker41c83d32013-03-20 14:39:14 +01002233 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002234 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2235 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002236 {
2237 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2238 return( ret );
2239 }
2240
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002241 dig_signed = p;
2242 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002243
2244 p += len;
2245 n += len;
2246
Paul Bakker41c83d32013-03-20 14:39:14 +01002247 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2248 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2249 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2250 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2251 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002252#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2253 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002254
Gergely Budai987bfb52014-01-19 21:48:42 +01002255#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002256 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002257 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2258 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002259 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002260 /*
2261 * Ephemeral ECDH parameters:
2262 *
2263 * struct {
2264 * ECParameters curve_params;
2265 * ECPoint public;
2266 * } ServerECDHParams;
2267 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002268 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002269#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002270 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002271
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002272 /* Match our preference list against the offered curves */
2273 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2274 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2275 if( (*curve)->grp_id == *gid )
2276 goto curve_matching_done;
2277
2278curve_matching_done:
2279#else
2280 curve = ssl->handshake->curves;
2281#endif
2282
2283 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002284 {
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002285 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2286 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002287 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002288
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002289 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002290
Paul Bakker41c83d32013-03-20 14:39:14 +01002291 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002292 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002293 {
2294 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2295 return( ret );
2296 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002297
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002298 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2299 p, SSL_MAX_CONTENT_LEN - n,
2300 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002301 {
2302 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2303 return( ret );
2304 }
2305
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002306 dig_signed = p;
2307 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002308
2309 p += len;
2310 n += len;
2311
Paul Bakker41c83d32013-03-20 14:39:14 +01002312 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2313 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002314#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002315
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002316#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002317 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2318 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002319 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002320 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2321 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002322 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002323 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002324 unsigned int hashlen = 0;
2325 unsigned char hash[64];
2326 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002327
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002328 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002329 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2330 */
Paul Bakker577e0062013-08-28 11:57:20 +02002331#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002332 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2333 {
2334 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2335
2336 if( md_alg == POLARSSL_MD_NONE )
2337 {
2338 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002339 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002340 }
2341 }
Paul Bakker577e0062013-08-28 11:57:20 +02002342 else
2343#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002344#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2345 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker577e0062013-08-28 11:57:20 +02002346 if ( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002347 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2348 {
2349 md_alg = POLARSSL_MD_SHA1;
2350 }
2351 else
Paul Bakker577e0062013-08-28 11:57:20 +02002352#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002353 {
2354 md_alg = POLARSSL_MD_NONE;
2355 }
2356
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002357 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002358 * Compute the hash to be signed
2359 */
Paul Bakker577e0062013-08-28 11:57:20 +02002360#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2361 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002362 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002363 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002364 md5_context md5;
2365 sha1_context sha1;
2366
2367 /*
2368 * digitally-signed struct {
2369 * opaque md5_hash[16];
2370 * opaque sha_hash[20];
2371 * };
2372 *
2373 * md5_hash
2374 * MD5(ClientHello.random + ServerHello.random
2375 * + ServerParams);
2376 * sha_hash
2377 * SHA(ClientHello.random + ServerHello.random
2378 * + ServerParams);
2379 */
2380 md5_starts( &md5 );
2381 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002382 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002383 md5_finish( &md5, hash );
2384
2385 sha1_starts( &sha1 );
2386 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002387 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002388 sha1_finish( &sha1, hash + 16 );
2389
2390 hashlen = 36;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002391 }
2392 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002393#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2394 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002395#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2396 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002397 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002398 {
2399 md_context_t ctx;
2400
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002401 /* Info from md_alg will be used instead */
2402 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002403
2404 /*
2405 * digitally-signed struct {
2406 * opaque client_random[32];
2407 * opaque server_random[32];
2408 * ServerDHParams params;
2409 * };
2410 */
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002411 if( ( ret = md_init_ctx( &ctx, md_info_from_type(md_alg) ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002412 {
2413 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2414 return( ret );
2415 }
2416
2417 md_starts( &ctx );
2418 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002419 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002420 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02002421
2422 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
2423 {
2424 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
2425 return( ret );
2426 }
2427
Paul Bakker23f36802012-09-28 14:15:14 +00002428 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002429 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002430#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2431 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002432 {
2433 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002434 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002435 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002436
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002437 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2438 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002439
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002440 /*
2441 * Make the signature
2442 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002443 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002444 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002445 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2446 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002447 }
Paul Bakker23f36802012-09-28 14:15:14 +00002448
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002449#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002450 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2451 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002452 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002453 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002454
2455 n += 2;
2456 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002457#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002458
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002459 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002460 p + 2 , &signature_len,
2461 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002462 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002463 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002464 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002465 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002466
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002467 *(p++) = (unsigned char)( signature_len >> 8 );
2468 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002469 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002470
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002471 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002472
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002473 p += signature_len;
2474 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002475 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002476#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002477 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2478 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002479
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002480 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002481 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2482 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2483
2484 ssl->state++;
2485
2486 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2487 {
2488 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2489 return( ret );
2490 }
2491
2492 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2493
2494 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002495}
2496
2497static int ssl_write_server_hello_done( ssl_context *ssl )
2498{
2499 int ret;
2500
2501 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2502
2503 ssl->out_msglen = 4;
2504 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2505 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2506
2507 ssl->state++;
2508
2509 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2510 {
2511 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2512 return( ret );
2513 }
2514
2515 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2516
2517 return( 0 );
2518}
2519
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002520#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2521 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2522static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2523 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002524{
2525 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002526 size_t n;
2527
2528 /*
2529 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2530 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002531 if( *p + 2 > end )
2532 {
2533 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2534 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2535 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002536
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002537 n = ( (*p)[0] << 8 ) | (*p)[1];
2538 *p += 2;
2539
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002540 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002541 {
2542 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2543 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2544 }
2545
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002546 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002547 {
2548 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2549 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2550 }
2551
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002552 *p += n;
2553
Paul Bakker70df2fb2013-04-17 17:19:09 +02002554 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2555
Paul Bakker70df2fb2013-04-17 17:19:09 +02002556 return( ret );
2557}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002558#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2559 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002560
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002561#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2562 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002563static int ssl_parse_encrypted_pms( ssl_context *ssl,
2564 const unsigned char *p,
2565 const unsigned char *end,
2566 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002567{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002568 int ret;
2569 size_t len = pk_get_len( ssl_own_key( ssl ) );
2570 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002571
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002572 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002573 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002574 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002575 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2576 }
2577
2578 /*
2579 * Decrypt the premaster using own private RSA key
2580 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002581#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2582 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002583 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2584 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002585 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2586 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002587 {
2588 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2589 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2590 }
2591 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002592#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002593
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002594 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002595 {
2596 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2597 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2598 }
2599
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002600 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2601 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002602 sizeof( ssl->handshake->premaster ) - pms_offset,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002603 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002604
2605 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002606 pms[0] != ssl->handshake->max_major_ver ||
2607 pms[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002608 {
2609 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2610
2611 /*
2612 * Protection against Bleichenbacher's attack:
2613 * invalid PKCS#1 v1.5 padding must not cause
2614 * the connection to end immediately; instead,
2615 * send a bad_record_mac later in the handshake.
2616 */
2617 ssl->handshake->pmslen = 48;
2618
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002619 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002620 if( ret != 0 )
2621 return( ret );
2622 }
2623
2624 return( ret );
2625}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002626#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2627 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002628
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002629#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002630static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2631 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002632{
Paul Bakker6db455e2013-09-18 17:29:31 +02002633 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002634 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002635
Paul Bakker6db455e2013-09-18 17:29:31 +02002636 if( ssl->f_psk == NULL &&
2637 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2638 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002639 {
2640 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2641 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2642 }
2643
2644 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002645 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002646 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002647 if( *p + 2 > end )
2648 {
2649 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2650 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2651 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002652
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002653 n = ( (*p)[0] << 8 ) | (*p)[1];
2654 *p += 2;
2655
2656 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002657 {
2658 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2659 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2660 }
2661
Paul Bakker6db455e2013-09-18 17:29:31 +02002662 if( ssl->f_psk != NULL )
2663 {
2664 if( ( ret != ssl->f_psk( ssl->p_psk, ssl, *p, n ) ) != 0 )
2665 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2666 }
2667
2668 if( ret == 0 )
2669 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002670 /* Identity is not a big secret since clients send it in the clear,
2671 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02002672 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002673 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002674 {
2675 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2676 }
2677 }
2678
2679 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002680 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002681 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002682 if( ( ret = ssl_send_alert_message( ssl,
2683 SSL_ALERT_LEVEL_FATAL,
2684 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2685 {
2686 return( ret );
2687 }
2688
2689 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002690 }
2691
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002692 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002693 ret = 0;
2694
Paul Bakkerfbb17802013-04-17 19:10:21 +02002695 return( ret );
2696}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002697#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002698
Paul Bakker5121ce52009-01-03 21:22:43 +00002699static int ssl_parse_client_key_exchange( ssl_context *ssl )
2700{
Paul Bakker23986e52011-04-24 08:57:21 +00002701 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002702 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002703
Paul Bakker41c83d32013-03-20 14:39:14 +01002704 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002705
2706 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2707
2708 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2709 {
2710 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2711 return( ret );
2712 }
2713
2714 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2715 {
2716 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002717 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002718 }
2719
2720 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2721 {
2722 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002723 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002724 }
2725
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002726#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002727 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002728 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002729 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002730 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002731
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002732 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002733 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002734 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2735 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002736 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002737
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002738 if( p != end )
2739 {
2740 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2741 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2742 }
2743
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002744 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
2745
2746 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2747 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002748 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002749 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002750 {
2751 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2752 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2753 }
2754
2755 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002756 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002757 else
2758#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002759#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002760 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2761 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2762 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002763 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002764 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2765 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2766 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002767 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002768 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002769 ssl->in_msg + 4, ssl->in_hslen - 4 ) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002770 {
2771 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2772 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2773 }
2774
2775 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2776
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002777 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2778 &ssl->handshake->pmslen,
2779 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002780 POLARSSL_MPI_MAX_SIZE,
2781 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002782 {
2783 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2784 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2785 }
2786
2787 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002788 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002789 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002790#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002791 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2792 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2793 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002794#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2795 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002796 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002797 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002798 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002799
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002800 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002801 {
2802 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2803 return( ret );
2804 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002805
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002806 if( p != end )
2807 {
2808 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2809 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2810 }
2811
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002812 if( ( ret = ssl_psk_derive_premaster( ssl,
2813 ciphersuite_info->key_exchange ) ) != 0 )
2814 {
2815 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2816 return( ret );
2817 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002818 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002819 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002820#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002821#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2822 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2823 {
2824 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002825 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002826
2827 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2828 {
2829 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2830 return( ret );
2831 }
2832
2833 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
2834 {
2835 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
2836 return( ret );
2837 }
2838
2839 if( ( ret = ssl_psk_derive_premaster( ssl,
2840 ciphersuite_info->key_exchange ) ) != 0 )
2841 {
2842 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2843 return( ret );
2844 }
2845 }
2846 else
2847#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002848#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2849 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2850 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002851 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002852 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002853
2854 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2855 {
2856 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2857 return( ret );
2858 }
2859 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2860 {
2861 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2862 return( ret );
2863 }
2864
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002865 if( p != end )
2866 {
2867 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2868 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2869 }
2870
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002871 if( ( ret = ssl_psk_derive_premaster( ssl,
2872 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002873 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002874 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2875 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002876 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002877 }
2878 else
2879#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002880#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2881 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2882 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002883 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002884 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002885
2886 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2887 {
2888 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2889 return( ret );
2890 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002891
2892 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2893 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002894 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002895 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2896 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002897 }
2898
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002899 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2900
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002901 if( ( ret = ssl_psk_derive_premaster( ssl,
2902 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002903 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002904 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002905 return( ret );
2906 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002907 }
2908 else
2909#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002910#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2911 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002912 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002913 if( ( ret = ssl_parse_encrypted_pms( ssl,
2914 ssl->in_msg + 4,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002915 ssl->in_msg + ssl->in_hslen,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002916 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002917 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002918 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002919 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002920 }
2921 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002922 else
2923#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2924 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002925 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002926 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002927 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002928
Paul Bakkerff60ee62010-03-16 21:09:09 +00002929 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2930 {
2931 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2932 return( ret );
2933 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002934
Paul Bakker5121ce52009-01-03 21:22:43 +00002935 ssl->state++;
2936
2937 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2938
2939 return( 0 );
2940}
2941
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002942#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2943 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002944 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2945 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002946static int ssl_parse_certificate_verify( ssl_context *ssl )
2947{
Paul Bakkerfbb17802013-04-17 19:10:21 +02002948 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002949
2950 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2951
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002952 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002953 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002954 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002955 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002956 {
2957 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2958 ssl->state++;
2959 return( 0 );
2960 }
2961
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002962 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2963 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002964}
2965#else
2966static int ssl_parse_certificate_verify( ssl_context *ssl )
2967{
2968 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002969 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002970 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002971 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002972 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02002973#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002974 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02002975#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002976 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002977 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2978
2979 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2980
2981 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002982 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002983 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002984 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2985 {
2986 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2987 ssl->state++;
2988 return( 0 );
2989 }
2990
Paul Bakkered27a042013-04-18 22:46:23 +02002991 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002992 {
2993 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2994 ssl->state++;
2995 return( 0 );
2996 }
2997
Paul Bakker48916f92012-09-16 19:57:18 +00002998 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002999
3000 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3001 {
3002 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3003 return( ret );
3004 }
3005
3006 ssl->state++;
3007
3008 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3009 {
3010 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003011 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003012 }
3013
3014 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
3015 {
3016 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003017 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003018 }
3019
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003020 /*
3021 * 0 . 0 handshake type
3022 * 1 . 3 handshake length
3023 * 4 . 5 sig alg (TLS 1.2 only)
3024 * 4+n . 5+n signature length (n = sa_len)
3025 * 6+n . 6+n+m signature (m = sig_len)
3026 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003027
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003028#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3029 defined(POLARSSL_SSL_PROTO_TLS1_1)
3030 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003031 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003032 sa_len = 0;
3033
Paul Bakkerc70b9822013-04-07 22:00:46 +02003034 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003035 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003036
3037 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3038 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3039 POLARSSL_PK_ECDSA ) )
3040 {
3041 hash_start += 16;
3042 hashlen -= 16;
3043 md_alg = POLARSSL_MD_SHA1;
3044 }
Paul Bakker926af752012-11-23 13:38:07 +01003045 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003046 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003047#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3048 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003049#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3050 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003051 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003052 sa_len = 2;
3053
Paul Bakker5121ce52009-01-03 21:22:43 +00003054 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003055 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003056 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003057 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003058 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003059 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3060 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003061 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3062 }
3063
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003064 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003065
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003066 /* Info from md_alg will be used instead */
3067 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003068
3069 /*
3070 * Signature
3071 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003072 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
3073 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003074 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003075 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3076 " for verify message" ) );
3077 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003078 }
3079
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003080 /*
3081 * Check the certificate's key type matches the signature alg
3082 */
3083 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3084 {
3085 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3086 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3087 }
Paul Bakker577e0062013-08-28 11:57:20 +02003088 }
3089 else
3090#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3091 {
3092 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003093 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003094 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003095
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003096 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01003097
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003098 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003099 {
3100 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003101 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003102 }
3103
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003104 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003105 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003106 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003107 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003108 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003109 return( ret );
3110 }
3111
3112 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3113
Paul Bakkered27a042013-04-18 22:46:23 +02003114 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003115}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003116#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3117 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3118 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003119
Paul Bakkera503a632013-08-14 13:48:06 +02003120#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003121static int ssl_write_new_session_ticket( ssl_context *ssl )
3122{
3123 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003124 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003125 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003126
3127 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3128
3129 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3130 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3131
3132 /*
3133 * struct {
3134 * uint32 ticket_lifetime_hint;
3135 * opaque ticket<0..2^16-1>;
3136 * } NewSessionTicket;
3137 *
3138 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3139 * 8 . 9 ticket_len (n)
3140 * 10 . 9+n ticket content
3141 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003142
3143 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3144 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3145 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3146 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003147
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003148 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3149 {
3150 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3151 tlen = 0;
3152 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003153
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003154 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3155 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003156
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003157 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003158
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003159 /*
3160 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3161 * ChangeCipherSpec share the same state.
3162 */
3163 ssl->handshake->new_session_ticket = 0;
3164
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003165 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3166 {
3167 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3168 return( ret );
3169 }
3170
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003171 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3172
3173 return( 0 );
3174}
Paul Bakkera503a632013-08-14 13:48:06 +02003175#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003176
Paul Bakker5121ce52009-01-03 21:22:43 +00003177/*
Paul Bakker1961b702013-01-25 14:49:24 +01003178 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003179 */
Paul Bakker1961b702013-01-25 14:49:24 +01003180int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003181{
3182 int ret = 0;
3183
Paul Bakker1961b702013-01-25 14:49:24 +01003184 if( ssl->state == SSL_HANDSHAKE_OVER )
3185 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003186
Paul Bakker1961b702013-01-25 14:49:24 +01003187 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3188
3189 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3190 return( ret );
3191
3192 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003193 {
Paul Bakker1961b702013-01-25 14:49:24 +01003194 case SSL_HELLO_REQUEST:
3195 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003196 break;
3197
Paul Bakker1961b702013-01-25 14:49:24 +01003198 /*
3199 * <== ClientHello
3200 */
3201 case SSL_CLIENT_HELLO:
3202 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003203 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003204
3205 /*
3206 * ==> ServerHello
3207 * Certificate
3208 * ( ServerKeyExchange )
3209 * ( CertificateRequest )
3210 * ServerHelloDone
3211 */
3212 case SSL_SERVER_HELLO:
3213 ret = ssl_write_server_hello( ssl );
3214 break;
3215
3216 case SSL_SERVER_CERTIFICATE:
3217 ret = ssl_write_certificate( ssl );
3218 break;
3219
3220 case SSL_SERVER_KEY_EXCHANGE:
3221 ret = ssl_write_server_key_exchange( ssl );
3222 break;
3223
3224 case SSL_CERTIFICATE_REQUEST:
3225 ret = ssl_write_certificate_request( ssl );
3226 break;
3227
3228 case SSL_SERVER_HELLO_DONE:
3229 ret = ssl_write_server_hello_done( ssl );
3230 break;
3231
3232 /*
3233 * <== ( Certificate/Alert )
3234 * ClientKeyExchange
3235 * ( CertificateVerify )
3236 * ChangeCipherSpec
3237 * Finished
3238 */
3239 case SSL_CLIENT_CERTIFICATE:
3240 ret = ssl_parse_certificate( ssl );
3241 break;
3242
3243 case SSL_CLIENT_KEY_EXCHANGE:
3244 ret = ssl_parse_client_key_exchange( ssl );
3245 break;
3246
3247 case SSL_CERTIFICATE_VERIFY:
3248 ret = ssl_parse_certificate_verify( ssl );
3249 break;
3250
3251 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3252 ret = ssl_parse_change_cipher_spec( ssl );
3253 break;
3254
3255 case SSL_CLIENT_FINISHED:
3256 ret = ssl_parse_finished( ssl );
3257 break;
3258
3259 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003260 * ==> ( NewSessionTicket )
3261 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003262 * Finished
3263 */
3264 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003265#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003266 if( ssl->handshake->new_session_ticket != 0 )
3267 ret = ssl_write_new_session_ticket( ssl );
3268 else
Paul Bakkera503a632013-08-14 13:48:06 +02003269#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003270 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003271 break;
3272
3273 case SSL_SERVER_FINISHED:
3274 ret = ssl_write_finished( ssl );
3275 break;
3276
3277 case SSL_FLUSH_BUFFERS:
3278 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3279 ssl->state = SSL_HANDSHAKE_WRAPUP;
3280 break;
3281
3282 case SSL_HANDSHAKE_WRAPUP:
3283 ssl_handshake_wrapup( ssl );
3284 break;
3285
3286 default:
3287 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3288 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003289 }
3290
Paul Bakker5121ce52009-01-03 21:22:43 +00003291 return( ret );
3292}
Paul Bakker9af723c2014-05-01 13:03:14 +02003293#endif /* POLARSSL_SSL_SRV_C */