blob: 9d5017541ac3394ba7664be07c3001494c0d07ec [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 server-side functions
3 *
Paul Bakker68884e32013-01-07 18:20:04 +01004 * Copyright (C) 2006-2013, 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
Paul Bakker40e46942009-01-03 21:51:57 +000026#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Paul Bakker40e46942009-01-03 21:51:57 +000028#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakker40e46942009-01-03 21:51:57 +000030#include "polarssl/debug.h"
31#include "polarssl/ssl.h"
Paul Bakker41c83d32013-03-20 14:39:14 +010032#if defined(POLARSSL_ECP_C)
33#include "polarssl/ecp.h"
34#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020036#if defined(POLARSSL_MEMORY_C)
37#include "polarssl/memory.h"
38#else
39#define polarssl_malloc malloc
40#define polarssl_free free
41#endif
42
Paul Bakker5121ce52009-01-03 21:22:43 +000043#include <stdlib.h>
44#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020045
46#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000047#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020048#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000049
Paul Bakkera503a632013-08-14 13:48:06 +020050#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020051/*
52 * Serialize a session in the following format:
53 * 0 . n-1 session structure, n = sizeof(ssl_session)
54 * n . n+2 peer_cert length = m (0 if no certificate)
55 * n+3 . n+2+m peer cert ASN.1
56 *
57 * Assumes ticket is NULL (always true on server side).
58 */
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020059static int ssl_save_session( const ssl_session *session,
60 unsigned char *buf, size_t buf_len,
61 size_t *olen )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020062{
63 unsigned char *p = buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020064 size_t left = buf_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020065#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020066 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020068
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020069 if( left < sizeof( ssl_session ) )
70 return( -1 );
71
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020072 memcpy( p, session, sizeof( ssl_session ) );
73 p += sizeof( ssl_session );
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020074 left -= sizeof( ssl_session );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020075
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020077 ((ssl_session *) buf)->peer_cert = NULL;
78
79 if( session->peer_cert == NULL )
80 cert_len = 0;
81 else
82 cert_len = session->peer_cert->raw.len;
83
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020084 if( left < 3 + cert_len )
85 return( -1 );
86
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020087 *p++ = (unsigned char)( cert_len >> 16 & 0xFF );
88 *p++ = (unsigned char)( cert_len >> 8 & 0xFF );
89 *p++ = (unsigned char)( cert_len & 0xFF );
90
91 if( session->peer_cert != NULL )
92 memcpy( p, session->peer_cert->raw.p, cert_len );
93
94 p += cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020095#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020096
97 *olen = p - buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020098
99 return( 0 );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200100}
101
102/*
103 * Unserialise session, see ssl_save_session()
104 */
105static int ssl_load_session( ssl_session *session,
106 const unsigned char *buf, size_t len )
107{
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200108 const unsigned char *p = buf;
109 const unsigned char * const end = buf + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200110#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200111 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200112#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200113
114 if( p + sizeof( ssl_session ) > end )
115 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
116
117 memcpy( session, p, sizeof( ssl_session ) );
118 p += sizeof( ssl_session );
119
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200120#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200121 if( p + 3 > end )
122 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
123
124 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
125 p += 3;
126
127 if( cert_len == 0 )
128 {
129 session->peer_cert = NULL;
130 }
131 else
132 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200133 int ret;
134
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200135 if( p + cert_len > end )
136 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
137
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200138 session->peer_cert = polarssl_malloc( sizeof( x509_crt ) );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200139
140 if( session->peer_cert == NULL )
141 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
142
Paul Bakkerb6b09562013-09-18 14:17:41 +0200143 x509_crt_init( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200144
Paul Bakkerddf26b42013-09-18 13:46:23 +0200145 if( ( ret = x509_crt_parse( session->peer_cert, p, cert_len ) ) != 0 )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200146 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200147 x509_crt_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200148 polarssl_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200149 session->peer_cert = NULL;
150 return( ret );
151 }
152
153 p += cert_len;
154 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200155#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200156
157 if( p != end )
158 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
159
160 return( 0 );
161}
162
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200163/*
164 * Create session ticket, secured as recommended in RFC 5077 section 4:
165 *
166 * struct {
167 * opaque key_name[16];
168 * opaque iv[16];
169 * opaque encrypted_state<0..2^16-1>;
170 * opaque mac[32];
171 * } ticket;
172 *
173 * (the internal state structure differs, however).
174 */
175static int ssl_write_ticket( ssl_context *ssl, size_t *tlen )
176{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200177 int ret;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200178 unsigned char * const start = ssl->out_msg + 10;
179 unsigned char *p = start;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200180 unsigned char *state;
181 unsigned char iv[16];
182 size_t clear_len, enc_len, pad_len, i;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200183
Manuel Pégourié-Gonnard0a201712013-08-23 16:25:16 +0200184 *tlen = 0;
185
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200186 if( ssl->ticket_keys == NULL )
187 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
188
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200189 /* Write key name */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200190 memcpy( p, ssl->ticket_keys->key_name, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200191 p += 16;
192
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200193 /* Generate and write IV (with a copy for aes_crypt) */
194 if( ( ret = ssl->f_rng( ssl->p_rng, p, 16 ) ) != 0 )
195 return( ret );
196 memcpy( iv, p, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200197 p += 16;
198
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200199 /*
200 * Dump session state
201 *
202 * After the session state itself, we still need room for 16 bytes of
203 * padding and 32 bytes of MAC, so there's only so much room left
204 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200205 state = p + 2;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200206 if( ssl_save_session( ssl->session_negotiate, state,
207 SSL_MAX_CONTENT_LEN - (state - ssl->out_ctr) - 48,
208 &clear_len ) != 0 )
209 {
210 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
211 }
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200212 SSL_DEBUG_BUF( 3, "session ticket cleartext", state, clear_len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200213
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200214 /* Apply PKCS padding */
215 pad_len = 16 - clear_len % 16;
216 enc_len = clear_len + pad_len;
217 for( i = clear_len; i < enc_len; i++ )
218 state[i] = (unsigned char) pad_len;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200219
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200220 /* Encrypt */
221 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->enc, AES_ENCRYPT,
222 enc_len, iv, state, state ) ) != 0 )
223 {
224 return( ret );
225 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200226
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200227 /* Write length */
228 *p++ = (unsigned char)( ( enc_len >> 8 ) & 0xFF );
229 *p++ = (unsigned char)( ( enc_len ) & 0xFF );
230 p = state + enc_len;
231
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200232 /* Compute and write MAC( key_name + iv + enc_state_len + enc_state ) */
233 sha256_hmac( ssl->ticket_keys->mac_key, 16, start, p - start, p, 0 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200234 p += 32;
235
236 *tlen = p - start;
237
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200238 SSL_DEBUG_BUF( 3, "session ticket structure", start, *tlen );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200239
240 return( 0 );
241}
242
243/*
244 * Load session ticket (see ssl_write_ticket for structure)
245 */
246static int ssl_parse_ticket( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200247 unsigned char *buf,
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200248 size_t len )
249{
250 int ret;
251 ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200252 unsigned char *key_name = buf;
253 unsigned char *iv = buf + 16;
254 unsigned char *enc_len_p = iv + 16;
255 unsigned char *ticket = enc_len_p + 2;
256 unsigned char *mac;
Manuel Pégourié-Gonnard34ced2d2013-09-20 11:37:39 +0200257 unsigned char computed_mac[32];
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200258 size_t enc_len, clear_len, i;
259 unsigned char pad_len;
260
261 SSL_DEBUG_BUF( 3, "session ticket structure", buf, len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200262
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200263 if( len < 34 || ssl->ticket_keys == NULL )
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200264 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
265
266 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
267 mac = ticket + enc_len;
268
269 if( len != enc_len + 66 )
270 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
271
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200272 /* Check name */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200273 if( memcmp( key_name, ssl->ticket_keys->key_name, 16 ) != 0 )
274 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200275
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200276 /* Check mac */
277 sha256_hmac( ssl->ticket_keys->mac_key, 16, buf, len - 32,
278 computed_mac, 0 );
279 ret = 0;
280 for( i = 0; i < 32; i++ )
281 if( mac[i] != computed_mac[i] )
282 ret = POLARSSL_ERR_SSL_INVALID_MAC;
283 if( ret != 0 )
284 return( ret );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200285
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200286 /* Decrypt */
287 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->dec, AES_DECRYPT,
288 enc_len, iv, ticket, ticket ) ) != 0 )
289 {
290 return( ret );
291 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200292
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200293 /* Check PKCS padding */
294 pad_len = ticket[enc_len - 1];
295
296 ret = 0;
297 for( i = 2; i < pad_len; i++ )
298 if( ticket[enc_len - i] != pad_len )
299 ret = POLARSSL_ERR_SSL_BAD_INPUT_DATA;
300 if( ret != 0 )
301 return( ret );
302
303 clear_len = enc_len - pad_len;
304
305 SSL_DEBUG_BUF( 3, "session ticket cleartext", ticket, clear_len );
306
307 /* Actually load session */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200308 if( ( ret = ssl_load_session( &session, ticket, clear_len ) ) != 0 )
309 {
310 SSL_DEBUG_MSG( 1, ( "failed to parse ticket content" ) );
311 memset( &session, 0, sizeof( ssl_session ) );
312 return( ret );
313 }
314
Paul Bakker606b4ba2013-08-14 16:52:14 +0200315#if defined(POLARSSL_HAVE_TIME)
316 /* Check if still valid */
317 if( (int) ( time( NULL) - session.start ) > ssl->ticket_lifetime )
318 {
319 SSL_DEBUG_MSG( 1, ( "session ticket expired" ) );
320 memset( &session, 0, sizeof( ssl_session ) );
321 return( POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED );
322 }
323#endif
324
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200325 /*
326 * Keep the session ID sent by the client, since we MUST send it back to
327 * inform him we're accepting the ticket (RFC 5077 section 3.4)
328 */
329 session.length = ssl->session_negotiate->length;
330 memcpy( &session.id, ssl->session_negotiate->id, session.length );
331
332 ssl_session_free( ssl->session_negotiate );
333 memcpy( ssl->session_negotiate, &session, sizeof( ssl_session ) );
334 memset( &session, 0, sizeof( ssl_session ) );
335
336 return( 0 );
337}
Paul Bakkera503a632013-08-14 13:48:06 +0200338#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200339
Paul Bakker0be444a2013-08-27 21:55:01 +0200340#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200341/*
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200342 * Wrapper around f_sni, allowing use of ssl_set_own_cert() but
343 * making it act on ssl->hanshake->sni_key_cert instead.
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200344 */
345static int ssl_sni_wrapper( ssl_context *ssl,
346 const unsigned char* name, size_t len )
347{
348 int ret;
349 ssl_key_cert *key_cert_ori = ssl->key_cert;
350
351 ssl->key_cert = NULL;
352 ret = ssl->f_sni( ssl->p_sni, ssl, name, len );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200353 ssl->handshake->sni_key_cert = ssl->key_cert;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200354
355 ssl->key_cert = key_cert_ori;
356
357 return( ret );
358}
359
Paul Bakker5701cdc2012-09-27 21:49:42 +0000360static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000361 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000362 size_t len )
363{
364 int ret;
365 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000366 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000367
368 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
369 if( servername_list_size + 2 != len )
370 {
371 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
372 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
373 }
374
375 p = buf + 2;
376 while( servername_list_size > 0 )
377 {
378 hostname_len = ( ( p[1] << 8 ) | p[2] );
379 if( hostname_len + 3 > servername_list_size )
380 {
381 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
382 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
383 }
384
385 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
386 {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200387 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000388 if( ret != 0 )
389 {
390 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
391 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
392 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
393 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000394 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000395 }
396
397 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000398 p += hostname_len + 3;
399 }
400
401 if( servername_list_size != 0 )
402 {
403 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
404 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000405 }
406
407 return( 0 );
408}
Paul Bakker0be444a2013-08-27 21:55:01 +0200409#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000410
Paul Bakker48916f92012-09-16 19:57:18 +0000411static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000412 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000413 size_t len )
414{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000415 int ret;
416
Paul Bakker48916f92012-09-16 19:57:18 +0000417 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
418 {
419 if( len != 1 || buf[0] != 0x0 )
420 {
421 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000422
423 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
424 return( ret );
425
Paul Bakker48916f92012-09-16 19:57:18 +0000426 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
427 }
428
429 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
430 }
431 else
432 {
433 if( len != 1 + ssl->verify_data_len ||
434 buf[0] != ssl->verify_data_len ||
435 memcmp( buf + 1, ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
436 {
437 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000438
439 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
440 return( ret );
441
Paul Bakker48916f92012-09-16 19:57:18 +0000442 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
443 }
444 }
445
446 return( 0 );
447}
448
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200449#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +0000450static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
451 const unsigned char *buf,
452 size_t len )
453{
454 size_t sig_alg_list_size;
455 const unsigned char *p;
456
457 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
458 if( sig_alg_list_size + 2 != len ||
459 sig_alg_list_size %2 != 0 )
460 {
461 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
462 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
463 }
464
465 p = buf + 2;
466 while( sig_alg_list_size > 0 )
467 {
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200468 /*
469 * For now, just ignore signature algorithm and rely on offered
470 * ciphersuites only. To be fixed later.
471 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200472#if defined(POLARSSL_SHA512_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000473 if( p[0] == SSL_HASH_SHA512 )
474 {
475 ssl->handshake->sig_alg = SSL_HASH_SHA512;
476 break;
477 }
478 if( p[0] == SSL_HASH_SHA384 )
479 {
480 ssl->handshake->sig_alg = SSL_HASH_SHA384;
481 break;
482 }
483#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200484#if defined(POLARSSL_SHA256_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000485 if( p[0] == SSL_HASH_SHA256 )
486 {
487 ssl->handshake->sig_alg = SSL_HASH_SHA256;
488 break;
489 }
490 if( p[0] == SSL_HASH_SHA224 )
491 {
492 ssl->handshake->sig_alg = SSL_HASH_SHA224;
493 break;
494 }
495#endif
496 if( p[0] == SSL_HASH_SHA1 )
497 {
498 ssl->handshake->sig_alg = SSL_HASH_SHA1;
499 break;
500 }
501 if( p[0] == SSL_HASH_MD5 )
502 {
503 ssl->handshake->sig_alg = SSL_HASH_MD5;
504 break;
505 }
506
507 sig_alg_list_size -= 2;
508 p += 2;
509 }
510
511 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
512 ssl->handshake->sig_alg ) );
513
514 return( 0 );
515}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200516#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker23f36802012-09-28 14:15:14 +0000517
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200518#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200519static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
520 const unsigned char *buf,
521 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100522{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200523 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100524 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200525 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100526
527 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
528 if( list_size + 2 != len ||
529 list_size % 2 != 0 )
530 {
531 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
532 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
533 }
534
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200535 /* Don't allow our peer to make use allocated too much memory,
536 * and leave room for a final 0 */
537 our_size = list_size / 2 + 1;
538 if( our_size > POLARSSL_ECP_DP_MAX )
539 our_size = POLARSSL_ECP_DP_MAX;
540
541 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
542 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
543
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200544 /* explicit void pointer cast for buggy MS compiler */
545 memset( (void *) curves, 0, our_size * sizeof( *curves ) );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200546 ssl->handshake->curves = curves;
547
Paul Bakker41c83d32013-03-20 14:39:14 +0100548 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200549 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100550 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200551 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200552
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200553 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100554 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200555 *curves++ = curve_info;
556 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100557 }
558
559 list_size -= 2;
560 p += 2;
561 }
562
563 return( 0 );
564}
565
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200566static int ssl_parse_supported_point_formats( ssl_context *ssl,
567 const unsigned char *buf,
568 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100569{
570 size_t list_size;
571 const unsigned char *p;
572
573 list_size = buf[0];
574 if( list_size + 1 != len )
575 {
576 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
577 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
578 }
579
580 p = buf + 2;
581 while( list_size > 0 )
582 {
583 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
584 p[0] == POLARSSL_ECP_PF_COMPRESSED )
585 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200586 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200587 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100588 return( 0 );
589 }
590
591 list_size--;
592 p++;
593 }
594
595 return( 0 );
596}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200597#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100598
Paul Bakker05decb22013-08-15 13:33:48 +0200599#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200600static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
601 const unsigned char *buf,
602 size_t len )
603{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200604 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200605 {
606 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
607 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
608 }
609
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200610 ssl->session_negotiate->mfl_code = buf[0];
611
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200612 return( 0 );
613}
Paul Bakker05decb22013-08-15 13:33:48 +0200614#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200615
Paul Bakker1f2bc622013-08-15 13:45:55 +0200616#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200617static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
618 const unsigned char *buf,
619 size_t len )
620{
621 if( len != 0 )
622 {
623 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
624 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
625 }
626
627 ((void) buf);
628
629 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
630
631 return( 0 );
632}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200633#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200634
Paul Bakkera503a632013-08-14 13:48:06 +0200635#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200636static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200637 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200638 size_t len )
639{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200640 int ret;
641
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200642 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
643 return( 0 );
644
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200645 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200646 ssl->handshake->new_session_ticket = 1;
647
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200648 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
649
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200650 if( len == 0 )
651 return( 0 );
652
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200653 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
654 {
655 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
656 return( 0 );
657 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200658
659 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200660 * Failures are ok: just ignore the ticket and proceed.
661 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200662 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
663 {
664 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200665 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200666 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200667
668 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
669
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200670 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200671
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200672 /* Don't send a new ticket after all, this one is OK */
673 ssl->handshake->new_session_ticket = 0;
674
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200675 return( 0 );
676}
Paul Bakkera503a632013-08-14 13:48:06 +0200677#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200678
Paul Bakker78a8c712013-03-06 17:01:52 +0100679#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
680static int ssl_parse_client_hello_v2( ssl_context *ssl )
681{
682 int ret;
683 unsigned int i, j;
684 size_t n;
685 unsigned int ciph_len, sess_len, chal_len;
686 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200687 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200688 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100689
690 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
691
692 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
693 {
694 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
695
696 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
697 return( ret );
698
699 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
700 }
701
702 buf = ssl->in_hdr;
703
704 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
705
706 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
707 buf[2] ) );
708 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
709 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
710 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
711 buf[3], buf[4] ) );
712
713 /*
714 * SSLv2 Client Hello
715 *
716 * Record layer:
717 * 0 . 1 message length
718 *
719 * SSL layer:
720 * 2 . 2 message type
721 * 3 . 4 protocol version
722 */
723 if( buf[2] != SSL_HS_CLIENT_HELLO ||
724 buf[3] != SSL_MAJOR_VERSION_3 )
725 {
726 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
727 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
728 }
729
730 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
731
732 if( n < 17 || n > 512 )
733 {
734 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
735 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
736 }
737
738 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200739 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
740 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100741
742 if( ssl->minor_ver < ssl->min_minor_ver )
743 {
744 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
745 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
746 ssl->min_major_ver, ssl->min_minor_ver ) );
747
748 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
749 SSL_ALERT_MSG_PROTOCOL_VERSION );
750 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
751 }
752
Paul Bakker2fbefde2013-06-29 16:01:15 +0200753 ssl->handshake->max_major_ver = buf[3];
754 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100755
756 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
757 {
758 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
759 return( ret );
760 }
761
762 ssl->handshake->update_checksum( ssl, buf + 2, n );
763
764 buf = ssl->in_msg;
765 n = ssl->in_left - 5;
766
767 /*
768 * 0 . 1 ciphersuitelist length
769 * 2 . 3 session id length
770 * 4 . 5 challenge length
771 * 6 . .. ciphersuitelist
772 * .. . .. session id
773 * .. . .. challenge
774 */
775 SSL_DEBUG_BUF( 4, "record contents", buf, n );
776
777 ciph_len = ( buf[0] << 8 ) | buf[1];
778 sess_len = ( buf[2] << 8 ) | buf[3];
779 chal_len = ( buf[4] << 8 ) | buf[5];
780
781 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
782 ciph_len, sess_len, chal_len ) );
783
784 /*
785 * Make sure each parameter length is valid
786 */
787 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
788 {
789 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
790 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
791 }
792
793 if( sess_len > 32 )
794 {
795 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
796 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
797 }
798
799 if( chal_len < 8 || chal_len > 32 )
800 {
801 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
802 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
803 }
804
805 if( n != 6 + ciph_len + sess_len + chal_len )
806 {
807 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
808 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
809 }
810
811 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
812 buf + 6, ciph_len );
813 SSL_DEBUG_BUF( 3, "client hello, session id",
814 buf + 6 + ciph_len, sess_len );
815 SSL_DEBUG_BUF( 3, "client hello, challenge",
816 buf + 6 + ciph_len + sess_len, chal_len );
817
818 p = buf + 6 + ciph_len;
819 ssl->session_negotiate->length = sess_len;
820 memset( ssl->session_negotiate->id, 0, sizeof( ssl->session_negotiate->id ) );
821 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
822
823 p += sess_len;
824 memset( ssl->handshake->randbytes, 0, 64 );
825 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
826
827 /*
828 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
829 */
830 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
831 {
832 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
833 {
834 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
835 if( ssl->renegotiation == SSL_RENEGOTIATION )
836 {
837 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
838
839 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
840 return( ret );
841
842 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
843 }
844 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
845 break;
846 }
847 }
848
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200849 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
850 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +0100851 {
852 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
853 {
Paul Bakker41c83d32013-03-20 14:39:14 +0100854 // Only allow non-ECC ciphersuites as we do not have extensions
855 //
Paul Bakker59c28a22013-06-29 15:33:42 +0200856 if( p[0] == 0 && p[1] == 0 &&
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200857 ( ( ciphersuites[i] >> 8 ) & 0xFF ) == 0 &&
858 p[2] == ( ciphersuites[i] & 0xFF ) )
Paul Bakker59c28a22013-06-29 15:33:42 +0200859 {
860 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
861
862 if( ciphersuite_info == NULL )
863 {
864 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %02x not found",
865 ciphersuites[i] ) );
866 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
867 }
868
Paul Bakker2fbefde2013-06-29 16:01:15 +0200869 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
870 ciphersuite_info->max_minor_ver < ssl->minor_ver )
871 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +0200872
Paul Bakker78a8c712013-03-06 17:01:52 +0100873 goto have_ciphersuite_v2;
Paul Bakker59c28a22013-06-29 15:33:42 +0200874 }
Paul Bakker78a8c712013-03-06 17:01:52 +0100875 }
876 }
877
878 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
879
880 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
881
882have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200883 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +0200884 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +0100885 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +0100886
887 /*
888 * SSLv2 Client Hello relevant renegotiation security checks
889 */
890 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
891 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
892 {
893 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
894
895 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
896 return( ret );
897
898 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
899 }
900
901 ssl->in_left = 0;
902 ssl->state++;
903
904 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
905
906 return( 0 );
907}
908#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
909
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200910#if defined(POLARSSL_X509_CRT_PARSE_C)
911#if defined(POLARSSL_ECDSA_C)
912static int ssl_key_matches_curves( pk_context *pk,
913 const ecp_curve_info **curves )
914{
915 const ecp_curve_info **crv = curves;
916 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
917
918 while( *crv != NULL )
919 {
920 if( (*crv)->grp_id == grp_id )
921 return( 1 );
922 crv++;
923 }
924
925 return( 0 );
926}
927#endif /* POLARSSL_ECDSA_C */
928
929/*
930 * Try picking a certificate for this ciphersuite,
931 * return 0 on success and -1 on failure.
932 */
933static int ssl_pick_cert( ssl_context *ssl,
934 const ssl_ciphersuite_t * ciphersuite_info )
935{
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200936 ssl_key_cert *cur, *list;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200937 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
938
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200939#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
940 if( ssl->handshake->sni_key_cert != NULL )
941 list = ssl->handshake->sni_key_cert;
942 else
943#endif
944 list = ssl->handshake->key_cert;
945
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200946 if( pk_alg == POLARSSL_PK_NONE )
947 return( 0 );
948
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200949 for( cur = list; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200950 {
951 if( ! pk_can_do( cur->key, pk_alg ) )
952 continue;
953
954#if defined(POLARSSL_ECDSA_C)
955 if( pk_alg == POLARSSL_PK_ECDSA )
956 {
957 if( ssl_key_matches_curves( cur->key, ssl->handshake->curves ) )
958 break;
959 }
960 else
961#endif
962 break;
963 }
964
965 if( cur == NULL )
966 return( -1 );
967
968 ssl->handshake->key_cert = cur;
969 return( 0 );
970}
971#endif /* POLARSSL_X509_CRT_PARSE_C */
972
Paul Bakker5121ce52009-01-03 21:22:43 +0000973static int ssl_parse_client_hello( ssl_context *ssl )
974{
Paul Bakker23986e52011-04-24 08:57:21 +0000975 int ret;
976 unsigned int i, j;
977 size_t n;
978 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +0000979 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +0000980 unsigned int ext_len = 0;
981 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000982 int renegotiation_info_seen = 0;
983 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200984 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +0100985 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000986
987 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
988
Paul Bakker48916f92012-09-16 19:57:18 +0000989 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
990 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000991 {
992 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
993 return( ret );
994 }
995
996 buf = ssl->in_hdr;
997
Paul Bakker78a8c712013-03-06 17:01:52 +0100998#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
999 if( ( buf[0] & 0x80 ) != 0 )
1000 return ssl_parse_client_hello_v2( ssl );
1001#endif
1002
Paul Bakkerec636f32012-09-09 19:17:02 +00001003 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1004
1005 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1006 buf[0] ) );
1007 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1008 ( buf[3] << 8 ) | buf[4] ) );
1009 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
1010 buf[1], buf[2] ) );
1011
1012 /*
1013 * SSLv3 Client Hello
1014 *
1015 * Record layer:
1016 * 0 . 0 message type
1017 * 1 . 2 protocol version
1018 * 3 . 4 message length
1019 */
1020 if( buf[0] != SSL_MSG_HANDSHAKE ||
1021 buf[1] != SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001022 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001023 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1024 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1025 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001026
Paul Bakkerec636f32012-09-09 19:17:02 +00001027 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +00001028
Manuel Pégourié-Gonnard72882b22013-08-02 13:36:00 +02001029 if( n < 45 || n > 2048 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001030 {
1031 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1032 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1033 }
1034
Paul Bakker48916f92012-09-16 19:57:18 +00001035 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1036 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001037 {
1038 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1039 return( ret );
1040 }
1041
1042 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +00001043 if( !ssl->renegotiation )
1044 n = ssl->in_left - 5;
1045 else
1046 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +00001047
Paul Bakker48916f92012-09-16 19:57:18 +00001048 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +00001049
1050 /*
1051 * SSL layer:
1052 * 0 . 0 handshake type
1053 * 1 . 3 handshake length
1054 * 4 . 5 protocol version
1055 * 6 . 9 UNIX time()
1056 * 10 . 37 random bytes
1057 * 38 . 38 session id length
1058 * 39 . 38+x session id
1059 * 39+x . 40+x ciphersuitelist length
1060 * 41+x . .. ciphersuitelist
1061 * .. . .. compression alg.
1062 * .. . .. extensions
1063 */
1064 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1065
1066 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1067 buf[0] ) );
1068 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1069 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1070 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1071 buf[4], buf[5] ) );
1072
1073 /*
1074 * Check the handshake type and protocol version
1075 */
1076 if( buf[0] != SSL_HS_CLIENT_HELLO ||
1077 buf[4] != SSL_MAJOR_VERSION_3 )
1078 {
1079 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1080 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1081 }
1082
1083 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +02001084 ssl->minor_ver = ( buf[5] <= ssl->max_minor_ver )
1085 ? buf[5] : ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001086
Paul Bakker1d29fb52012-09-28 13:28:45 +00001087 if( ssl->minor_ver < ssl->min_minor_ver )
1088 {
1089 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
1090 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001091 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001092
1093 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1094 SSL_ALERT_MSG_PROTOCOL_VERSION );
1095
1096 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1097 }
1098
Paul Bakker2fbefde2013-06-29 16:01:15 +02001099 ssl->handshake->max_major_ver = buf[4];
1100 ssl->handshake->max_minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001101
Paul Bakker48916f92012-09-16 19:57:18 +00001102 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001103
1104 /*
1105 * Check the handshake message length
1106 */
1107 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1108 {
1109 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1110 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1111 }
1112
1113 /*
1114 * Check the session length
1115 */
1116 sess_len = buf[38];
1117
1118 if( sess_len > 32 )
1119 {
1120 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1121 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1122 }
1123
Paul Bakker48916f92012-09-16 19:57:18 +00001124 ssl->session_negotiate->length = sess_len;
1125 memset( ssl->session_negotiate->id, 0,
1126 sizeof( ssl->session_negotiate->id ) );
1127 memcpy( ssl->session_negotiate->id, buf + 39,
1128 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001129
1130 /*
1131 * Check the ciphersuitelist length
1132 */
1133 ciph_len = ( buf[39 + sess_len] << 8 )
1134 | ( buf[40 + sess_len] );
1135
1136 if( ciph_len < 2 || ciph_len > 256 || ( ciph_len % 2 ) != 0 )
1137 {
1138 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1139 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1140 }
1141
1142 /*
1143 * Check the compression algorithms length
1144 */
1145 comp_len = buf[41 + sess_len + ciph_len];
1146
1147 if( comp_len < 1 || comp_len > 16 )
1148 {
1149 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1150 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1151 }
1152
Paul Bakker48916f92012-09-16 19:57:18 +00001153 /*
1154 * Check the extension length
1155 */
1156 if( n > 42 + sess_len + ciph_len + comp_len )
1157 {
1158 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1159 | ( buf[43 + sess_len + ciph_len + comp_len] );
1160
1161 if( ( ext_len > 0 && ext_len < 4 ) ||
1162 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1163 {
1164 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1165 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1166 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1167 }
1168 }
1169
1170 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001171#if defined(POLARSSL_ZLIB_SUPPORT)
1172 for( i = 0; i < comp_len; ++i )
1173 {
Paul Bakker48916f92012-09-16 19:57:18 +00001174 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001175 {
Paul Bakker48916f92012-09-16 19:57:18 +00001176 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001177 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001178 }
1179 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001180#endif
1181
Paul Bakkerec636f32012-09-09 19:17:02 +00001182 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1183 buf + 6, 32 );
1184 SSL_DEBUG_BUF( 3, "client hello, session id",
1185 buf + 38, sess_len );
1186 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1187 buf + 41 + sess_len, ciph_len );
1188 SSL_DEBUG_BUF( 3, "client hello, compression",
1189 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001190
Paul Bakkerec636f32012-09-09 19:17:02 +00001191 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001192 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1193 */
1194 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1195 {
1196 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1197 {
1198 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1199 if( ssl->renegotiation == SSL_RENEGOTIATION )
1200 {
1201 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001202
1203 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1204 return( ret );
1205
Paul Bakker48916f92012-09-16 19:57:18 +00001206 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1207 }
1208 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1209 break;
1210 }
1211 }
1212
Paul Bakker48916f92012-09-16 19:57:18 +00001213 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001214
1215 while( ext_len )
1216 {
1217 unsigned int ext_id = ( ( ext[0] << 8 )
1218 | ( ext[1] ) );
1219 unsigned int ext_size = ( ( ext[2] << 8 )
1220 | ( ext[3] ) );
1221
1222 if( ext_size + 4 > ext_len )
1223 {
1224 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1225 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1226 }
1227 switch( ext_id )
1228 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001229#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001230 case TLS_EXT_SERVERNAME:
1231 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1232 if( ssl->f_sni == NULL )
1233 break;
1234
1235 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1236 if( ret != 0 )
1237 return( ret );
1238 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001239#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001240
Paul Bakker48916f92012-09-16 19:57:18 +00001241 case TLS_EXT_RENEGOTIATION_INFO:
1242 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1243 renegotiation_info_seen = 1;
1244
Paul Bakker23f36802012-09-28 14:15:14 +00001245 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1246 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001247 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001248 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001249
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001250#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001251 case TLS_EXT_SIG_ALG:
1252 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1253 if( ssl->renegotiation == SSL_RENEGOTIATION )
1254 break;
1255
1256 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1257 if( ret != 0 )
1258 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001259 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001260#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001261
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001262#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001263 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1264 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1265
1266 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1267 if( ret != 0 )
1268 return( ret );
1269 break;
1270
1271 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1272 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001273 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001274
1275 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1276 if( ret != 0 )
1277 return( ret );
1278 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001279#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001280
Paul Bakker05decb22013-08-15 13:33:48 +02001281#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001282 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1283 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1284
1285 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1286 if( ret != 0 )
1287 return( ret );
1288 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001289#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001290
Paul Bakker1f2bc622013-08-15 13:45:55 +02001291#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001292 case TLS_EXT_TRUNCATED_HMAC:
1293 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1294
1295 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1296 if( ret != 0 )
1297 return( ret );
1298 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001299#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001300
Paul Bakkera503a632013-08-14 13:48:06 +02001301#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001302 case TLS_EXT_SESSION_TICKET:
1303 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1304
1305 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1306 if( ret != 0 )
1307 return( ret );
1308 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001309#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001310
Paul Bakker48916f92012-09-16 19:57:18 +00001311 default:
1312 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1313 ext_id ) );
1314 }
1315
1316 ext_len -= 4 + ext_size;
1317 ext += 4 + ext_size;
1318
1319 if( ext_len > 0 && ext_len < 4 )
1320 {
1321 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1322 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1323 }
1324 }
1325
1326 /*
1327 * Renegotiation security checks
1328 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001329 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1330 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1331 {
1332 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1333 handshake_failure = 1;
1334 }
1335 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1336 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1337 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001338 {
1339 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001340 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001341 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001342 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1343 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1344 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001345 {
1346 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001347 handshake_failure = 1;
1348 }
1349 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1350 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1351 renegotiation_info_seen == 1 )
1352 {
1353 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1354 handshake_failure = 1;
1355 }
1356
1357 if( handshake_failure == 1 )
1358 {
1359 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1360 return( ret );
1361
Paul Bakker48916f92012-09-16 19:57:18 +00001362 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1363 }
Paul Bakker380da532012-04-18 16:10:25 +00001364
Paul Bakker41c83d32013-03-20 14:39:14 +01001365 /*
1366 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001367 * (At the end because we need information from the EC-based extensions
1368 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001369 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001370 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
1371 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001372 {
1373 for( j = 0, p = buf + 41 + sess_len; j < ciph_len;
1374 j += 2, p += 2 )
1375 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001376 if( p[0] == ( ( ciphersuites[i] >> 8 ) & 0xFF ) &&
1377 p[1] == ( ( ciphersuites[i] ) & 0xFF ) )
Paul Bakker41c83d32013-03-20 14:39:14 +01001378 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001379 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
Paul Bakker41c83d32013-03-20 14:39:14 +01001380
1381 if( ciphersuite_info == NULL )
1382 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001383 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found",
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001384 ciphersuites[i] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001385 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1386 }
1387
Paul Bakker2fbefde2013-06-29 16:01:15 +02001388 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
1389 ciphersuite_info->max_minor_ver < ssl->minor_ver )
1390 continue;
1391
Paul Bakker5fd49172013-08-19 13:29:26 +02001392#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001393 if( ssl_ciphersuite_uses_ec( ciphersuite_info ) &&
Paul Bakkercaa3af42013-09-26 13:32:43 +02001394 ( ssl->handshake->curves == NULL ||
1395 ssl->handshake->curves[0] == NULL ) )
Paul Bakker41c83d32013-03-20 14:39:14 +01001396 continue;
Paul Bakker5fd49172013-08-19 13:29:26 +02001397#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001398
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001399#if defined(POLARSSL_X509_CRT_PARSE_C)
1400 /*
1401 * Final check: if ciphersuite requires us to have a
1402 * certificate/key of a particular type:
1403 * - select the appropriate certificate if we have one, or
1404 * - try the next ciphersuite if we don't
1405 * This must be done last since we modify the key_cert list.
1406 */
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02001407 if( ssl_pick_cert( ssl, ciphersuite_info ) != 0 )
1408 continue;
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02001409#endif
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001410
Paul Bakker41c83d32013-03-20 14:39:14 +01001411 goto have_ciphersuite;
1412 }
1413 }
1414 }
1415
1416 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1417
1418 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1419 return( ret );
1420
1421 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1422
1423have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001424 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001425 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1426 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1427
Paul Bakker5121ce52009-01-03 21:22:43 +00001428 ssl->in_left = 0;
1429 ssl->state++;
1430
1431 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1432
1433 return( 0 );
1434}
1435
Paul Bakker1f2bc622013-08-15 13:45:55 +02001436#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001437static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1438 unsigned char *buf,
1439 size_t *olen )
1440{
1441 unsigned char *p = buf;
1442
1443 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1444 {
1445 *olen = 0;
1446 return;
1447 }
1448
1449 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1450
1451 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1452 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1453
1454 *p++ = 0x00;
1455 *p++ = 0x00;
1456
1457 *olen = 4;
1458}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001459#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001460
Paul Bakkera503a632013-08-14 13:48:06 +02001461#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001462static void ssl_write_session_ticket_ext( ssl_context *ssl,
1463 unsigned char *buf,
1464 size_t *olen )
1465{
1466 unsigned char *p = buf;
1467
1468 if( ssl->handshake->new_session_ticket == 0 )
1469 {
1470 *olen = 0;
1471 return;
1472 }
1473
1474 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1475
1476 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1477 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1478
1479 *p++ = 0x00;
1480 *p++ = 0x00;
1481
1482 *olen = 4;
1483}
Paul Bakkera503a632013-08-14 13:48:06 +02001484#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001485
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001486static void ssl_write_renegotiation_ext( ssl_context *ssl,
1487 unsigned char *buf,
1488 size_t *olen )
1489{
1490 unsigned char *p = buf;
1491
1492 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1493 {
1494 *olen = 0;
1495 return;
1496 }
1497
1498 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1499
1500 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1501 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1502
1503 *p++ = 0x00;
1504 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1505 *p++ = ssl->verify_data_len * 2 & 0xFF;
1506
1507 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1508 p += ssl->verify_data_len;
1509 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1510 p += ssl->verify_data_len;
1511
1512 *olen = 5 + ssl->verify_data_len * 2;
1513}
1514
Paul Bakker05decb22013-08-15 13:33:48 +02001515#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001516static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1517 unsigned char *buf,
1518 size_t *olen )
1519{
1520 unsigned char *p = buf;
1521
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001522 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1523 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001524 *olen = 0;
1525 return;
1526 }
1527
1528 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1529
1530 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1531 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1532
1533 *p++ = 0x00;
1534 *p++ = 1;
1535
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001536 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001537
1538 *olen = 5;
1539}
Paul Bakker05decb22013-08-15 13:33:48 +02001540#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001541
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001542#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001543static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1544 unsigned char *buf,
1545 size_t *olen )
1546{
1547 unsigned char *p = buf;
1548 ((void) ssl);
1549
Paul Bakker677377f2013-10-28 12:54:26 +01001550 if( ( ssl->handshake->cli_exts &
1551 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
1552 {
1553 *olen = 0;
1554 return;
1555 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001556
1557 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1558
1559 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1560 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1561
1562 *p++ = 0x00;
1563 *p++ = 2;
1564
1565 *p++ = 1;
1566 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1567
1568 *olen = 6;
1569}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001570#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001571
Paul Bakker5121ce52009-01-03 21:22:43 +00001572static int ssl_write_server_hello( ssl_context *ssl )
1573{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001574#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001575 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001576#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001577 int ret;
1578 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001579 unsigned char *buf, *p;
1580
1581 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1582
1583 /*
1584 * 0 . 0 handshake type
1585 * 1 . 3 handshake length
1586 * 4 . 5 protocol version
1587 * 6 . 9 UNIX time()
1588 * 10 . 37 random bytes
1589 */
1590 buf = ssl->out_msg;
1591 p = buf + 4;
1592
1593 *p++ = (unsigned char) ssl->major_ver;
1594 *p++ = (unsigned char) ssl->minor_ver;
1595
1596 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1597 buf[4], buf[5] ) );
1598
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001599#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001600 t = time( NULL );
1601 *p++ = (unsigned char)( t >> 24 );
1602 *p++ = (unsigned char)( t >> 16 );
1603 *p++ = (unsigned char)( t >> 8 );
1604 *p++ = (unsigned char)( t );
1605
1606 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001607#else
1608 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1609 return( ret );
1610
1611 p += 4;
1612#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001613
Paul Bakkera3d195c2011-11-27 21:07:34 +00001614 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1615 return( ret );
1616
1617 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001618
Paul Bakker48916f92012-09-16 19:57:18 +00001619 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001620
1621 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1622
1623 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001624 * Resume is 0 by default, see ssl_handshake_init().
1625 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1626 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001627 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001628 if( ssl->handshake->resume == 0 &&
1629 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001630 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001631 ssl->f_get_cache != NULL &&
1632 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1633 {
1634 ssl->handshake->resume = 1;
1635 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001636
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001637 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001638 {
1639 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001640 * New session, create a new session id,
1641 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001642 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001643 ssl->state++;
1644
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001645#if defined(POLARSSL_HAVE_TIME)
1646 ssl->session_negotiate->start = time( NULL );
1647#endif
1648
Paul Bakkera503a632013-08-14 13:48:06 +02001649#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001650 if( ssl->handshake->new_session_ticket != 0 )
1651 {
1652 ssl->session_negotiate->length = n = 0;
1653 memset( ssl->session_negotiate->id, 0, 32 );
1654 }
1655 else
1656#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001657 {
1658 ssl->session_negotiate->length = n = 32;
1659 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001660 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001661 return( ret );
1662 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001663 }
1664 else
1665 {
1666 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001667 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001668 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001669 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001670 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001671
1672 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1673 {
1674 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1675 return( ret );
1676 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001677 }
1678
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001679 /*
1680 * 38 . 38 session id length
1681 * 39 . 38+n session id
1682 * 39+n . 40+n chosen ciphersuite
1683 * 41+n . 41+n chosen compression alg.
1684 * 42+n . 43+n extensions length
1685 * 44+n . 43+n+m extensions
1686 */
1687 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001688 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1689 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001690
1691 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1692 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1693 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001694 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001695
Paul Bakker48916f92012-09-16 19:57:18 +00001696 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1697 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1698 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001699
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001700 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
1701 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001702 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001703 ssl->session_negotiate->compression ) );
1704
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001705 /*
1706 * First write extensions, then the total length
1707 */
1708 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1709 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001710
Paul Bakker05decb22013-08-15 13:33:48 +02001711#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001712 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1713 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001714#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001715
Paul Bakker1f2bc622013-08-15 13:45:55 +02001716#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001717 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1718 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001719#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001720
Paul Bakkera503a632013-08-14 13:48:06 +02001721#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001722 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1723 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001724#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001725
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001726#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001727 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1728 ext_len += olen;
1729#endif
1730
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001731 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001732
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001733 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1734 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1735 p += ext_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001736
1737 ssl->out_msglen = p - buf;
1738 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1739 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1740
1741 ret = ssl_write_record( ssl );
1742
1743 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1744
1745 return( ret );
1746}
1747
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001748#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1749 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001750 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1751 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001752static int ssl_write_certificate_request( ssl_context *ssl )
1753{
Paul Bakkered27a042013-04-18 22:46:23 +02001754 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1755 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001756
1757 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1758
1759 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001760 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1761 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001762 {
1763 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1764 ssl->state++;
1765 return( 0 );
1766 }
1767
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001768 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001769 return( ret );
1770}
1771#else
1772static int ssl_write_certificate_request( ssl_context *ssl )
1773{
1774 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1775 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001776 size_t dn_size, total_dn_size; /* excluding length bytes */
1777 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00001778 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001779 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001780
1781 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1782
1783 ssl->state++;
1784
Paul Bakkerfbb17802013-04-17 19:10:21 +02001785 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001786 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001787 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001788 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001789 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001790 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001791 return( 0 );
1792 }
1793
1794 /*
1795 * 0 . 0 handshake type
1796 * 1 . 3 handshake length
1797 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01001798 * 5 .. m-1 cert types
1799 * m .. m+1 sig alg length (TLS 1.2 only)
1800 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001801 * n .. n+1 length of all DNs
1802 * n+2 .. n+3 length of DN 1
1803 * n+4 .. ... Distinguished Name #1
1804 * ... .. ... length of DN 2, etc.
1805 */
1806 buf = ssl->out_msg;
1807 p = buf + 4;
1808
1809 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001810 * Supported certificate types
1811 *
1812 * ClientCertificateType certificate_types<1..2^8-1>;
1813 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00001814 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001815 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01001816
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001817#if defined(POLARSSL_RSA_C)
1818 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
1819#endif
1820#if defined(POLARSSL_ECDSA_C)
1821 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
1822#endif
1823
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001824 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001825 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01001826
Paul Bakker577e0062013-08-28 11:57:20 +02001827 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001828#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01001829 /*
1830 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01001831 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001832 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
1833 *
1834 * struct {
1835 * HashAlgorithm hash;
1836 * SignatureAlgorithm signature;
1837 * } SignatureAndHashAlgorithm;
1838 *
1839 * enum { (255) } HashAlgorithm;
1840 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01001841 */
Paul Bakker21dca692013-01-03 11:41:08 +01001842 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01001843 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001844 /*
1845 * Only use current running hash algorithm that is already required
1846 * for requested ciphersuite.
1847 */
Paul Bakker926af752012-11-23 13:38:07 +01001848 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
1849
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001850 if( ssl->transform_negotiate->ciphersuite_info->mac ==
1851 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01001852 {
1853 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
1854 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02001855
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001856 /*
1857 * Supported signature algorithms
1858 */
1859#if defined(POLARSSL_RSA_C)
1860 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1861 p[2 + sa_len++] = SSL_SIG_RSA;
1862#endif
1863#if defined(POLARSSL_ECDSA_C)
1864 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1865 p[2 + sa_len++] = SSL_SIG_ECDSA;
1866#endif
Paul Bakker926af752012-11-23 13:38:07 +01001867
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001868 p[0] = (unsigned char)( sa_len >> 8 );
1869 p[1] = (unsigned char)( sa_len );
1870 sa_len += 2;
1871 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01001872 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001873#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001874
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001875 /*
1876 * DistinguishedName certificate_authorities<0..2^16-1>;
1877 * opaque DistinguishedName<1..2^16-1>;
1878 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001879 p += 2;
1880 crt = ssl->ca_chain;
1881
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001882 total_dn_size = 0;
Paul Bakker29087132010-03-21 21:03:34 +00001883 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001884 {
1885 if( p - buf > 4096 )
1886 break;
1887
Paul Bakker926af752012-11-23 13:38:07 +01001888 dn_size = crt->subject_raw.len;
1889 *p++ = (unsigned char)( dn_size >> 8 );
1890 *p++ = (unsigned char)( dn_size );
1891 memcpy( p, crt->subject_raw.p, dn_size );
1892 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001893
Paul Bakker926af752012-11-23 13:38:07 +01001894 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
1895
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001896 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01001897 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00001898 }
1899
Paul Bakker926af752012-11-23 13:38:07 +01001900 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001901 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1902 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001903 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
1904 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001905
1906 ret = ssl_write_record( ssl );
1907
1908 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
1909
1910 return( ret );
1911}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001912#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
1913 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
1914 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001915
Paul Bakker41c83d32013-03-20 14:39:14 +01001916static int ssl_write_server_key_exchange( ssl_context *ssl )
1917{
Paul Bakker23986e52011-04-24 08:57:21 +00001918 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001919 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001920 const ssl_ciphersuite_t *ciphersuite_info =
1921 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001922
1923#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1924 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
1925 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001926 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02001927 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001928 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001929 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001930 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001931 ((void) dig_signed);
1932 ((void) dig_signed_len);
1933#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001934
Paul Bakker5121ce52009-01-03 21:22:43 +00001935 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
1936
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001937 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
1938 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1939 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001940 {
1941 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
1942 ssl->state++;
1943 return( 0 );
1944 }
1945
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001946#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
1947 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1948 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1949 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001950 {
1951 /* TODO: Support identity hints */
1952 *(p++) = 0x00;
1953 *(p++) = 0x00;
1954
1955 n += 2;
1956 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001957#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
1958 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001959
1960#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1961 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1962 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1963 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00001964 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001965 /*
1966 * Ephemeral DH parameters:
1967 *
1968 * struct {
1969 * opaque dh_p<1..2^16-1>;
1970 * opaque dh_g<1..2^16-1>;
1971 * opaque dh_Ys<1..2^16-1>;
1972 * } ServerDHParams;
1973 */
1974 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
1975 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
1976 {
1977 SSL_DEBUG_RET( 1, "mpi_copy", ret );
1978 return( ret );
1979 }
Paul Bakker48916f92012-09-16 19:57:18 +00001980
Paul Bakker41c83d32013-03-20 14:39:14 +01001981 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001982 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001983 p,
1984 &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001985 {
1986 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
1987 return( ret );
1988 }
1989
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001990 dig_signed = p;
1991 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001992
1993 p += len;
1994 n += len;
1995
Paul Bakker41c83d32013-03-20 14:39:14 +01001996 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
1997 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1998 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1999 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2000 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002001#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2002 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002003
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002004#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002005 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2006 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2007
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002008 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002009 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2010 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002011 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002012 /*
2013 * Ephemeral ECDH parameters:
2014 *
2015 * struct {
2016 * ECParameters curve_params;
2017 * ECPoint public;
2018 * } ServerECDHParams;
2019 */
Paul Bakker41c83d32013-03-20 14:39:14 +01002020 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02002021 ssl->handshake->curves[0]->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002022 {
2023 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2024 return( ret );
2025 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002026
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02002027 SSL_DEBUG_MSG( 2, ( "ECDH curve size: %d",
2028 (int) ssl->handshake->ecdh_ctx.grp.nbits ) );
2029
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002030 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2031 p, SSL_MAX_CONTENT_LEN - n,
2032 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002033 {
2034 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2035 return( ret );
2036 }
2037
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002038 dig_signed = p;
2039 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002040
2041 p += len;
2042 n += len;
2043
Paul Bakker41c83d32013-03-20 14:39:14 +01002044 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2045 }
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002046#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002047 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2048 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002049
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002050#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002051 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2052 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002053 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002054 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2055 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002056 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002057 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002058 unsigned int hashlen = 0;
2059 unsigned char hash[64];
2060 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002061
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002062 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002063 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2064 */
Paul Bakker577e0062013-08-28 11:57:20 +02002065#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002066 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2067 {
2068 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2069
2070 if( md_alg == POLARSSL_MD_NONE )
2071 {
2072 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2073 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2074 }
2075 }
Paul Bakker577e0062013-08-28 11:57:20 +02002076 else
2077#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002078#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2079 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker577e0062013-08-28 11:57:20 +02002080 if ( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002081 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2082 {
2083 md_alg = POLARSSL_MD_SHA1;
2084 }
2085 else
Paul Bakker577e0062013-08-28 11:57:20 +02002086#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002087 {
2088 md_alg = POLARSSL_MD_NONE;
2089 }
2090
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002091 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002092 * Compute the hash to be signed
2093 */
Paul Bakker577e0062013-08-28 11:57:20 +02002094#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2095 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002096 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002097 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002098 md5_context md5;
2099 sha1_context sha1;
2100
2101 /*
2102 * digitally-signed struct {
2103 * opaque md5_hash[16];
2104 * opaque sha_hash[20];
2105 * };
2106 *
2107 * md5_hash
2108 * MD5(ClientHello.random + ServerHello.random
2109 * + ServerParams);
2110 * sha_hash
2111 * SHA(ClientHello.random + ServerHello.random
2112 * + ServerParams);
2113 */
2114 md5_starts( &md5 );
2115 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002116 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002117 md5_finish( &md5, hash );
2118
2119 sha1_starts( &sha1 );
2120 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002121 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002122 sha1_finish( &sha1, hash + 16 );
2123
2124 hashlen = 36;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002125 }
2126 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002127#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2128 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002129#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2130 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002131 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002132 {
2133 md_context_t ctx;
2134
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002135 /* Info from md_alg will be used instead */
2136 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002137
2138 /*
2139 * digitally-signed struct {
2140 * opaque client_random[32];
2141 * opaque server_random[32];
2142 * ServerDHParams params;
2143 * };
2144 */
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002145 if( ( ret = md_init_ctx( &ctx, md_info_from_type(md_alg) ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002146 {
2147 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2148 return( ret );
2149 }
2150
2151 md_starts( &ctx );
2152 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002153 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002154 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02002155
2156 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
2157 {
2158 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
2159 return( ret );
2160 }
2161
Paul Bakker23f36802012-09-28 14:15:14 +00002162 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002163 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002164#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2165 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002166 {
2167 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002168 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02002169 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002170
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002171 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2172 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002173
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002174 /*
2175 * Make the signature
2176 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002177 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002178 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002179 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2180 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002181 }
Paul Bakker23f36802012-09-28 14:15:14 +00002182
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002183#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002184 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2185 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002186 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002187 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002188
2189 n += 2;
2190 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002191#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002192
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002193 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002194 p + 2 , &signature_len,
2195 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002196 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002197 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002198 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002199 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002200
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002201 *(p++) = (unsigned char)( signature_len >> 8 );
2202 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002203 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002204
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002205 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002206
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002207 p += signature_len;
2208 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002209 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002210#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002211 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2212 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002213
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002214 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002215 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2216 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2217
2218 ssl->state++;
2219
2220 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2221 {
2222 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2223 return( ret );
2224 }
2225
2226 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2227
2228 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002229}
2230
2231static int ssl_write_server_hello_done( ssl_context *ssl )
2232{
2233 int ret;
2234
2235 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2236
2237 ssl->out_msglen = 4;
2238 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2239 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2240
2241 ssl->state++;
2242
2243 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2244 {
2245 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2246 return( ret );
2247 }
2248
2249 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2250
2251 return( 0 );
2252}
2253
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002254#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2255 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2256static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2257 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002258{
2259 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002260 size_t n;
2261
2262 /*
2263 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2264 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002265 if( *p + 2 > end )
2266 {
2267 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2268 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2269 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002270
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002271 n = ( (*p)[0] << 8 ) | (*p)[1];
2272 *p += 2;
2273
2274 if( n < 1 || n > ssl->handshake->dhm_ctx.len || *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002275 {
2276 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2277 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2278 }
2279
2280 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002281 *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002282 {
2283 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2284 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2285 }
2286
2287 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2288
Paul Bakker70df2fb2013-04-17 17:19:09 +02002289 return( ret );
2290}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002291#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2292 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002293
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002294#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2295 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002296static int ssl_parse_encrypted_pms( ssl_context *ssl,
2297 const unsigned char *p,
2298 const unsigned char *end,
2299 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002300{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002301 int ret;
2302 size_t len = pk_get_len( ssl_own_key( ssl ) );
2303 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002304
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002305 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002306 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002307 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002308 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2309 }
2310
2311 /*
2312 * Decrypt the premaster using own private RSA key
2313 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002314#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2315 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002316 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2317 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002318 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2319 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002320 {
2321 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2322 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2323 }
2324 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002325#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002326
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002327 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002328 {
2329 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2330 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2331 }
2332
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002333 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2334 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002335 sizeof(ssl->handshake->premaster),
2336 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002337
2338 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002339 pms[0] != ssl->handshake->max_major_ver ||
2340 pms[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002341 {
2342 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2343
2344 /*
2345 * Protection against Bleichenbacher's attack:
2346 * invalid PKCS#1 v1.5 padding must not cause
2347 * the connection to end immediately; instead,
2348 * send a bad_record_mac later in the handshake.
2349 */
2350 ssl->handshake->pmslen = 48;
2351
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002352 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002353 if( ret != 0 )
2354 return( ret );
2355 }
2356
2357 return( ret );
2358}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002359#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2360 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002361
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002362#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002363static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2364 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002365{
Paul Bakker6db455e2013-09-18 17:29:31 +02002366 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002367 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002368
Paul Bakker6db455e2013-09-18 17:29:31 +02002369 if( ssl->f_psk == NULL &&
2370 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2371 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002372 {
2373 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2374 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2375 }
2376
2377 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002378 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002379 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002380 if( *p + 2 > end )
2381 {
2382 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2383 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2384 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002385
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002386 n = ( (*p)[0] << 8 ) | (*p)[1];
2387 *p += 2;
2388
2389 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002390 {
2391 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2392 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2393 }
2394
Paul Bakker6db455e2013-09-18 17:29:31 +02002395 if( ssl->f_psk != NULL )
2396 {
2397 if( ( ret != ssl->f_psk( ssl->p_psk, ssl, *p, n ) ) != 0 )
2398 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2399 }
2400
2401 if( ret == 0 )
2402 {
2403 if( n != ssl->psk_identity_len ||
2404 memcmp( ssl->psk_identity, *p, n ) != 0 )
2405 {
2406 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2407 }
2408 }
2409
2410 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002411 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002412 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002413 if( ( ret = ssl_send_alert_message( ssl,
2414 SSL_ALERT_LEVEL_FATAL,
2415 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2416 {
2417 return( ret );
2418 }
2419
2420 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002421 }
2422
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002423 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002424 ret = 0;
2425
Paul Bakkerfbb17802013-04-17 19:10:21 +02002426 return( ret );
2427}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002428#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002429
Paul Bakker5121ce52009-01-03 21:22:43 +00002430static int ssl_parse_client_key_exchange( ssl_context *ssl )
2431{
Paul Bakker23986e52011-04-24 08:57:21 +00002432 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002433 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002434
Paul Bakker41c83d32013-03-20 14:39:14 +01002435 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002436
2437 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2438
2439 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2440 {
2441 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2442 return( ret );
2443 }
2444
2445 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2446 {
2447 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002448 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002449 }
2450
2451 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2452 {
2453 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002454 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002455 }
2456
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002457#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002458 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002459 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002460 unsigned char *p = ssl->in_msg + 4;
2461 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2462
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002463 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002464 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002465 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2466 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002467 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002468
2469 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
2470
2471 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2472 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002473 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002474 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002475 {
2476 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2477 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2478 }
2479
2480 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002481 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002482 else
2483#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002484#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2485 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2486 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2487 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002488 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002489 size_t n = ssl->in_msg[3];
2490
2491 if( n < 1 || n > mpi_size( &ssl->handshake->ecdh_ctx.grp.P ) * 2 + 2 ||
2492 n + 4 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002493 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002494 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2495 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002496 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002497
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002498 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2499 ssl->in_msg + 4, n ) ) != 0 )
2500 {
2501 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2502 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2503 }
2504
2505 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2506
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002507 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2508 &ssl->handshake->pmslen,
2509 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002510 POLARSSL_MPI_MAX_SIZE,
2511 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002512 {
2513 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2514 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2515 }
2516
2517 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002518 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002519 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002520#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2521 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002522#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2523 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002524 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002525 unsigned char *p = ssl->in_msg + 4;
2526 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2527
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002528 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002529 {
2530 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2531 return( ret );
2532 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002533
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002534 if( ( ret = ssl_psk_derive_premaster( ssl,
2535 ciphersuite_info->key_exchange ) ) != 0 )
2536 {
2537 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2538 return( ret );
2539 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002540 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002541 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002542#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002543#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2544 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2545 {
2546 unsigned char *p = ssl->in_msg + 4;
2547 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2548
2549 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2550 {
2551 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2552 return( ret );
2553 }
2554
2555 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
2556 {
2557 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
2558 return( ret );
2559 }
2560
2561 if( ( ret = ssl_psk_derive_premaster( ssl,
2562 ciphersuite_info->key_exchange ) ) != 0 )
2563 {
2564 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2565 return( ret );
2566 }
2567 }
2568 else
2569#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002570#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2571 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2572 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002573 unsigned char *p = ssl->in_msg + 4;
2574 unsigned char *end = ssl->in_msg + ssl->in_msglen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002575
2576 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2577 {
2578 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2579 return( ret );
2580 }
2581 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2582 {
2583 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2584 return( ret );
2585 }
2586
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002587 if( ( ret = ssl_psk_derive_premaster( ssl,
2588 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002589 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002590 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2591 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002592 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002593 }
2594 else
2595#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002596#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2597 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2598 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002599 unsigned char *p = ssl->in_msg + 4;
2600 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2601
2602 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2603 {
2604 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2605 return( ret );
2606 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002607
2608 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2609 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002610 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002611 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2612 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002613 }
2614
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002615 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2616
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002617 if( ( ret = ssl_psk_derive_premaster( ssl,
2618 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002619 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002620 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002621 return( ret );
2622 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002623 }
2624 else
2625#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002626#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2627 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002628 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002629 if( ( ret = ssl_parse_encrypted_pms( ssl,
2630 ssl->in_msg + 4,
2631 ssl->in_msg + ssl->in_msglen,
2632 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002633 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002634 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_ecrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002635 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002636 }
2637 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002638 else
2639#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2640 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002641 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002642 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2643 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002644
Paul Bakkerff60ee62010-03-16 21:09:09 +00002645 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2646 {
2647 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2648 return( ret );
2649 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002650
Paul Bakker5121ce52009-01-03 21:22:43 +00002651 ssl->state++;
2652
2653 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2654
2655 return( 0 );
2656}
2657
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002658#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2659 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002660 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2661 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002662static int ssl_parse_certificate_verify( ssl_context *ssl )
2663{
Paul Bakkered27a042013-04-18 22:46:23 +02002664 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002665 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002666
2667 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2668
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002669 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002670 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002671 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002672 {
2673 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2674 ssl->state++;
2675 return( 0 );
2676 }
2677
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002678 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002679 return( ret );
2680}
2681#else
2682static int ssl_parse_certificate_verify( ssl_context *ssl )
2683{
2684 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002685 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002686 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002687 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002688 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02002689#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002690 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02002691#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002692 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002693 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2694
2695 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2696
2697 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002698 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002699 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2700 {
2701 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2702 ssl->state++;
2703 return( 0 );
2704 }
2705
Paul Bakkered27a042013-04-18 22:46:23 +02002706 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002707 {
2708 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2709 ssl->state++;
2710 return( 0 );
2711 }
2712
Paul Bakker48916f92012-09-16 19:57:18 +00002713 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002714
2715 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2716 {
2717 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2718 return( ret );
2719 }
2720
2721 ssl->state++;
2722
2723 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2724 {
2725 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002726 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002727 }
2728
2729 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
2730 {
2731 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002732 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002733 }
2734
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002735 /*
2736 * 0 . 0 handshake type
2737 * 1 . 3 handshake length
2738 * 4 . 5 sig alg (TLS 1.2 only)
2739 * 4+n . 5+n signature length (n = sa_len)
2740 * 6+n . 6+n+m signature (m = sig_len)
2741 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002742
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002743#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2744 defined(POLARSSL_SSL_PROTO_TLS1_1)
2745 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002746 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002747 sa_len = 0;
2748
Paul Bakkerc70b9822013-04-07 22:00:46 +02002749 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002750 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002751
2752 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
2753 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
2754 POLARSSL_PK_ECDSA ) )
2755 {
2756 hash_start += 16;
2757 hashlen -= 16;
2758 md_alg = POLARSSL_MD_SHA1;
2759 }
Paul Bakker926af752012-11-23 13:38:07 +01002760 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002761 else
2762#endif
Paul Bakker577e0062013-08-28 11:57:20 +02002763#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2764 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002765 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002766 sa_len = 2;
2767
Paul Bakker5121ce52009-01-03 21:22:43 +00002768 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002769 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00002770 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002771 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002772 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002773 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2774 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01002775 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2776 }
2777
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002778 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01002779
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002780 /* Info from md_alg will be used instead */
2781 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002782
2783 /*
2784 * Signature
2785 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002786 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
2787 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002788 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002789 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2790 " for verify message" ) );
2791 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002792 }
2793
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002794 /*
2795 * Check the certificate's key type matches the signature alg
2796 */
2797 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
2798 {
2799 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
2800 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2801 }
Paul Bakker577e0062013-08-28 11:57:20 +02002802 }
2803 else
2804#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2805 {
2806 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002807 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002808 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002809
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002810 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01002811
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002812 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002813 {
2814 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002815 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002816 }
2817
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002818 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002819 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002820 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002821 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002822 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002823 return( ret );
2824 }
2825
2826 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
2827
Paul Bakkered27a042013-04-18 22:46:23 +02002828 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002829}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002830#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2831 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2832 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002833
Paul Bakkera503a632013-08-14 13:48:06 +02002834#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002835static int ssl_write_new_session_ticket( ssl_context *ssl )
2836{
2837 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002838 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002839 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002840
2841 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
2842
2843 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2844 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
2845
2846 /*
2847 * struct {
2848 * uint32 ticket_lifetime_hint;
2849 * opaque ticket<0..2^16-1>;
2850 * } NewSessionTicket;
2851 *
2852 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
2853 * 8 . 9 ticket_len (n)
2854 * 10 . 9+n ticket content
2855 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002856
2857 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
2858 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
2859 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
2860 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002861
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02002862 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
2863 {
2864 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
2865 tlen = 0;
2866 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002867
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002868 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
2869 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002870
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002871 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002872
2873 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2874 {
2875 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2876 return( ret );
2877 }
2878
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002879 /* No need to remember writing a NewSessionTicket any more */
2880 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002881
2882 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
2883
2884 return( 0 );
2885}
Paul Bakkera503a632013-08-14 13:48:06 +02002886#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002887
Paul Bakker5121ce52009-01-03 21:22:43 +00002888/*
Paul Bakker1961b702013-01-25 14:49:24 +01002889 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002890 */
Paul Bakker1961b702013-01-25 14:49:24 +01002891int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002892{
2893 int ret = 0;
2894
Paul Bakker1961b702013-01-25 14:49:24 +01002895 if( ssl->state == SSL_HANDSHAKE_OVER )
2896 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002897
Paul Bakker1961b702013-01-25 14:49:24 +01002898 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
2899
2900 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2901 return( ret );
2902
2903 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002904 {
Paul Bakker1961b702013-01-25 14:49:24 +01002905 case SSL_HELLO_REQUEST:
2906 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002907 break;
2908
Paul Bakker1961b702013-01-25 14:49:24 +01002909 /*
2910 * <== ClientHello
2911 */
2912 case SSL_CLIENT_HELLO:
2913 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002914 break;
Paul Bakker1961b702013-01-25 14:49:24 +01002915
2916 /*
2917 * ==> ServerHello
2918 * Certificate
2919 * ( ServerKeyExchange )
2920 * ( CertificateRequest )
2921 * ServerHelloDone
2922 */
2923 case SSL_SERVER_HELLO:
2924 ret = ssl_write_server_hello( ssl );
2925 break;
2926
2927 case SSL_SERVER_CERTIFICATE:
2928 ret = ssl_write_certificate( ssl );
2929 break;
2930
2931 case SSL_SERVER_KEY_EXCHANGE:
2932 ret = ssl_write_server_key_exchange( ssl );
2933 break;
2934
2935 case SSL_CERTIFICATE_REQUEST:
2936 ret = ssl_write_certificate_request( ssl );
2937 break;
2938
2939 case SSL_SERVER_HELLO_DONE:
2940 ret = ssl_write_server_hello_done( ssl );
2941 break;
2942
2943 /*
2944 * <== ( Certificate/Alert )
2945 * ClientKeyExchange
2946 * ( CertificateVerify )
2947 * ChangeCipherSpec
2948 * Finished
2949 */
2950 case SSL_CLIENT_CERTIFICATE:
2951 ret = ssl_parse_certificate( ssl );
2952 break;
2953
2954 case SSL_CLIENT_KEY_EXCHANGE:
2955 ret = ssl_parse_client_key_exchange( ssl );
2956 break;
2957
2958 case SSL_CERTIFICATE_VERIFY:
2959 ret = ssl_parse_certificate_verify( ssl );
2960 break;
2961
2962 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2963 ret = ssl_parse_change_cipher_spec( ssl );
2964 break;
2965
2966 case SSL_CLIENT_FINISHED:
2967 ret = ssl_parse_finished( ssl );
2968 break;
2969
2970 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002971 * ==> ( NewSessionTicket )
2972 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002973 * Finished
2974 */
2975 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002976#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002977 if( ssl->handshake->new_session_ticket != 0 )
2978 ret = ssl_write_new_session_ticket( ssl );
2979 else
Paul Bakkera503a632013-08-14 13:48:06 +02002980#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002981 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002982 break;
2983
2984 case SSL_SERVER_FINISHED:
2985 ret = ssl_write_finished( ssl );
2986 break;
2987
2988 case SSL_FLUSH_BUFFERS:
2989 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2990 ssl->state = SSL_HANDSHAKE_WRAPUP;
2991 break;
2992
2993 case SSL_HANDSHAKE_WRAPUP:
2994 ssl_handshake_wrapup( ssl );
2995 break;
2996
2997 default:
2998 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2999 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003000 }
3001
Paul Bakker5121ce52009-01-03 21:22:43 +00003002 return( ret );
3003}
Paul Bakker5121ce52009-01-03 21:22:43 +00003004#endif