blob: 5b35b9427006230c655c95e8b5f6ec008266a53e [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" ) );
1273
1274 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1275 if( ret != 0 )
1276 return( ret );
1277 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001278#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001279
Paul Bakker05decb22013-08-15 13:33:48 +02001280#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001281 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1282 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1283
1284 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1285 if( ret != 0 )
1286 return( ret );
1287 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001288#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001289
Paul Bakker1f2bc622013-08-15 13:45:55 +02001290#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001291 case TLS_EXT_TRUNCATED_HMAC:
1292 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1293
1294 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1295 if( ret != 0 )
1296 return( ret );
1297 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001298#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001299
Paul Bakkera503a632013-08-14 13:48:06 +02001300#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001301 case TLS_EXT_SESSION_TICKET:
1302 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1303
1304 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1305 if( ret != 0 )
1306 return( ret );
1307 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001308#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001309
Paul Bakker48916f92012-09-16 19:57:18 +00001310 default:
1311 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1312 ext_id ) );
1313 }
1314
1315 ext_len -= 4 + ext_size;
1316 ext += 4 + ext_size;
1317
1318 if( ext_len > 0 && ext_len < 4 )
1319 {
1320 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1321 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1322 }
1323 }
1324
1325 /*
1326 * Renegotiation security checks
1327 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001328 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1329 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1330 {
1331 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1332 handshake_failure = 1;
1333 }
1334 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1335 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1336 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001337 {
1338 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001339 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001340 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001341 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1342 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1343 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001344 {
1345 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001346 handshake_failure = 1;
1347 }
1348 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1349 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1350 renegotiation_info_seen == 1 )
1351 {
1352 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1353 handshake_failure = 1;
1354 }
1355
1356 if( handshake_failure == 1 )
1357 {
1358 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1359 return( ret );
1360
Paul Bakker48916f92012-09-16 19:57:18 +00001361 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1362 }
Paul Bakker380da532012-04-18 16:10:25 +00001363
Paul Bakker41c83d32013-03-20 14:39:14 +01001364 /*
1365 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001366 * (At the end because we need information from the EC-based extensions
1367 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001368 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001369 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
1370 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001371 {
1372 for( j = 0, p = buf + 41 + sess_len; j < ciph_len;
1373 j += 2, p += 2 )
1374 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001375 if( p[0] == ( ( ciphersuites[i] >> 8 ) & 0xFF ) &&
1376 p[1] == ( ( ciphersuites[i] ) & 0xFF ) )
Paul Bakker41c83d32013-03-20 14:39:14 +01001377 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001378 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
Paul Bakker41c83d32013-03-20 14:39:14 +01001379
1380 if( ciphersuite_info == NULL )
1381 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001382 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found",
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001383 ciphersuites[i] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001384 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1385 }
1386
Paul Bakker2fbefde2013-06-29 16:01:15 +02001387 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
1388 ciphersuite_info->max_minor_ver < ssl->minor_ver )
1389 continue;
1390
Paul Bakker5fd49172013-08-19 13:29:26 +02001391#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001392 if( ssl_ciphersuite_uses_ec( ciphersuite_info ) &&
Paul Bakkercaa3af42013-09-26 13:32:43 +02001393 ( ssl->handshake->curves == NULL ||
1394 ssl->handshake->curves[0] == NULL ) )
Paul Bakker41c83d32013-03-20 14:39:14 +01001395 continue;
Paul Bakker5fd49172013-08-19 13:29:26 +02001396#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001397
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001398#if defined(POLARSSL_X509_CRT_PARSE_C)
1399 /*
1400 * Final check: if ciphersuite requires us to have a
1401 * certificate/key of a particular type:
1402 * - select the appropriate certificate if we have one, or
1403 * - try the next ciphersuite if we don't
1404 * This must be done last since we modify the key_cert list.
1405 */
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02001406 if( ssl_pick_cert( ssl, ciphersuite_info ) != 0 )
1407 continue;
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02001408#endif
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001409
Paul Bakker41c83d32013-03-20 14:39:14 +01001410 goto have_ciphersuite;
1411 }
1412 }
1413 }
1414
1415 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1416
1417 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1418 return( ret );
1419
1420 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1421
1422have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001423 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001424 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1425 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1426
Paul Bakker5121ce52009-01-03 21:22:43 +00001427 ssl->in_left = 0;
1428 ssl->state++;
1429
1430 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1431
1432 return( 0 );
1433}
1434
Paul Bakker1f2bc622013-08-15 13:45:55 +02001435#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001436static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1437 unsigned char *buf,
1438 size_t *olen )
1439{
1440 unsigned char *p = buf;
1441
1442 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1443 {
1444 *olen = 0;
1445 return;
1446 }
1447
1448 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1449
1450 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1451 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1452
1453 *p++ = 0x00;
1454 *p++ = 0x00;
1455
1456 *olen = 4;
1457}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001458#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001459
Paul Bakkera503a632013-08-14 13:48:06 +02001460#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001461static void ssl_write_session_ticket_ext( ssl_context *ssl,
1462 unsigned char *buf,
1463 size_t *olen )
1464{
1465 unsigned char *p = buf;
1466
1467 if( ssl->handshake->new_session_ticket == 0 )
1468 {
1469 *olen = 0;
1470 return;
1471 }
1472
1473 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1474
1475 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1476 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1477
1478 *p++ = 0x00;
1479 *p++ = 0x00;
1480
1481 *olen = 4;
1482}
Paul Bakkera503a632013-08-14 13:48:06 +02001483#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001484
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001485static void ssl_write_renegotiation_ext( ssl_context *ssl,
1486 unsigned char *buf,
1487 size_t *olen )
1488{
1489 unsigned char *p = buf;
1490
1491 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1492 {
1493 *olen = 0;
1494 return;
1495 }
1496
1497 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1498
1499 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1500 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1501
1502 *p++ = 0x00;
1503 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1504 *p++ = ssl->verify_data_len * 2 & 0xFF;
1505
1506 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1507 p += ssl->verify_data_len;
1508 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1509 p += ssl->verify_data_len;
1510
1511 *olen = 5 + ssl->verify_data_len * 2;
1512}
1513
Paul Bakker05decb22013-08-15 13:33:48 +02001514#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001515static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1516 unsigned char *buf,
1517 size_t *olen )
1518{
1519 unsigned char *p = buf;
1520
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001521 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1522 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001523 *olen = 0;
1524 return;
1525 }
1526
1527 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1528
1529 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1530 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1531
1532 *p++ = 0x00;
1533 *p++ = 1;
1534
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001535 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001536
1537 *olen = 5;
1538}
Paul Bakker05decb22013-08-15 13:33:48 +02001539#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001540
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001541#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001542static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1543 unsigned char *buf,
1544 size_t *olen )
1545{
1546 unsigned char *p = buf;
1547 ((void) ssl);
1548
1549 *olen = 0;
1550
1551 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1552
1553 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1554 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1555
1556 *p++ = 0x00;
1557 *p++ = 2;
1558
1559 *p++ = 1;
1560 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1561
1562 *olen = 6;
1563}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001564#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001565
Paul Bakker5121ce52009-01-03 21:22:43 +00001566static int ssl_write_server_hello( ssl_context *ssl )
1567{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001568#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001569 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001570#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001571 int ret;
1572 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001573 unsigned char *buf, *p;
1574
1575 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1576
1577 /*
1578 * 0 . 0 handshake type
1579 * 1 . 3 handshake length
1580 * 4 . 5 protocol version
1581 * 6 . 9 UNIX time()
1582 * 10 . 37 random bytes
1583 */
1584 buf = ssl->out_msg;
1585 p = buf + 4;
1586
1587 *p++ = (unsigned char) ssl->major_ver;
1588 *p++ = (unsigned char) ssl->minor_ver;
1589
1590 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1591 buf[4], buf[5] ) );
1592
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001593#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001594 t = time( NULL );
1595 *p++ = (unsigned char)( t >> 24 );
1596 *p++ = (unsigned char)( t >> 16 );
1597 *p++ = (unsigned char)( t >> 8 );
1598 *p++ = (unsigned char)( t );
1599
1600 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001601#else
1602 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1603 return( ret );
1604
1605 p += 4;
1606#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001607
Paul Bakkera3d195c2011-11-27 21:07:34 +00001608 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1609 return( ret );
1610
1611 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001612
Paul Bakker48916f92012-09-16 19:57:18 +00001613 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001614
1615 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1616
1617 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001618 * Resume is 0 by default, see ssl_handshake_init().
1619 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1620 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001621 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001622 if( ssl->handshake->resume == 0 &&
1623 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001624 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001625 ssl->f_get_cache != NULL &&
1626 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1627 {
1628 ssl->handshake->resume = 1;
1629 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001630
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001631 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001632 {
1633 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001634 * New session, create a new session id,
1635 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001636 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001637 ssl->state++;
1638
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001639#if defined(POLARSSL_HAVE_TIME)
1640 ssl->session_negotiate->start = time( NULL );
1641#endif
1642
Paul Bakkera503a632013-08-14 13:48:06 +02001643#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001644 if( ssl->handshake->new_session_ticket != 0 )
1645 {
1646 ssl->session_negotiate->length = n = 0;
1647 memset( ssl->session_negotiate->id, 0, 32 );
1648 }
1649 else
1650#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001651 {
1652 ssl->session_negotiate->length = n = 32;
1653 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001654 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001655 return( ret );
1656 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001657 }
1658 else
1659 {
1660 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001661 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001662 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001663 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001664 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001665
1666 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1667 {
1668 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1669 return( ret );
1670 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001671 }
1672
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001673 /*
1674 * 38 . 38 session id length
1675 * 39 . 38+n session id
1676 * 39+n . 40+n chosen ciphersuite
1677 * 41+n . 41+n chosen compression alg.
1678 * 42+n . 43+n extensions length
1679 * 44+n . 43+n+m extensions
1680 */
1681 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001682 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1683 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001684
1685 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1686 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1687 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001688 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001689
Paul Bakker48916f92012-09-16 19:57:18 +00001690 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1691 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1692 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001693
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001694 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
1695 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001696 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001697 ssl->session_negotiate->compression ) );
1698
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001699 /*
1700 * First write extensions, then the total length
1701 */
1702 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1703 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001704
Paul Bakker05decb22013-08-15 13:33:48 +02001705#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001706 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1707 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001708#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001709
Paul Bakker1f2bc622013-08-15 13:45:55 +02001710#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001711 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1712 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001713#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001714
Paul Bakkera503a632013-08-14 13:48:06 +02001715#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001716 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1717 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001718#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001719
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001720#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001721 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1722 ext_len += olen;
1723#endif
1724
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001725 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001726
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001727 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1728 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1729 p += ext_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001730
1731 ssl->out_msglen = p - buf;
1732 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1733 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1734
1735 ret = ssl_write_record( ssl );
1736
1737 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1738
1739 return( ret );
1740}
1741
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001742#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1743 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001744 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1745 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001746static int ssl_write_certificate_request( ssl_context *ssl )
1747{
Paul Bakkered27a042013-04-18 22:46:23 +02001748 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1749 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001750
1751 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1752
1753 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001754 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1755 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001756 {
1757 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1758 ssl->state++;
1759 return( 0 );
1760 }
1761
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001762 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001763 return( ret );
1764}
1765#else
1766static int ssl_write_certificate_request( ssl_context *ssl )
1767{
1768 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1769 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001770 size_t dn_size, total_dn_size; /* excluding length bytes */
1771 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00001772 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001773 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001774
1775 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1776
1777 ssl->state++;
1778
Paul Bakkerfbb17802013-04-17 19:10:21 +02001779 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001780 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001781 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001782 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001783 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001784 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001785 return( 0 );
1786 }
1787
1788 /*
1789 * 0 . 0 handshake type
1790 * 1 . 3 handshake length
1791 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01001792 * 5 .. m-1 cert types
1793 * m .. m+1 sig alg length (TLS 1.2 only)
1794 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001795 * n .. n+1 length of all DNs
1796 * n+2 .. n+3 length of DN 1
1797 * n+4 .. ... Distinguished Name #1
1798 * ... .. ... length of DN 2, etc.
1799 */
1800 buf = ssl->out_msg;
1801 p = buf + 4;
1802
1803 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001804 * Supported certificate types
1805 *
1806 * ClientCertificateType certificate_types<1..2^8-1>;
1807 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00001808 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001809 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01001810
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001811#if defined(POLARSSL_RSA_C)
1812 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
1813#endif
1814#if defined(POLARSSL_ECDSA_C)
1815 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
1816#endif
1817
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001818 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001819 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01001820
Paul Bakker577e0062013-08-28 11:57:20 +02001821 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001822#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01001823 /*
1824 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01001825 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001826 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
1827 *
1828 * struct {
1829 * HashAlgorithm hash;
1830 * SignatureAlgorithm signature;
1831 * } SignatureAndHashAlgorithm;
1832 *
1833 * enum { (255) } HashAlgorithm;
1834 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01001835 */
Paul Bakker21dca692013-01-03 11:41:08 +01001836 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01001837 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001838 /*
1839 * Only use current running hash algorithm that is already required
1840 * for requested ciphersuite.
1841 */
Paul Bakker926af752012-11-23 13:38:07 +01001842 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
1843
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001844 if( ssl->transform_negotiate->ciphersuite_info->mac ==
1845 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01001846 {
1847 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
1848 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02001849
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001850 /*
1851 * Supported signature algorithms
1852 */
1853#if defined(POLARSSL_RSA_C)
1854 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1855 p[2 + sa_len++] = SSL_SIG_RSA;
1856#endif
1857#if defined(POLARSSL_ECDSA_C)
1858 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1859 p[2 + sa_len++] = SSL_SIG_ECDSA;
1860#endif
Paul Bakker926af752012-11-23 13:38:07 +01001861
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001862 p[0] = (unsigned char)( sa_len >> 8 );
1863 p[1] = (unsigned char)( sa_len );
1864 sa_len += 2;
1865 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01001866 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001867#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001868
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001869 /*
1870 * DistinguishedName certificate_authorities<0..2^16-1>;
1871 * opaque DistinguishedName<1..2^16-1>;
1872 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001873 p += 2;
1874 crt = ssl->ca_chain;
1875
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001876 total_dn_size = 0;
Paul Bakker29087132010-03-21 21:03:34 +00001877 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001878 {
1879 if( p - buf > 4096 )
1880 break;
1881
Paul Bakker926af752012-11-23 13:38:07 +01001882 dn_size = crt->subject_raw.len;
1883 *p++ = (unsigned char)( dn_size >> 8 );
1884 *p++ = (unsigned char)( dn_size );
1885 memcpy( p, crt->subject_raw.p, dn_size );
1886 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001887
Paul Bakker926af752012-11-23 13:38:07 +01001888 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
1889
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001890 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01001891 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00001892 }
1893
Paul Bakker926af752012-11-23 13:38:07 +01001894 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001895 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1896 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001897 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
1898 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001899
1900 ret = ssl_write_record( ssl );
1901
1902 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
1903
1904 return( ret );
1905}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001906#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
1907 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
1908 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001909
Paul Bakker41c83d32013-03-20 14:39:14 +01001910static int ssl_write_server_key_exchange( ssl_context *ssl )
1911{
Paul Bakker23986e52011-04-24 08:57:21 +00001912 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001913 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001914 const ssl_ciphersuite_t *ciphersuite_info =
1915 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001916
1917#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1918 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
1919 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001920 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02001921 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001922 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001923 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001924 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001925 ((void) dig_signed);
1926 ((void) dig_signed_len);
1927#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001928
Paul Bakker5121ce52009-01-03 21:22:43 +00001929 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
1930
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001931 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
1932 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1933 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001934 {
1935 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
1936 ssl->state++;
1937 return( 0 );
1938 }
1939
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001940#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
1941 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1942 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1943 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001944 {
1945 /* TODO: Support identity hints */
1946 *(p++) = 0x00;
1947 *(p++) = 0x00;
1948
1949 n += 2;
1950 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001951#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
1952 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001953
1954#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1955 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1956 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1957 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00001958 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001959 /*
1960 * Ephemeral DH parameters:
1961 *
1962 * struct {
1963 * opaque dh_p<1..2^16-1>;
1964 * opaque dh_g<1..2^16-1>;
1965 * opaque dh_Ys<1..2^16-1>;
1966 * } ServerDHParams;
1967 */
1968 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
1969 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
1970 {
1971 SSL_DEBUG_RET( 1, "mpi_copy", ret );
1972 return( ret );
1973 }
Paul Bakker48916f92012-09-16 19:57:18 +00001974
Paul Bakker41c83d32013-03-20 14:39:14 +01001975 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001976 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001977 p,
1978 &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001979 {
1980 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
1981 return( ret );
1982 }
1983
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001984 dig_signed = p;
1985 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001986
1987 p += len;
1988 n += len;
1989
Paul Bakker41c83d32013-03-20 14:39:14 +01001990 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
1991 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1992 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1993 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
1994 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001995#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1996 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001997
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001998#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001999 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2000 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2001
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002002 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002003 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2004 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002005 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002006 /*
2007 * Ephemeral ECDH parameters:
2008 *
2009 * struct {
2010 * ECParameters curve_params;
2011 * ECPoint public;
2012 * } ServerECDHParams;
2013 */
Paul Bakker41c83d32013-03-20 14:39:14 +01002014 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02002015 ssl->handshake->curves[0]->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002016 {
2017 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2018 return( ret );
2019 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002020
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02002021 SSL_DEBUG_MSG( 2, ( "ECDH curve size: %d",
2022 (int) ssl->handshake->ecdh_ctx.grp.nbits ) );
2023
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002024 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2025 p, SSL_MAX_CONTENT_LEN - n,
2026 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002027 {
2028 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2029 return( ret );
2030 }
2031
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002032 dig_signed = p;
2033 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002034
2035 p += len;
2036 n += len;
2037
Paul Bakker41c83d32013-03-20 14:39:14 +01002038 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2039 }
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002040#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002041 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2042 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002043
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002044#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002045 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2046 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002047 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002048 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2049 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002050 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002051 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002052 unsigned int hashlen = 0;
2053 unsigned char hash[64];
2054 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002055
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002056 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002057 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2058 */
Paul Bakker577e0062013-08-28 11:57:20 +02002059#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002060 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2061 {
2062 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2063
2064 if( md_alg == POLARSSL_MD_NONE )
2065 {
2066 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2067 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2068 }
2069 }
Paul Bakker577e0062013-08-28 11:57:20 +02002070 else
2071#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002072#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2073 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker577e0062013-08-28 11:57:20 +02002074 if ( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002075 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2076 {
2077 md_alg = POLARSSL_MD_SHA1;
2078 }
2079 else
Paul Bakker577e0062013-08-28 11:57:20 +02002080#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002081 {
2082 md_alg = POLARSSL_MD_NONE;
2083 }
2084
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002085 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002086 * Compute the hash to be signed
2087 */
Paul Bakker577e0062013-08-28 11:57:20 +02002088#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2089 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002090 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002091 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002092 md5_context md5;
2093 sha1_context sha1;
2094
2095 /*
2096 * digitally-signed struct {
2097 * opaque md5_hash[16];
2098 * opaque sha_hash[20];
2099 * };
2100 *
2101 * md5_hash
2102 * MD5(ClientHello.random + ServerHello.random
2103 * + ServerParams);
2104 * sha_hash
2105 * SHA(ClientHello.random + ServerHello.random
2106 * + ServerParams);
2107 */
2108 md5_starts( &md5 );
2109 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002110 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002111 md5_finish( &md5, hash );
2112
2113 sha1_starts( &sha1 );
2114 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002115 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002116 sha1_finish( &sha1, hash + 16 );
2117
2118 hashlen = 36;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002119 }
2120 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002121#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2122 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002123#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2124 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002125 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002126 {
2127 md_context_t ctx;
2128
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002129 /* Info from md_alg will be used instead */
2130 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002131
2132 /*
2133 * digitally-signed struct {
2134 * opaque client_random[32];
2135 * opaque server_random[32];
2136 * ServerDHParams params;
2137 * };
2138 */
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002139 if( ( ret = md_init_ctx( &ctx, md_info_from_type(md_alg) ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002140 {
2141 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2142 return( ret );
2143 }
2144
2145 md_starts( &ctx );
2146 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002147 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002148 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02002149
2150 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
2151 {
2152 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
2153 return( ret );
2154 }
2155
Paul Bakker23f36802012-09-28 14:15:14 +00002156 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002157 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002158#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2159 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002160 {
2161 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002162 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02002163 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002164
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002165 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2166 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002167
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002168 /*
2169 * Make the signature
2170 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002171 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002172 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002173 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2174 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002175 }
Paul Bakker23f36802012-09-28 14:15:14 +00002176
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002177#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002178 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2179 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002180 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002181 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002182
2183 n += 2;
2184 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002185#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002186
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002187 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002188 p + 2 , &signature_len,
2189 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002190 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002191 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002192 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002193 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002194
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002195 *(p++) = (unsigned char)( signature_len >> 8 );
2196 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002197 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002198
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002199 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002200
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002201 p += signature_len;
2202 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002203 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002204#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002205 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2206 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002207
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002208 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002209 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2210 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2211
2212 ssl->state++;
2213
2214 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2215 {
2216 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2217 return( ret );
2218 }
2219
2220 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2221
2222 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002223}
2224
2225static int ssl_write_server_hello_done( ssl_context *ssl )
2226{
2227 int ret;
2228
2229 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2230
2231 ssl->out_msglen = 4;
2232 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2233 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2234
2235 ssl->state++;
2236
2237 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2238 {
2239 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2240 return( ret );
2241 }
2242
2243 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2244
2245 return( 0 );
2246}
2247
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002248#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2249 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2250static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2251 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002252{
2253 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002254 size_t n;
2255
2256 /*
2257 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2258 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002259 if( *p + 2 > end )
2260 {
2261 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2262 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2263 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002264
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002265 n = ( (*p)[0] << 8 ) | (*p)[1];
2266 *p += 2;
2267
2268 if( n < 1 || n > ssl->handshake->dhm_ctx.len || *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002269 {
2270 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2271 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2272 }
2273
2274 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002275 *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002276 {
2277 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2278 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2279 }
2280
2281 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2282
Paul Bakker70df2fb2013-04-17 17:19:09 +02002283 return( ret );
2284}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002285#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2286 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002287
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002288#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2289 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002290static int ssl_parse_encrypted_pms( ssl_context *ssl,
2291 const unsigned char *p,
2292 const unsigned char *end,
2293 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002294{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002295 int ret;
2296 size_t len = pk_get_len( ssl_own_key( ssl ) );
2297 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002298
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002299 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002300 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002301 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002302 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2303 }
2304
2305 /*
2306 * Decrypt the premaster using own private RSA key
2307 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002308#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2309 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002310 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2311 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002312 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2313 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002314 {
2315 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2316 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2317 }
2318 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002319#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002320
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002321 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002322 {
2323 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2324 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2325 }
2326
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002327 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2328 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002329 sizeof(ssl->handshake->premaster),
2330 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002331
2332 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002333 pms[0] != ssl->handshake->max_major_ver ||
2334 pms[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002335 {
2336 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2337
2338 /*
2339 * Protection against Bleichenbacher's attack:
2340 * invalid PKCS#1 v1.5 padding must not cause
2341 * the connection to end immediately; instead,
2342 * send a bad_record_mac later in the handshake.
2343 */
2344 ssl->handshake->pmslen = 48;
2345
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002346 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002347 if( ret != 0 )
2348 return( ret );
2349 }
2350
2351 return( ret );
2352}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002353#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2354 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002355
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002356#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002357static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2358 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002359{
Paul Bakker6db455e2013-09-18 17:29:31 +02002360 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002361 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002362
Paul Bakker6db455e2013-09-18 17:29:31 +02002363 if( ssl->f_psk == NULL &&
2364 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2365 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002366 {
2367 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2368 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2369 }
2370
2371 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002372 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002373 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002374 if( *p + 2 > end )
2375 {
2376 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2377 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2378 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002379
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002380 n = ( (*p)[0] << 8 ) | (*p)[1];
2381 *p += 2;
2382
2383 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002384 {
2385 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2386 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2387 }
2388
Paul Bakker6db455e2013-09-18 17:29:31 +02002389 if( ssl->f_psk != NULL )
2390 {
2391 if( ( ret != ssl->f_psk( ssl->p_psk, ssl, *p, n ) ) != 0 )
2392 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2393 }
2394
2395 if( ret == 0 )
2396 {
2397 if( n != ssl->psk_identity_len ||
2398 memcmp( ssl->psk_identity, *p, n ) != 0 )
2399 {
2400 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2401 }
2402 }
2403
2404 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002405 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002406 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002407 if( ( ret = ssl_send_alert_message( ssl,
2408 SSL_ALERT_LEVEL_FATAL,
2409 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2410 {
2411 return( ret );
2412 }
2413
2414 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002415 }
2416
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002417 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002418 ret = 0;
2419
Paul Bakkerfbb17802013-04-17 19:10:21 +02002420 return( ret );
2421}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002422#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002423
Paul Bakker5121ce52009-01-03 21:22:43 +00002424static int ssl_parse_client_key_exchange( ssl_context *ssl )
2425{
Paul Bakker23986e52011-04-24 08:57:21 +00002426 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002427 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002428
Paul Bakker41c83d32013-03-20 14:39:14 +01002429 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002430
2431 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2432
2433 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2434 {
2435 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2436 return( ret );
2437 }
2438
2439 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2440 {
2441 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002442 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002443 }
2444
2445 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
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
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002451#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002452 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002453 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002454 unsigned char *p = ssl->in_msg + 4;
2455 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2456
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002457 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002458 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002459 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2460 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002462
2463 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
2464
2465 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2466 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002467 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002468 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002469 {
2470 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2471 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2472 }
2473
2474 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002475 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002476 else
2477#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002478#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2479 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2480 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2481 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002482 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002483 size_t n = ssl->in_msg[3];
2484
2485 if( n < 1 || n > mpi_size( &ssl->handshake->ecdh_ctx.grp.P ) * 2 + 2 ||
2486 n + 4 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002487 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002488 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2489 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002490 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002491
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002492 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2493 ssl->in_msg + 4, n ) ) != 0 )
2494 {
2495 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2496 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2497 }
2498
2499 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2500
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002501 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2502 &ssl->handshake->pmslen,
2503 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002504 POLARSSL_MPI_MAX_SIZE,
2505 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002506 {
2507 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2508 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2509 }
2510
2511 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002512 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002513 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002514#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2515 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002516#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2517 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002518 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002519 unsigned char *p = ssl->in_msg + 4;
2520 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2521
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002522 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002523 {
2524 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2525 return( ret );
2526 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002527
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002528 if( ( ret = ssl_psk_derive_premaster( ssl,
2529 ciphersuite_info->key_exchange ) ) != 0 )
2530 {
2531 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2532 return( ret );
2533 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002534 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002535 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002536#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002537#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2538 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2539 {
2540 unsigned char *p = ssl->in_msg + 4;
2541 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2542
2543 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2544 {
2545 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2546 return( ret );
2547 }
2548
2549 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
2550 {
2551 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
2552 return( ret );
2553 }
2554
2555 if( ( ret = ssl_psk_derive_premaster( ssl,
2556 ciphersuite_info->key_exchange ) ) != 0 )
2557 {
2558 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2559 return( ret );
2560 }
2561 }
2562 else
2563#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002564#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2565 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2566 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002567 unsigned char *p = ssl->in_msg + 4;
2568 unsigned char *end = ssl->in_msg + ssl->in_msglen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002569
2570 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2571 {
2572 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2573 return( ret );
2574 }
2575 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2576 {
2577 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2578 return( ret );
2579 }
2580
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002581 if( ( ret = ssl_psk_derive_premaster( ssl,
2582 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002583 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002584 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2585 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002586 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002587 }
2588 else
2589#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002590#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2591 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2592 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002593 unsigned char *p = ssl->in_msg + 4;
2594 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2595
2596 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2597 {
2598 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2599 return( ret );
2600 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002601
2602 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2603 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002604 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002605 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2606 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002607 }
2608
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002609 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2610
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002611 if( ( ret = ssl_psk_derive_premaster( ssl,
2612 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002613 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002614 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002615 return( ret );
2616 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002617 }
2618 else
2619#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002620#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2621 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002622 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002623 if( ( ret = ssl_parse_encrypted_pms( ssl,
2624 ssl->in_msg + 4,
2625 ssl->in_msg + ssl->in_msglen,
2626 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002627 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002628 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_ecrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002629 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002630 }
2631 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002632 else
2633#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2634 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002635 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002636 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2637 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002638
Paul Bakkerff60ee62010-03-16 21:09:09 +00002639 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2640 {
2641 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2642 return( ret );
2643 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002644
Paul Bakker5121ce52009-01-03 21:22:43 +00002645 ssl->state++;
2646
2647 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2648
2649 return( 0 );
2650}
2651
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002652#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2653 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002654 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2655 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002656static int ssl_parse_certificate_verify( ssl_context *ssl )
2657{
Paul Bakkered27a042013-04-18 22:46:23 +02002658 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002659 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002660
2661 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2662
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002663 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002664 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002665 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002666 {
2667 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2668 ssl->state++;
2669 return( 0 );
2670 }
2671
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002672 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002673 return( ret );
2674}
2675#else
2676static int ssl_parse_certificate_verify( ssl_context *ssl )
2677{
2678 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002679 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002680 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002681 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002682 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02002683#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002684 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02002685#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002686 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002687 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2688
2689 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2690
2691 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002692 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002693 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2694 {
2695 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2696 ssl->state++;
2697 return( 0 );
2698 }
2699
Paul Bakkered27a042013-04-18 22:46:23 +02002700 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002701 {
2702 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2703 ssl->state++;
2704 return( 0 );
2705 }
2706
Paul Bakker48916f92012-09-16 19:57:18 +00002707 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002708
2709 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2710 {
2711 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2712 return( ret );
2713 }
2714
2715 ssl->state++;
2716
2717 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2718 {
2719 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002720 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002721 }
2722
2723 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
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
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002729 /*
2730 * 0 . 0 handshake type
2731 * 1 . 3 handshake length
2732 * 4 . 5 sig alg (TLS 1.2 only)
2733 * 4+n . 5+n signature length (n = sa_len)
2734 * 6+n . 6+n+m signature (m = sig_len)
2735 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002736
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002737#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2738 defined(POLARSSL_SSL_PROTO_TLS1_1)
2739 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002740 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002741 sa_len = 0;
2742
Paul Bakkerc70b9822013-04-07 22:00:46 +02002743 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002744 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002745
2746 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
2747 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
2748 POLARSSL_PK_ECDSA ) )
2749 {
2750 hash_start += 16;
2751 hashlen -= 16;
2752 md_alg = POLARSSL_MD_SHA1;
2753 }
Paul Bakker926af752012-11-23 13:38:07 +01002754 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002755 else
2756#endif
Paul Bakker577e0062013-08-28 11:57:20 +02002757#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2758 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002759 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002760 sa_len = 2;
2761
Paul Bakker5121ce52009-01-03 21:22:43 +00002762 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002763 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00002764 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002765 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002766 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002767 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2768 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01002769 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2770 }
2771
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002772 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01002773
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002774 /* Info from md_alg will be used instead */
2775 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002776
2777 /*
2778 * Signature
2779 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002780 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
2781 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002782 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002783 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2784 " for verify message" ) );
2785 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002786 }
2787
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002788 /*
2789 * Check the certificate's key type matches the signature alg
2790 */
2791 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
2792 {
2793 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
2794 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2795 }
Paul Bakker577e0062013-08-28 11:57:20 +02002796 }
2797 else
2798#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2799 {
2800 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002801 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002802 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002803
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002804 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01002805
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002806 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002807 {
2808 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002809 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002810 }
2811
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002812 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002813 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002814 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002815 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002816 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002817 return( ret );
2818 }
2819
2820 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
2821
Paul Bakkered27a042013-04-18 22:46:23 +02002822 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002823}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002824#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2825 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2826 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002827
Paul Bakkera503a632013-08-14 13:48:06 +02002828#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002829static int ssl_write_new_session_ticket( ssl_context *ssl )
2830{
2831 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002832 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002833 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002834
2835 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
2836
2837 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2838 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
2839
2840 /*
2841 * struct {
2842 * uint32 ticket_lifetime_hint;
2843 * opaque ticket<0..2^16-1>;
2844 * } NewSessionTicket;
2845 *
2846 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
2847 * 8 . 9 ticket_len (n)
2848 * 10 . 9+n ticket content
2849 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002850
2851 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
2852 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
2853 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
2854 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002855
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02002856 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
2857 {
2858 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
2859 tlen = 0;
2860 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002861
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002862 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
2863 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002864
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002865 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002866
2867 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2868 {
2869 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2870 return( ret );
2871 }
2872
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002873 /* No need to remember writing a NewSessionTicket any more */
2874 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002875
2876 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
2877
2878 return( 0 );
2879}
Paul Bakkera503a632013-08-14 13:48:06 +02002880#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002881
Paul Bakker5121ce52009-01-03 21:22:43 +00002882/*
Paul Bakker1961b702013-01-25 14:49:24 +01002883 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002884 */
Paul Bakker1961b702013-01-25 14:49:24 +01002885int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002886{
2887 int ret = 0;
2888
Paul Bakker1961b702013-01-25 14:49:24 +01002889 if( ssl->state == SSL_HANDSHAKE_OVER )
2890 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002891
Paul Bakker1961b702013-01-25 14:49:24 +01002892 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
2893
2894 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2895 return( ret );
2896
2897 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002898 {
Paul Bakker1961b702013-01-25 14:49:24 +01002899 case SSL_HELLO_REQUEST:
2900 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002901 break;
2902
Paul Bakker1961b702013-01-25 14:49:24 +01002903 /*
2904 * <== ClientHello
2905 */
2906 case SSL_CLIENT_HELLO:
2907 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002908 break;
Paul Bakker1961b702013-01-25 14:49:24 +01002909
2910 /*
2911 * ==> ServerHello
2912 * Certificate
2913 * ( ServerKeyExchange )
2914 * ( CertificateRequest )
2915 * ServerHelloDone
2916 */
2917 case SSL_SERVER_HELLO:
2918 ret = ssl_write_server_hello( ssl );
2919 break;
2920
2921 case SSL_SERVER_CERTIFICATE:
2922 ret = ssl_write_certificate( ssl );
2923 break;
2924
2925 case SSL_SERVER_KEY_EXCHANGE:
2926 ret = ssl_write_server_key_exchange( ssl );
2927 break;
2928
2929 case SSL_CERTIFICATE_REQUEST:
2930 ret = ssl_write_certificate_request( ssl );
2931 break;
2932
2933 case SSL_SERVER_HELLO_DONE:
2934 ret = ssl_write_server_hello_done( ssl );
2935 break;
2936
2937 /*
2938 * <== ( Certificate/Alert )
2939 * ClientKeyExchange
2940 * ( CertificateVerify )
2941 * ChangeCipherSpec
2942 * Finished
2943 */
2944 case SSL_CLIENT_CERTIFICATE:
2945 ret = ssl_parse_certificate( ssl );
2946 break;
2947
2948 case SSL_CLIENT_KEY_EXCHANGE:
2949 ret = ssl_parse_client_key_exchange( ssl );
2950 break;
2951
2952 case SSL_CERTIFICATE_VERIFY:
2953 ret = ssl_parse_certificate_verify( ssl );
2954 break;
2955
2956 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2957 ret = ssl_parse_change_cipher_spec( ssl );
2958 break;
2959
2960 case SSL_CLIENT_FINISHED:
2961 ret = ssl_parse_finished( ssl );
2962 break;
2963
2964 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002965 * ==> ( NewSessionTicket )
2966 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002967 * Finished
2968 */
2969 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002970#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002971 if( ssl->handshake->new_session_ticket != 0 )
2972 ret = ssl_write_new_session_ticket( ssl );
2973 else
Paul Bakkera503a632013-08-14 13:48:06 +02002974#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002975 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002976 break;
2977
2978 case SSL_SERVER_FINISHED:
2979 ret = ssl_write_finished( ssl );
2980 break;
2981
2982 case SSL_FLUSH_BUFFERS:
2983 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2984 ssl->state = SSL_HANDSHAKE_WRAPUP;
2985 break;
2986
2987 case SSL_HANDSHAKE_WRAPUP:
2988 ssl_handshake_wrapup( ssl );
2989 break;
2990
2991 default:
2992 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2993 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002994 }
2995
Paul Bakker5121ce52009-01-03 21:22:43 +00002996 return( ret );
2997}
Paul Bakker5121ce52009-01-03 21:22:43 +00002998#endif