blob: 9c90268c1146acebaa9ee02e34239bdeeddb2392 [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)
Paul Bakker5701cdc2012-09-27 21:49:42 +0000341static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000342 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000343 size_t len )
344{
345 int ret;
346 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000347 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000348
349 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
350 if( servername_list_size + 2 != len )
351 {
352 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
353 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
354 }
355
356 p = buf + 2;
357 while( servername_list_size > 0 )
358 {
359 hostname_len = ( ( p[1] << 8 ) | p[2] );
360 if( hostname_len + 3 > servername_list_size )
361 {
362 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
363 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
364 }
365
366 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
367 {
368 ret = ssl->f_sni( ssl->p_sni, ssl, p + 3, hostname_len );
369 if( ret != 0 )
370 {
371 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
372 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
373 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
374 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000375 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000376 }
377
378 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000379 p += hostname_len + 3;
380 }
381
382 if( servername_list_size != 0 )
383 {
384 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
385 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000386 }
387
388 return( 0 );
389}
Paul Bakker0be444a2013-08-27 21:55:01 +0200390#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000391
Paul Bakker48916f92012-09-16 19:57:18 +0000392static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000393 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000394 size_t len )
395{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000396 int ret;
397
Paul Bakker48916f92012-09-16 19:57:18 +0000398 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
399 {
400 if( len != 1 || buf[0] != 0x0 )
401 {
402 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000403
404 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
405 return( ret );
406
Paul Bakker48916f92012-09-16 19:57:18 +0000407 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
408 }
409
410 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
411 }
412 else
413 {
414 if( len != 1 + ssl->verify_data_len ||
415 buf[0] != ssl->verify_data_len ||
416 memcmp( buf + 1, ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
417 {
418 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000419
420 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
421 return( ret );
422
Paul Bakker48916f92012-09-16 19:57:18 +0000423 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
424 }
425 }
426
427 return( 0 );
428}
429
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200430#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +0000431static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
432 const unsigned char *buf,
433 size_t len )
434{
435 size_t sig_alg_list_size;
436 const unsigned char *p;
437
438 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
439 if( sig_alg_list_size + 2 != len ||
440 sig_alg_list_size %2 != 0 )
441 {
442 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
443 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
444 }
445
446 p = buf + 2;
447 while( sig_alg_list_size > 0 )
448 {
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200449 /*
450 * For now, just ignore signature algorithm and rely on offered
451 * ciphersuites only. To be fixed later.
452 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200453#if defined(POLARSSL_SHA512_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000454 if( p[0] == SSL_HASH_SHA512 )
455 {
456 ssl->handshake->sig_alg = SSL_HASH_SHA512;
457 break;
458 }
459 if( p[0] == SSL_HASH_SHA384 )
460 {
461 ssl->handshake->sig_alg = SSL_HASH_SHA384;
462 break;
463 }
464#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200465#if defined(POLARSSL_SHA256_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000466 if( p[0] == SSL_HASH_SHA256 )
467 {
468 ssl->handshake->sig_alg = SSL_HASH_SHA256;
469 break;
470 }
471 if( p[0] == SSL_HASH_SHA224 )
472 {
473 ssl->handshake->sig_alg = SSL_HASH_SHA224;
474 break;
475 }
476#endif
477 if( p[0] == SSL_HASH_SHA1 )
478 {
479 ssl->handshake->sig_alg = SSL_HASH_SHA1;
480 break;
481 }
482 if( p[0] == SSL_HASH_MD5 )
483 {
484 ssl->handshake->sig_alg = SSL_HASH_MD5;
485 break;
486 }
487
488 sig_alg_list_size -= 2;
489 p += 2;
490 }
491
492 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
493 ssl->handshake->sig_alg ) );
494
495 return( 0 );
496}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200497#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker23f36802012-09-28 14:15:14 +0000498
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200499#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200500static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
501 const unsigned char *buf,
502 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100503{
504 size_t list_size;
505 const unsigned char *p;
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200506 const ecp_curve_info *curve_info;
Paul Bakker41c83d32013-03-20 14:39:14 +0100507
508 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
509 if( list_size + 2 != len ||
510 list_size % 2 != 0 )
511 {
512 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
513 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
514 }
515
516 p = buf + 2;
517 while( list_size > 0 )
518 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200519 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200520
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200521 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100522 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200523 ssl->handshake->ec_curve = curve_info->grp_id;
Paul Bakker41c83d32013-03-20 14:39:14 +0100524 return( 0 );
525 }
526
527 list_size -= 2;
528 p += 2;
529 }
530
531 return( 0 );
532}
533
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200534static int ssl_parse_supported_point_formats( ssl_context *ssl,
535 const unsigned char *buf,
536 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100537{
538 size_t list_size;
539 const unsigned char *p;
540
541 list_size = buf[0];
542 if( list_size + 1 != len )
543 {
544 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
545 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
546 }
547
548 p = buf + 2;
549 while( list_size > 0 )
550 {
551 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
552 p[0] == POLARSSL_ECP_PF_COMPRESSED )
553 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200554 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200555 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100556 return( 0 );
557 }
558
559 list_size--;
560 p++;
561 }
562
563 return( 0 );
564}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200565#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100566
Paul Bakker05decb22013-08-15 13:33:48 +0200567#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200568static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
569 const unsigned char *buf,
570 size_t len )
571{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200572 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200573 {
574 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
575 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
576 }
577
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200578 ssl->session_negotiate->mfl_code = buf[0];
579
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200580 return( 0 );
581}
Paul Bakker05decb22013-08-15 13:33:48 +0200582#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200583
Paul Bakker1f2bc622013-08-15 13:45:55 +0200584#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200585static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
586 const unsigned char *buf,
587 size_t len )
588{
589 if( len != 0 )
590 {
591 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
592 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
593 }
594
595 ((void) buf);
596
597 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
598
599 return( 0 );
600}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200601#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200602
Paul Bakkera503a632013-08-14 13:48:06 +0200603#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200604static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200605 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200606 size_t len )
607{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200608 int ret;
609
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200610 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
611 return( 0 );
612
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200613 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200614 ssl->handshake->new_session_ticket = 1;
615
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200616 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
617
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200618 if( len == 0 )
619 return( 0 );
620
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200621 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
622 {
623 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
624 return( 0 );
625 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200626
627 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200628 * Failures are ok: just ignore the ticket and proceed.
629 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200630 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
631 {
632 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200633 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200634 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200635
636 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
637
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200638 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200639
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200640 /* Don't send a new ticket after all, this one is OK */
641 ssl->handshake->new_session_ticket = 0;
642
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200643 return( 0 );
644}
Paul Bakkera503a632013-08-14 13:48:06 +0200645#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200646
Paul Bakker78a8c712013-03-06 17:01:52 +0100647#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
648static int ssl_parse_client_hello_v2( ssl_context *ssl )
649{
650 int ret;
651 unsigned int i, j;
652 size_t n;
653 unsigned int ciph_len, sess_len, chal_len;
654 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200655 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200656 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100657
658 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
659
660 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
661 {
662 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
663
664 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
665 return( ret );
666
667 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
668 }
669
670 buf = ssl->in_hdr;
671
672 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
673
674 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
675 buf[2] ) );
676 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
677 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
678 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
679 buf[3], buf[4] ) );
680
681 /*
682 * SSLv2 Client Hello
683 *
684 * Record layer:
685 * 0 . 1 message length
686 *
687 * SSL layer:
688 * 2 . 2 message type
689 * 3 . 4 protocol version
690 */
691 if( buf[2] != SSL_HS_CLIENT_HELLO ||
692 buf[3] != SSL_MAJOR_VERSION_3 )
693 {
694 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
695 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
696 }
697
698 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
699
700 if( n < 17 || n > 512 )
701 {
702 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
703 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
704 }
705
706 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200707 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
708 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100709
710 if( ssl->minor_ver < ssl->min_minor_ver )
711 {
712 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
713 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
714 ssl->min_major_ver, ssl->min_minor_ver ) );
715
716 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
717 SSL_ALERT_MSG_PROTOCOL_VERSION );
718 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
719 }
720
Paul Bakker2fbefde2013-06-29 16:01:15 +0200721 ssl->handshake->max_major_ver = buf[3];
722 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100723
724 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
725 {
726 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
727 return( ret );
728 }
729
730 ssl->handshake->update_checksum( ssl, buf + 2, n );
731
732 buf = ssl->in_msg;
733 n = ssl->in_left - 5;
734
735 /*
736 * 0 . 1 ciphersuitelist length
737 * 2 . 3 session id length
738 * 4 . 5 challenge length
739 * 6 . .. ciphersuitelist
740 * .. . .. session id
741 * .. . .. challenge
742 */
743 SSL_DEBUG_BUF( 4, "record contents", buf, n );
744
745 ciph_len = ( buf[0] << 8 ) | buf[1];
746 sess_len = ( buf[2] << 8 ) | buf[3];
747 chal_len = ( buf[4] << 8 ) | buf[5];
748
749 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
750 ciph_len, sess_len, chal_len ) );
751
752 /*
753 * Make sure each parameter length is valid
754 */
755 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
756 {
757 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
758 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
759 }
760
761 if( sess_len > 32 )
762 {
763 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
764 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
765 }
766
767 if( chal_len < 8 || chal_len > 32 )
768 {
769 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
770 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
771 }
772
773 if( n != 6 + ciph_len + sess_len + chal_len )
774 {
775 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
776 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
777 }
778
779 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
780 buf + 6, ciph_len );
781 SSL_DEBUG_BUF( 3, "client hello, session id",
782 buf + 6 + ciph_len, sess_len );
783 SSL_DEBUG_BUF( 3, "client hello, challenge",
784 buf + 6 + ciph_len + sess_len, chal_len );
785
786 p = buf + 6 + ciph_len;
787 ssl->session_negotiate->length = sess_len;
788 memset( ssl->session_negotiate->id, 0, sizeof( ssl->session_negotiate->id ) );
789 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
790
791 p += sess_len;
792 memset( ssl->handshake->randbytes, 0, 64 );
793 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
794
795 /*
796 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
797 */
798 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
799 {
800 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
801 {
802 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
803 if( ssl->renegotiation == SSL_RENEGOTIATION )
804 {
805 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
806
807 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
808 return( ret );
809
810 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
811 }
812 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
813 break;
814 }
815 }
816
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200817 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
818 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +0100819 {
820 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
821 {
Paul Bakker41c83d32013-03-20 14:39:14 +0100822 // Only allow non-ECC ciphersuites as we do not have extensions
823 //
Paul Bakker59c28a22013-06-29 15:33:42 +0200824 if( p[0] == 0 && p[1] == 0 &&
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200825 ( ( ciphersuites[i] >> 8 ) & 0xFF ) == 0 &&
826 p[2] == ( ciphersuites[i] & 0xFF ) )
Paul Bakker59c28a22013-06-29 15:33:42 +0200827 {
828 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
829
830 if( ciphersuite_info == NULL )
831 {
832 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %02x not found",
833 ciphersuites[i] ) );
834 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
835 }
836
Paul Bakker2fbefde2013-06-29 16:01:15 +0200837 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
838 ciphersuite_info->max_minor_ver < ssl->minor_ver )
839 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +0200840
Paul Bakker78a8c712013-03-06 17:01:52 +0100841 goto have_ciphersuite_v2;
Paul Bakker59c28a22013-06-29 15:33:42 +0200842 }
Paul Bakker78a8c712013-03-06 17:01:52 +0100843 }
844 }
845
846 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
847
848 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
849
850have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200851 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +0200852 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +0100853 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +0100854
855 /*
856 * SSLv2 Client Hello relevant renegotiation security checks
857 */
858 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
859 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
860 {
861 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
862
863 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
864 return( ret );
865
866 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
867 }
868
869 ssl->in_left = 0;
870 ssl->state++;
871
872 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
873
874 return( 0 );
875}
876#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
877
Paul Bakker5121ce52009-01-03 21:22:43 +0000878static int ssl_parse_client_hello( ssl_context *ssl )
879{
Paul Bakker23986e52011-04-24 08:57:21 +0000880 int ret;
881 unsigned int i, j;
882 size_t n;
883 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +0000884 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +0000885 unsigned int ext_len = 0;
886 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000887 int renegotiation_info_seen = 0;
888 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200889 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +0100890 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +0200891#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +0200892 pk_type_t pk_alg;
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +0200893#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000894
895 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
896
Paul Bakker48916f92012-09-16 19:57:18 +0000897 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
898 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000899 {
900 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
901 return( ret );
902 }
903
904 buf = ssl->in_hdr;
905
Paul Bakker78a8c712013-03-06 17:01:52 +0100906#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
907 if( ( buf[0] & 0x80 ) != 0 )
908 return ssl_parse_client_hello_v2( ssl );
909#endif
910
Paul Bakkerec636f32012-09-09 19:17:02 +0000911 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
912
913 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
914 buf[0] ) );
915 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
916 ( buf[3] << 8 ) | buf[4] ) );
917 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
918 buf[1], buf[2] ) );
919
920 /*
921 * SSLv3 Client Hello
922 *
923 * Record layer:
924 * 0 . 0 message type
925 * 1 . 2 protocol version
926 * 3 . 4 message length
927 */
928 if( buf[0] != SSL_MSG_HANDSHAKE ||
929 buf[1] != SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000930 {
Paul Bakkerec636f32012-09-09 19:17:02 +0000931 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
932 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
933 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000934
Paul Bakkerec636f32012-09-09 19:17:02 +0000935 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000936
Manuel Pégourié-Gonnard72882b22013-08-02 13:36:00 +0200937 if( n < 45 || n > 2048 )
Paul Bakkerec636f32012-09-09 19:17:02 +0000938 {
939 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
940 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
941 }
942
Paul Bakker48916f92012-09-16 19:57:18 +0000943 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
944 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +0000945 {
946 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
947 return( ret );
948 }
949
950 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +0000951 if( !ssl->renegotiation )
952 n = ssl->in_left - 5;
953 else
954 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +0000955
Paul Bakker48916f92012-09-16 19:57:18 +0000956 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +0000957
958 /*
959 * SSL layer:
960 * 0 . 0 handshake type
961 * 1 . 3 handshake length
962 * 4 . 5 protocol version
963 * 6 . 9 UNIX time()
964 * 10 . 37 random bytes
965 * 38 . 38 session id length
966 * 39 . 38+x session id
967 * 39+x . 40+x ciphersuitelist length
968 * 41+x . .. ciphersuitelist
969 * .. . .. compression alg.
970 * .. . .. extensions
971 */
972 SSL_DEBUG_BUF( 4, "record contents", buf, n );
973
974 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
975 buf[0] ) );
976 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
977 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
978 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
979 buf[4], buf[5] ) );
980
981 /*
982 * Check the handshake type and protocol version
983 */
984 if( buf[0] != SSL_HS_CLIENT_HELLO ||
985 buf[4] != SSL_MAJOR_VERSION_3 )
986 {
987 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
988 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
989 }
990
991 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200992 ssl->minor_ver = ( buf[5] <= ssl->max_minor_ver )
993 ? buf[5] : ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +0000994
Paul Bakker1d29fb52012-09-28 13:28:45 +0000995 if( ssl->minor_ver < ssl->min_minor_ver )
996 {
997 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
998 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +0000999 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001000
1001 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1002 SSL_ALERT_MSG_PROTOCOL_VERSION );
1003
1004 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1005 }
1006
Paul Bakker2fbefde2013-06-29 16:01:15 +02001007 ssl->handshake->max_major_ver = buf[4];
1008 ssl->handshake->max_minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001009
Paul Bakker48916f92012-09-16 19:57:18 +00001010 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001011
1012 /*
1013 * Check the handshake message length
1014 */
1015 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1016 {
1017 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1018 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1019 }
1020
1021 /*
1022 * Check the session length
1023 */
1024 sess_len = buf[38];
1025
1026 if( sess_len > 32 )
1027 {
1028 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1029 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1030 }
1031
Paul Bakker48916f92012-09-16 19:57:18 +00001032 ssl->session_negotiate->length = sess_len;
1033 memset( ssl->session_negotiate->id, 0,
1034 sizeof( ssl->session_negotiate->id ) );
1035 memcpy( ssl->session_negotiate->id, buf + 39,
1036 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001037
1038 /*
1039 * Check the ciphersuitelist length
1040 */
1041 ciph_len = ( buf[39 + sess_len] << 8 )
1042 | ( buf[40 + sess_len] );
1043
1044 if( ciph_len < 2 || ciph_len > 256 || ( ciph_len % 2 ) != 0 )
1045 {
1046 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1047 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1048 }
1049
1050 /*
1051 * Check the compression algorithms length
1052 */
1053 comp_len = buf[41 + sess_len + ciph_len];
1054
1055 if( comp_len < 1 || comp_len > 16 )
1056 {
1057 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1058 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1059 }
1060
Paul Bakker48916f92012-09-16 19:57:18 +00001061 /*
1062 * Check the extension length
1063 */
1064 if( n > 42 + sess_len + ciph_len + comp_len )
1065 {
1066 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1067 | ( buf[43 + sess_len + ciph_len + comp_len] );
1068
1069 if( ( ext_len > 0 && ext_len < 4 ) ||
1070 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1071 {
1072 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1073 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1074 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1075 }
1076 }
1077
1078 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001079#if defined(POLARSSL_ZLIB_SUPPORT)
1080 for( i = 0; i < comp_len; ++i )
1081 {
Paul Bakker48916f92012-09-16 19:57:18 +00001082 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001083 {
Paul Bakker48916f92012-09-16 19:57:18 +00001084 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001085 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001086 }
1087 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001088#endif
1089
Paul Bakkerec636f32012-09-09 19:17:02 +00001090 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1091 buf + 6, 32 );
1092 SSL_DEBUG_BUF( 3, "client hello, session id",
1093 buf + 38, sess_len );
1094 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1095 buf + 41 + sess_len, ciph_len );
1096 SSL_DEBUG_BUF( 3, "client hello, compression",
1097 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001098
Paul Bakkerec636f32012-09-09 19:17:02 +00001099 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001100 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1101 */
1102 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1103 {
1104 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1105 {
1106 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1107 if( ssl->renegotiation == SSL_RENEGOTIATION )
1108 {
1109 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001110
1111 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1112 return( ret );
1113
Paul Bakker48916f92012-09-16 19:57:18 +00001114 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1115 }
1116 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1117 break;
1118 }
1119 }
1120
Paul Bakker48916f92012-09-16 19:57:18 +00001121 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001122
1123 while( ext_len )
1124 {
1125 unsigned int ext_id = ( ( ext[0] << 8 )
1126 | ( ext[1] ) );
1127 unsigned int ext_size = ( ( ext[2] << 8 )
1128 | ( ext[3] ) );
1129
1130 if( ext_size + 4 > ext_len )
1131 {
1132 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1133 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1134 }
1135 switch( ext_id )
1136 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001137#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001138 case TLS_EXT_SERVERNAME:
1139 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1140 if( ssl->f_sni == NULL )
1141 break;
1142
1143 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1144 if( ret != 0 )
1145 return( ret );
1146 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001147#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001148
Paul Bakker48916f92012-09-16 19:57:18 +00001149 case TLS_EXT_RENEGOTIATION_INFO:
1150 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1151 renegotiation_info_seen = 1;
1152
Paul Bakker23f36802012-09-28 14:15:14 +00001153 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1154 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001155 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001156 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001157
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001158#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001159 case TLS_EXT_SIG_ALG:
1160 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1161 if( ssl->renegotiation == SSL_RENEGOTIATION )
1162 break;
1163
1164 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1165 if( ret != 0 )
1166 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001167 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001168#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001169
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001170#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001171 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1172 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1173
1174 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1175 if( ret != 0 )
1176 return( ret );
1177 break;
1178
1179 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1180 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1181
1182 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1183 if( ret != 0 )
1184 return( ret );
1185 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001186#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001187
Paul Bakker05decb22013-08-15 13:33:48 +02001188#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001189 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1190 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1191
1192 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1193 if( ret != 0 )
1194 return( ret );
1195 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001196#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001197
Paul Bakker1f2bc622013-08-15 13:45:55 +02001198#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001199 case TLS_EXT_TRUNCATED_HMAC:
1200 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1201
1202 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1203 if( ret != 0 )
1204 return( ret );
1205 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001206#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001207
Paul Bakkera503a632013-08-14 13:48:06 +02001208#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001209 case TLS_EXT_SESSION_TICKET:
1210 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1211
1212 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1213 if( ret != 0 )
1214 return( ret );
1215 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001216#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001217
Paul Bakker48916f92012-09-16 19:57:18 +00001218 default:
1219 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1220 ext_id ) );
1221 }
1222
1223 ext_len -= 4 + ext_size;
1224 ext += 4 + ext_size;
1225
1226 if( ext_len > 0 && ext_len < 4 )
1227 {
1228 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1229 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1230 }
1231 }
1232
1233 /*
1234 * Renegotiation security checks
1235 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001236 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1237 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1238 {
1239 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1240 handshake_failure = 1;
1241 }
1242 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1243 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1244 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001245 {
1246 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001247 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001248 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001249 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1250 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1251 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001252 {
1253 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001254 handshake_failure = 1;
1255 }
1256 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1257 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1258 renegotiation_info_seen == 1 )
1259 {
1260 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1261 handshake_failure = 1;
1262 }
1263
1264 if( handshake_failure == 1 )
1265 {
1266 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1267 return( ret );
1268
Paul Bakker48916f92012-09-16 19:57:18 +00001269 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1270 }
Paul Bakker380da532012-04-18 16:10:25 +00001271
Paul Bakker41c83d32013-03-20 14:39:14 +01001272 /*
1273 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001274 * (At the end because we need information from the EC-based extensions
1275 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001276 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001277 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
1278 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001279 {
1280 for( j = 0, p = buf + 41 + sess_len; j < ciph_len;
1281 j += 2, p += 2 )
1282 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001283 if( p[0] == ( ( ciphersuites[i] >> 8 ) & 0xFF ) &&
1284 p[1] == ( ( ciphersuites[i] ) & 0xFF ) )
Paul Bakker41c83d32013-03-20 14:39:14 +01001285 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001286 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
Paul Bakker41c83d32013-03-20 14:39:14 +01001287
1288 if( ciphersuite_info == NULL )
1289 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001290 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found",
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001291 ciphersuites[i] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001292 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1293 }
1294
Paul Bakker2fbefde2013-06-29 16:01:15 +02001295 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
1296 ciphersuite_info->max_minor_ver < ssl->minor_ver )
1297 continue;
1298
Paul Bakker5fd49172013-08-19 13:29:26 +02001299#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001300 if( ssl_ciphersuite_uses_ec( ciphersuite_info ) &&
Paul Bakker41c83d32013-03-20 14:39:14 +01001301 ssl->handshake->ec_curve == 0 )
1302 continue;
Paul Bakker5fd49172013-08-19 13:29:26 +02001303#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001304
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001305#if defined(POLARSSL_X509_CRT_PARSE_C)
1306 /*
1307 * Final check: if ciphersuite requires us to have a
1308 * certificate/key of a particular type:
1309 * - select the appropriate certificate if we have one, or
1310 * - try the next ciphersuite if we don't
1311 * This must be done last since we modify the key_cert list.
1312 */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001313 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001314 if( pk_alg != POLARSSL_PK_NONE )
1315 {
1316 ssl_key_cert *good = NULL;
1317 ssl_key_cert *cur = ssl->key_cert;
1318
1319 while( cur != NULL && good == NULL )
1320 {
1321 if( pk_can_do( cur->key, pk_alg ) )
1322 good = cur;
1323 cur = cur->next;
1324 }
1325
1326 if( good == NULL )
1327 continue;
1328 else
1329 ssl->handshake->key_cert = good;
1330 }
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02001331#endif
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001332
Paul Bakker41c83d32013-03-20 14:39:14 +01001333 goto have_ciphersuite;
1334 }
1335 }
1336 }
1337
1338 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1339
1340 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1341 return( ret );
1342
1343 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1344
1345have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001346 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001347 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1348 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1349
Paul Bakker5121ce52009-01-03 21:22:43 +00001350 ssl->in_left = 0;
1351 ssl->state++;
1352
1353 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1354
1355 return( 0 );
1356}
1357
Paul Bakker1f2bc622013-08-15 13:45:55 +02001358#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001359static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1360 unsigned char *buf,
1361 size_t *olen )
1362{
1363 unsigned char *p = buf;
1364
1365 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1366 {
1367 *olen = 0;
1368 return;
1369 }
1370
1371 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1372
1373 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1374 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1375
1376 *p++ = 0x00;
1377 *p++ = 0x00;
1378
1379 *olen = 4;
1380}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001381#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001382
Paul Bakkera503a632013-08-14 13:48:06 +02001383#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001384static void ssl_write_session_ticket_ext( ssl_context *ssl,
1385 unsigned char *buf,
1386 size_t *olen )
1387{
1388 unsigned char *p = buf;
1389
1390 if( ssl->handshake->new_session_ticket == 0 )
1391 {
1392 *olen = 0;
1393 return;
1394 }
1395
1396 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1397
1398 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1399 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1400
1401 *p++ = 0x00;
1402 *p++ = 0x00;
1403
1404 *olen = 4;
1405}
Paul Bakkera503a632013-08-14 13:48:06 +02001406#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001407
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001408static void ssl_write_renegotiation_ext( ssl_context *ssl,
1409 unsigned char *buf,
1410 size_t *olen )
1411{
1412 unsigned char *p = buf;
1413
1414 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1415 {
1416 *olen = 0;
1417 return;
1418 }
1419
1420 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1421
1422 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1423 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1424
1425 *p++ = 0x00;
1426 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1427 *p++ = ssl->verify_data_len * 2 & 0xFF;
1428
1429 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1430 p += ssl->verify_data_len;
1431 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1432 p += ssl->verify_data_len;
1433
1434 *olen = 5 + ssl->verify_data_len * 2;
1435}
1436
Paul Bakker05decb22013-08-15 13:33:48 +02001437#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001438static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1439 unsigned char *buf,
1440 size_t *olen )
1441{
1442 unsigned char *p = buf;
1443
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001444 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1445 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001446 *olen = 0;
1447 return;
1448 }
1449
1450 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1451
1452 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1453 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1454
1455 *p++ = 0x00;
1456 *p++ = 1;
1457
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001458 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001459
1460 *olen = 5;
1461}
Paul Bakker05decb22013-08-15 13:33:48 +02001462#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001463
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001464#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001465static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1466 unsigned char *buf,
1467 size_t *olen )
1468{
1469 unsigned char *p = buf;
1470 ((void) ssl);
1471
1472 *olen = 0;
1473
1474 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1475
1476 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1477 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1478
1479 *p++ = 0x00;
1480 *p++ = 2;
1481
1482 *p++ = 1;
1483 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1484
1485 *olen = 6;
1486}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001487#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001488
Paul Bakker5121ce52009-01-03 21:22:43 +00001489static int ssl_write_server_hello( ssl_context *ssl )
1490{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001491#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001492 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001493#endif
Paul Bakkera3d195c2011-11-27 21:07:34 +00001494 int ret, n;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001495 size_t olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001496 unsigned char *buf, *p;
1497
1498 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1499
1500 /*
1501 * 0 . 0 handshake type
1502 * 1 . 3 handshake length
1503 * 4 . 5 protocol version
1504 * 6 . 9 UNIX time()
1505 * 10 . 37 random bytes
1506 */
1507 buf = ssl->out_msg;
1508 p = buf + 4;
1509
1510 *p++ = (unsigned char) ssl->major_ver;
1511 *p++ = (unsigned char) ssl->minor_ver;
1512
1513 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1514 buf[4], buf[5] ) );
1515
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001516#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001517 t = time( NULL );
1518 *p++ = (unsigned char)( t >> 24 );
1519 *p++ = (unsigned char)( t >> 16 );
1520 *p++ = (unsigned char)( t >> 8 );
1521 *p++ = (unsigned char)( t );
1522
1523 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001524#else
1525 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1526 return( ret );
1527
1528 p += 4;
1529#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001530
Paul Bakkera3d195c2011-11-27 21:07:34 +00001531 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1532 return( ret );
1533
1534 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001535
Paul Bakker48916f92012-09-16 19:57:18 +00001536 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001537
1538 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1539
1540 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001541 * Resume is 0 by default, see ssl_handshake_init().
1542 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1543 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001544 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001545 if( ssl->handshake->resume == 0 &&
1546 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001547 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001548 ssl->f_get_cache != NULL &&
1549 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1550 {
1551 ssl->handshake->resume = 1;
1552 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001553
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001554 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001555 {
1556 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001557 * New session, create a new session id,
1558 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001559 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001560 ssl->state++;
1561
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001562#if defined(POLARSSL_HAVE_TIME)
1563 ssl->session_negotiate->start = time( NULL );
1564#endif
1565
Paul Bakkera503a632013-08-14 13:48:06 +02001566#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001567 if( ssl->handshake->new_session_ticket != 0 )
1568 {
1569 ssl->session_negotiate->length = n = 0;
1570 memset( ssl->session_negotiate->id, 0, 32 );
1571 }
1572 else
1573#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001574 {
1575 ssl->session_negotiate->length = n = 32;
1576 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001577 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001578 return( ret );
1579 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001580 }
1581 else
1582 {
1583 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001584 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001585 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001586 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001587 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001588
1589 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1590 {
1591 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1592 return( ret );
1593 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001594 }
1595
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001596 /*
1597 * 38 . 38 session id length
1598 * 39 . 38+n session id
1599 * 39+n . 40+n chosen ciphersuite
1600 * 41+n . 41+n chosen compression alg.
1601 * 42+n . 43+n extensions length
1602 * 44+n . 43+n+m extensions
1603 */
1604 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001605 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1606 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001607
1608 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1609 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1610 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001611 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001612
Paul Bakker48916f92012-09-16 19:57:18 +00001613 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1614 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1615 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001616
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001617 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
1618 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001619 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001620 ssl->session_negotiate->compression ) );
1621
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001622 /*
1623 * First write extensions, then the total length
1624 */
1625 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1626 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001627
Paul Bakker05decb22013-08-15 13:33:48 +02001628#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001629 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1630 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001631#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001632
Paul Bakker1f2bc622013-08-15 13:45:55 +02001633#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001634 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1635 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001636#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001637
Paul Bakkera503a632013-08-14 13:48:06 +02001638#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001639 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1640 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001641#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001642
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001643#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001644 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1645 ext_len += olen;
1646#endif
1647
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001648 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001649
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001650 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1651 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1652 p += ext_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001653
1654 ssl->out_msglen = p - buf;
1655 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1656 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1657
1658 ret = ssl_write_record( ssl );
1659
1660 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1661
1662 return( ret );
1663}
1664
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001665#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1666 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001667 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1668 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001669static int ssl_write_certificate_request( ssl_context *ssl )
1670{
Paul Bakkered27a042013-04-18 22:46:23 +02001671 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1672 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001673
1674 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1675
1676 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1677 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1678 {
1679 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1680 ssl->state++;
1681 return( 0 );
1682 }
1683
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001684 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001685 return( ret );
1686}
1687#else
1688static int ssl_write_certificate_request( ssl_context *ssl )
1689{
1690 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1691 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001692 size_t dn_size, total_dn_size; /* excluding length bytes */
1693 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00001694 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001695 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001696
1697 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1698
1699 ssl->state++;
1700
Paul Bakkerfbb17802013-04-17 19:10:21 +02001701 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001702 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001703 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001704 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001705 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001706 return( 0 );
1707 }
1708
1709 /*
1710 * 0 . 0 handshake type
1711 * 1 . 3 handshake length
1712 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01001713 * 5 .. m-1 cert types
1714 * m .. m+1 sig alg length (TLS 1.2 only)
1715 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001716 * n .. n+1 length of all DNs
1717 * n+2 .. n+3 length of DN 1
1718 * n+4 .. ... Distinguished Name #1
1719 * ... .. ... length of DN 2, etc.
1720 */
1721 buf = ssl->out_msg;
1722 p = buf + 4;
1723
1724 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001725 * Supported certificate types
1726 *
1727 * ClientCertificateType certificate_types<1..2^8-1>;
1728 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00001729 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001730 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01001731
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001732#if defined(POLARSSL_RSA_C)
1733 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
1734#endif
1735#if defined(POLARSSL_ECDSA_C)
1736 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
1737#endif
1738
1739 p[0] = ct_len++;
1740 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01001741
Paul Bakker577e0062013-08-28 11:57:20 +02001742 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001743#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01001744 /*
1745 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01001746 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001747 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
1748 *
1749 * struct {
1750 * HashAlgorithm hash;
1751 * SignatureAlgorithm signature;
1752 * } SignatureAndHashAlgorithm;
1753 *
1754 * enum { (255) } HashAlgorithm;
1755 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01001756 */
Paul Bakker21dca692013-01-03 11:41:08 +01001757 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01001758 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001759 /*
1760 * Only use current running hash algorithm that is already required
1761 * for requested ciphersuite.
1762 */
Paul Bakker926af752012-11-23 13:38:07 +01001763 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
1764
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001765 if( ssl->transform_negotiate->ciphersuite_info->mac ==
1766 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01001767 {
1768 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
1769 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02001770
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001771 /*
1772 * Supported signature algorithms
1773 */
1774#if defined(POLARSSL_RSA_C)
1775 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1776 p[2 + sa_len++] = SSL_SIG_RSA;
1777#endif
1778#if defined(POLARSSL_ECDSA_C)
1779 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1780 p[2 + sa_len++] = SSL_SIG_ECDSA;
1781#endif
Paul Bakker926af752012-11-23 13:38:07 +01001782
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001783 p[0] = (unsigned char)( sa_len >> 8 );
1784 p[1] = (unsigned char)( sa_len );
1785 sa_len += 2;
1786 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01001787 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001788#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001789
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001790 /*
1791 * DistinguishedName certificate_authorities<0..2^16-1>;
1792 * opaque DistinguishedName<1..2^16-1>;
1793 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001794 p += 2;
1795 crt = ssl->ca_chain;
1796
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001797 total_dn_size = 0;
Paul Bakker29087132010-03-21 21:03:34 +00001798 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001799 {
1800 if( p - buf > 4096 )
1801 break;
1802
Paul Bakker926af752012-11-23 13:38:07 +01001803 dn_size = crt->subject_raw.len;
1804 *p++ = (unsigned char)( dn_size >> 8 );
1805 *p++ = (unsigned char)( dn_size );
1806 memcpy( p, crt->subject_raw.p, dn_size );
1807 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001808
Paul Bakker926af752012-11-23 13:38:07 +01001809 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
1810
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001811 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01001812 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00001813 }
1814
Paul Bakker926af752012-11-23 13:38:07 +01001815 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001816 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1817 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001818 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
1819 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001820
1821 ret = ssl_write_record( ssl );
1822
1823 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
1824
1825 return( ret );
1826}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001827#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
1828 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
1829 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001830
Paul Bakker41c83d32013-03-20 14:39:14 +01001831static int ssl_write_server_key_exchange( ssl_context *ssl )
1832{
Paul Bakker23986e52011-04-24 08:57:21 +00001833 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001834 size_t n = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001835 const ssl_ciphersuite_t *ciphersuite_info;
1836
1837#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1838 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
1839 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1840 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001841 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001842 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001843 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001844 ((void) dig_signed);
1845 ((void) dig_signed_len);
1846#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001847
Paul Bakker41c83d32013-03-20 14:39:14 +01001848 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001849
1850 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
1851
Paul Bakker41c83d32013-03-20 14:39:14 +01001852 if( ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_RSA &&
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001853 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_ECDHE_RSA &&
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001854 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA &&
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001855 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001856 {
1857 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
1858 ssl->state++;
1859 return( 0 );
1860 }
1861
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001862#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1863 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1864 {
1865 /* TODO: Support identity hints */
1866 *(p++) = 0x00;
1867 *(p++) = 0x00;
1868
1869 n += 2;
1870 }
1871#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1872
1873#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1874 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1875 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1876 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00001877 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001878 /*
1879 * Ephemeral DH parameters:
1880 *
1881 * struct {
1882 * opaque dh_p<1..2^16-1>;
1883 * opaque dh_g<1..2^16-1>;
1884 * opaque dh_Ys<1..2^16-1>;
1885 * } ServerDHParams;
1886 */
1887 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
1888 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
1889 {
1890 SSL_DEBUG_RET( 1, "mpi_copy", ret );
1891 return( ret );
1892 }
Paul Bakker48916f92012-09-16 19:57:18 +00001893
Paul Bakker41c83d32013-03-20 14:39:14 +01001894 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
1895 mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001896 p,
1897 &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001898 {
1899 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
1900 return( ret );
1901 }
1902
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001903 dig_signed = p;
1904 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001905
1906 p += len;
1907 n += len;
1908
Paul Bakker41c83d32013-03-20 14:39:14 +01001909 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
1910 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1911 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1912 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
1913 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001914#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1915 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001916
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001917#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1918 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1919 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1920 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001921 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001922 /*
1923 * Ephemeral ECDH parameters:
1924 *
1925 * struct {
1926 * ECParameters curve_params;
1927 * ECPoint public;
1928 * } ServerECDHParams;
1929 */
Paul Bakker41c83d32013-03-20 14:39:14 +01001930 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
1931 ssl->handshake->ec_curve ) ) != 0 )
1932 {
1933 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
1934 return( ret );
1935 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001936
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001937 SSL_DEBUG_MSG( 2, ( "ECDH curve size: %d",
1938 (int) ssl->handshake->ecdh_ctx.grp.nbits ) );
1939
Paul Bakker41c83d32013-03-20 14:39:14 +01001940 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001941 &len,
1942 p,
Paul Bakker41c83d32013-03-20 14:39:14 +01001943 1000, ssl->f_rng, ssl->p_rng ) ) != 0 )
1944 {
1945 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
1946 return( ret );
1947 }
1948
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001949 dig_signed = p;
1950 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001951
1952 p += len;
1953 n += len;
1954
Paul Bakker41c83d32013-03-20 14:39:14 +01001955 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
1956 }
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001957#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1958 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001959
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001960#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001961 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1962 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001963 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001964 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1965 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001966 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001967 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001968 unsigned int hashlen = 0;
1969 unsigned char hash[64];
1970 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00001971
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001972 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001973 * Choose hash algorithm. NONE means MD5 + SHA1 here.
1974 */
Paul Bakker577e0062013-08-28 11:57:20 +02001975#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001976 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
1977 {
1978 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
1979
1980 if( md_alg == POLARSSL_MD_NONE )
1981 {
1982 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1983 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1984 }
1985 }
Paul Bakker577e0062013-08-28 11:57:20 +02001986 else
1987#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001988#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1989 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker577e0062013-08-28 11:57:20 +02001990 if ( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001991 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
1992 {
1993 md_alg = POLARSSL_MD_SHA1;
1994 }
1995 else
Paul Bakker577e0062013-08-28 11:57:20 +02001996#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001997 {
1998 md_alg = POLARSSL_MD_NONE;
1999 }
2000
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002001 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002002 * Compute the hash to be signed
2003 */
Paul Bakker577e0062013-08-28 11:57:20 +02002004#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2005 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002006 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002007 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002008 md5_context md5;
2009 sha1_context sha1;
2010
2011 /*
2012 * digitally-signed struct {
2013 * opaque md5_hash[16];
2014 * opaque sha_hash[20];
2015 * };
2016 *
2017 * md5_hash
2018 * MD5(ClientHello.random + ServerHello.random
2019 * + ServerParams);
2020 * sha_hash
2021 * SHA(ClientHello.random + ServerHello.random
2022 * + ServerParams);
2023 */
2024 md5_starts( &md5 );
2025 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002026 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002027 md5_finish( &md5, hash );
2028
2029 sha1_starts( &sha1 );
2030 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002031 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002032 sha1_finish( &sha1, hash + 16 );
2033
2034 hashlen = 36;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002035 }
2036 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002037#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2038 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002039#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2040 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002041 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002042 {
2043 md_context_t ctx;
2044
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002045 /* Info from md_alg will be used instead */
2046 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002047
2048 /*
2049 * digitally-signed struct {
2050 * opaque client_random[32];
2051 * opaque server_random[32];
2052 * ServerDHParams params;
2053 * };
2054 */
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002055 if( ( ret = md_init_ctx( &ctx, md_info_from_type(md_alg) ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002056 {
2057 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2058 return( ret );
2059 }
2060
2061 md_starts( &ctx );
2062 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002063 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002064 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02002065
2066 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
2067 {
2068 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
2069 return( ret );
2070 }
2071
Paul Bakker23f36802012-09-28 14:15:14 +00002072 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002073 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002074#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2075 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002076 {
2077 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002078 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02002079 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002080
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002081 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2082 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002083
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002084 /*
2085 * Make the signature
2086 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002087 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002088 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002089 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2090 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002091 }
Paul Bakker23f36802012-09-28 14:15:14 +00002092
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002093#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002094 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2095 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002096 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002097 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002098
2099 n += 2;
2100 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002101#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002102
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002103 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002104 p + 2 , &signature_len,
2105 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002106 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002107 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002108 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002109 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002110
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002111 *(p++) = (unsigned char)( signature_len >> 8 );
2112 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002113 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002114
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002115 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002116
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002117 p += signature_len;
2118 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002119 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002120#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002121 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2122 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002123
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002124 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2126 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2127
2128 ssl->state++;
2129
2130 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2131 {
2132 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2133 return( ret );
2134 }
2135
2136 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2137
2138 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002139}
2140
2141static int ssl_write_server_hello_done( ssl_context *ssl )
2142{
2143 int ret;
2144
2145 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2146
2147 ssl->out_msglen = 4;
2148 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2149 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2150
2151 ssl->state++;
2152
2153 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2154 {
2155 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2156 return( ret );
2157 }
2158
2159 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2160
2161 return( 0 );
2162}
2163
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002164#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2165 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2166static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2167 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002168{
2169 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002170 size_t n;
2171
2172 /*
2173 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2174 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002175 if( *p + 2 > end )
2176 {
2177 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2178 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2179 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002180
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002181 n = ( (*p)[0] << 8 ) | (*p)[1];
2182 *p += 2;
2183
2184 if( n < 1 || n > ssl->handshake->dhm_ctx.len || *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002185 {
2186 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2187 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2188 }
2189
2190 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002191 *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002192 {
2193 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2194 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2195 }
2196
2197 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2198
Paul Bakker70df2fb2013-04-17 17:19:09 +02002199 return( ret );
2200}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002201#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2202 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002203
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02002204#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2205 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002206static int ssl_parse_client_ecdh_public( ssl_context *ssl )
2207{
2208 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002209 size_t n;
2210
2211 /*
2212 * Receive client public key and calculate premaster
2213 */
2214 n = ssl->in_msg[3];
2215
2216 if( n < 1 || n > mpi_size( &ssl->handshake->ecdh_ctx.grp.P ) * 2 + 2 ||
2217 n + 4 != ssl->in_hslen )
2218 {
2219 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2220 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2221 }
2222
2223 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2224 ssl->in_msg + 4, n ) ) != 0 )
2225 {
2226 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2227 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2228 }
2229
2230 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2231
Paul Bakker70df2fb2013-04-17 17:19:09 +02002232 return( ret );
2233}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02002234#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2235 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002236
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002237#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002238static int ssl_parse_encrypted_pms_secret( ssl_context *ssl )
2239{
2240 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2241 size_t i, n = 0;
2242
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002243 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002244 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002245 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002246 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2247 }
2248
2249 /*
2250 * Decrypt the premaster using own private RSA key
2251 */
2252 i = 4;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002253 n = pk_get_len( ssl_own_key( ssl ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002254 ssl->handshake->pmslen = 48;
2255
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002256#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2257 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002258 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2259 {
2260 i += 2;
2261 if( ssl->in_msg[4] != ( ( n >> 8 ) & 0xFF ) ||
2262 ssl->in_msg[5] != ( ( n ) & 0xFF ) )
2263 {
2264 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2265 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2266 }
2267 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002268#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002269
2270 if( ssl->in_hslen != i + n )
2271 {
2272 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2273 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2274 }
2275
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002276 ret = pk_decrypt( ssl_own_key( ssl ),
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002277 ssl->in_msg + i, n,
2278 ssl->handshake->premaster, &ssl->handshake->pmslen,
2279 sizeof(ssl->handshake->premaster),
2280 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002281
2282 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Paul Bakker2fbefde2013-06-29 16:01:15 +02002283 ssl->handshake->premaster[0] != ssl->handshake->max_major_ver ||
2284 ssl->handshake->premaster[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002285 {
2286 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2287
2288 /*
2289 * Protection against Bleichenbacher's attack:
2290 * invalid PKCS#1 v1.5 padding must not cause
2291 * the connection to end immediately; instead,
2292 * send a bad_record_mac later in the handshake.
2293 */
2294 ssl->handshake->pmslen = 48;
2295
2296 ret = ssl->f_rng( ssl->p_rng, ssl->handshake->premaster,
2297 ssl->handshake->pmslen );
2298 if( ret != 0 )
2299 return( ret );
2300 }
2301
2302 return( ret );
2303}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002304#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002305
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002306#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2307 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2308static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2309 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002310{
Paul Bakker6db455e2013-09-18 17:29:31 +02002311 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002312 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002313
Paul Bakker6db455e2013-09-18 17:29:31 +02002314 if( ssl->f_psk == NULL &&
2315 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2316 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002317 {
2318 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2319 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2320 }
2321
2322 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002323 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002324 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002325 if( *p + 2 > end )
2326 {
2327 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2328 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2329 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002330
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002331 n = ( (*p)[0] << 8 ) | (*p)[1];
2332 *p += 2;
2333
2334 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002335 {
2336 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2337 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2338 }
2339
Paul Bakker6db455e2013-09-18 17:29:31 +02002340 if( ssl->f_psk != NULL )
2341 {
2342 if( ( ret != ssl->f_psk( ssl->p_psk, ssl, *p, n ) ) != 0 )
2343 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2344 }
2345
2346 if( ret == 0 )
2347 {
2348 if( n != ssl->psk_identity_len ||
2349 memcmp( ssl->psk_identity, *p, n ) != 0 )
2350 {
2351 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2352 }
2353 }
2354
2355 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002356 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002357 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002358 if( ( ret = ssl_send_alert_message( ssl,
2359 SSL_ALERT_LEVEL_FATAL,
2360 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2361 {
2362 return( ret );
2363 }
2364
2365 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002366 }
2367
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002368 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002369 ret = 0;
2370
Paul Bakkerfbb17802013-04-17 19:10:21 +02002371 return( ret );
2372}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002373#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
2374 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002375
Paul Bakker5121ce52009-01-03 21:22:43 +00002376static int ssl_parse_client_key_exchange( ssl_context *ssl )
2377{
Paul Bakker23986e52011-04-24 08:57:21 +00002378 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002379 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002380
Paul Bakker41c83d32013-03-20 14:39:14 +01002381 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002382
2383 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2384
2385 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2386 {
2387 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2388 return( ret );
2389 }
2390
2391 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2392 {
2393 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002394 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002395 }
2396
2397 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2398 {
2399 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002400 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002401 }
2402
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002403#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002404 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002405 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002406 unsigned char *p = ssl->in_msg + 4;
2407 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2408
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002409 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002410 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002411 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2412 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002413 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002414
2415 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
2416
Manuel Pégourié-Gonnard032c34e2013-09-07 13:06:27 +02002417 /* No blinding needed for DHE, but will be needed for fixed DH! */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002418 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2419 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002420 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002421 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002422 {
2423 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2424 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2425 }
2426
2427 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002428 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002429 else
2430#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002431#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2432 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2433 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2434 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002435 {
2436 if( ( ret = ssl_parse_client_ecdh_public( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002437 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002438 SSL_DEBUG_RET( 1, ( "ssl_parse_client_ecdh_public" ), ret );
2439 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002440 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002441
2442 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2443 &ssl->handshake->pmslen,
2444 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002445 POLARSSL_MPI_MAX_SIZE,
2446 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002447 {
2448 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2449 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2450 }
2451
2452 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002453 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002454 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002455#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2456 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002457#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2458 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002459 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002460 unsigned char *p = ssl->in_msg + 4;
2461 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2462
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002463 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002464 {
2465 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2466 return( ret );
2467 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002468
2469 // Set up the premaster secret
2470 //
2471 p = ssl->handshake->premaster;
2472 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2473 *(p++) = (unsigned char)( ssl->psk_len );
2474 p += ssl->psk_len;
2475
2476 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2477 *(p++) = (unsigned char)( ssl->psk_len );
2478 memcpy( p, ssl->psk, ssl->psk_len );
2479 p += ssl->psk_len;
2480
2481 ssl->handshake->pmslen = 4 + 2 * ssl->psk_len;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002482 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002483 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002484#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
2485#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2486 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2487 {
2488 size_t n;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002489 unsigned char *p = ssl->in_msg + 4;
2490 unsigned char *end = ssl->in_msg + ssl->in_msglen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002491
2492 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2493 {
2494 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2495 return( ret );
2496 }
2497 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2498 {
2499 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2500 return( ret );
2501 }
2502
2503 // Set up the premaster secret
2504 //
2505 p = ssl->handshake->premaster;
2506 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len >> 8 );
2507 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len );
2508
Paul Bakker577e0062013-08-28 11:57:20 +02002509 n = ssl->handshake->dhm_ctx.len;
2510
Manuel Pégourié-Gonnard032c34e2013-09-07 13:06:27 +02002511 /* No blinding needed since this is ephemeral DHM */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002512 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002513 p, &n, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002514 {
2515 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2516 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2517 }
2518
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002519 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2520
2521 p += ssl->handshake->dhm_ctx.len;
2522
2523 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2524 *(p++) = (unsigned char)( ssl->psk_len );
2525 memcpy( p, ssl->psk, ssl->psk_len );
2526 p += ssl->psk_len;
2527
2528 ssl->handshake->pmslen = 4 + ssl->handshake->dhm_ctx.len + ssl->psk_len;
2529 }
2530 else
2531#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
2532#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2533 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002534 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002535 if( ( ret = ssl_parse_encrypted_pms_secret( ssl ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002536 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002537 SSL_DEBUG_RET( 1, ( "ssl_parse_client_ecdh_public" ), ret );
2538 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002539 }
2540 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002541 else
2542#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2543 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002544 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002545 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2546 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002547
Paul Bakkerff60ee62010-03-16 21:09:09 +00002548 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2549 {
2550 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2551 return( ret );
2552 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002553
Paul Bakker5121ce52009-01-03 21:22:43 +00002554 ssl->state++;
2555
2556 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2557
2558 return( 0 );
2559}
2560
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002561#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2562 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002563 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2564 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002565static int ssl_parse_certificate_verify( ssl_context *ssl )
2566{
Paul Bakkered27a042013-04-18 22:46:23 +02002567 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002568 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002569
2570 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2571
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002572 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2573 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002574 {
2575 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2576 ssl->state++;
2577 return( 0 );
2578 }
2579
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002580 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002581 return( ret );
2582}
2583#else
2584static int ssl_parse_certificate_verify( ssl_context *ssl )
2585{
2586 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002587 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002588 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002589 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002590 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02002591#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002592 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02002593#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002594 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002595 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2596
2597 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2598
2599 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2600 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2601 {
2602 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2603 ssl->state++;
2604 return( 0 );
2605 }
2606
Paul Bakkered27a042013-04-18 22:46:23 +02002607 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002608 {
2609 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2610 ssl->state++;
2611 return( 0 );
2612 }
2613
Paul Bakker48916f92012-09-16 19:57:18 +00002614 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002615
2616 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2617 {
2618 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2619 return( ret );
2620 }
2621
2622 ssl->state++;
2623
2624 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2625 {
2626 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002627 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002628 }
2629
2630 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
2631 {
2632 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002633 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002634 }
2635
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002636 /*
2637 * 0 . 0 handshake type
2638 * 1 . 3 handshake length
2639 * 4 . 5 sig alg (TLS 1.2 only)
2640 * 4+n . 5+n signature length (n = sa_len)
2641 * 6+n . 6+n+m signature (m = sig_len)
2642 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002643
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002644#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2645 defined(POLARSSL_SSL_PROTO_TLS1_1)
2646 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002647 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002648 sa_len = 0;
2649
Paul Bakkerc70b9822013-04-07 22:00:46 +02002650 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002651 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002652
2653 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
2654 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
2655 POLARSSL_PK_ECDSA ) )
2656 {
2657 hash_start += 16;
2658 hashlen -= 16;
2659 md_alg = POLARSSL_MD_SHA1;
2660 }
Paul Bakker926af752012-11-23 13:38:07 +01002661 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002662 else
2663#endif
Paul Bakker577e0062013-08-28 11:57:20 +02002664#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2665 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002666 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002667 sa_len = 2;
2668
Paul Bakker5121ce52009-01-03 21:22:43 +00002669 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002670 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00002671 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002672 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002673 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002674 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2675 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01002676 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2677 }
2678
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002679 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01002680
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002681 /* Info from md_alg will be used instead */
2682 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002683
2684 /*
2685 * Signature
2686 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002687 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
2688 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002689 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002690 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2691 " for verify message" ) );
2692 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002693 }
2694
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002695 /*
2696 * Check the certificate's key type matches the signature alg
2697 */
2698 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
2699 {
2700 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
2701 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2702 }
Paul Bakker577e0062013-08-28 11:57:20 +02002703 }
2704 else
2705#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2706 {
2707 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002708 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002709 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002710
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002711 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01002712
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002713 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002714 {
2715 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002716 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002717 }
2718
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002719 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002720 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002721 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002722 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002723 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002724 return( ret );
2725 }
2726
2727 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
2728
Paul Bakkered27a042013-04-18 22:46:23 +02002729 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002730}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002731#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2732 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2733 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002734
Paul Bakkera503a632013-08-14 13:48:06 +02002735#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002736static int ssl_write_new_session_ticket( ssl_context *ssl )
2737{
2738 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002739 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002740 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002741
2742 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
2743
2744 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2745 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
2746
2747 /*
2748 * struct {
2749 * uint32 ticket_lifetime_hint;
2750 * opaque ticket<0..2^16-1>;
2751 * } NewSessionTicket;
2752 *
2753 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
2754 * 8 . 9 ticket_len (n)
2755 * 10 . 9+n ticket content
2756 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002757
2758 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
2759 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
2760 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
2761 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002762
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02002763 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
2764 {
2765 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
2766 tlen = 0;
2767 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002768
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002769 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
2770 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002771
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002772 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002773
2774 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2775 {
2776 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2777 return( ret );
2778 }
2779
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002780 /* No need to remember writing a NewSessionTicket any more */
2781 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002782
2783 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
2784
2785 return( 0 );
2786}
Paul Bakkera503a632013-08-14 13:48:06 +02002787#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002788
Paul Bakker5121ce52009-01-03 21:22:43 +00002789/*
Paul Bakker1961b702013-01-25 14:49:24 +01002790 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002791 */
Paul Bakker1961b702013-01-25 14:49:24 +01002792int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002793{
2794 int ret = 0;
2795
Paul Bakker1961b702013-01-25 14:49:24 +01002796 if( ssl->state == SSL_HANDSHAKE_OVER )
2797 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002798
Paul Bakker1961b702013-01-25 14:49:24 +01002799 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
2800
2801 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2802 return( ret );
2803
2804 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002805 {
Paul Bakker1961b702013-01-25 14:49:24 +01002806 case SSL_HELLO_REQUEST:
2807 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002808 break;
2809
Paul Bakker1961b702013-01-25 14:49:24 +01002810 /*
2811 * <== ClientHello
2812 */
2813 case SSL_CLIENT_HELLO:
2814 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002815 break;
Paul Bakker1961b702013-01-25 14:49:24 +01002816
2817 /*
2818 * ==> ServerHello
2819 * Certificate
2820 * ( ServerKeyExchange )
2821 * ( CertificateRequest )
2822 * ServerHelloDone
2823 */
2824 case SSL_SERVER_HELLO:
2825 ret = ssl_write_server_hello( ssl );
2826 break;
2827
2828 case SSL_SERVER_CERTIFICATE:
2829 ret = ssl_write_certificate( ssl );
2830 break;
2831
2832 case SSL_SERVER_KEY_EXCHANGE:
2833 ret = ssl_write_server_key_exchange( ssl );
2834 break;
2835
2836 case SSL_CERTIFICATE_REQUEST:
2837 ret = ssl_write_certificate_request( ssl );
2838 break;
2839
2840 case SSL_SERVER_HELLO_DONE:
2841 ret = ssl_write_server_hello_done( ssl );
2842 break;
2843
2844 /*
2845 * <== ( Certificate/Alert )
2846 * ClientKeyExchange
2847 * ( CertificateVerify )
2848 * ChangeCipherSpec
2849 * Finished
2850 */
2851 case SSL_CLIENT_CERTIFICATE:
2852 ret = ssl_parse_certificate( ssl );
2853 break;
2854
2855 case SSL_CLIENT_KEY_EXCHANGE:
2856 ret = ssl_parse_client_key_exchange( ssl );
2857 break;
2858
2859 case SSL_CERTIFICATE_VERIFY:
2860 ret = ssl_parse_certificate_verify( ssl );
2861 break;
2862
2863 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2864 ret = ssl_parse_change_cipher_spec( ssl );
2865 break;
2866
2867 case SSL_CLIENT_FINISHED:
2868 ret = ssl_parse_finished( ssl );
2869 break;
2870
2871 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002872 * ==> ( NewSessionTicket )
2873 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002874 * Finished
2875 */
2876 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002877#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002878 if( ssl->handshake->new_session_ticket != 0 )
2879 ret = ssl_write_new_session_ticket( ssl );
2880 else
Paul Bakkera503a632013-08-14 13:48:06 +02002881#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002882 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002883 break;
2884
2885 case SSL_SERVER_FINISHED:
2886 ret = ssl_write_finished( ssl );
2887 break;
2888
2889 case SSL_FLUSH_BUFFERS:
2890 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2891 ssl->state = SSL_HANDSHAKE_WRAPUP;
2892 break;
2893
2894 case SSL_HANDSHAKE_WRAPUP:
2895 ssl_handshake_wrapup( ssl );
2896 break;
2897
2898 default:
2899 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2900 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002901 }
2902
Paul Bakker5121ce52009-01-03 21:22:43 +00002903 return( ret );
2904}
Paul Bakker5121ce52009-01-03 21:22:43 +00002905#endif