blob: adf5a623fc03693650f8798d70892da7966ff72a [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;
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020065#if defined(POLARSSL_X509_PARSE_C)
66 size_t cert_len;
67#endif /* POLARSSL_X509_PARSE_C */
68
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
76#if defined(POLARSSL_X509_PARSE_C)
77 ((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;
95#endif /* POLARSSL_X509_PARSE_C */
96
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{
108 int ret;
109 const unsigned char *p = buf;
110 const unsigned char * const end = buf + len;
111#if defined(POLARSSL_X509_PARSE_C)
112 size_t cert_len;
113#endif /* POLARSSL_X509_PARSE_C */
114
115 if( p + sizeof( ssl_session ) > end )
116 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
117
118 memcpy( session, p, sizeof( ssl_session ) );
119 p += sizeof( ssl_session );
120
121#if defined(POLARSSL_X509_PARSE_C)
122 if( p + 3 > end )
123 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
124
125 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
126 p += 3;
127
128 if( cert_len == 0 )
129 {
130 session->peer_cert = NULL;
131 }
132 else
133 {
134 if( p + cert_len > end )
135 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
136
137 session->peer_cert = polarssl_malloc( cert_len );
138
139 if( session->peer_cert == NULL )
140 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
141
142 memset( session->peer_cert, 0, sizeof( x509_cert ) );
143
144 if( ( ret = x509parse_crt( session->peer_cert, p, cert_len ) ) != 0 )
145 {
146 polarssl_free( session->peer_cert );
147 free( session->peer_cert );
148 session->peer_cert = NULL;
149 return( ret );
150 }
151
152 p += cert_len;
153 }
154#endif /* POLARSSL_X509_PARSE_C */
155
156 if( p != end )
157 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
158
159 return( 0 );
160}
161
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200162/*
163 * Create session ticket, secured as recommended in RFC 5077 section 4:
164 *
165 * struct {
166 * opaque key_name[16];
167 * opaque iv[16];
168 * opaque encrypted_state<0..2^16-1>;
169 * opaque mac[32];
170 * } ticket;
171 *
172 * (the internal state structure differs, however).
173 */
174static int ssl_write_ticket( ssl_context *ssl, size_t *tlen )
175{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200176 int ret;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200177 unsigned char * const start = ssl->out_msg + 10;
178 unsigned char *p = start;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200179 unsigned char *state;
180 unsigned char iv[16];
181 size_t clear_len, enc_len, pad_len, i;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200182
Manuel Pégourié-Gonnard0a201712013-08-23 16:25:16 +0200183 *tlen = 0;
184
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200185 if( ssl->ticket_keys == NULL )
186 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
187
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200188 /* Write key name */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200189 memcpy( p, ssl->ticket_keys->key_name, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200190 p += 16;
191
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200192 /* Generate and write IV (with a copy for aes_crypt) */
193 if( ( ret = ssl->f_rng( ssl->p_rng, p, 16 ) ) != 0 )
194 return( ret );
195 memcpy( iv, p, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200196 p += 16;
197
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200198 /*
199 * Dump session state
200 *
201 * After the session state itself, we still need room for 16 bytes of
202 * padding and 32 bytes of MAC, so there's only so much room left
203 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200204 state = p + 2;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200205 if( ssl_save_session( ssl->session_negotiate, state,
206 SSL_MAX_CONTENT_LEN - (state - ssl->out_ctr) - 48,
207 &clear_len ) != 0 )
208 {
209 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
210 }
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200211 SSL_DEBUG_BUF( 3, "session ticket cleartext", state, clear_len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200212
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200213 /* Apply PKCS padding */
214 pad_len = 16 - clear_len % 16;
215 enc_len = clear_len + pad_len;
216 for( i = clear_len; i < enc_len; i++ )
217 state[i] = (unsigned char) pad_len;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200218
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200219 /* Encrypt */
220 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->enc, AES_ENCRYPT,
221 enc_len, iv, state, state ) ) != 0 )
222 {
223 return( ret );
224 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200225
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200226 /* Write length */
227 *p++ = (unsigned char)( ( enc_len >> 8 ) & 0xFF );
228 *p++ = (unsigned char)( ( enc_len ) & 0xFF );
229 p = state + enc_len;
230
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200231 /* Compute and write MAC( key_name + iv + enc_state_len + enc_state ) */
232 sha256_hmac( ssl->ticket_keys->mac_key, 16, start, p - start, p, 0 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200233 p += 32;
234
235 *tlen = p - start;
236
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200237 SSL_DEBUG_BUF( 3, "session ticket structure", start, *tlen );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200238
239 return( 0 );
240}
241
242/*
243 * Load session ticket (see ssl_write_ticket for structure)
244 */
245static int ssl_parse_ticket( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200246 unsigned char *buf,
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200247 size_t len )
248{
249 int ret;
250 ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200251 unsigned char *key_name = buf;
252 unsigned char *iv = buf + 16;
253 unsigned char *enc_len_p = iv + 16;
254 unsigned char *ticket = enc_len_p + 2;
255 unsigned char *mac;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200256 unsigned char computed_mac[16];
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200257 size_t enc_len, clear_len, i;
258 unsigned char pad_len;
259
260 SSL_DEBUG_BUF( 3, "session ticket structure", buf, len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200261
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200262 if( len < 34 || ssl->ticket_keys == NULL )
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200263 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
264
265 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
266 mac = ticket + enc_len;
267
268 if( len != enc_len + 66 )
269 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
270
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200271 /* Check name */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200272 if( memcmp( key_name, ssl->ticket_keys->key_name, 16 ) != 0 )
273 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200274
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200275 /* Check mac */
276 sha256_hmac( ssl->ticket_keys->mac_key, 16, buf, len - 32,
277 computed_mac, 0 );
278 ret = 0;
279 for( i = 0; i < 32; i++ )
280 if( mac[i] != computed_mac[i] )
281 ret = POLARSSL_ERR_SSL_INVALID_MAC;
282 if( ret != 0 )
283 return( ret );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200284
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200285 /* Decrypt */
286 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->dec, AES_DECRYPT,
287 enc_len, iv, ticket, ticket ) ) != 0 )
288 {
289 return( ret );
290 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200291
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200292 /* Check PKCS padding */
293 pad_len = ticket[enc_len - 1];
294
295 ret = 0;
296 for( i = 2; i < pad_len; i++ )
297 if( ticket[enc_len - i] != pad_len )
298 ret = POLARSSL_ERR_SSL_BAD_INPUT_DATA;
299 if( ret != 0 )
300 return( ret );
301
302 clear_len = enc_len - pad_len;
303
304 SSL_DEBUG_BUF( 3, "session ticket cleartext", ticket, clear_len );
305
306 /* Actually load session */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200307 if( ( ret = ssl_load_session( &session, ticket, clear_len ) ) != 0 )
308 {
309 SSL_DEBUG_MSG( 1, ( "failed to parse ticket content" ) );
310 memset( &session, 0, sizeof( ssl_session ) );
311 return( ret );
312 }
313
Paul Bakker606b4ba2013-08-14 16:52:14 +0200314#if defined(POLARSSL_HAVE_TIME)
315 /* Check if still valid */
316 if( (int) ( time( NULL) - session.start ) > ssl->ticket_lifetime )
317 {
318 SSL_DEBUG_MSG( 1, ( "session ticket expired" ) );
319 memset( &session, 0, sizeof( ssl_session ) );
320 return( POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED );
321 }
322#endif
323
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200324 /*
325 * Keep the session ID sent by the client, since we MUST send it back to
326 * inform him we're accepting the ticket (RFC 5077 section 3.4)
327 */
328 session.length = ssl->session_negotiate->length;
329 memcpy( &session.id, ssl->session_negotiate->id, session.length );
330
331 ssl_session_free( ssl->session_negotiate );
332 memcpy( ssl->session_negotiate, &session, sizeof( ssl_session ) );
333 memset( &session, 0, sizeof( ssl_session ) );
334
335 return( 0 );
336}
Paul Bakkera503a632013-08-14 13:48:06 +0200337#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200338
Paul Bakker0be444a2013-08-27 21:55:01 +0200339#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +0000340static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000341 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000342 size_t len )
343{
344 int ret;
345 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000346 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000347
348 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
349 if( servername_list_size + 2 != len )
350 {
351 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
352 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
353 }
354
355 p = buf + 2;
356 while( servername_list_size > 0 )
357 {
358 hostname_len = ( ( p[1] << 8 ) | p[2] );
359 if( hostname_len + 3 > servername_list_size )
360 {
361 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
362 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
363 }
364
365 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
366 {
367 ret = ssl->f_sni( ssl->p_sni, ssl, p + 3, hostname_len );
368 if( ret != 0 )
369 {
370 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
371 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
372 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
373 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000374 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000375 }
376
377 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000378 p += hostname_len + 3;
379 }
380
381 if( servername_list_size != 0 )
382 {
383 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
384 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000385 }
386
387 return( 0 );
388}
Paul Bakker0be444a2013-08-27 21:55:01 +0200389#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000390
Paul Bakker48916f92012-09-16 19:57:18 +0000391static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000392 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000393 size_t len )
394{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000395 int ret;
396
Paul Bakker48916f92012-09-16 19:57:18 +0000397 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
398 {
399 if( len != 1 || buf[0] != 0x0 )
400 {
401 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000402
403 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
404 return( ret );
405
Paul Bakker48916f92012-09-16 19:57:18 +0000406 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
407 }
408
409 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
410 }
411 else
412 {
413 if( len != 1 + ssl->verify_data_len ||
414 buf[0] != ssl->verify_data_len ||
415 memcmp( buf + 1, ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
416 {
417 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000418
419 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
420 return( ret );
421
Paul Bakker48916f92012-09-16 19:57:18 +0000422 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
423 }
424 }
425
426 return( 0 );
427}
428
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200429#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +0000430static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
431 const unsigned char *buf,
432 size_t len )
433{
434 size_t sig_alg_list_size;
435 const unsigned char *p;
436
437 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
438 if( sig_alg_list_size + 2 != len ||
439 sig_alg_list_size %2 != 0 )
440 {
441 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
442 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
443 }
444
445 p = buf + 2;
446 while( sig_alg_list_size > 0 )
447 {
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200448 /*
449 * For now, just ignore signature algorithm and rely on offered
450 * ciphersuites only. To be fixed later.
451 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200452#if defined(POLARSSL_SHA512_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000453 if( p[0] == SSL_HASH_SHA512 )
454 {
455 ssl->handshake->sig_alg = SSL_HASH_SHA512;
456 break;
457 }
458 if( p[0] == SSL_HASH_SHA384 )
459 {
460 ssl->handshake->sig_alg = SSL_HASH_SHA384;
461 break;
462 }
463#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200464#if defined(POLARSSL_SHA256_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000465 if( p[0] == SSL_HASH_SHA256 )
466 {
467 ssl->handshake->sig_alg = SSL_HASH_SHA256;
468 break;
469 }
470 if( p[0] == SSL_HASH_SHA224 )
471 {
472 ssl->handshake->sig_alg = SSL_HASH_SHA224;
473 break;
474 }
475#endif
476 if( p[0] == SSL_HASH_SHA1 )
477 {
478 ssl->handshake->sig_alg = SSL_HASH_SHA1;
479 break;
480 }
481 if( p[0] == SSL_HASH_MD5 )
482 {
483 ssl->handshake->sig_alg = SSL_HASH_MD5;
484 break;
485 }
486
487 sig_alg_list_size -= 2;
488 p += 2;
489 }
490
491 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
492 ssl->handshake->sig_alg ) );
493
494 return( 0 );
495}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200496#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker23f36802012-09-28 14:15:14 +0000497
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200498#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200499static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
500 const unsigned char *buf,
501 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100502{
503 size_t list_size;
504 const unsigned char *p;
505
506 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
507 if( list_size + 2 != len ||
508 list_size % 2 != 0 )
509 {
510 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
511 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
512 }
513
514 p = buf + 2;
515 while( list_size > 0 )
516 {
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200517#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
518 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP192R1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100519 {
520 ssl->handshake->ec_curve = p[1];
521 return( 0 );
522 }
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200523#endif
524#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
525 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP224R1 )
526 {
527 ssl->handshake->ec_curve = p[1];
528 return( 0 );
529 }
530#endif
531#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
532 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP256R1 )
533 {
534 ssl->handshake->ec_curve = p[1];
535 return( 0 );
536 }
537#endif
538#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
539 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP384R1 )
540 {
541 ssl->handshake->ec_curve = p[1];
542 return( 0 );
543 }
544#endif
545#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
546 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP521R1 )
547 {
548 ssl->handshake->ec_curve = p[1];
549 return( 0 );
550 }
551#endif
Paul Bakker41c83d32013-03-20 14:39:14 +0100552
553 list_size -= 2;
554 p += 2;
555 }
556
557 return( 0 );
558}
559
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200560static int ssl_parse_supported_point_formats( ssl_context *ssl,
561 const unsigned char *buf,
562 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100563{
564 size_t list_size;
565 const unsigned char *p;
566
567 list_size = buf[0];
568 if( list_size + 1 != len )
569 {
570 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
571 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
572 }
573
574 p = buf + 2;
575 while( list_size > 0 )
576 {
577 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
578 p[0] == POLARSSL_ECP_PF_COMPRESSED )
579 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200580 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200581 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100582 return( 0 );
583 }
584
585 list_size--;
586 p++;
587 }
588
589 return( 0 );
590}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200591#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100592
Paul Bakker05decb22013-08-15 13:33:48 +0200593#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200594static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
595 const unsigned char *buf,
596 size_t len )
597{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200598 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200599 {
600 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
601 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
602 }
603
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200604 ssl->session_negotiate->mfl_code = buf[0];
605
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200606 return( 0 );
607}
Paul Bakker05decb22013-08-15 13:33:48 +0200608#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200609
Paul Bakker1f2bc622013-08-15 13:45:55 +0200610#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200611static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
612 const unsigned char *buf,
613 size_t len )
614{
615 if( len != 0 )
616 {
617 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
618 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
619 }
620
621 ((void) buf);
622
623 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
624
625 return( 0 );
626}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200627#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200628
Paul Bakkera503a632013-08-14 13:48:06 +0200629#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200630static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200631 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200632 size_t len )
633{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200634 int ret;
635
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200636 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
637 return( 0 );
638
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200639 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200640 ssl->handshake->new_session_ticket = 1;
641
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200642 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
643
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200644 if( len == 0 )
645 return( 0 );
646
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200647 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
648 {
649 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
650 return( 0 );
651 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200652
653 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200654 * Failures are ok: just ignore the ticket and proceed.
655 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200656 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
657 {
658 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200659 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200660 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200661
662 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
663
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200664 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200665
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200666 /* Don't send a new ticket after all, this one is OK */
667 ssl->handshake->new_session_ticket = 0;
668
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200669 return( 0 );
670}
Paul Bakkera503a632013-08-14 13:48:06 +0200671#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200672
Paul Bakker78a8c712013-03-06 17:01:52 +0100673#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
674static int ssl_parse_client_hello_v2( ssl_context *ssl )
675{
676 int ret;
677 unsigned int i, j;
678 size_t n;
679 unsigned int ciph_len, sess_len, chal_len;
680 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200681 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200682 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100683
684 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
685
686 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
687 {
688 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
689
690 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
691 return( ret );
692
693 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
694 }
695
696 buf = ssl->in_hdr;
697
698 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
699
700 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
701 buf[2] ) );
702 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
703 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
704 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
705 buf[3], buf[4] ) );
706
707 /*
708 * SSLv2 Client Hello
709 *
710 * Record layer:
711 * 0 . 1 message length
712 *
713 * SSL layer:
714 * 2 . 2 message type
715 * 3 . 4 protocol version
716 */
717 if( buf[2] != SSL_HS_CLIENT_HELLO ||
718 buf[3] != SSL_MAJOR_VERSION_3 )
719 {
720 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
721 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
722 }
723
724 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
725
726 if( n < 17 || n > 512 )
727 {
728 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
729 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
730 }
731
732 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200733 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
734 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100735
736 if( ssl->minor_ver < ssl->min_minor_ver )
737 {
738 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
739 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
740 ssl->min_major_ver, ssl->min_minor_ver ) );
741
742 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
743 SSL_ALERT_MSG_PROTOCOL_VERSION );
744 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
745 }
746
Paul Bakker2fbefde2013-06-29 16:01:15 +0200747 ssl->handshake->max_major_ver = buf[3];
748 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100749
750 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
751 {
752 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
753 return( ret );
754 }
755
756 ssl->handshake->update_checksum( ssl, buf + 2, n );
757
758 buf = ssl->in_msg;
759 n = ssl->in_left - 5;
760
761 /*
762 * 0 . 1 ciphersuitelist length
763 * 2 . 3 session id length
764 * 4 . 5 challenge length
765 * 6 . .. ciphersuitelist
766 * .. . .. session id
767 * .. . .. challenge
768 */
769 SSL_DEBUG_BUF( 4, "record contents", buf, n );
770
771 ciph_len = ( buf[0] << 8 ) | buf[1];
772 sess_len = ( buf[2] << 8 ) | buf[3];
773 chal_len = ( buf[4] << 8 ) | buf[5];
774
775 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
776 ciph_len, sess_len, chal_len ) );
777
778 /*
779 * Make sure each parameter length is valid
780 */
781 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
782 {
783 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
784 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
785 }
786
787 if( sess_len > 32 )
788 {
789 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
790 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
791 }
792
793 if( chal_len < 8 || chal_len > 32 )
794 {
795 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
796 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
797 }
798
799 if( n != 6 + ciph_len + sess_len + chal_len )
800 {
801 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
802 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
803 }
804
805 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
806 buf + 6, ciph_len );
807 SSL_DEBUG_BUF( 3, "client hello, session id",
808 buf + 6 + ciph_len, sess_len );
809 SSL_DEBUG_BUF( 3, "client hello, challenge",
810 buf + 6 + ciph_len + sess_len, chal_len );
811
812 p = buf + 6 + ciph_len;
813 ssl->session_negotiate->length = sess_len;
814 memset( ssl->session_negotiate->id, 0, sizeof( ssl->session_negotiate->id ) );
815 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
816
817 p += sess_len;
818 memset( ssl->handshake->randbytes, 0, 64 );
819 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
820
821 /*
822 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
823 */
824 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
825 {
826 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
827 {
828 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
829 if( ssl->renegotiation == SSL_RENEGOTIATION )
830 {
831 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
832
833 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
834 return( ret );
835
836 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
837 }
838 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
839 break;
840 }
841 }
842
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200843 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
844 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +0100845 {
846 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
847 {
Paul Bakker41c83d32013-03-20 14:39:14 +0100848 // Only allow non-ECC ciphersuites as we do not have extensions
849 //
Paul Bakker59c28a22013-06-29 15:33:42 +0200850 if( p[0] == 0 && p[1] == 0 &&
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200851 ( ( ciphersuites[i] >> 8 ) & 0xFF ) == 0 &&
852 p[2] == ( ciphersuites[i] & 0xFF ) )
Paul Bakker59c28a22013-06-29 15:33:42 +0200853 {
854 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
855
856 if( ciphersuite_info == NULL )
857 {
858 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %02x not found",
859 ciphersuites[i] ) );
860 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
861 }
862
Paul Bakker2fbefde2013-06-29 16:01:15 +0200863 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
864 ciphersuite_info->max_minor_ver < ssl->minor_ver )
865 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +0200866
Paul Bakker78a8c712013-03-06 17:01:52 +0100867 goto have_ciphersuite_v2;
Paul Bakker59c28a22013-06-29 15:33:42 +0200868 }
Paul Bakker78a8c712013-03-06 17:01:52 +0100869 }
870 }
871
872 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
873
874 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
875
876have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200877 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +0200878 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +0100879 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +0100880
881 /*
882 * SSLv2 Client Hello relevant renegotiation security checks
883 */
884 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
885 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
886 {
887 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
888
889 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
890 return( ret );
891
892 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
893 }
894
895 ssl->in_left = 0;
896 ssl->state++;
897
898 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
899
900 return( 0 );
901}
902#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
903
Paul Bakker5121ce52009-01-03 21:22:43 +0000904static int ssl_parse_client_hello( ssl_context *ssl )
905{
Paul Bakker23986e52011-04-24 08:57:21 +0000906 int ret;
907 unsigned int i, j;
908 size_t n;
909 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +0000910 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +0000911 unsigned int ext_len = 0;
912 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000913 int renegotiation_info_seen = 0;
914 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200915 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +0100916 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +0200917 pk_type_t pk_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +0000918
919 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
920
Paul Bakker48916f92012-09-16 19:57:18 +0000921 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
922 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000923 {
924 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
925 return( ret );
926 }
927
928 buf = ssl->in_hdr;
929
Paul Bakker78a8c712013-03-06 17:01:52 +0100930#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
931 if( ( buf[0] & 0x80 ) != 0 )
932 return ssl_parse_client_hello_v2( ssl );
933#endif
934
Paul Bakkerec636f32012-09-09 19:17:02 +0000935 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
936
937 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
938 buf[0] ) );
939 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
940 ( buf[3] << 8 ) | buf[4] ) );
941 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
942 buf[1], buf[2] ) );
943
944 /*
945 * SSLv3 Client Hello
946 *
947 * Record layer:
948 * 0 . 0 message type
949 * 1 . 2 protocol version
950 * 3 . 4 message length
951 */
952 if( buf[0] != SSL_MSG_HANDSHAKE ||
953 buf[1] != SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000954 {
Paul Bakkerec636f32012-09-09 19:17:02 +0000955 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
956 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
957 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000958
Paul Bakkerec636f32012-09-09 19:17:02 +0000959 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000960
Manuel Pégourié-Gonnard72882b22013-08-02 13:36:00 +0200961 if( n < 45 || n > 2048 )
Paul Bakkerec636f32012-09-09 19:17:02 +0000962 {
963 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
964 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
965 }
966
Paul Bakker48916f92012-09-16 19:57:18 +0000967 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
968 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +0000969 {
970 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
971 return( ret );
972 }
973
974 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +0000975 if( !ssl->renegotiation )
976 n = ssl->in_left - 5;
977 else
978 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +0000979
Paul Bakker48916f92012-09-16 19:57:18 +0000980 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +0000981
982 /*
983 * SSL layer:
984 * 0 . 0 handshake type
985 * 1 . 3 handshake length
986 * 4 . 5 protocol version
987 * 6 . 9 UNIX time()
988 * 10 . 37 random bytes
989 * 38 . 38 session id length
990 * 39 . 38+x session id
991 * 39+x . 40+x ciphersuitelist length
992 * 41+x . .. ciphersuitelist
993 * .. . .. compression alg.
994 * .. . .. extensions
995 */
996 SSL_DEBUG_BUF( 4, "record contents", buf, n );
997
998 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
999 buf[0] ) );
1000 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1001 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1002 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1003 buf[4], buf[5] ) );
1004
1005 /*
1006 * Check the handshake type and protocol version
1007 */
1008 if( buf[0] != SSL_HS_CLIENT_HELLO ||
1009 buf[4] != SSL_MAJOR_VERSION_3 )
1010 {
1011 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1012 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1013 }
1014
1015 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +02001016 ssl->minor_ver = ( buf[5] <= ssl->max_minor_ver )
1017 ? buf[5] : ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001018
Paul Bakker1d29fb52012-09-28 13:28:45 +00001019 if( ssl->minor_ver < ssl->min_minor_ver )
1020 {
1021 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
1022 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001023 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001024
1025 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1026 SSL_ALERT_MSG_PROTOCOL_VERSION );
1027
1028 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1029 }
1030
Paul Bakker2fbefde2013-06-29 16:01:15 +02001031 ssl->handshake->max_major_ver = buf[4];
1032 ssl->handshake->max_minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001033
Paul Bakker48916f92012-09-16 19:57:18 +00001034 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001035
1036 /*
1037 * Check the handshake message length
1038 */
1039 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1040 {
1041 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1042 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1043 }
1044
1045 /*
1046 * Check the session length
1047 */
1048 sess_len = buf[38];
1049
1050 if( sess_len > 32 )
1051 {
1052 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1053 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1054 }
1055
Paul Bakker48916f92012-09-16 19:57:18 +00001056 ssl->session_negotiate->length = sess_len;
1057 memset( ssl->session_negotiate->id, 0,
1058 sizeof( ssl->session_negotiate->id ) );
1059 memcpy( ssl->session_negotiate->id, buf + 39,
1060 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001061
1062 /*
1063 * Check the ciphersuitelist length
1064 */
1065 ciph_len = ( buf[39 + sess_len] << 8 )
1066 | ( buf[40 + sess_len] );
1067
1068 if( ciph_len < 2 || ciph_len > 256 || ( ciph_len % 2 ) != 0 )
1069 {
1070 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1071 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1072 }
1073
1074 /*
1075 * Check the compression algorithms length
1076 */
1077 comp_len = buf[41 + sess_len + ciph_len];
1078
1079 if( comp_len < 1 || comp_len > 16 )
1080 {
1081 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1082 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1083 }
1084
Paul Bakker48916f92012-09-16 19:57:18 +00001085 /*
1086 * Check the extension length
1087 */
1088 if( n > 42 + sess_len + ciph_len + comp_len )
1089 {
1090 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1091 | ( buf[43 + sess_len + ciph_len + comp_len] );
1092
1093 if( ( ext_len > 0 && ext_len < 4 ) ||
1094 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1095 {
1096 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1097 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1098 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1099 }
1100 }
1101
1102 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001103#if defined(POLARSSL_ZLIB_SUPPORT)
1104 for( i = 0; i < comp_len; ++i )
1105 {
Paul Bakker48916f92012-09-16 19:57:18 +00001106 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001107 {
Paul Bakker48916f92012-09-16 19:57:18 +00001108 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001109 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001110 }
1111 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001112#endif
1113
Paul Bakkerec636f32012-09-09 19:17:02 +00001114 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1115 buf + 6, 32 );
1116 SSL_DEBUG_BUF( 3, "client hello, session id",
1117 buf + 38, sess_len );
1118 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1119 buf + 41 + sess_len, ciph_len );
1120 SSL_DEBUG_BUF( 3, "client hello, compression",
1121 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001122
Paul Bakkerec636f32012-09-09 19:17:02 +00001123 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001124 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1125 */
1126 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1127 {
1128 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1129 {
1130 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1131 if( ssl->renegotiation == SSL_RENEGOTIATION )
1132 {
1133 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001134
1135 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1136 return( ret );
1137
Paul Bakker48916f92012-09-16 19:57:18 +00001138 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1139 }
1140 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1141 break;
1142 }
1143 }
1144
Paul Bakker48916f92012-09-16 19:57:18 +00001145 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001146
1147 while( ext_len )
1148 {
1149 unsigned int ext_id = ( ( ext[0] << 8 )
1150 | ( ext[1] ) );
1151 unsigned int ext_size = ( ( ext[2] << 8 )
1152 | ( ext[3] ) );
1153
1154 if( ext_size + 4 > ext_len )
1155 {
1156 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1157 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1158 }
1159 switch( ext_id )
1160 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001161#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001162 case TLS_EXT_SERVERNAME:
1163 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1164 if( ssl->f_sni == NULL )
1165 break;
1166
1167 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1168 if( ret != 0 )
1169 return( ret );
1170 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001171#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001172
Paul Bakker48916f92012-09-16 19:57:18 +00001173 case TLS_EXT_RENEGOTIATION_INFO:
1174 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1175 renegotiation_info_seen = 1;
1176
Paul Bakker23f36802012-09-28 14:15:14 +00001177 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1178 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001179 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001180 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001181
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001182#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001183 case TLS_EXT_SIG_ALG:
1184 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1185 if( ssl->renegotiation == SSL_RENEGOTIATION )
1186 break;
1187
1188 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1189 if( ret != 0 )
1190 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001191 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001192#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001193
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001194#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001195 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1196 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1197
1198 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1199 if( ret != 0 )
1200 return( ret );
1201 break;
1202
1203 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1204 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1205
1206 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1207 if( ret != 0 )
1208 return( ret );
1209 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001210#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001211
Paul Bakker05decb22013-08-15 13:33:48 +02001212#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001213 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1214 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1215
1216 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1217 if( ret != 0 )
1218 return( ret );
1219 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001220#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001221
Paul Bakker1f2bc622013-08-15 13:45:55 +02001222#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001223 case TLS_EXT_TRUNCATED_HMAC:
1224 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1225
1226 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1227 if( ret != 0 )
1228 return( ret );
1229 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001230#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001231
Paul Bakkera503a632013-08-14 13:48:06 +02001232#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001233 case TLS_EXT_SESSION_TICKET:
1234 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1235
1236 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1237 if( ret != 0 )
1238 return( ret );
1239 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001240#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001241
Paul Bakker48916f92012-09-16 19:57:18 +00001242 default:
1243 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1244 ext_id ) );
1245 }
1246
1247 ext_len -= 4 + ext_size;
1248 ext += 4 + ext_size;
1249
1250 if( ext_len > 0 && ext_len < 4 )
1251 {
1252 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1253 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1254 }
1255 }
1256
1257 /*
1258 * Renegotiation security checks
1259 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001260 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1261 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1262 {
1263 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1264 handshake_failure = 1;
1265 }
1266 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1267 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1268 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001269 {
1270 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001271 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001272 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001273 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1274 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1275 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001276 {
1277 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001278 handshake_failure = 1;
1279 }
1280 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1281 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1282 renegotiation_info_seen == 1 )
1283 {
1284 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1285 handshake_failure = 1;
1286 }
1287
1288 if( handshake_failure == 1 )
1289 {
1290 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1291 return( ret );
1292
Paul Bakker48916f92012-09-16 19:57:18 +00001293 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1294 }
Paul Bakker380da532012-04-18 16:10:25 +00001295
Paul Bakker41c83d32013-03-20 14:39:14 +01001296 /*
1297 * Search for a matching ciphersuite
1298 * (At the end because we need information from the EC-based extensions)
1299 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001300 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
1301 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001302 {
1303 for( j = 0, p = buf + 41 + sess_len; j < ciph_len;
1304 j += 2, p += 2 )
1305 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001306 if( p[0] == ( ( ciphersuites[i] >> 8 ) & 0xFF ) &&
1307 p[1] == ( ( ciphersuites[i] ) & 0xFF ) )
Paul Bakker41c83d32013-03-20 14:39:14 +01001308 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001309 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
Paul Bakker41c83d32013-03-20 14:39:14 +01001310
1311 if( ciphersuite_info == NULL )
1312 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001313 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found",
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001314 ciphersuites[i] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001315 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1316 }
1317
Paul Bakker2fbefde2013-06-29 16:01:15 +02001318 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
1319 ciphersuite_info->max_minor_ver < ssl->minor_ver )
1320 continue;
1321
Paul Bakker5fd49172013-08-19 13:29:26 +02001322#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001323 if( ( ciphersuite_info->flags & POLARSSL_CIPHERSUITE_EC ) &&
1324 ssl->handshake->ec_curve == 0 )
1325 continue;
Paul Bakker5fd49172013-08-19 13:29:26 +02001326#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001327
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001328 /* If ciphersuite requires us to have a private key of a
1329 * certain type, make sure we do */
1330 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
1331 if( pk_alg != POLARSSL_PK_NONE &&
1332 ( ssl->pk_key == NULL ||
1333 ! pk_can_do( ssl->pk_key, pk_alg ) ) )
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001334 continue;
1335
Paul Bakker41c83d32013-03-20 14:39:14 +01001336 goto have_ciphersuite;
1337 }
1338 }
1339 }
1340
1341 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1342
1343 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1344 return( ret );
1345
1346 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1347
1348have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001349 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001350 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1351 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1352
Paul Bakker5121ce52009-01-03 21:22:43 +00001353 ssl->in_left = 0;
1354 ssl->state++;
1355
1356 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1357
1358 return( 0 );
1359}
1360
Paul Bakker1f2bc622013-08-15 13:45:55 +02001361#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001362static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1363 unsigned char *buf,
1364 size_t *olen )
1365{
1366 unsigned char *p = buf;
1367
1368 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1369 {
1370 *olen = 0;
1371 return;
1372 }
1373
1374 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1375
1376 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1377 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1378
1379 *p++ = 0x00;
1380 *p++ = 0x00;
1381
1382 *olen = 4;
1383}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001384#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001385
Paul Bakkera503a632013-08-14 13:48:06 +02001386#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001387static void ssl_write_session_ticket_ext( ssl_context *ssl,
1388 unsigned char *buf,
1389 size_t *olen )
1390{
1391 unsigned char *p = buf;
1392
1393 if( ssl->handshake->new_session_ticket == 0 )
1394 {
1395 *olen = 0;
1396 return;
1397 }
1398
1399 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1400
1401 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1402 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1403
1404 *p++ = 0x00;
1405 *p++ = 0x00;
1406
1407 *olen = 4;
1408}
Paul Bakkera503a632013-08-14 13:48:06 +02001409#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001410
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001411static void ssl_write_renegotiation_ext( ssl_context *ssl,
1412 unsigned char *buf,
1413 size_t *olen )
1414{
1415 unsigned char *p = buf;
1416
1417 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1418 {
1419 *olen = 0;
1420 return;
1421 }
1422
1423 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1424
1425 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1426 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1427
1428 *p++ = 0x00;
1429 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1430 *p++ = ssl->verify_data_len * 2 & 0xFF;
1431
1432 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1433 p += ssl->verify_data_len;
1434 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1435 p += ssl->verify_data_len;
1436
1437 *olen = 5 + ssl->verify_data_len * 2;
1438}
1439
Paul Bakker05decb22013-08-15 13:33:48 +02001440#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001441static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1442 unsigned char *buf,
1443 size_t *olen )
1444{
1445 unsigned char *p = buf;
1446
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001447 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1448 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001449 *olen = 0;
1450 return;
1451 }
1452
1453 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1454
1455 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1456 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1457
1458 *p++ = 0x00;
1459 *p++ = 1;
1460
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001461 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001462
1463 *olen = 5;
1464}
Paul Bakker05decb22013-08-15 13:33:48 +02001465#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001466
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001467#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001468static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1469 unsigned char *buf,
1470 size_t *olen )
1471{
1472 unsigned char *p = buf;
1473 ((void) ssl);
1474
1475 *olen = 0;
1476
1477 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1478
1479 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1480 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1481
1482 *p++ = 0x00;
1483 *p++ = 2;
1484
1485 *p++ = 1;
1486 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1487
1488 *olen = 6;
1489}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001490#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001491
Paul Bakker5121ce52009-01-03 21:22:43 +00001492static int ssl_write_server_hello( ssl_context *ssl )
1493{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001494#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001495 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001496#endif
Paul Bakkera3d195c2011-11-27 21:07:34 +00001497 int ret, n;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001498 size_t olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001499 unsigned char *buf, *p;
1500
1501 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1502
1503 /*
1504 * 0 . 0 handshake type
1505 * 1 . 3 handshake length
1506 * 4 . 5 protocol version
1507 * 6 . 9 UNIX time()
1508 * 10 . 37 random bytes
1509 */
1510 buf = ssl->out_msg;
1511 p = buf + 4;
1512
1513 *p++ = (unsigned char) ssl->major_ver;
1514 *p++ = (unsigned char) ssl->minor_ver;
1515
1516 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1517 buf[4], buf[5] ) );
1518
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001519#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001520 t = time( NULL );
1521 *p++ = (unsigned char)( t >> 24 );
1522 *p++ = (unsigned char)( t >> 16 );
1523 *p++ = (unsigned char)( t >> 8 );
1524 *p++ = (unsigned char)( t );
1525
1526 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001527#else
1528 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1529 return( ret );
1530
1531 p += 4;
1532#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001533
Paul Bakkera3d195c2011-11-27 21:07:34 +00001534 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1535 return( ret );
1536
1537 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001538
Paul Bakker48916f92012-09-16 19:57:18 +00001539 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001540
1541 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1542
1543 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001544 * Resume is 0 by default, see ssl_handshake_init().
1545 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1546 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001547 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001548 if( ssl->handshake->resume == 0 &&
1549 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001550 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001551 ssl->f_get_cache != NULL &&
1552 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1553 {
1554 ssl->handshake->resume = 1;
1555 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001556
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001557 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001558 {
1559 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001560 * New session, create a new session id,
1561 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001562 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001563 ssl->state++;
1564
Paul Bakkera503a632013-08-14 13:48:06 +02001565#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001566 if( ssl->handshake->new_session_ticket == 0 )
1567 {
1568 ssl->session_negotiate->length = n = 32;
1569 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001570 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001571 return( ret );
1572 }
1573 else
1574 {
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001575 ssl->session_negotiate->length = n = 0;
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001576 memset( ssl->session_negotiate->id, 0, 32 );
1577 }
Paul Bakkera503a632013-08-14 13:48:06 +02001578#else
1579 ssl->session_negotiate->length = n = 32;
1580 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
1581 n ) ) != 0 )
1582 return( ret );
1583#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001584 }
1585 else
1586 {
1587 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001588 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001589 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001590 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001591 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001592
1593 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1594 {
1595 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1596 return( ret );
1597 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001598 }
1599
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001600 /*
1601 * 38 . 38 session id length
1602 * 39 . 38+n session id
1603 * 39+n . 40+n chosen ciphersuite
1604 * 41+n . 41+n chosen compression alg.
1605 * 42+n . 43+n extensions length
1606 * 44+n . 43+n+m extensions
1607 */
1608 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001609 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1610 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001611
1612 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1613 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1614 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001615 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001616
Paul Bakker48916f92012-09-16 19:57:18 +00001617 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1618 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1619 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001620
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001621 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: 0x%04X",
Paul Bakker48916f92012-09-16 19:57:18 +00001622 ssl->session_negotiate->ciphersuite ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001623 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001624 ssl->session_negotiate->compression ) );
1625
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001626 /*
1627 * First write extensions, then the total length
1628 */
1629 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1630 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001631
Paul Bakker05decb22013-08-15 13:33:48 +02001632#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001633 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1634 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001635#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001636
Paul Bakker1f2bc622013-08-15 13:45:55 +02001637#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001638 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1639 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001640#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001641
Paul Bakkera503a632013-08-14 13:48:06 +02001642#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001643 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1644 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001645#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001646
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001647#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001648 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1649 ext_len += olen;
1650#endif
1651
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001652 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001653
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001654 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1655 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1656 p += ext_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001657
1658 ssl->out_msglen = p - buf;
1659 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1660 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1661
1662 ret = ssl_write_record( ssl );
1663
1664 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1665
1666 return( ret );
1667}
1668
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001669#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1670 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
1671 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001672static int ssl_write_certificate_request( ssl_context *ssl )
1673{
Paul Bakkered27a042013-04-18 22:46:23 +02001674 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1675 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001676
1677 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1678
1679 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1680 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1681 {
1682 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1683 ssl->state++;
1684 return( 0 );
1685 }
1686
1687 return( ret );
1688}
1689#else
1690static int ssl_write_certificate_request( ssl_context *ssl )
1691{
1692 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1693 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001694 size_t dn_size, total_dn_size; /* excluding length bytes */
1695 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00001696 unsigned char *buf, *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001697 const x509_cert *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001698
1699 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1700
1701 ssl->state++;
1702
Paul Bakkerfbb17802013-04-17 19:10:21 +02001703 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001704 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001705 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001706 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001707 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001708 return( 0 );
1709 }
1710
1711 /*
1712 * 0 . 0 handshake type
1713 * 1 . 3 handshake length
1714 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01001715 * 5 .. m-1 cert types
1716 * m .. m+1 sig alg length (TLS 1.2 only)
1717 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001718 * n .. n+1 length of all DNs
1719 * n+2 .. n+3 length of DN 1
1720 * n+4 .. ... Distinguished Name #1
1721 * ... .. ... length of DN 2, etc.
1722 */
1723 buf = ssl->out_msg;
1724 p = buf + 4;
1725
1726 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001727 * Supported certificate types
1728 *
1729 * ClientCertificateType certificate_types<1..2^8-1>;
1730 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00001731 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001732 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01001733
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001734#if defined(POLARSSL_RSA_C)
1735 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
1736#endif
1737#if defined(POLARSSL_ECDSA_C)
1738 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
1739#endif
1740
1741 p[0] = ct_len++;
1742 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01001743
Paul Bakker577e0062013-08-28 11:57:20 +02001744 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001745#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01001746 /*
1747 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01001748 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001749 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
1750 *
1751 * struct {
1752 * HashAlgorithm hash;
1753 * SignatureAlgorithm signature;
1754 * } SignatureAndHashAlgorithm;
1755 *
1756 * enum { (255) } HashAlgorithm;
1757 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01001758 */
Paul Bakker21dca692013-01-03 11:41:08 +01001759 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01001760 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001761 /*
1762 * Only use current running hash algorithm that is already required
1763 * for requested ciphersuite.
1764 */
Paul Bakker926af752012-11-23 13:38:07 +01001765 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
1766
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001767 if( ssl->transform_negotiate->ciphersuite_info->mac ==
1768 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01001769 {
1770 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
1771 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02001772
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001773 /*
1774 * Supported signature algorithms
1775 */
1776#if defined(POLARSSL_RSA_C)
1777 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1778 p[2 + sa_len++] = SSL_SIG_RSA;
1779#endif
1780#if defined(POLARSSL_ECDSA_C)
1781 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1782 p[2 + sa_len++] = SSL_SIG_ECDSA;
1783#endif
Paul Bakker926af752012-11-23 13:38:07 +01001784
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001785 p[0] = (unsigned char)( sa_len >> 8 );
1786 p[1] = (unsigned char)( sa_len );
1787 sa_len += 2;
1788 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01001789 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001790#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001791
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001792 /*
1793 * DistinguishedName certificate_authorities<0..2^16-1>;
1794 * opaque DistinguishedName<1..2^16-1>;
1795 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001796 p += 2;
1797 crt = ssl->ca_chain;
1798
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001799 total_dn_size = 0;
Paul Bakker29087132010-03-21 21:03:34 +00001800 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001801 {
1802 if( p - buf > 4096 )
1803 break;
1804
Paul Bakker926af752012-11-23 13:38:07 +01001805 dn_size = crt->subject_raw.len;
1806 *p++ = (unsigned char)( dn_size >> 8 );
1807 *p++ = (unsigned char)( dn_size );
1808 memcpy( p, crt->subject_raw.p, dn_size );
1809 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001810
Paul Bakker926af752012-11-23 13:38:07 +01001811 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
1812
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001813 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01001814 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00001815 }
1816
Paul Bakker926af752012-11-23 13:38:07 +01001817 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001818 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1819 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001820 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
1821 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001822
1823 ret = ssl_write_record( ssl );
1824
1825 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
1826
1827 return( ret );
1828}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001829#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
1830 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
1831 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001832
Paul Bakker41c83d32013-03-20 14:39:14 +01001833static int ssl_write_server_key_exchange( ssl_context *ssl )
1834{
Paul Bakker23986e52011-04-24 08:57:21 +00001835 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001836 size_t n = 0, len;
Paul Bakker23f36802012-09-28 14:15:14 +00001837 unsigned char hash[64];
Paul Bakkerc70b9822013-04-07 22:00:46 +02001838 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker35a7fe52012-10-31 09:07:14 +00001839 unsigned int hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001840 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001841 unsigned char *dig_signed = p;
1842 size_t dig_signed_len = 0;
Paul Bakker41c83d32013-03-20 14:39:14 +01001843
1844 const ssl_ciphersuite_t *ciphersuite_info;
1845 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001846
1847 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
1848
Paul Bakker41c83d32013-03-20 14:39:14 +01001849 if( ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_RSA &&
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001850 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_ECDHE_RSA &&
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001851 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA &&
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001852 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001853 {
1854 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
1855 ssl->state++;
1856 return( 0 );
1857 }
1858
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001859#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1860 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1861 {
1862 /* TODO: Support identity hints */
1863 *(p++) = 0x00;
1864 *(p++) = 0x00;
1865
1866 n += 2;
1867 }
1868#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1869
1870#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1871 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1872 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1873 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00001874 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001875 /*
1876 * Ephemeral DH parameters:
1877 *
1878 * struct {
1879 * opaque dh_p<1..2^16-1>;
1880 * opaque dh_g<1..2^16-1>;
1881 * opaque dh_Ys<1..2^16-1>;
1882 * } ServerDHParams;
1883 */
1884 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
1885 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
1886 {
1887 SSL_DEBUG_RET( 1, "mpi_copy", ret );
1888 return( ret );
1889 }
Paul Bakker48916f92012-09-16 19:57:18 +00001890
Paul Bakker41c83d32013-03-20 14:39:14 +01001891 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
1892 mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001893 p,
1894 &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001895 {
1896 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
1897 return( ret );
1898 }
1899
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001900 dig_signed = p;
1901 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001902
1903 p += len;
1904 n += len;
1905
Paul Bakker41c83d32013-03-20 14:39:14 +01001906 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
1907 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1908 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1909 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
1910 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001911#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1912 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001913
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001914#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1915 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1916 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1917 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001918 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001919 /*
1920 * Ephemeral ECDH parameters:
1921 *
1922 * struct {
1923 * ECParameters curve_params;
1924 * ECPoint public;
1925 * } ServerECDHParams;
1926 */
Paul Bakker41c83d32013-03-20 14:39:14 +01001927 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
1928 ssl->handshake->ec_curve ) ) != 0 )
1929 {
1930 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
1931 return( ret );
1932 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001933
Paul Bakker41c83d32013-03-20 14:39:14 +01001934 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001935 &len,
1936 p,
Paul Bakker41c83d32013-03-20 14:39:14 +01001937 1000, ssl->f_rng, ssl->p_rng ) ) != 0 )
1938 {
1939 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
1940 return( ret );
1941 }
1942
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001943 dig_signed = p;
1944 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001945
1946 p += len;
1947 n += len;
1948
Paul Bakker41c83d32013-03-20 14:39:14 +01001949 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
1950 }
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001951#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1952 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001953
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001954#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001955 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1956 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001957 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001958 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1959 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001960 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001961 size_t signature_len = 0;
Paul Bakker23f36802012-09-28 14:15:14 +00001962
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001963 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001964 * Choose hash algorithm. NONE means MD5 + SHA1 here.
1965 */
Paul Bakker577e0062013-08-28 11:57:20 +02001966#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001967 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
1968 {
1969 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
1970
1971 if( md_alg == POLARSSL_MD_NONE )
1972 {
1973 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1974 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1975 }
1976 }
Paul Bakker577e0062013-08-28 11:57:20 +02001977 else
1978#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001979#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1980 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker577e0062013-08-28 11:57:20 +02001981 if ( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001982 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
1983 {
1984 md_alg = POLARSSL_MD_SHA1;
1985 }
1986 else
Paul Bakker577e0062013-08-28 11:57:20 +02001987#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001988 {
1989 md_alg = POLARSSL_MD_NONE;
1990 }
1991
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001992 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001993 * Compute the hash to be signed
1994 */
Paul Bakker577e0062013-08-28 11:57:20 +02001995#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1996 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001997 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00001998 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001999 md5_context md5;
2000 sha1_context sha1;
2001
2002 /*
2003 * digitally-signed struct {
2004 * opaque md5_hash[16];
2005 * opaque sha_hash[20];
2006 * };
2007 *
2008 * md5_hash
2009 * MD5(ClientHello.random + ServerHello.random
2010 * + ServerParams);
2011 * sha_hash
2012 * SHA(ClientHello.random + ServerHello.random
2013 * + ServerParams);
2014 */
2015 md5_starts( &md5 );
2016 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002017 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002018 md5_finish( &md5, hash );
2019
2020 sha1_starts( &sha1 );
2021 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002022 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002023 sha1_finish( &sha1, hash + 16 );
2024
2025 hashlen = 36;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002026 }
2027 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002028#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2029 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002030#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2031 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002032 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002033 {
2034 md_context_t ctx;
2035
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002036 /* Info from md_alg will be used instead */
2037 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002038
2039 /*
2040 * digitally-signed struct {
2041 * opaque client_random[32];
2042 * opaque server_random[32];
2043 * ServerDHParams params;
2044 * };
2045 */
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002046 if( ( ret = md_init_ctx( &ctx, md_info_from_type(md_alg) ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002047 {
2048 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2049 return( ret );
2050 }
2051
2052 md_starts( &ctx );
2053 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002054 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002055 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02002056
2057 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
2058 {
2059 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
2060 return( ret );
2061 }
2062
Paul Bakker23f36802012-09-28 14:15:14 +00002063 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002064 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002065#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2066 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002067 {
2068 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002069 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02002070 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002071
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002072 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2073 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002074
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002075 /*
2076 * Make the signature
2077 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02002078 if( ssl->pk_key == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002079 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002080 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2081 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002082 }
Paul Bakker23f36802012-09-28 14:15:14 +00002083
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002084#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002085 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2086 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002087 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002088 *(p++) = ssl_sig_from_pk( ssl->pk_key );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002089
2090 n += 2;
2091 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002092#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002093
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002094 if( ( ret = pk_sign( ssl->pk_key, md_alg, hash, hashlen,
2095 p + 2 , &signature_len,
2096 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002097 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002098 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002099 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002100 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002101
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002102 *(p++) = (unsigned char)( signature_len >> 8 );
2103 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002104 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002105
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002106 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002107
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002108 p += signature_len;
2109 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002110 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002111#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002112 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2113 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002114
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002115 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002116 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2117 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2118
2119 ssl->state++;
2120
2121 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2122 {
2123 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2124 return( ret );
2125 }
2126
2127 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2128
2129 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002130}
2131
2132static int ssl_write_server_hello_done( ssl_context *ssl )
2133{
2134 int ret;
2135
2136 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2137
2138 ssl->out_msglen = 4;
2139 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2140 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2141
2142 ssl->state++;
2143
2144 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2145 {
2146 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2147 return( ret );
2148 }
2149
2150 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2151
2152 return( 0 );
2153}
2154
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002155#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2156 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2157static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2158 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002159{
2160 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002161 size_t n;
2162
2163 /*
2164 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2165 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002166 if( *p + 2 > end )
2167 {
2168 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2169 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2170 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002171
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002172 n = ( (*p)[0] << 8 ) | (*p)[1];
2173 *p += 2;
2174
2175 if( n < 1 || n > ssl->handshake->dhm_ctx.len || *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002176 {
2177 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2178 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2179 }
2180
2181 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002182 *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002183 {
2184 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2185 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2186 }
2187
2188 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2189
Paul Bakker70df2fb2013-04-17 17:19:09 +02002190 return( ret );
2191}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002192#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2193 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002194
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02002195#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2196 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002197static int ssl_parse_client_ecdh_public( ssl_context *ssl )
2198{
2199 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002200 size_t n;
2201
2202 /*
2203 * Receive client public key and calculate premaster
2204 */
2205 n = ssl->in_msg[3];
2206
2207 if( n < 1 || n > mpi_size( &ssl->handshake->ecdh_ctx.grp.P ) * 2 + 2 ||
2208 n + 4 != ssl->in_hslen )
2209 {
2210 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2211 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2212 }
2213
2214 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2215 ssl->in_msg + 4, n ) ) != 0 )
2216 {
2217 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2218 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2219 }
2220
2221 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2222
Paul Bakker70df2fb2013-04-17 17:19:09 +02002223 return( ret );
2224}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02002225#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2226 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002227
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002228#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002229static int ssl_parse_encrypted_pms_secret( ssl_context *ssl )
2230{
2231 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2232 size_t i, n = 0;
2233
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002234 if( ! pk_can_do( ssl->pk_key, POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002235 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002236 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002237 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2238 }
2239
2240 /*
2241 * Decrypt the premaster using own private RSA key
2242 */
2243 i = 4;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002244 n = pk_get_len( ssl->pk_key );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002245 ssl->handshake->pmslen = 48;
2246
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002247#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2248 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002249 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2250 {
2251 i += 2;
2252 if( ssl->in_msg[4] != ( ( n >> 8 ) & 0xFF ) ||
2253 ssl->in_msg[5] != ( ( n ) & 0xFF ) )
2254 {
2255 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2256 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2257 }
2258 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002259#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002260
2261 if( ssl->in_hslen != i + n )
2262 {
2263 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2264 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2265 }
2266
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002267 ret = pk_decrypt( ssl->pk_key,
2268 ssl->in_msg + i, n,
2269 ssl->handshake->premaster, &ssl->handshake->pmslen,
2270 sizeof(ssl->handshake->premaster),
2271 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002272
2273 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Paul Bakker2fbefde2013-06-29 16:01:15 +02002274 ssl->handshake->premaster[0] != ssl->handshake->max_major_ver ||
2275 ssl->handshake->premaster[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002276 {
2277 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2278
2279 /*
2280 * Protection against Bleichenbacher's attack:
2281 * invalid PKCS#1 v1.5 padding must not cause
2282 * the connection to end immediately; instead,
2283 * send a bad_record_mac later in the handshake.
2284 */
2285 ssl->handshake->pmslen = 48;
2286
2287 ret = ssl->f_rng( ssl->p_rng, ssl->handshake->premaster,
2288 ssl->handshake->pmslen );
2289 if( ret != 0 )
2290 return( ret );
2291 }
2292
2293 return( ret );
2294}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002295#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002296
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002297#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2298 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2299static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2300 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002301{
2302 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002303 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002304
2305 if( ssl->psk == NULL || ssl->psk_identity == NULL ||
2306 ssl->psk_identity_len == 0 || ssl->psk_len == 0 )
2307 {
2308 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2309 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2310 }
2311
2312 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002313 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002314 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002315 if( *p + 2 > end )
2316 {
2317 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2318 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2319 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002320
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002321 n = ( (*p)[0] << 8 ) | (*p)[1];
2322 *p += 2;
2323
2324 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002325 {
2326 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2327 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2328 }
2329
2330 if( n != ssl->psk_identity_len ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002331 memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002332 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002333 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002334 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2335 }
2336
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002337 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002338 ret = 0;
2339
Paul Bakkerfbb17802013-04-17 19:10:21 +02002340 return( ret );
2341}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002342#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
2343 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002344
Paul Bakker5121ce52009-01-03 21:22:43 +00002345static int ssl_parse_client_key_exchange( ssl_context *ssl )
2346{
Paul Bakker23986e52011-04-24 08:57:21 +00002347 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002348 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002349 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002350
Paul Bakker41c83d32013-03-20 14:39:14 +01002351 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002352
2353 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2354
2355 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2356 {
2357 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2358 return( ret );
2359 }
2360
2361 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2362 {
2363 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002364 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002365 }
2366
2367 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2368 {
2369 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002370 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002371 }
2372
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002373 p = ssl->in_msg + 4;
2374 end = ssl->in_msg + ssl->in_msglen;
2375
2376#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002377 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002378 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002379 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002380 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002381 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2382 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002383 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002384
2385 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
2386
2387 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2388 ssl->handshake->premaster,
2389 &ssl->handshake->pmslen ) ) != 0 )
2390 {
2391 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2392 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2393 }
2394
2395 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002396 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002397 else
2398#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002399#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2400 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2401 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2402 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002403 {
2404 if( ( ret = ssl_parse_client_ecdh_public( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002405 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002406 SSL_DEBUG_RET( 1, ( "ssl_parse_client_ecdh_public" ), ret );
2407 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002408 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002409
2410 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2411 &ssl->handshake->pmslen,
2412 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002413 POLARSSL_MPI_MAX_SIZE,
2414 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002415 {
2416 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2417 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2418 }
2419
2420 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002421 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002422 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002423#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2424 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002425#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2426 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002427 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002428 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002429 {
2430 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2431 return( ret );
2432 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002433
2434 // Set up the premaster secret
2435 //
2436 p = ssl->handshake->premaster;
2437 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2438 *(p++) = (unsigned char)( ssl->psk_len );
2439 p += ssl->psk_len;
2440
2441 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2442 *(p++) = (unsigned char)( ssl->psk_len );
2443 memcpy( p, ssl->psk, ssl->psk_len );
2444 p += ssl->psk_len;
2445
2446 ssl->handshake->pmslen = 4 + 2 * ssl->psk_len;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002447 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002448 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002449#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
2450#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2451 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2452 {
2453 size_t n;
2454
2455 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2456 {
2457 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2458 return( ret );
2459 }
2460 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2461 {
2462 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2463 return( ret );
2464 }
2465
2466 // Set up the premaster secret
2467 //
2468 p = ssl->handshake->premaster;
2469 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len >> 8 );
2470 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len );
2471
Paul Bakker577e0062013-08-28 11:57:20 +02002472 n = ssl->handshake->dhm_ctx.len;
2473
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002474 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2475 p, &n ) ) != 0 )
2476 {
2477 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2478 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2479 }
2480
2481 if( n != ssl->handshake->dhm_ctx.len )
2482 {
2483 SSL_DEBUG_MSG( 1, ( "dhm_calc_secret result smaller than DHM" ) );
2484 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2485 }
2486
2487 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2488
2489 p += ssl->handshake->dhm_ctx.len;
2490
2491 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2492 *(p++) = (unsigned char)( ssl->psk_len );
2493 memcpy( p, ssl->psk, ssl->psk_len );
2494 p += ssl->psk_len;
2495
2496 ssl->handshake->pmslen = 4 + ssl->handshake->dhm_ctx.len + ssl->psk_len;
2497 }
2498 else
2499#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
2500#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2501 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002502 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002503 if( ( ret = ssl_parse_encrypted_pms_secret( ssl ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002504 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002505 SSL_DEBUG_RET( 1, ( "ssl_parse_client_ecdh_public" ), ret );
2506 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002507 }
2508 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002509 else
2510#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2511 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002512 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002513 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2514 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002515
Paul Bakkerff60ee62010-03-16 21:09:09 +00002516 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2517 {
2518 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2519 return( ret );
2520 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002521
Paul Bakker5121ce52009-01-03 21:22:43 +00002522 ssl->state++;
2523
2524 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2525
2526 return( 0 );
2527}
2528
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002529#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2530 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2531 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002532static int ssl_parse_certificate_verify( ssl_context *ssl )
2533{
Paul Bakkered27a042013-04-18 22:46:23 +02002534 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002535 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002536
2537 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2538
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002539 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2540 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002541 {
2542 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2543 ssl->state++;
2544 return( 0 );
2545 }
2546
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002547 return( ret );
2548}
2549#else
2550static int ssl_parse_certificate_verify( ssl_context *ssl )
2551{
2552 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002553 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002554 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002555 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002556 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02002557#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002558 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02002559#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002560 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002561 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2562
2563 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2564
2565 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2566 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2567 {
2568 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2569 ssl->state++;
2570 return( 0 );
2571 }
2572
Paul Bakkered27a042013-04-18 22:46:23 +02002573 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002574 {
2575 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2576 ssl->state++;
2577 return( 0 );
2578 }
2579
Paul Bakker48916f92012-09-16 19:57:18 +00002580 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002581
2582 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2583 {
2584 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2585 return( ret );
2586 }
2587
2588 ssl->state++;
2589
2590 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2591 {
2592 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002593 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002594 }
2595
2596 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
2597 {
2598 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002599 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002600 }
2601
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002602 /*
2603 * 0 . 0 handshake type
2604 * 1 . 3 handshake length
2605 * 4 . 5 sig alg (TLS 1.2 only)
2606 * 4+n . 5+n signature length (n = sa_len)
2607 * 6+n . 6+n+m signature (m = sig_len)
2608 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002609
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002610#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2611 defined(POLARSSL_SSL_PROTO_TLS1_1)
2612 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002613 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002614 sa_len = 0;
2615
Paul Bakkerc70b9822013-04-07 22:00:46 +02002616 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002617 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002618
2619 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
2620 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
2621 POLARSSL_PK_ECDSA ) )
2622 {
2623 hash_start += 16;
2624 hashlen -= 16;
2625 md_alg = POLARSSL_MD_SHA1;
2626 }
Paul Bakker926af752012-11-23 13:38:07 +01002627 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002628 else
2629#endif
Paul Bakker577e0062013-08-28 11:57:20 +02002630#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2631 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002632 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002633 sa_len = 2;
2634
Paul Bakker5121ce52009-01-03 21:22:43 +00002635 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002636 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00002637 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002638 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002639 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002640 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2641 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01002642 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2643 }
2644
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002645 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01002646
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002647 /* Info from md_alg will be used instead */
2648 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002649
2650 /*
2651 * Signature
2652 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002653 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
2654 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002655 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002656 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2657 " for verify message" ) );
2658 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002659 }
2660
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002661 /*
2662 * Check the certificate's key type matches the signature alg
2663 */
2664 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
2665 {
2666 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
2667 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2668 }
Paul Bakker577e0062013-08-28 11:57:20 +02002669 }
2670 else
2671#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2672 {
2673 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002674 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002675 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002676
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002677 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01002678
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002679 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002680 {
2681 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002682 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002683 }
2684
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002685 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002686 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002687 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002688 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002689 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002690 return( ret );
2691 }
2692
2693 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
2694
Paul Bakkered27a042013-04-18 22:46:23 +02002695 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002696}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002697#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2698 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2699 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002700
Paul Bakkera503a632013-08-14 13:48:06 +02002701#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002702static int ssl_write_new_session_ticket( ssl_context *ssl )
2703{
2704 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002705 size_t tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002706
2707 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
2708
2709 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2710 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
2711
2712 /*
2713 * struct {
2714 * uint32 ticket_lifetime_hint;
2715 * opaque ticket<0..2^16-1>;
2716 * } NewSessionTicket;
2717 *
2718 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
2719 * 8 . 9 ticket_len (n)
2720 * 10 . 9+n ticket content
2721 */
2722 ssl->out_msg[4] = 0x00;
2723 ssl->out_msg[5] = 0x00;
2724 ssl->out_msg[6] = 0x00;
2725 ssl->out_msg[7] = 0x00;
2726
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02002727 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
2728 {
2729 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
2730 tlen = 0;
2731 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002732
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002733 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
2734 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002735
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002736 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002737
2738 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2739 {
2740 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2741 return( ret );
2742 }
2743
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002744 /* No need to remember writing a NewSessionTicket any more */
2745 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002746
2747 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
2748
2749 return( 0 );
2750}
Paul Bakkera503a632013-08-14 13:48:06 +02002751#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002752
Paul Bakker5121ce52009-01-03 21:22:43 +00002753/*
Paul Bakker1961b702013-01-25 14:49:24 +01002754 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002755 */
Paul Bakker1961b702013-01-25 14:49:24 +01002756int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002757{
2758 int ret = 0;
2759
Paul Bakker1961b702013-01-25 14:49:24 +01002760 if( ssl->state == SSL_HANDSHAKE_OVER )
2761 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002762
Paul Bakker1961b702013-01-25 14:49:24 +01002763 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
2764
2765 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2766 return( ret );
2767
2768 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002769 {
Paul Bakker1961b702013-01-25 14:49:24 +01002770 case SSL_HELLO_REQUEST:
2771 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002772 break;
2773
Paul Bakker1961b702013-01-25 14:49:24 +01002774 /*
2775 * <== ClientHello
2776 */
2777 case SSL_CLIENT_HELLO:
2778 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002779 break;
Paul Bakker1961b702013-01-25 14:49:24 +01002780
2781 /*
2782 * ==> ServerHello
2783 * Certificate
2784 * ( ServerKeyExchange )
2785 * ( CertificateRequest )
2786 * ServerHelloDone
2787 */
2788 case SSL_SERVER_HELLO:
2789 ret = ssl_write_server_hello( ssl );
2790 break;
2791
2792 case SSL_SERVER_CERTIFICATE:
2793 ret = ssl_write_certificate( ssl );
2794 break;
2795
2796 case SSL_SERVER_KEY_EXCHANGE:
2797 ret = ssl_write_server_key_exchange( ssl );
2798 break;
2799
2800 case SSL_CERTIFICATE_REQUEST:
2801 ret = ssl_write_certificate_request( ssl );
2802 break;
2803
2804 case SSL_SERVER_HELLO_DONE:
2805 ret = ssl_write_server_hello_done( ssl );
2806 break;
2807
2808 /*
2809 * <== ( Certificate/Alert )
2810 * ClientKeyExchange
2811 * ( CertificateVerify )
2812 * ChangeCipherSpec
2813 * Finished
2814 */
2815 case SSL_CLIENT_CERTIFICATE:
2816 ret = ssl_parse_certificate( ssl );
2817 break;
2818
2819 case SSL_CLIENT_KEY_EXCHANGE:
2820 ret = ssl_parse_client_key_exchange( ssl );
2821 break;
2822
2823 case SSL_CERTIFICATE_VERIFY:
2824 ret = ssl_parse_certificate_verify( ssl );
2825 break;
2826
2827 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2828 ret = ssl_parse_change_cipher_spec( ssl );
2829 break;
2830
2831 case SSL_CLIENT_FINISHED:
2832 ret = ssl_parse_finished( ssl );
2833 break;
2834
2835 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002836 * ==> ( NewSessionTicket )
2837 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002838 * Finished
2839 */
2840 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002841#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002842 if( ssl->handshake->new_session_ticket != 0 )
2843 ret = ssl_write_new_session_ticket( ssl );
2844 else
Paul Bakkera503a632013-08-14 13:48:06 +02002845#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002846 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002847 break;
2848
2849 case SSL_SERVER_FINISHED:
2850 ret = ssl_write_finished( ssl );
2851 break;
2852
2853 case SSL_FLUSH_BUFFERS:
2854 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2855 ssl->state = SSL_HANDSHAKE_WRAPUP;
2856 break;
2857
2858 case SSL_HANDSHAKE_WRAPUP:
2859 ssl_handshake_wrapup( ssl );
2860 break;
2861
2862 default:
2863 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2864 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002865 }
2866
Paul Bakker5121ce52009-01-03 21:22:43 +00002867 return( ret );
2868}
Paul Bakker5121ce52009-01-03 21:22:43 +00002869#endif