blob: dd31a6414d03b675510cfad26e7a5f356811cac4 [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é-Gonnard70380392013-09-16 16:19:53 +0200506 ecp_group_id grp_id;
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é-Gonnard70380392013-09-16 16:19:53 +0200519 grp_id = ecp_grp_id_from_named_curve( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200520
521 if( grp_id != POLARSSL_ECP_DP_NONE )
Paul Bakker41c83d32013-03-20 14:39:14 +0100522 {
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200523 ssl->handshake->ec_curve = 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é-Gonnardabae74c2013-08-20 13:53:44 +0200891 pk_type_t pk_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +0000892
893 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
894
Paul Bakker48916f92012-09-16 19:57:18 +0000895 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
896 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000897 {
898 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
899 return( ret );
900 }
901
902 buf = ssl->in_hdr;
903
Paul Bakker78a8c712013-03-06 17:01:52 +0100904#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
905 if( ( buf[0] & 0x80 ) != 0 )
906 return ssl_parse_client_hello_v2( ssl );
907#endif
908
Paul Bakkerec636f32012-09-09 19:17:02 +0000909 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
910
911 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
912 buf[0] ) );
913 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
914 ( buf[3] << 8 ) | buf[4] ) );
915 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
916 buf[1], buf[2] ) );
917
918 /*
919 * SSLv3 Client Hello
920 *
921 * Record layer:
922 * 0 . 0 message type
923 * 1 . 2 protocol version
924 * 3 . 4 message length
925 */
926 if( buf[0] != SSL_MSG_HANDSHAKE ||
927 buf[1] != SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000928 {
Paul Bakkerec636f32012-09-09 19:17:02 +0000929 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
930 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
931 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000932
Paul Bakkerec636f32012-09-09 19:17:02 +0000933 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000934
Manuel Pégourié-Gonnard72882b22013-08-02 13:36:00 +0200935 if( n < 45 || n > 2048 )
Paul Bakkerec636f32012-09-09 19:17:02 +0000936 {
937 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
938 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
939 }
940
Paul Bakker48916f92012-09-16 19:57:18 +0000941 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
942 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +0000943 {
944 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
945 return( ret );
946 }
947
948 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +0000949 if( !ssl->renegotiation )
950 n = ssl->in_left - 5;
951 else
952 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +0000953
Paul Bakker48916f92012-09-16 19:57:18 +0000954 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +0000955
956 /*
957 * SSL layer:
958 * 0 . 0 handshake type
959 * 1 . 3 handshake length
960 * 4 . 5 protocol version
961 * 6 . 9 UNIX time()
962 * 10 . 37 random bytes
963 * 38 . 38 session id length
964 * 39 . 38+x session id
965 * 39+x . 40+x ciphersuitelist length
966 * 41+x . .. ciphersuitelist
967 * .. . .. compression alg.
968 * .. . .. extensions
969 */
970 SSL_DEBUG_BUF( 4, "record contents", buf, n );
971
972 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
973 buf[0] ) );
974 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
975 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
976 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
977 buf[4], buf[5] ) );
978
979 /*
980 * Check the handshake type and protocol version
981 */
982 if( buf[0] != SSL_HS_CLIENT_HELLO ||
983 buf[4] != SSL_MAJOR_VERSION_3 )
984 {
985 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
986 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
987 }
988
989 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200990 ssl->minor_ver = ( buf[5] <= ssl->max_minor_ver )
991 ? buf[5] : ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +0000992
Paul Bakker1d29fb52012-09-28 13:28:45 +0000993 if( ssl->minor_ver < ssl->min_minor_ver )
994 {
995 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
996 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +0000997 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +0000998
999 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1000 SSL_ALERT_MSG_PROTOCOL_VERSION );
1001
1002 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1003 }
1004
Paul Bakker2fbefde2013-06-29 16:01:15 +02001005 ssl->handshake->max_major_ver = buf[4];
1006 ssl->handshake->max_minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001007
Paul Bakker48916f92012-09-16 19:57:18 +00001008 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001009
1010 /*
1011 * Check the handshake message length
1012 */
1013 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1014 {
1015 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1016 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1017 }
1018
1019 /*
1020 * Check the session length
1021 */
1022 sess_len = buf[38];
1023
1024 if( sess_len > 32 )
1025 {
1026 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1027 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1028 }
1029
Paul Bakker48916f92012-09-16 19:57:18 +00001030 ssl->session_negotiate->length = sess_len;
1031 memset( ssl->session_negotiate->id, 0,
1032 sizeof( ssl->session_negotiate->id ) );
1033 memcpy( ssl->session_negotiate->id, buf + 39,
1034 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001035
1036 /*
1037 * Check the ciphersuitelist length
1038 */
1039 ciph_len = ( buf[39 + sess_len] << 8 )
1040 | ( buf[40 + sess_len] );
1041
1042 if( ciph_len < 2 || ciph_len > 256 || ( ciph_len % 2 ) != 0 )
1043 {
1044 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1045 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1046 }
1047
1048 /*
1049 * Check the compression algorithms length
1050 */
1051 comp_len = buf[41 + sess_len + ciph_len];
1052
1053 if( comp_len < 1 || comp_len > 16 )
1054 {
1055 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1056 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1057 }
1058
Paul Bakker48916f92012-09-16 19:57:18 +00001059 /*
1060 * Check the extension length
1061 */
1062 if( n > 42 + sess_len + ciph_len + comp_len )
1063 {
1064 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1065 | ( buf[43 + sess_len + ciph_len + comp_len] );
1066
1067 if( ( ext_len > 0 && ext_len < 4 ) ||
1068 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1069 {
1070 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1071 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1072 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1073 }
1074 }
1075
1076 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001077#if defined(POLARSSL_ZLIB_SUPPORT)
1078 for( i = 0; i < comp_len; ++i )
1079 {
Paul Bakker48916f92012-09-16 19:57:18 +00001080 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001081 {
Paul Bakker48916f92012-09-16 19:57:18 +00001082 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001083 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001084 }
1085 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001086#endif
1087
Paul Bakkerec636f32012-09-09 19:17:02 +00001088 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1089 buf + 6, 32 );
1090 SSL_DEBUG_BUF( 3, "client hello, session id",
1091 buf + 38, sess_len );
1092 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1093 buf + 41 + sess_len, ciph_len );
1094 SSL_DEBUG_BUF( 3, "client hello, compression",
1095 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001096
Paul Bakkerec636f32012-09-09 19:17:02 +00001097 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001098 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1099 */
1100 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1101 {
1102 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1103 {
1104 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1105 if( ssl->renegotiation == SSL_RENEGOTIATION )
1106 {
1107 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001108
1109 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1110 return( ret );
1111
Paul Bakker48916f92012-09-16 19:57:18 +00001112 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1113 }
1114 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1115 break;
1116 }
1117 }
1118
Paul Bakker48916f92012-09-16 19:57:18 +00001119 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001120
1121 while( ext_len )
1122 {
1123 unsigned int ext_id = ( ( ext[0] << 8 )
1124 | ( ext[1] ) );
1125 unsigned int ext_size = ( ( ext[2] << 8 )
1126 | ( ext[3] ) );
1127
1128 if( ext_size + 4 > ext_len )
1129 {
1130 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1131 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1132 }
1133 switch( ext_id )
1134 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001135#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001136 case TLS_EXT_SERVERNAME:
1137 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1138 if( ssl->f_sni == NULL )
1139 break;
1140
1141 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1142 if( ret != 0 )
1143 return( ret );
1144 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001145#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001146
Paul Bakker48916f92012-09-16 19:57:18 +00001147 case TLS_EXT_RENEGOTIATION_INFO:
1148 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1149 renegotiation_info_seen = 1;
1150
Paul Bakker23f36802012-09-28 14:15:14 +00001151 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1152 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001153 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001154 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001155
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001156#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001157 case TLS_EXT_SIG_ALG:
1158 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1159 if( ssl->renegotiation == SSL_RENEGOTIATION )
1160 break;
1161
1162 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1163 if( ret != 0 )
1164 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001165 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001166#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001167
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001168#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001169 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1170 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1171
1172 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1173 if( ret != 0 )
1174 return( ret );
1175 break;
1176
1177 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1178 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1179
1180 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1181 if( ret != 0 )
1182 return( ret );
1183 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001184#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001185
Paul Bakker05decb22013-08-15 13:33:48 +02001186#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001187 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1188 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1189
1190 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1191 if( ret != 0 )
1192 return( ret );
1193 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001194#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001195
Paul Bakker1f2bc622013-08-15 13:45:55 +02001196#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001197 case TLS_EXT_TRUNCATED_HMAC:
1198 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1199
1200 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1201 if( ret != 0 )
1202 return( ret );
1203 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001204#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001205
Paul Bakkera503a632013-08-14 13:48:06 +02001206#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001207 case TLS_EXT_SESSION_TICKET:
1208 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1209
1210 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1211 if( ret != 0 )
1212 return( ret );
1213 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001214#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001215
Paul Bakker48916f92012-09-16 19:57:18 +00001216 default:
1217 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1218 ext_id ) );
1219 }
1220
1221 ext_len -= 4 + ext_size;
1222 ext += 4 + ext_size;
1223
1224 if( ext_len > 0 && ext_len < 4 )
1225 {
1226 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1227 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1228 }
1229 }
1230
1231 /*
1232 * Renegotiation security checks
1233 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001234 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1235 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1236 {
1237 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1238 handshake_failure = 1;
1239 }
1240 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1241 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1242 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001243 {
1244 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001245 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001246 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001247 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1248 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1249 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001250 {
1251 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001252 handshake_failure = 1;
1253 }
1254 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1255 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1256 renegotiation_info_seen == 1 )
1257 {
1258 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1259 handshake_failure = 1;
1260 }
1261
1262 if( handshake_failure == 1 )
1263 {
1264 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1265 return( ret );
1266
Paul Bakker48916f92012-09-16 19:57:18 +00001267 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1268 }
Paul Bakker380da532012-04-18 16:10:25 +00001269
Paul Bakker41c83d32013-03-20 14:39:14 +01001270 /*
1271 * Search for a matching ciphersuite
1272 * (At the end because we need information from the EC-based extensions)
1273 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001274 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
1275 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001276 {
1277 for( j = 0, p = buf + 41 + sess_len; j < ciph_len;
1278 j += 2, p += 2 )
1279 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001280 if( p[0] == ( ( ciphersuites[i] >> 8 ) & 0xFF ) &&
1281 p[1] == ( ( ciphersuites[i] ) & 0xFF ) )
Paul Bakker41c83d32013-03-20 14:39:14 +01001282 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001283 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
Paul Bakker41c83d32013-03-20 14:39:14 +01001284
1285 if( ciphersuite_info == NULL )
1286 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001287 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found",
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001288 ciphersuites[i] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001289 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1290 }
1291
Paul Bakker2fbefde2013-06-29 16:01:15 +02001292 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
1293 ciphersuite_info->max_minor_ver < ssl->minor_ver )
1294 continue;
1295
Paul Bakker5fd49172013-08-19 13:29:26 +02001296#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001297 if( ssl_ciphersuite_uses_ec( ciphersuite_info ) &&
Paul Bakker41c83d32013-03-20 14:39:14 +01001298 ssl->handshake->ec_curve == 0 )
1299 continue;
Paul Bakker5fd49172013-08-19 13:29:26 +02001300#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001301
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001302 /* If ciphersuite requires us to have a private key of a
1303 * certain type, make sure we do */
1304 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
1305 if( pk_alg != POLARSSL_PK_NONE &&
1306 ( ssl->pk_key == NULL ||
1307 ! pk_can_do( ssl->pk_key, pk_alg ) ) )
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001308 continue;
1309
Paul Bakker41c83d32013-03-20 14:39:14 +01001310 goto have_ciphersuite;
1311 }
1312 }
1313 }
1314
1315 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1316
1317 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1318 return( ret );
1319
1320 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1321
1322have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001323 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001324 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1325 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1326
Paul Bakker5121ce52009-01-03 21:22:43 +00001327 ssl->in_left = 0;
1328 ssl->state++;
1329
1330 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1331
1332 return( 0 );
1333}
1334
Paul Bakker1f2bc622013-08-15 13:45:55 +02001335#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001336static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1337 unsigned char *buf,
1338 size_t *olen )
1339{
1340 unsigned char *p = buf;
1341
1342 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1343 {
1344 *olen = 0;
1345 return;
1346 }
1347
1348 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1349
1350 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1351 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1352
1353 *p++ = 0x00;
1354 *p++ = 0x00;
1355
1356 *olen = 4;
1357}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001358#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001359
Paul Bakkera503a632013-08-14 13:48:06 +02001360#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001361static void ssl_write_session_ticket_ext( ssl_context *ssl,
1362 unsigned char *buf,
1363 size_t *olen )
1364{
1365 unsigned char *p = buf;
1366
1367 if( ssl->handshake->new_session_ticket == 0 )
1368 {
1369 *olen = 0;
1370 return;
1371 }
1372
1373 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1374
1375 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1376 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1377
1378 *p++ = 0x00;
1379 *p++ = 0x00;
1380
1381 *olen = 4;
1382}
Paul Bakkera503a632013-08-14 13:48:06 +02001383#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001384
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001385static void ssl_write_renegotiation_ext( ssl_context *ssl,
1386 unsigned char *buf,
1387 size_t *olen )
1388{
1389 unsigned char *p = buf;
1390
1391 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1392 {
1393 *olen = 0;
1394 return;
1395 }
1396
1397 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1398
1399 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1400 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1401
1402 *p++ = 0x00;
1403 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1404 *p++ = ssl->verify_data_len * 2 & 0xFF;
1405
1406 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1407 p += ssl->verify_data_len;
1408 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1409 p += ssl->verify_data_len;
1410
1411 *olen = 5 + ssl->verify_data_len * 2;
1412}
1413
Paul Bakker05decb22013-08-15 13:33:48 +02001414#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001415static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1416 unsigned char *buf,
1417 size_t *olen )
1418{
1419 unsigned char *p = buf;
1420
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001421 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1422 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001423 *olen = 0;
1424 return;
1425 }
1426
1427 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1428
1429 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1430 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1431
1432 *p++ = 0x00;
1433 *p++ = 1;
1434
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001435 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001436
1437 *olen = 5;
1438}
Paul Bakker05decb22013-08-15 13:33:48 +02001439#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001440
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001441#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001442static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1443 unsigned char *buf,
1444 size_t *olen )
1445{
1446 unsigned char *p = buf;
1447 ((void) ssl);
1448
1449 *olen = 0;
1450
1451 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1452
1453 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1454 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1455
1456 *p++ = 0x00;
1457 *p++ = 2;
1458
1459 *p++ = 1;
1460 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1461
1462 *olen = 6;
1463}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001464#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001465
Paul Bakker5121ce52009-01-03 21:22:43 +00001466static int ssl_write_server_hello( ssl_context *ssl )
1467{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001468#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001469 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001470#endif
Paul Bakkera3d195c2011-11-27 21:07:34 +00001471 int ret, n;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001472 size_t olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001473 unsigned char *buf, *p;
1474
1475 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1476
1477 /*
1478 * 0 . 0 handshake type
1479 * 1 . 3 handshake length
1480 * 4 . 5 protocol version
1481 * 6 . 9 UNIX time()
1482 * 10 . 37 random bytes
1483 */
1484 buf = ssl->out_msg;
1485 p = buf + 4;
1486
1487 *p++ = (unsigned char) ssl->major_ver;
1488 *p++ = (unsigned char) ssl->minor_ver;
1489
1490 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1491 buf[4], buf[5] ) );
1492
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001493#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001494 t = time( NULL );
1495 *p++ = (unsigned char)( t >> 24 );
1496 *p++ = (unsigned char)( t >> 16 );
1497 *p++ = (unsigned char)( t >> 8 );
1498 *p++ = (unsigned char)( t );
1499
1500 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001501#else
1502 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1503 return( ret );
1504
1505 p += 4;
1506#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001507
Paul Bakkera3d195c2011-11-27 21:07:34 +00001508 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1509 return( ret );
1510
1511 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001512
Paul Bakker48916f92012-09-16 19:57:18 +00001513 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001514
1515 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1516
1517 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001518 * Resume is 0 by default, see ssl_handshake_init().
1519 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1520 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001521 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001522 if( ssl->handshake->resume == 0 &&
1523 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001524 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001525 ssl->f_get_cache != NULL &&
1526 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1527 {
1528 ssl->handshake->resume = 1;
1529 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001530
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001531 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001532 {
1533 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001534 * New session, create a new session id,
1535 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001536 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001537 ssl->state++;
1538
Paul Bakkera503a632013-08-14 13:48:06 +02001539#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001540 if( ssl->handshake->new_session_ticket == 0 )
1541 {
1542 ssl->session_negotiate->length = n = 32;
1543 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001544 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001545 return( ret );
1546 }
1547 else
1548 {
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001549 ssl->session_negotiate->length = n = 0;
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001550 memset( ssl->session_negotiate->id, 0, 32 );
1551 }
Paul Bakkera503a632013-08-14 13:48:06 +02001552#else
1553 ssl->session_negotiate->length = n = 32;
1554 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
1555 n ) ) != 0 )
1556 return( ret );
1557#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001558 }
1559 else
1560 {
1561 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001562 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001563 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001564 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001565 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001566
1567 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1568 {
1569 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1570 return( ret );
1571 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001572 }
1573
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001574 /*
1575 * 38 . 38 session id length
1576 * 39 . 38+n session id
1577 * 39+n . 40+n chosen ciphersuite
1578 * 41+n . 41+n chosen compression alg.
1579 * 42+n . 43+n extensions length
1580 * 44+n . 43+n+m extensions
1581 */
1582 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001583 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1584 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001585
1586 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1587 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1588 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001589 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001590
Paul Bakker48916f92012-09-16 19:57:18 +00001591 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1592 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1593 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001594
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001595 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
1596 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001597 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001598 ssl->session_negotiate->compression ) );
1599
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001600 /*
1601 * First write extensions, then the total length
1602 */
1603 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1604 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001605
Paul Bakker05decb22013-08-15 13:33:48 +02001606#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001607 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1608 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001609#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001610
Paul Bakker1f2bc622013-08-15 13:45:55 +02001611#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001612 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1613 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001614#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001615
Paul Bakkera503a632013-08-14 13:48:06 +02001616#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001617 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1618 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001619#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001620
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001621#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001622 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1623 ext_len += olen;
1624#endif
1625
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001626 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001627
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001628 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1629 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1630 p += ext_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001631
1632 ssl->out_msglen = p - buf;
1633 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1634 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1635
1636 ret = ssl_write_record( ssl );
1637
1638 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1639
1640 return( ret );
1641}
1642
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001643#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1644 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001645 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1646 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001647static int ssl_write_certificate_request( ssl_context *ssl )
1648{
Paul Bakkered27a042013-04-18 22:46:23 +02001649 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1650 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001651
1652 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1653
1654 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1655 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1656 {
1657 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1658 ssl->state++;
1659 return( 0 );
1660 }
1661
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001662 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001663 return( ret );
1664}
1665#else
1666static int ssl_write_certificate_request( ssl_context *ssl )
1667{
1668 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1669 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001670 size_t dn_size, total_dn_size; /* excluding length bytes */
1671 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00001672 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001673 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001674
1675 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1676
1677 ssl->state++;
1678
Paul Bakkerfbb17802013-04-17 19:10:21 +02001679 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001680 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001681 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001682 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001683 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001684 return( 0 );
1685 }
1686
1687 /*
1688 * 0 . 0 handshake type
1689 * 1 . 3 handshake length
1690 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01001691 * 5 .. m-1 cert types
1692 * m .. m+1 sig alg length (TLS 1.2 only)
1693 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001694 * n .. n+1 length of all DNs
1695 * n+2 .. n+3 length of DN 1
1696 * n+4 .. ... Distinguished Name #1
1697 * ... .. ... length of DN 2, etc.
1698 */
1699 buf = ssl->out_msg;
1700 p = buf + 4;
1701
1702 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001703 * Supported certificate types
1704 *
1705 * ClientCertificateType certificate_types<1..2^8-1>;
1706 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00001707 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001708 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01001709
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001710#if defined(POLARSSL_RSA_C)
1711 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
1712#endif
1713#if defined(POLARSSL_ECDSA_C)
1714 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
1715#endif
1716
1717 p[0] = ct_len++;
1718 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01001719
Paul Bakker577e0062013-08-28 11:57:20 +02001720 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001721#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01001722 /*
1723 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01001724 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001725 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
1726 *
1727 * struct {
1728 * HashAlgorithm hash;
1729 * SignatureAlgorithm signature;
1730 * } SignatureAndHashAlgorithm;
1731 *
1732 * enum { (255) } HashAlgorithm;
1733 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01001734 */
Paul Bakker21dca692013-01-03 11:41:08 +01001735 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01001736 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001737 /*
1738 * Only use current running hash algorithm that is already required
1739 * for requested ciphersuite.
1740 */
Paul Bakker926af752012-11-23 13:38:07 +01001741 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
1742
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001743 if( ssl->transform_negotiate->ciphersuite_info->mac ==
1744 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01001745 {
1746 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
1747 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02001748
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001749 /*
1750 * Supported signature algorithms
1751 */
1752#if defined(POLARSSL_RSA_C)
1753 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1754 p[2 + sa_len++] = SSL_SIG_RSA;
1755#endif
1756#if defined(POLARSSL_ECDSA_C)
1757 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1758 p[2 + sa_len++] = SSL_SIG_ECDSA;
1759#endif
Paul Bakker926af752012-11-23 13:38:07 +01001760
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001761 p[0] = (unsigned char)( sa_len >> 8 );
1762 p[1] = (unsigned char)( sa_len );
1763 sa_len += 2;
1764 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01001765 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001766#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001767
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001768 /*
1769 * DistinguishedName certificate_authorities<0..2^16-1>;
1770 * opaque DistinguishedName<1..2^16-1>;
1771 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001772 p += 2;
1773 crt = ssl->ca_chain;
1774
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001775 total_dn_size = 0;
Paul Bakker29087132010-03-21 21:03:34 +00001776 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001777 {
1778 if( p - buf > 4096 )
1779 break;
1780
Paul Bakker926af752012-11-23 13:38:07 +01001781 dn_size = crt->subject_raw.len;
1782 *p++ = (unsigned char)( dn_size >> 8 );
1783 *p++ = (unsigned char)( dn_size );
1784 memcpy( p, crt->subject_raw.p, dn_size );
1785 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001786
Paul Bakker926af752012-11-23 13:38:07 +01001787 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
1788
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001789 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01001790 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00001791 }
1792
Paul Bakker926af752012-11-23 13:38:07 +01001793 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001794 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1795 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001796 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
1797 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001798
1799 ret = ssl_write_record( ssl );
1800
1801 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
1802
1803 return( ret );
1804}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001805#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
1806 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
1807 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001808
Paul Bakker41c83d32013-03-20 14:39:14 +01001809static int ssl_write_server_key_exchange( ssl_context *ssl )
1810{
Paul Bakker23986e52011-04-24 08:57:21 +00001811 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001812 size_t n = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001813 const ssl_ciphersuite_t *ciphersuite_info;
1814
1815#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1816 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
1817 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1818 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001819 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001820 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001821 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001822 ((void) dig_signed);
1823 ((void) dig_signed_len);
1824#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001825
Paul Bakker41c83d32013-03-20 14:39:14 +01001826 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001827
1828 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
1829
Paul Bakker41c83d32013-03-20 14:39:14 +01001830 if( ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_RSA &&
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001831 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_ECDHE_RSA &&
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001832 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA &&
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001833 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001834 {
1835 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
1836 ssl->state++;
1837 return( 0 );
1838 }
1839
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001840#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1841 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1842 {
1843 /* TODO: Support identity hints */
1844 *(p++) = 0x00;
1845 *(p++) = 0x00;
1846
1847 n += 2;
1848 }
1849#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1850
1851#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1852 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1853 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1854 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00001855 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001856 /*
1857 * Ephemeral DH parameters:
1858 *
1859 * struct {
1860 * opaque dh_p<1..2^16-1>;
1861 * opaque dh_g<1..2^16-1>;
1862 * opaque dh_Ys<1..2^16-1>;
1863 * } ServerDHParams;
1864 */
1865 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
1866 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
1867 {
1868 SSL_DEBUG_RET( 1, "mpi_copy", ret );
1869 return( ret );
1870 }
Paul Bakker48916f92012-09-16 19:57:18 +00001871
Paul Bakker41c83d32013-03-20 14:39:14 +01001872 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
1873 mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001874 p,
1875 &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001876 {
1877 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
1878 return( ret );
1879 }
1880
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001881 dig_signed = p;
1882 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001883
1884 p += len;
1885 n += len;
1886
Paul Bakker41c83d32013-03-20 14:39:14 +01001887 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
1888 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1889 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1890 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
1891 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001892#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1893 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001894
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001895#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1896 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1897 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1898 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001899 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001900 /*
1901 * Ephemeral ECDH parameters:
1902 *
1903 * struct {
1904 * ECParameters curve_params;
1905 * ECPoint public;
1906 * } ServerECDHParams;
1907 */
Paul Bakker41c83d32013-03-20 14:39:14 +01001908 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
1909 ssl->handshake->ec_curve ) ) != 0 )
1910 {
1911 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
1912 return( ret );
1913 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001914
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001915 SSL_DEBUG_MSG( 2, ( "ECDH curve size: %d",
1916 (int) ssl->handshake->ecdh_ctx.grp.nbits ) );
1917
Paul Bakker41c83d32013-03-20 14:39:14 +01001918 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001919 &len,
1920 p,
Paul Bakker41c83d32013-03-20 14:39:14 +01001921 1000, ssl->f_rng, ssl->p_rng ) ) != 0 )
1922 {
1923 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
1924 return( ret );
1925 }
1926
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001927 dig_signed = p;
1928 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001929
1930 p += len;
1931 n += len;
1932
Paul Bakker41c83d32013-03-20 14:39:14 +01001933 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
1934 }
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001935#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1936 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001937
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001938#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001939 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1940 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001941 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001942 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1943 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001944 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001945 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001946 unsigned int hashlen = 0;
1947 unsigned char hash[64];
1948 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00001949
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001950 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001951 * Choose hash algorithm. NONE means MD5 + SHA1 here.
1952 */
Paul Bakker577e0062013-08-28 11:57:20 +02001953#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001954 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
1955 {
1956 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
1957
1958 if( md_alg == POLARSSL_MD_NONE )
1959 {
1960 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1961 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1962 }
1963 }
Paul Bakker577e0062013-08-28 11:57:20 +02001964 else
1965#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001966#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1967 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker577e0062013-08-28 11:57:20 +02001968 if ( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001969 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
1970 {
1971 md_alg = POLARSSL_MD_SHA1;
1972 }
1973 else
Paul Bakker577e0062013-08-28 11:57:20 +02001974#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001975 {
1976 md_alg = POLARSSL_MD_NONE;
1977 }
1978
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001979 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001980 * Compute the hash to be signed
1981 */
Paul Bakker577e0062013-08-28 11:57:20 +02001982#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1983 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001984 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00001985 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001986 md5_context md5;
1987 sha1_context sha1;
1988
1989 /*
1990 * digitally-signed struct {
1991 * opaque md5_hash[16];
1992 * opaque sha_hash[20];
1993 * };
1994 *
1995 * md5_hash
1996 * MD5(ClientHello.random + ServerHello.random
1997 * + ServerParams);
1998 * sha_hash
1999 * SHA(ClientHello.random + ServerHello.random
2000 * + ServerParams);
2001 */
2002 md5_starts( &md5 );
2003 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002004 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002005 md5_finish( &md5, hash );
2006
2007 sha1_starts( &sha1 );
2008 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002009 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002010 sha1_finish( &sha1, hash + 16 );
2011
2012 hashlen = 36;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002013 }
2014 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002015#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2016 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002017#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2018 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002019 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002020 {
2021 md_context_t ctx;
2022
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002023 /* Info from md_alg will be used instead */
2024 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002025
2026 /*
2027 * digitally-signed struct {
2028 * opaque client_random[32];
2029 * opaque server_random[32];
2030 * ServerDHParams params;
2031 * };
2032 */
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002033 if( ( ret = md_init_ctx( &ctx, md_info_from_type(md_alg) ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002034 {
2035 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2036 return( ret );
2037 }
2038
2039 md_starts( &ctx );
2040 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002041 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002042 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02002043
2044 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
2045 {
2046 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
2047 return( ret );
2048 }
2049
Paul Bakker23f36802012-09-28 14:15:14 +00002050 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002051 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002052#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2053 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002054 {
2055 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002056 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02002057 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002058
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002059 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2060 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002061
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002062 /*
2063 * Make the signature
2064 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02002065 if( ssl->pk_key == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002066 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002067 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2068 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002069 }
Paul Bakker23f36802012-09-28 14:15:14 +00002070
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002071#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002072 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2073 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002074 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002075 *(p++) = ssl_sig_from_pk( ssl->pk_key );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002076
2077 n += 2;
2078 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002079#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002080
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002081 if( ( ret = pk_sign( ssl->pk_key, md_alg, hash, hashlen,
2082 p + 2 , &signature_len,
2083 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002084 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002085 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002086 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002087 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002088
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002089 *(p++) = (unsigned char)( signature_len >> 8 );
2090 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002091 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002092
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002093 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002094
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002095 p += signature_len;
2096 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002097 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002098#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002099 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2100 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002101
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002102 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2104 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2105
2106 ssl->state++;
2107
2108 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2109 {
2110 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2111 return( ret );
2112 }
2113
2114 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2115
2116 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002117}
2118
2119static int ssl_write_server_hello_done( ssl_context *ssl )
2120{
2121 int ret;
2122
2123 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2124
2125 ssl->out_msglen = 4;
2126 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2127 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2128
2129 ssl->state++;
2130
2131 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2132 {
2133 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2134 return( ret );
2135 }
2136
2137 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2138
2139 return( 0 );
2140}
2141
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002142#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2143 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2144static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2145 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002146{
2147 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002148 size_t n;
2149
2150 /*
2151 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2152 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002153 if( *p + 2 > end )
2154 {
2155 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2156 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2157 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002158
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002159 n = ( (*p)[0] << 8 ) | (*p)[1];
2160 *p += 2;
2161
2162 if( n < 1 || n > ssl->handshake->dhm_ctx.len || *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002163 {
2164 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2165 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2166 }
2167
2168 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002169 *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002170 {
2171 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2172 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2173 }
2174
2175 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2176
Paul Bakker70df2fb2013-04-17 17:19:09 +02002177 return( ret );
2178}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002179#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2180 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002181
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02002182#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2183 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002184static int ssl_parse_client_ecdh_public( ssl_context *ssl )
2185{
2186 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002187 size_t n;
2188
2189 /*
2190 * Receive client public key and calculate premaster
2191 */
2192 n = ssl->in_msg[3];
2193
2194 if( n < 1 || n > mpi_size( &ssl->handshake->ecdh_ctx.grp.P ) * 2 + 2 ||
2195 n + 4 != ssl->in_hslen )
2196 {
2197 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2198 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2199 }
2200
2201 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2202 ssl->in_msg + 4, n ) ) != 0 )
2203 {
2204 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2205 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2206 }
2207
2208 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2209
Paul Bakker70df2fb2013-04-17 17:19:09 +02002210 return( ret );
2211}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02002212#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2213 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002214
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002215#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002216static int ssl_parse_encrypted_pms_secret( ssl_context *ssl )
2217{
2218 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2219 size_t i, n = 0;
2220
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002221 if( ! pk_can_do( ssl->pk_key, POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002222 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002223 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002224 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2225 }
2226
2227 /*
2228 * Decrypt the premaster using own private RSA key
2229 */
2230 i = 4;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002231 n = pk_get_len( ssl->pk_key );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002232 ssl->handshake->pmslen = 48;
2233
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002234#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2235 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002236 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2237 {
2238 i += 2;
2239 if( ssl->in_msg[4] != ( ( n >> 8 ) & 0xFF ) ||
2240 ssl->in_msg[5] != ( ( n ) & 0xFF ) )
2241 {
2242 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2243 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2244 }
2245 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002246#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002247
2248 if( ssl->in_hslen != i + n )
2249 {
2250 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2251 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2252 }
2253
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002254 ret = pk_decrypt( ssl->pk_key,
2255 ssl->in_msg + i, n,
2256 ssl->handshake->premaster, &ssl->handshake->pmslen,
2257 sizeof(ssl->handshake->premaster),
2258 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002259
2260 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Paul Bakker2fbefde2013-06-29 16:01:15 +02002261 ssl->handshake->premaster[0] != ssl->handshake->max_major_ver ||
2262 ssl->handshake->premaster[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002263 {
2264 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2265
2266 /*
2267 * Protection against Bleichenbacher's attack:
2268 * invalid PKCS#1 v1.5 padding must not cause
2269 * the connection to end immediately; instead,
2270 * send a bad_record_mac later in the handshake.
2271 */
2272 ssl->handshake->pmslen = 48;
2273
2274 ret = ssl->f_rng( ssl->p_rng, ssl->handshake->premaster,
2275 ssl->handshake->pmslen );
2276 if( ret != 0 )
2277 return( ret );
2278 }
2279
2280 return( ret );
2281}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002282#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002283
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002284#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2285 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2286static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2287 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002288{
Paul Bakker6db455e2013-09-18 17:29:31 +02002289 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002290 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002291
Paul Bakker6db455e2013-09-18 17:29:31 +02002292 if( ssl->f_psk == NULL &&
2293 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2294 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002295 {
2296 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2297 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2298 }
2299
2300 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002301 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002302 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002303 if( *p + 2 > end )
2304 {
2305 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2306 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2307 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002308
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002309 n = ( (*p)[0] << 8 ) | (*p)[1];
2310 *p += 2;
2311
2312 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002313 {
2314 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2315 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2316 }
2317
Paul Bakker6db455e2013-09-18 17:29:31 +02002318 if( ssl->f_psk != NULL )
2319 {
2320 if( ( ret != ssl->f_psk( ssl->p_psk, ssl, *p, n ) ) != 0 )
2321 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2322 }
2323
2324 if( ret == 0 )
2325 {
2326 if( n != ssl->psk_identity_len ||
2327 memcmp( ssl->psk_identity, *p, n ) != 0 )
2328 {
2329 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2330 }
2331 }
2332
2333 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002334 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002335 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002336 if( ( ret = ssl_send_alert_message( ssl,
2337 SSL_ALERT_LEVEL_FATAL,
2338 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2339 {
2340 return( ret );
2341 }
2342
2343 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002344 }
2345
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002346 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002347 ret = 0;
2348
Paul Bakkerfbb17802013-04-17 19:10:21 +02002349 return( ret );
2350}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002351#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
2352 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002353
Paul Bakker5121ce52009-01-03 21:22:43 +00002354static int ssl_parse_client_key_exchange( ssl_context *ssl )
2355{
Paul Bakker23986e52011-04-24 08:57:21 +00002356 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002357 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002358
Paul Bakker41c83d32013-03-20 14:39:14 +01002359 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002360
2361 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2362
2363 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2364 {
2365 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2366 return( ret );
2367 }
2368
2369 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2370 {
2371 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002372 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002373 }
2374
2375 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2376 {
2377 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002378 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002379 }
2380
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002381#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002382 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002383 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002384 unsigned char *p = ssl->in_msg + 4;
2385 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2386
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002387 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002388 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002389 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2390 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002391 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002392
2393 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
2394
Manuel Pégourié-Gonnard032c34e2013-09-07 13:06:27 +02002395 /* No blinding needed for DHE, but will be needed for fixed DH! */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002396 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2397 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002398 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002399 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002400 {
2401 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2402 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2403 }
2404
2405 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002406 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002407 else
2408#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002409#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2410 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2411 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2412 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002413 {
2414 if( ( ret = ssl_parse_client_ecdh_public( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002415 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002416 SSL_DEBUG_RET( 1, ( "ssl_parse_client_ecdh_public" ), ret );
2417 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002418 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002419
2420 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2421 &ssl->handshake->pmslen,
2422 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002423 POLARSSL_MPI_MAX_SIZE,
2424 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002425 {
2426 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2427 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2428 }
2429
2430 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002431 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002432 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002433#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2434 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002435#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2436 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002437 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002438 unsigned char *p = ssl->in_msg + 4;
2439 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2440
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002441 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002442 {
2443 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2444 return( ret );
2445 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002446
2447 // Set up the premaster secret
2448 //
2449 p = ssl->handshake->premaster;
2450 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2451 *(p++) = (unsigned char)( ssl->psk_len );
2452 p += ssl->psk_len;
2453
2454 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2455 *(p++) = (unsigned char)( ssl->psk_len );
2456 memcpy( p, ssl->psk, ssl->psk_len );
2457 p += ssl->psk_len;
2458
2459 ssl->handshake->pmslen = 4 + 2 * ssl->psk_len;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002460 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002462#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
2463#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2464 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2465 {
2466 size_t n;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002467 unsigned char *p = ssl->in_msg + 4;
2468 unsigned char *end = ssl->in_msg + ssl->in_msglen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002469
2470 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2471 {
2472 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2473 return( ret );
2474 }
2475 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2476 {
2477 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2478 return( ret );
2479 }
2480
2481 // Set up the premaster secret
2482 //
2483 p = ssl->handshake->premaster;
2484 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len >> 8 );
2485 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len );
2486
Paul Bakker577e0062013-08-28 11:57:20 +02002487 n = ssl->handshake->dhm_ctx.len;
2488
Manuel Pégourié-Gonnard032c34e2013-09-07 13:06:27 +02002489 /* No blinding needed since this is ephemeral DHM */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002490 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002491 p, &n, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002492 {
2493 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2494 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2495 }
2496
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002497 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2498
2499 p += ssl->handshake->dhm_ctx.len;
2500
2501 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2502 *(p++) = (unsigned char)( ssl->psk_len );
2503 memcpy( p, ssl->psk, ssl->psk_len );
2504 p += ssl->psk_len;
2505
2506 ssl->handshake->pmslen = 4 + ssl->handshake->dhm_ctx.len + ssl->psk_len;
2507 }
2508 else
2509#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
2510#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2511 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002512 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002513 if( ( ret = ssl_parse_encrypted_pms_secret( ssl ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002514 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002515 SSL_DEBUG_RET( 1, ( "ssl_parse_client_ecdh_public" ), ret );
2516 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002517 }
2518 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002519 else
2520#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2521 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002522 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002523 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2524 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002525
Paul Bakkerff60ee62010-03-16 21:09:09 +00002526 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2527 {
2528 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2529 return( ret );
2530 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002531
Paul Bakker5121ce52009-01-03 21:22:43 +00002532 ssl->state++;
2533
2534 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2535
2536 return( 0 );
2537}
2538
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002539#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2540 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002541 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2542 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002543static int ssl_parse_certificate_verify( ssl_context *ssl )
2544{
Paul Bakkered27a042013-04-18 22:46:23 +02002545 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002546 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002547
2548 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2549
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002550 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2551 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002552 {
2553 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2554 ssl->state++;
2555 return( 0 );
2556 }
2557
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002558 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002559 return( ret );
2560}
2561#else
2562static int ssl_parse_certificate_verify( ssl_context *ssl )
2563{
2564 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002565 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002566 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002567 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002568 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02002569#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002570 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02002571#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002572 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002573 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2574
2575 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2576
2577 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2578 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2579 {
2580 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2581 ssl->state++;
2582 return( 0 );
2583 }
2584
Paul Bakkered27a042013-04-18 22:46:23 +02002585 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002586 {
2587 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2588 ssl->state++;
2589 return( 0 );
2590 }
2591
Paul Bakker48916f92012-09-16 19:57:18 +00002592 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002593
2594 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2595 {
2596 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2597 return( ret );
2598 }
2599
2600 ssl->state++;
2601
2602 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2603 {
2604 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002605 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002606 }
2607
2608 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
2609 {
2610 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002611 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002612 }
2613
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002614 /*
2615 * 0 . 0 handshake type
2616 * 1 . 3 handshake length
2617 * 4 . 5 sig alg (TLS 1.2 only)
2618 * 4+n . 5+n signature length (n = sa_len)
2619 * 6+n . 6+n+m signature (m = sig_len)
2620 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002621
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002622#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2623 defined(POLARSSL_SSL_PROTO_TLS1_1)
2624 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002625 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002626 sa_len = 0;
2627
Paul Bakkerc70b9822013-04-07 22:00:46 +02002628 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002629 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002630
2631 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
2632 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
2633 POLARSSL_PK_ECDSA ) )
2634 {
2635 hash_start += 16;
2636 hashlen -= 16;
2637 md_alg = POLARSSL_MD_SHA1;
2638 }
Paul Bakker926af752012-11-23 13:38:07 +01002639 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002640 else
2641#endif
Paul Bakker577e0062013-08-28 11:57:20 +02002642#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2643 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002644 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002645 sa_len = 2;
2646
Paul Bakker5121ce52009-01-03 21:22:43 +00002647 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002648 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00002649 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002650 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002651 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002652 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2653 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01002654 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2655 }
2656
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002657 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01002658
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002659 /* Info from md_alg will be used instead */
2660 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002661
2662 /*
2663 * Signature
2664 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002665 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
2666 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002667 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002668 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2669 " for verify message" ) );
2670 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002671 }
2672
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002673 /*
2674 * Check the certificate's key type matches the signature alg
2675 */
2676 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
2677 {
2678 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
2679 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2680 }
Paul Bakker577e0062013-08-28 11:57:20 +02002681 }
2682 else
2683#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2684 {
2685 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002686 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002687 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002688
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002689 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01002690
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002691 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002692 {
2693 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002694 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002695 }
2696
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002697 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002698 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002699 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002700 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002701 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002702 return( ret );
2703 }
2704
2705 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
2706
Paul Bakkered27a042013-04-18 22:46:23 +02002707 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002708}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002709#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2710 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2711 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002712
Paul Bakkera503a632013-08-14 13:48:06 +02002713#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002714static int ssl_write_new_session_ticket( ssl_context *ssl )
2715{
2716 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002717 size_t tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002718
2719 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
2720
2721 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2722 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
2723
2724 /*
2725 * struct {
2726 * uint32 ticket_lifetime_hint;
2727 * opaque ticket<0..2^16-1>;
2728 * } NewSessionTicket;
2729 *
2730 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
2731 * 8 . 9 ticket_len (n)
2732 * 10 . 9+n ticket content
2733 */
2734 ssl->out_msg[4] = 0x00;
2735 ssl->out_msg[5] = 0x00;
2736 ssl->out_msg[6] = 0x00;
2737 ssl->out_msg[7] = 0x00;
2738
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02002739 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
2740 {
2741 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
2742 tlen = 0;
2743 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002744
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002745 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
2746 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002747
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002748 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002749
2750 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2751 {
2752 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2753 return( ret );
2754 }
2755
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002756 /* No need to remember writing a NewSessionTicket any more */
2757 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002758
2759 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
2760
2761 return( 0 );
2762}
Paul Bakkera503a632013-08-14 13:48:06 +02002763#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002764
Paul Bakker5121ce52009-01-03 21:22:43 +00002765/*
Paul Bakker1961b702013-01-25 14:49:24 +01002766 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002767 */
Paul Bakker1961b702013-01-25 14:49:24 +01002768int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002769{
2770 int ret = 0;
2771
Paul Bakker1961b702013-01-25 14:49:24 +01002772 if( ssl->state == SSL_HANDSHAKE_OVER )
2773 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002774
Paul Bakker1961b702013-01-25 14:49:24 +01002775 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
2776
2777 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2778 return( ret );
2779
2780 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002781 {
Paul Bakker1961b702013-01-25 14:49:24 +01002782 case SSL_HELLO_REQUEST:
2783 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002784 break;
2785
Paul Bakker1961b702013-01-25 14:49:24 +01002786 /*
2787 * <== ClientHello
2788 */
2789 case SSL_CLIENT_HELLO:
2790 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002791 break;
Paul Bakker1961b702013-01-25 14:49:24 +01002792
2793 /*
2794 * ==> ServerHello
2795 * Certificate
2796 * ( ServerKeyExchange )
2797 * ( CertificateRequest )
2798 * ServerHelloDone
2799 */
2800 case SSL_SERVER_HELLO:
2801 ret = ssl_write_server_hello( ssl );
2802 break;
2803
2804 case SSL_SERVER_CERTIFICATE:
2805 ret = ssl_write_certificate( ssl );
2806 break;
2807
2808 case SSL_SERVER_KEY_EXCHANGE:
2809 ret = ssl_write_server_key_exchange( ssl );
2810 break;
2811
2812 case SSL_CERTIFICATE_REQUEST:
2813 ret = ssl_write_certificate_request( ssl );
2814 break;
2815
2816 case SSL_SERVER_HELLO_DONE:
2817 ret = ssl_write_server_hello_done( ssl );
2818 break;
2819
2820 /*
2821 * <== ( Certificate/Alert )
2822 * ClientKeyExchange
2823 * ( CertificateVerify )
2824 * ChangeCipherSpec
2825 * Finished
2826 */
2827 case SSL_CLIENT_CERTIFICATE:
2828 ret = ssl_parse_certificate( ssl );
2829 break;
2830
2831 case SSL_CLIENT_KEY_EXCHANGE:
2832 ret = ssl_parse_client_key_exchange( ssl );
2833 break;
2834
2835 case SSL_CERTIFICATE_VERIFY:
2836 ret = ssl_parse_certificate_verify( ssl );
2837 break;
2838
2839 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2840 ret = ssl_parse_change_cipher_spec( ssl );
2841 break;
2842
2843 case SSL_CLIENT_FINISHED:
2844 ret = ssl_parse_finished( ssl );
2845 break;
2846
2847 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002848 * ==> ( NewSessionTicket )
2849 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002850 * Finished
2851 */
2852 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002853#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002854 if( ssl->handshake->new_session_ticket != 0 )
2855 ret = ssl_write_new_session_ticket( ssl );
2856 else
Paul Bakkera503a632013-08-14 13:48:06 +02002857#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002858 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002859 break;
2860
2861 case SSL_SERVER_FINISHED:
2862 ret = ssl_write_finished( ssl );
2863 break;
2864
2865 case SSL_FLUSH_BUFFERS:
2866 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2867 ssl->state = SSL_HANDSHAKE_WRAPUP;
2868 break;
2869
2870 case SSL_HANDSHAKE_WRAPUP:
2871 ssl_handshake_wrapup( ssl );
2872 break;
2873
2874 default:
2875 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2876 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002877 }
2878
Paul Bakker5121ce52009-01-03 21:22:43 +00002879 return( ret );
2880}
Paul Bakker5121ce52009-01-03 21:22:43 +00002881#endif