blob: e28c835c50ddfa5065d6abdb7fe05fe0eff6cc76 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 server-side functions
3 *
Paul Bakker68884e32013-01-07 18:20:04 +01004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Paul Bakker40e46942009-01-03 21:51:57 +000026#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Paul Bakker40e46942009-01-03 21:51:57 +000028#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakker40e46942009-01-03 21:51:57 +000030#include "polarssl/debug.h"
31#include "polarssl/ssl.h"
Paul Bakker41c83d32013-03-20 14:39:14 +010032#if defined(POLARSSL_ECP_C)
33#include "polarssl/ecp.h"
34#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020036#if defined(POLARSSL_MEMORY_C)
37#include "polarssl/memory.h"
38#else
39#define polarssl_malloc malloc
40#define polarssl_free free
41#endif
42
Paul Bakker5121ce52009-01-03 21:22:43 +000043#include <stdlib.h>
44#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020045
46#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000047#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020048#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000049
Paul Bakkera503a632013-08-14 13:48:06 +020050#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020051/*
52 * Serialize a session in the following format:
53 * 0 . n-1 session structure, n = sizeof(ssl_session)
54 * n . n+2 peer_cert length = m (0 if no certificate)
55 * n+3 . n+2+m peer cert ASN.1
56 *
57 * Assumes ticket is NULL (always true on server side).
58 */
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020059static int ssl_save_session( const ssl_session *session,
60 unsigned char *buf, size_t buf_len,
61 size_t *olen )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020062{
63 unsigned char *p = buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020064 size_t left = buf_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020065#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020066 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020068
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020069 if( left < sizeof( ssl_session ) )
70 return( -1 );
71
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020072 memcpy( p, session, sizeof( ssl_session ) );
73 p += sizeof( ssl_session );
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020074 left -= sizeof( ssl_session );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020075
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020077 ((ssl_session *) buf)->peer_cert = NULL;
78
79 if( session->peer_cert == NULL )
80 cert_len = 0;
81 else
82 cert_len = session->peer_cert->raw.len;
83
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020084 if( left < 3 + cert_len )
85 return( -1 );
86
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020087 *p++ = (unsigned char)( cert_len >> 16 & 0xFF );
88 *p++ = (unsigned char)( cert_len >> 8 & 0xFF );
89 *p++ = (unsigned char)( cert_len & 0xFF );
90
91 if( session->peer_cert != NULL )
92 memcpy( p, session->peer_cert->raw.p, cert_len );
93
94 p += cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020095#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020096
97 *olen = p - buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020098
99 return( 0 );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200100}
101
102/*
103 * Unserialise session, see ssl_save_session()
104 */
105static int ssl_load_session( ssl_session *session,
106 const unsigned char *buf, size_t len )
107{
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200108 const unsigned char *p = buf;
109 const unsigned char * const end = buf + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200110#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200111 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200112#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200113
114 if( p + sizeof( ssl_session ) > end )
115 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
116
117 memcpy( session, p, sizeof( ssl_session ) );
118 p += sizeof( ssl_session );
119
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200120#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200121 if( p + 3 > end )
122 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
123
124 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
125 p += 3;
126
127 if( cert_len == 0 )
128 {
129 session->peer_cert = NULL;
130 }
131 else
132 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200133 int ret;
134
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200135 if( p + cert_len > end )
136 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
137
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200138 session->peer_cert = polarssl_malloc( sizeof( x509_crt ) );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200139
140 if( session->peer_cert == NULL )
141 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
142
Paul Bakkerb6b09562013-09-18 14:17:41 +0200143 x509_crt_init( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200144
Paul Bakkerddf26b42013-09-18 13:46:23 +0200145 if( ( ret = x509_crt_parse( session->peer_cert, p, cert_len ) ) != 0 )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200146 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200147 x509_crt_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200148 polarssl_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200149 session->peer_cert = NULL;
150 return( ret );
151 }
152
153 p += cert_len;
154 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200155#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200156
157 if( p != end )
158 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
159
160 return( 0 );
161}
162
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200163/*
164 * Create session ticket, secured as recommended in RFC 5077 section 4:
165 *
166 * struct {
167 * opaque key_name[16];
168 * opaque iv[16];
169 * opaque encrypted_state<0..2^16-1>;
170 * opaque mac[32];
171 * } ticket;
172 *
173 * (the internal state structure differs, however).
174 */
175static int ssl_write_ticket( ssl_context *ssl, size_t *tlen )
176{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200177 int ret;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200178 unsigned char * const start = ssl->out_msg + 10;
179 unsigned char *p = start;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200180 unsigned char *state;
181 unsigned char iv[16];
182 size_t clear_len, enc_len, pad_len, i;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200183
Manuel Pégourié-Gonnard0a201712013-08-23 16:25:16 +0200184 *tlen = 0;
185
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200186 if( ssl->ticket_keys == NULL )
187 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
188
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200189 /* Write key name */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200190 memcpy( p, ssl->ticket_keys->key_name, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200191 p += 16;
192
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200193 /* Generate and write IV (with a copy for aes_crypt) */
194 if( ( ret = ssl->f_rng( ssl->p_rng, p, 16 ) ) != 0 )
195 return( ret );
196 memcpy( iv, p, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200197 p += 16;
198
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200199 /*
200 * Dump session state
201 *
202 * After the session state itself, we still need room for 16 bytes of
203 * padding and 32 bytes of MAC, so there's only so much room left
204 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200205 state = p + 2;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200206 if( ssl_save_session( ssl->session_negotiate, state,
207 SSL_MAX_CONTENT_LEN - (state - ssl->out_ctr) - 48,
208 &clear_len ) != 0 )
209 {
210 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
211 }
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200212 SSL_DEBUG_BUF( 3, "session ticket cleartext", state, clear_len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200213
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200214 /* Apply PKCS padding */
215 pad_len = 16 - clear_len % 16;
216 enc_len = clear_len + pad_len;
217 for( i = clear_len; i < enc_len; i++ )
218 state[i] = (unsigned char) pad_len;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200219
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200220 /* Encrypt */
221 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->enc, AES_ENCRYPT,
222 enc_len, iv, state, state ) ) != 0 )
223 {
224 return( ret );
225 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200226
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200227 /* Write length */
228 *p++ = (unsigned char)( ( enc_len >> 8 ) & 0xFF );
229 *p++ = (unsigned char)( ( enc_len ) & 0xFF );
230 p = state + enc_len;
231
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200232 /* Compute and write MAC( key_name + iv + enc_state_len + enc_state ) */
233 sha256_hmac( ssl->ticket_keys->mac_key, 16, start, p - start, p, 0 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200234 p += 32;
235
236 *tlen = p - start;
237
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200238 SSL_DEBUG_BUF( 3, "session ticket structure", start, *tlen );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200239
240 return( 0 );
241}
242
243/*
244 * Load session ticket (see ssl_write_ticket for structure)
245 */
246static int ssl_parse_ticket( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200247 unsigned char *buf,
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200248 size_t len )
249{
250 int ret;
251 ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200252 unsigned char *key_name = buf;
253 unsigned char *iv = buf + 16;
254 unsigned char *enc_len_p = iv + 16;
255 unsigned char *ticket = enc_len_p + 2;
256 unsigned char *mac;
Manuel Pégourié-Gonnard34ced2d2013-09-20 11:37:39 +0200257 unsigned char computed_mac[32];
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200258 size_t enc_len, clear_len, i;
259 unsigned char pad_len;
260
261 SSL_DEBUG_BUF( 3, "session ticket structure", buf, len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200262
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200263 if( len < 34 || ssl->ticket_keys == NULL )
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200264 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
265
266 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
267 mac = ticket + enc_len;
268
269 if( len != enc_len + 66 )
270 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
271
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200272 /* Check name */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200273 if( memcmp( key_name, ssl->ticket_keys->key_name, 16 ) != 0 )
274 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200275
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200276 /* Check mac */
277 sha256_hmac( ssl->ticket_keys->mac_key, 16, buf, len - 32,
278 computed_mac, 0 );
279 ret = 0;
280 for( i = 0; i < 32; i++ )
281 if( mac[i] != computed_mac[i] )
282 ret = POLARSSL_ERR_SSL_INVALID_MAC;
283 if( ret != 0 )
284 return( ret );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200285
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200286 /* Decrypt */
287 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->dec, AES_DECRYPT,
288 enc_len, iv, ticket, ticket ) ) != 0 )
289 {
290 return( ret );
291 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200292
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200293 /* Check PKCS padding */
294 pad_len = ticket[enc_len - 1];
295
296 ret = 0;
297 for( i = 2; i < pad_len; i++ )
298 if( ticket[enc_len - i] != pad_len )
299 ret = POLARSSL_ERR_SSL_BAD_INPUT_DATA;
300 if( ret != 0 )
301 return( ret );
302
303 clear_len = enc_len - pad_len;
304
305 SSL_DEBUG_BUF( 3, "session ticket cleartext", ticket, clear_len );
306
307 /* Actually load session */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200308 if( ( ret = ssl_load_session( &session, ticket, clear_len ) ) != 0 )
309 {
310 SSL_DEBUG_MSG( 1, ( "failed to parse ticket content" ) );
311 memset( &session, 0, sizeof( ssl_session ) );
312 return( ret );
313 }
314
Paul Bakker606b4ba2013-08-14 16:52:14 +0200315#if defined(POLARSSL_HAVE_TIME)
316 /* Check if still valid */
317 if( (int) ( time( NULL) - session.start ) > ssl->ticket_lifetime )
318 {
319 SSL_DEBUG_MSG( 1, ( "session ticket expired" ) );
320 memset( &session, 0, sizeof( ssl_session ) );
321 return( POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED );
322 }
323#endif
324
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200325 /*
326 * Keep the session ID sent by the client, since we MUST send it back to
327 * inform him we're accepting the ticket (RFC 5077 section 3.4)
328 */
329 session.length = ssl->session_negotiate->length;
330 memcpy( &session.id, ssl->session_negotiate->id, session.length );
331
332 ssl_session_free( ssl->session_negotiate );
333 memcpy( ssl->session_negotiate, &session, sizeof( ssl_session ) );
334 memset( &session, 0, sizeof( ssl_session ) );
335
336 return( 0 );
337}
Paul Bakkera503a632013-08-14 13:48:06 +0200338#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200339
Paul Bakker0be444a2013-08-27 21:55:01 +0200340#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +0000341static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000342 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000343 size_t len )
344{
345 int ret;
346 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000347 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000348
349 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
350 if( servername_list_size + 2 != len )
351 {
352 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
353 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
354 }
355
356 p = buf + 2;
357 while( servername_list_size > 0 )
358 {
359 hostname_len = ( ( p[1] << 8 ) | p[2] );
360 if( hostname_len + 3 > servername_list_size )
361 {
362 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
363 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
364 }
365
366 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
367 {
368 ret = ssl->f_sni( ssl->p_sni, ssl, p + 3, hostname_len );
369 if( ret != 0 )
370 {
371 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
372 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
373 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
374 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000375 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000376 }
377
378 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000379 p += hostname_len + 3;
380 }
381
382 if( servername_list_size != 0 )
383 {
384 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
385 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000386 }
387
388 return( 0 );
389}
Paul Bakker0be444a2013-08-27 21:55:01 +0200390#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000391
Paul Bakker48916f92012-09-16 19:57:18 +0000392static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000393 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000394 size_t len )
395{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000396 int ret;
397
Paul Bakker48916f92012-09-16 19:57:18 +0000398 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
399 {
400 if( len != 1 || buf[0] != 0x0 )
401 {
402 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000403
404 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
405 return( ret );
406
Paul Bakker48916f92012-09-16 19:57:18 +0000407 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
408 }
409
410 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
411 }
412 else
413 {
414 if( len != 1 + ssl->verify_data_len ||
415 buf[0] != ssl->verify_data_len ||
416 memcmp( buf + 1, ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
417 {
418 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000419
420 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
421 return( ret );
422
Paul Bakker48916f92012-09-16 19:57:18 +0000423 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
424 }
425 }
426
427 return( 0 );
428}
429
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200430#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +0000431static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
432 const unsigned char *buf,
433 size_t len )
434{
435 size_t sig_alg_list_size;
436 const unsigned char *p;
437
438 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
439 if( sig_alg_list_size + 2 != len ||
440 sig_alg_list_size %2 != 0 )
441 {
442 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
443 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
444 }
445
446 p = buf + 2;
447 while( sig_alg_list_size > 0 )
448 {
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200449 /*
450 * For now, just ignore signature algorithm and rely on offered
451 * ciphersuites only. To be fixed later.
452 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200453#if defined(POLARSSL_SHA512_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000454 if( p[0] == SSL_HASH_SHA512 )
455 {
456 ssl->handshake->sig_alg = SSL_HASH_SHA512;
457 break;
458 }
459 if( p[0] == SSL_HASH_SHA384 )
460 {
461 ssl->handshake->sig_alg = SSL_HASH_SHA384;
462 break;
463 }
464#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200465#if defined(POLARSSL_SHA256_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000466 if( p[0] == SSL_HASH_SHA256 )
467 {
468 ssl->handshake->sig_alg = SSL_HASH_SHA256;
469 break;
470 }
471 if( p[0] == SSL_HASH_SHA224 )
472 {
473 ssl->handshake->sig_alg = SSL_HASH_SHA224;
474 break;
475 }
476#endif
477 if( p[0] == SSL_HASH_SHA1 )
478 {
479 ssl->handshake->sig_alg = SSL_HASH_SHA1;
480 break;
481 }
482 if( p[0] == SSL_HASH_MD5 )
483 {
484 ssl->handshake->sig_alg = SSL_HASH_MD5;
485 break;
486 }
487
488 sig_alg_list_size -= 2;
489 p += 2;
490 }
491
492 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
493 ssl->handshake->sig_alg ) );
494
495 return( 0 );
496}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200497#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker23f36802012-09-28 14:15:14 +0000498
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200499#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200500static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
501 const unsigned char *buf,
502 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100503{
504 size_t list_size;
505 const unsigned char *p;
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200506 ecp_group_id grp_id;
Paul Bakker41c83d32013-03-20 14:39:14 +0100507
508 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
509 if( list_size + 2 != len ||
510 list_size % 2 != 0 )
511 {
512 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
513 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
514 }
515
516 p = buf + 2;
517 while( list_size > 0 )
518 {
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200519 grp_id = ecp_grp_id_from_named_curve( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200520
521 if( grp_id != POLARSSL_ECP_DP_NONE )
Paul Bakker41c83d32013-03-20 14:39:14 +0100522 {
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200523 ssl->handshake->ec_curve = grp_id;
Paul Bakker41c83d32013-03-20 14:39:14 +0100524 return( 0 );
525 }
526
527 list_size -= 2;
528 p += 2;
529 }
530
531 return( 0 );
532}
533
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200534static int ssl_parse_supported_point_formats( ssl_context *ssl,
535 const unsigned char *buf,
536 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100537{
538 size_t list_size;
539 const unsigned char *p;
540
541 list_size = buf[0];
542 if( list_size + 1 != len )
543 {
544 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
545 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
546 }
547
548 p = buf + 2;
549 while( list_size > 0 )
550 {
551 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
552 p[0] == POLARSSL_ECP_PF_COMPRESSED )
553 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200554 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200555 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100556 return( 0 );
557 }
558
559 list_size--;
560 p++;
561 }
562
563 return( 0 );
564}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200565#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100566
Paul Bakker05decb22013-08-15 13:33:48 +0200567#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200568static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
569 const unsigned char *buf,
570 size_t len )
571{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200572 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200573 {
574 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
575 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
576 }
577
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200578 ssl->session_negotiate->mfl_code = buf[0];
579
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200580 return( 0 );
581}
Paul Bakker05decb22013-08-15 13:33:48 +0200582#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200583
Paul Bakker1f2bc622013-08-15 13:45:55 +0200584#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200585static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
586 const unsigned char *buf,
587 size_t len )
588{
589 if( len != 0 )
590 {
591 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
592 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
593 }
594
595 ((void) buf);
596
597 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
598
599 return( 0 );
600}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200601#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200602
Paul Bakkera503a632013-08-14 13:48:06 +0200603#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200604static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200605 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200606 size_t len )
607{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200608 int ret;
609
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200610 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
611 return( 0 );
612
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200613 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200614 ssl->handshake->new_session_ticket = 1;
615
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200616 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
617
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200618 if( len == 0 )
619 return( 0 );
620
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200621 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
622 {
623 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
624 return( 0 );
625 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200626
627 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200628 * Failures are ok: just ignore the ticket and proceed.
629 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200630 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
631 {
632 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200633 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200634 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200635
636 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
637
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200638 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200639
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200640 /* Don't send a new ticket after all, this one is OK */
641 ssl->handshake->new_session_ticket = 0;
642
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200643 return( 0 );
644}
Paul Bakkera503a632013-08-14 13:48:06 +0200645#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200646
Paul Bakker78a8c712013-03-06 17:01:52 +0100647#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
648static int ssl_parse_client_hello_v2( ssl_context *ssl )
649{
650 int ret;
651 unsigned int i, j;
652 size_t n;
653 unsigned int ciph_len, sess_len, chal_len;
654 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200655 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200656 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100657
658 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
659
660 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
661 {
662 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
663
664 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
665 return( ret );
666
667 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
668 }
669
670 buf = ssl->in_hdr;
671
672 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
673
674 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
675 buf[2] ) );
676 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
677 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
678 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
679 buf[3], buf[4] ) );
680
681 /*
682 * SSLv2 Client Hello
683 *
684 * Record layer:
685 * 0 . 1 message length
686 *
687 * SSL layer:
688 * 2 . 2 message type
689 * 3 . 4 protocol version
690 */
691 if( buf[2] != SSL_HS_CLIENT_HELLO ||
692 buf[3] != SSL_MAJOR_VERSION_3 )
693 {
694 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
695 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
696 }
697
698 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
699
700 if( n < 17 || n > 512 )
701 {
702 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
703 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
704 }
705
706 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200707 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
708 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100709
710 if( ssl->minor_ver < ssl->min_minor_ver )
711 {
712 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
713 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
714 ssl->min_major_ver, ssl->min_minor_ver ) );
715
716 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
717 SSL_ALERT_MSG_PROTOCOL_VERSION );
718 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
719 }
720
Paul Bakker2fbefde2013-06-29 16:01:15 +0200721 ssl->handshake->max_major_ver = buf[3];
722 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100723
724 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
725 {
726 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
727 return( ret );
728 }
729
730 ssl->handshake->update_checksum( ssl, buf + 2, n );
731
732 buf = ssl->in_msg;
733 n = ssl->in_left - 5;
734
735 /*
736 * 0 . 1 ciphersuitelist length
737 * 2 . 3 session id length
738 * 4 . 5 challenge length
739 * 6 . .. ciphersuitelist
740 * .. . .. session id
741 * .. . .. challenge
742 */
743 SSL_DEBUG_BUF( 4, "record contents", buf, n );
744
745 ciph_len = ( buf[0] << 8 ) | buf[1];
746 sess_len = ( buf[2] << 8 ) | buf[3];
747 chal_len = ( buf[4] << 8 ) | buf[5];
748
749 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
750 ciph_len, sess_len, chal_len ) );
751
752 /*
753 * Make sure each parameter length is valid
754 */
755 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
756 {
757 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
758 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
759 }
760
761 if( sess_len > 32 )
762 {
763 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
764 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
765 }
766
767 if( chal_len < 8 || chal_len > 32 )
768 {
769 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
770 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
771 }
772
773 if( n != 6 + ciph_len + sess_len + chal_len )
774 {
775 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
776 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
777 }
778
779 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
780 buf + 6, ciph_len );
781 SSL_DEBUG_BUF( 3, "client hello, session id",
782 buf + 6 + ciph_len, sess_len );
783 SSL_DEBUG_BUF( 3, "client hello, challenge",
784 buf + 6 + ciph_len + sess_len, chal_len );
785
786 p = buf + 6 + ciph_len;
787 ssl->session_negotiate->length = sess_len;
788 memset( ssl->session_negotiate->id, 0, sizeof( ssl->session_negotiate->id ) );
789 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
790
791 p += sess_len;
792 memset( ssl->handshake->randbytes, 0, 64 );
793 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
794
795 /*
796 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
797 */
798 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
799 {
800 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
801 {
802 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
803 if( ssl->renegotiation == SSL_RENEGOTIATION )
804 {
805 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
806
807 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
808 return( ret );
809
810 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
811 }
812 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
813 break;
814 }
815 }
816
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200817 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
818 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +0100819 {
820 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
821 {
Paul Bakker41c83d32013-03-20 14:39:14 +0100822 // Only allow non-ECC ciphersuites as we do not have extensions
823 //
Paul Bakker59c28a22013-06-29 15:33:42 +0200824 if( p[0] == 0 && p[1] == 0 &&
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200825 ( ( ciphersuites[i] >> 8 ) & 0xFF ) == 0 &&
826 p[2] == ( ciphersuites[i] & 0xFF ) )
Paul Bakker59c28a22013-06-29 15:33:42 +0200827 {
828 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
829
830 if( ciphersuite_info == NULL )
831 {
832 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %02x not found",
833 ciphersuites[i] ) );
834 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
835 }
836
Paul Bakker2fbefde2013-06-29 16:01:15 +0200837 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
838 ciphersuite_info->max_minor_ver < ssl->minor_ver )
839 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +0200840
Paul Bakker78a8c712013-03-06 17:01:52 +0100841 goto have_ciphersuite_v2;
Paul Bakker59c28a22013-06-29 15:33:42 +0200842 }
Paul Bakker78a8c712013-03-06 17:01:52 +0100843 }
844 }
845
846 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
847
848 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
849
850have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200851 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +0200852 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +0100853 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +0100854
855 /*
856 * SSLv2 Client Hello relevant renegotiation security checks
857 */
858 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
859 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
860 {
861 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
862
863 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
864 return( ret );
865
866 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
867 }
868
869 ssl->in_left = 0;
870 ssl->state++;
871
872 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
873
874 return( 0 );
875}
876#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
877
Paul Bakker5121ce52009-01-03 21:22:43 +0000878static int ssl_parse_client_hello( ssl_context *ssl )
879{
Paul Bakker23986e52011-04-24 08:57:21 +0000880 int ret;
881 unsigned int i, j;
882 size_t n;
883 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +0000884 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +0000885 unsigned int ext_len = 0;
886 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000887 int renegotiation_info_seen = 0;
888 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200889 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +0100890 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +0200891#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +0200892 pk_type_t pk_alg;
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +0200893#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000894
895 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
896
Paul Bakker48916f92012-09-16 19:57:18 +0000897 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
898 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000899 {
900 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
901 return( ret );
902 }
903
904 buf = ssl->in_hdr;
905
Paul Bakker78a8c712013-03-06 17:01:52 +0100906#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
907 if( ( buf[0] & 0x80 ) != 0 )
908 return ssl_parse_client_hello_v2( ssl );
909#endif
910
Paul Bakkerec636f32012-09-09 19:17:02 +0000911 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
912
913 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
914 buf[0] ) );
915 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
916 ( buf[3] << 8 ) | buf[4] ) );
917 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
918 buf[1], buf[2] ) );
919
920 /*
921 * SSLv3 Client Hello
922 *
923 * Record layer:
924 * 0 . 0 message type
925 * 1 . 2 protocol version
926 * 3 . 4 message length
927 */
928 if( buf[0] != SSL_MSG_HANDSHAKE ||
929 buf[1] != SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000930 {
Paul Bakkerec636f32012-09-09 19:17:02 +0000931 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
932 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
933 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000934
Paul Bakkerec636f32012-09-09 19:17:02 +0000935 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000936
Manuel Pégourié-Gonnard72882b22013-08-02 13:36:00 +0200937 if( n < 45 || n > 2048 )
Paul Bakkerec636f32012-09-09 19:17:02 +0000938 {
939 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
940 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
941 }
942
Paul Bakker48916f92012-09-16 19:57:18 +0000943 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
944 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +0000945 {
946 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
947 return( ret );
948 }
949
950 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +0000951 if( !ssl->renegotiation )
952 n = ssl->in_left - 5;
953 else
954 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +0000955
Paul Bakker48916f92012-09-16 19:57:18 +0000956 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +0000957
958 /*
959 * SSL layer:
960 * 0 . 0 handshake type
961 * 1 . 3 handshake length
962 * 4 . 5 protocol version
963 * 6 . 9 UNIX time()
964 * 10 . 37 random bytes
965 * 38 . 38 session id length
966 * 39 . 38+x session id
967 * 39+x . 40+x ciphersuitelist length
968 * 41+x . .. ciphersuitelist
969 * .. . .. compression alg.
970 * .. . .. extensions
971 */
972 SSL_DEBUG_BUF( 4, "record contents", buf, n );
973
974 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
975 buf[0] ) );
976 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
977 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
978 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
979 buf[4], buf[5] ) );
980
981 /*
982 * Check the handshake type and protocol version
983 */
984 if( buf[0] != SSL_HS_CLIENT_HELLO ||
985 buf[4] != SSL_MAJOR_VERSION_3 )
986 {
987 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
988 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
989 }
990
991 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200992 ssl->minor_ver = ( buf[5] <= ssl->max_minor_ver )
993 ? buf[5] : ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +0000994
Paul Bakker1d29fb52012-09-28 13:28:45 +0000995 if( ssl->minor_ver < ssl->min_minor_ver )
996 {
997 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
998 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +0000999 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001000
1001 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1002 SSL_ALERT_MSG_PROTOCOL_VERSION );
1003
1004 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1005 }
1006
Paul Bakker2fbefde2013-06-29 16:01:15 +02001007 ssl->handshake->max_major_ver = buf[4];
1008 ssl->handshake->max_minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001009
Paul Bakker48916f92012-09-16 19:57:18 +00001010 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001011
1012 /*
1013 * Check the handshake message length
1014 */
1015 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1016 {
1017 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1018 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1019 }
1020
1021 /*
1022 * Check the session length
1023 */
1024 sess_len = buf[38];
1025
1026 if( sess_len > 32 )
1027 {
1028 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1029 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1030 }
1031
Paul Bakker48916f92012-09-16 19:57:18 +00001032 ssl->session_negotiate->length = sess_len;
1033 memset( ssl->session_negotiate->id, 0,
1034 sizeof( ssl->session_negotiate->id ) );
1035 memcpy( ssl->session_negotiate->id, buf + 39,
1036 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001037
1038 /*
1039 * Check the ciphersuitelist length
1040 */
1041 ciph_len = ( buf[39 + sess_len] << 8 )
1042 | ( buf[40 + sess_len] );
1043
1044 if( ciph_len < 2 || ciph_len > 256 || ( ciph_len % 2 ) != 0 )
1045 {
1046 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1047 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1048 }
1049
1050 /*
1051 * Check the compression algorithms length
1052 */
1053 comp_len = buf[41 + sess_len + ciph_len];
1054
1055 if( comp_len < 1 || comp_len > 16 )
1056 {
1057 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1058 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1059 }
1060
Paul Bakker48916f92012-09-16 19:57:18 +00001061 /*
1062 * Check the extension length
1063 */
1064 if( n > 42 + sess_len + ciph_len + comp_len )
1065 {
1066 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1067 | ( buf[43 + sess_len + ciph_len + comp_len] );
1068
1069 if( ( ext_len > 0 && ext_len < 4 ) ||
1070 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1071 {
1072 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1073 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1074 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1075 }
1076 }
1077
1078 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001079#if defined(POLARSSL_ZLIB_SUPPORT)
1080 for( i = 0; i < comp_len; ++i )
1081 {
Paul Bakker48916f92012-09-16 19:57:18 +00001082 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001083 {
Paul Bakker48916f92012-09-16 19:57:18 +00001084 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001085 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001086 }
1087 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001088#endif
1089
Paul Bakkerec636f32012-09-09 19:17:02 +00001090 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1091 buf + 6, 32 );
1092 SSL_DEBUG_BUF( 3, "client hello, session id",
1093 buf + 38, sess_len );
1094 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1095 buf + 41 + sess_len, ciph_len );
1096 SSL_DEBUG_BUF( 3, "client hello, compression",
1097 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001098
Paul Bakkerec636f32012-09-09 19:17:02 +00001099 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001100 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1101 */
1102 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1103 {
1104 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1105 {
1106 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1107 if( ssl->renegotiation == SSL_RENEGOTIATION )
1108 {
1109 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001110
1111 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1112 return( ret );
1113
Paul Bakker48916f92012-09-16 19:57:18 +00001114 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1115 }
1116 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1117 break;
1118 }
1119 }
1120
Paul Bakker48916f92012-09-16 19:57:18 +00001121 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001122
1123 while( ext_len )
1124 {
1125 unsigned int ext_id = ( ( ext[0] << 8 )
1126 | ( ext[1] ) );
1127 unsigned int ext_size = ( ( ext[2] << 8 )
1128 | ( ext[3] ) );
1129
1130 if( ext_size + 4 > ext_len )
1131 {
1132 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1133 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1134 }
1135 switch( ext_id )
1136 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001137#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001138 case TLS_EXT_SERVERNAME:
1139 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1140 if( ssl->f_sni == NULL )
1141 break;
1142
1143 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1144 if( ret != 0 )
1145 return( ret );
1146 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001147#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001148
Paul Bakker48916f92012-09-16 19:57:18 +00001149 case TLS_EXT_RENEGOTIATION_INFO:
1150 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1151 renegotiation_info_seen = 1;
1152
Paul Bakker23f36802012-09-28 14:15:14 +00001153 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1154 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001155 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001156 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001157
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001158#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001159 case TLS_EXT_SIG_ALG:
1160 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1161 if( ssl->renegotiation == SSL_RENEGOTIATION )
1162 break;
1163
1164 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1165 if( ret != 0 )
1166 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001167 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001168#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001169
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001170#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001171 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1172 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1173
1174 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1175 if( ret != 0 )
1176 return( ret );
1177 break;
1178
1179 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1180 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1181
1182 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1183 if( ret != 0 )
1184 return( ret );
1185 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001186#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001187
Paul Bakker05decb22013-08-15 13:33:48 +02001188#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001189 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1190 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1191
1192 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1193 if( ret != 0 )
1194 return( ret );
1195 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001196#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001197
Paul Bakker1f2bc622013-08-15 13:45:55 +02001198#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001199 case TLS_EXT_TRUNCATED_HMAC:
1200 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1201
1202 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1203 if( ret != 0 )
1204 return( ret );
1205 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001206#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001207
Paul Bakkera503a632013-08-14 13:48:06 +02001208#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001209 case TLS_EXT_SESSION_TICKET:
1210 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1211
1212 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1213 if( ret != 0 )
1214 return( ret );
1215 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001216#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001217
Paul Bakker48916f92012-09-16 19:57:18 +00001218 default:
1219 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1220 ext_id ) );
1221 }
1222
1223 ext_len -= 4 + ext_size;
1224 ext += 4 + ext_size;
1225
1226 if( ext_len > 0 && ext_len < 4 )
1227 {
1228 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1229 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1230 }
1231 }
1232
1233 /*
1234 * Renegotiation security checks
1235 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001236 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1237 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1238 {
1239 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1240 handshake_failure = 1;
1241 }
1242 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1243 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1244 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001245 {
1246 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001247 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001248 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001249 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1250 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1251 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001252 {
1253 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001254 handshake_failure = 1;
1255 }
1256 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1257 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1258 renegotiation_info_seen == 1 )
1259 {
1260 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1261 handshake_failure = 1;
1262 }
1263
1264 if( handshake_failure == 1 )
1265 {
1266 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1267 return( ret );
1268
Paul Bakker48916f92012-09-16 19:57:18 +00001269 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1270 }
Paul Bakker380da532012-04-18 16:10:25 +00001271
Paul Bakker41c83d32013-03-20 14:39:14 +01001272 /*
1273 * Search for a matching ciphersuite
1274 * (At the end because we need information from the EC-based extensions)
1275 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001276 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
1277 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001278 {
1279 for( j = 0, p = buf + 41 + sess_len; j < ciph_len;
1280 j += 2, p += 2 )
1281 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001282 if( p[0] == ( ( ciphersuites[i] >> 8 ) & 0xFF ) &&
1283 p[1] == ( ( ciphersuites[i] ) & 0xFF ) )
Paul Bakker41c83d32013-03-20 14:39:14 +01001284 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001285 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
Paul Bakker41c83d32013-03-20 14:39:14 +01001286
1287 if( ciphersuite_info == NULL )
1288 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001289 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found",
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001290 ciphersuites[i] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001291 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1292 }
1293
Paul Bakker2fbefde2013-06-29 16:01:15 +02001294 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
1295 ciphersuite_info->max_minor_ver < ssl->minor_ver )
1296 continue;
1297
Paul Bakker5fd49172013-08-19 13:29:26 +02001298#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001299 if( ssl_ciphersuite_uses_ec( ciphersuite_info ) &&
Paul Bakker41c83d32013-03-20 14:39:14 +01001300 ssl->handshake->ec_curve == 0 )
1301 continue;
Paul Bakker5fd49172013-08-19 13:29:26 +02001302#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001303
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001304 /* If ciphersuite requires us to have a private key of a
1305 * certain type, make sure we do */
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02001306#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001307 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
1308 if( pk_alg != POLARSSL_PK_NONE &&
1309 ( ssl->pk_key == NULL ||
1310 ! pk_can_do( ssl->pk_key, pk_alg ) ) )
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001311 continue;
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02001312#endif
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001313
Paul Bakker41c83d32013-03-20 14:39:14 +01001314 goto have_ciphersuite;
1315 }
1316 }
1317 }
1318
1319 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1320
1321 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1322 return( ret );
1323
1324 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1325
1326have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001327 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001328 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1329 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1330
Paul Bakker5121ce52009-01-03 21:22:43 +00001331 ssl->in_left = 0;
1332 ssl->state++;
1333
1334 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1335
1336 return( 0 );
1337}
1338
Paul Bakker1f2bc622013-08-15 13:45:55 +02001339#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001340static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1341 unsigned char *buf,
1342 size_t *olen )
1343{
1344 unsigned char *p = buf;
1345
1346 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1347 {
1348 *olen = 0;
1349 return;
1350 }
1351
1352 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1353
1354 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1355 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1356
1357 *p++ = 0x00;
1358 *p++ = 0x00;
1359
1360 *olen = 4;
1361}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001362#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001363
Paul Bakkera503a632013-08-14 13:48:06 +02001364#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001365static void ssl_write_session_ticket_ext( ssl_context *ssl,
1366 unsigned char *buf,
1367 size_t *olen )
1368{
1369 unsigned char *p = buf;
1370
1371 if( ssl->handshake->new_session_ticket == 0 )
1372 {
1373 *olen = 0;
1374 return;
1375 }
1376
1377 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1378
1379 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1380 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1381
1382 *p++ = 0x00;
1383 *p++ = 0x00;
1384
1385 *olen = 4;
1386}
Paul Bakkera503a632013-08-14 13:48:06 +02001387#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001388
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001389static void ssl_write_renegotiation_ext( ssl_context *ssl,
1390 unsigned char *buf,
1391 size_t *olen )
1392{
1393 unsigned char *p = buf;
1394
1395 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1396 {
1397 *olen = 0;
1398 return;
1399 }
1400
1401 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1402
1403 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1404 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1405
1406 *p++ = 0x00;
1407 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1408 *p++ = ssl->verify_data_len * 2 & 0xFF;
1409
1410 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1411 p += ssl->verify_data_len;
1412 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1413 p += ssl->verify_data_len;
1414
1415 *olen = 5 + ssl->verify_data_len * 2;
1416}
1417
Paul Bakker05decb22013-08-15 13:33:48 +02001418#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001419static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1420 unsigned char *buf,
1421 size_t *olen )
1422{
1423 unsigned char *p = buf;
1424
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001425 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1426 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001427 *olen = 0;
1428 return;
1429 }
1430
1431 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1432
1433 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1434 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1435
1436 *p++ = 0x00;
1437 *p++ = 1;
1438
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001439 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001440
1441 *olen = 5;
1442}
Paul Bakker05decb22013-08-15 13:33:48 +02001443#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001444
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001445#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001446static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1447 unsigned char *buf,
1448 size_t *olen )
1449{
1450 unsigned char *p = buf;
1451 ((void) ssl);
1452
1453 *olen = 0;
1454
1455 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1456
1457 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1458 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1459
1460 *p++ = 0x00;
1461 *p++ = 2;
1462
1463 *p++ = 1;
1464 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1465
1466 *olen = 6;
1467}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001468#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001469
Paul Bakker5121ce52009-01-03 21:22:43 +00001470static int ssl_write_server_hello( ssl_context *ssl )
1471{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001472#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001473 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001474#endif
Paul Bakkera3d195c2011-11-27 21:07:34 +00001475 int ret, n;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001476 size_t olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001477 unsigned char *buf, *p;
1478
1479 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1480
1481 /*
1482 * 0 . 0 handshake type
1483 * 1 . 3 handshake length
1484 * 4 . 5 protocol version
1485 * 6 . 9 UNIX time()
1486 * 10 . 37 random bytes
1487 */
1488 buf = ssl->out_msg;
1489 p = buf + 4;
1490
1491 *p++ = (unsigned char) ssl->major_ver;
1492 *p++ = (unsigned char) ssl->minor_ver;
1493
1494 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1495 buf[4], buf[5] ) );
1496
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001497#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001498 t = time( NULL );
1499 *p++ = (unsigned char)( t >> 24 );
1500 *p++ = (unsigned char)( t >> 16 );
1501 *p++ = (unsigned char)( t >> 8 );
1502 *p++ = (unsigned char)( t );
1503
1504 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001505#else
1506 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1507 return( ret );
1508
1509 p += 4;
1510#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001511
Paul Bakkera3d195c2011-11-27 21:07:34 +00001512 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1513 return( ret );
1514
1515 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001516
Paul Bakker48916f92012-09-16 19:57:18 +00001517 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001518
1519 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1520
1521 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001522 * Resume is 0 by default, see ssl_handshake_init().
1523 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1524 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001525 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001526 if( ssl->handshake->resume == 0 &&
1527 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001528 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001529 ssl->f_get_cache != NULL &&
1530 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1531 {
1532 ssl->handshake->resume = 1;
1533 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001534
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001535 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001536 {
1537 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001538 * New session, create a new session id,
1539 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001540 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001541 ssl->state++;
1542
Paul Bakkera503a632013-08-14 13:48:06 +02001543#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001544 if( ssl->handshake->new_session_ticket == 0 )
1545 {
1546 ssl->session_negotiate->length = n = 32;
1547 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001548 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001549 return( ret );
1550 }
1551 else
1552 {
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001553 ssl->session_negotiate->length = n = 0;
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001554 memset( ssl->session_negotiate->id, 0, 32 );
1555 }
Paul Bakkera503a632013-08-14 13:48:06 +02001556#else
1557 ssl->session_negotiate->length = n = 32;
1558 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
1559 n ) ) != 0 )
1560 return( ret );
1561#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001562 }
1563 else
1564 {
1565 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001566 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001567 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001568 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001569 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001570
1571 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1572 {
1573 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1574 return( ret );
1575 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001576 }
1577
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001578 /*
1579 * 38 . 38 session id length
1580 * 39 . 38+n session id
1581 * 39+n . 40+n chosen ciphersuite
1582 * 41+n . 41+n chosen compression alg.
1583 * 42+n . 43+n extensions length
1584 * 44+n . 43+n+m extensions
1585 */
1586 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001587 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1588 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001589
1590 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1591 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1592 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001593 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001594
Paul Bakker48916f92012-09-16 19:57:18 +00001595 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1596 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1597 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001598
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001599 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
1600 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001601 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001602 ssl->session_negotiate->compression ) );
1603
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001604 /*
1605 * First write extensions, then the total length
1606 */
1607 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1608 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001609
Paul Bakker05decb22013-08-15 13:33:48 +02001610#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001611 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1612 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001613#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001614
Paul Bakker1f2bc622013-08-15 13:45:55 +02001615#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001616 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1617 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001618#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001619
Paul Bakkera503a632013-08-14 13:48:06 +02001620#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001621 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1622 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001623#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001624
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001625#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001626 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1627 ext_len += olen;
1628#endif
1629
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001630 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001631
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001632 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1633 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1634 p += ext_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001635
1636 ssl->out_msglen = p - buf;
1637 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1638 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1639
1640 ret = ssl_write_record( ssl );
1641
1642 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1643
1644 return( ret );
1645}
1646
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001647#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1648 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001649 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1650 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001651static int ssl_write_certificate_request( ssl_context *ssl )
1652{
Paul Bakkered27a042013-04-18 22:46:23 +02001653 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1654 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001655
1656 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1657
1658 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1659 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1660 {
1661 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1662 ssl->state++;
1663 return( 0 );
1664 }
1665
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001666 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001667 return( ret );
1668}
1669#else
1670static int ssl_write_certificate_request( ssl_context *ssl )
1671{
1672 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1673 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001674 size_t dn_size, total_dn_size; /* excluding length bytes */
1675 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00001676 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001677 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001678
1679 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1680
1681 ssl->state++;
1682
Paul Bakkerfbb17802013-04-17 19:10:21 +02001683 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001684 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001685 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001686 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001687 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001688 return( 0 );
1689 }
1690
1691 /*
1692 * 0 . 0 handshake type
1693 * 1 . 3 handshake length
1694 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01001695 * 5 .. m-1 cert types
1696 * m .. m+1 sig alg length (TLS 1.2 only)
1697 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001698 * n .. n+1 length of all DNs
1699 * n+2 .. n+3 length of DN 1
1700 * n+4 .. ... Distinguished Name #1
1701 * ... .. ... length of DN 2, etc.
1702 */
1703 buf = ssl->out_msg;
1704 p = buf + 4;
1705
1706 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001707 * Supported certificate types
1708 *
1709 * ClientCertificateType certificate_types<1..2^8-1>;
1710 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00001711 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001712 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01001713
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001714#if defined(POLARSSL_RSA_C)
1715 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
1716#endif
1717#if defined(POLARSSL_ECDSA_C)
1718 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
1719#endif
1720
1721 p[0] = ct_len++;
1722 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01001723
Paul Bakker577e0062013-08-28 11:57:20 +02001724 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001725#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01001726 /*
1727 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01001728 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001729 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
1730 *
1731 * struct {
1732 * HashAlgorithm hash;
1733 * SignatureAlgorithm signature;
1734 * } SignatureAndHashAlgorithm;
1735 *
1736 * enum { (255) } HashAlgorithm;
1737 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01001738 */
Paul Bakker21dca692013-01-03 11:41:08 +01001739 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01001740 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001741 /*
1742 * Only use current running hash algorithm that is already required
1743 * for requested ciphersuite.
1744 */
Paul Bakker926af752012-11-23 13:38:07 +01001745 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
1746
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001747 if( ssl->transform_negotiate->ciphersuite_info->mac ==
1748 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01001749 {
1750 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
1751 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02001752
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001753 /*
1754 * Supported signature algorithms
1755 */
1756#if defined(POLARSSL_RSA_C)
1757 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1758 p[2 + sa_len++] = SSL_SIG_RSA;
1759#endif
1760#if defined(POLARSSL_ECDSA_C)
1761 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1762 p[2 + sa_len++] = SSL_SIG_ECDSA;
1763#endif
Paul Bakker926af752012-11-23 13:38:07 +01001764
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001765 p[0] = (unsigned char)( sa_len >> 8 );
1766 p[1] = (unsigned char)( sa_len );
1767 sa_len += 2;
1768 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01001769 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001770#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001771
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001772 /*
1773 * DistinguishedName certificate_authorities<0..2^16-1>;
1774 * opaque DistinguishedName<1..2^16-1>;
1775 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001776 p += 2;
1777 crt = ssl->ca_chain;
1778
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001779 total_dn_size = 0;
Paul Bakker29087132010-03-21 21:03:34 +00001780 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001781 {
1782 if( p - buf > 4096 )
1783 break;
1784
Paul Bakker926af752012-11-23 13:38:07 +01001785 dn_size = crt->subject_raw.len;
1786 *p++ = (unsigned char)( dn_size >> 8 );
1787 *p++ = (unsigned char)( dn_size );
1788 memcpy( p, crt->subject_raw.p, dn_size );
1789 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001790
Paul Bakker926af752012-11-23 13:38:07 +01001791 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
1792
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001793 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01001794 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00001795 }
1796
Paul Bakker926af752012-11-23 13:38:07 +01001797 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001798 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1799 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001800 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
1801 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001802
1803 ret = ssl_write_record( ssl );
1804
1805 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
1806
1807 return( ret );
1808}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001809#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
1810 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
1811 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001812
Paul Bakker41c83d32013-03-20 14:39:14 +01001813static int ssl_write_server_key_exchange( ssl_context *ssl )
1814{
Paul Bakker23986e52011-04-24 08:57:21 +00001815 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001816 size_t n = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001817 const ssl_ciphersuite_t *ciphersuite_info;
1818
1819#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1820 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
1821 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1822 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001823 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001824 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001825 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001826 ((void) dig_signed);
1827 ((void) dig_signed_len);
1828#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001829
Paul Bakker41c83d32013-03-20 14:39:14 +01001830 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001831
1832 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
1833
Paul Bakker41c83d32013-03-20 14:39:14 +01001834 if( ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_RSA &&
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001835 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_ECDHE_RSA &&
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001836 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA &&
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001837 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001838 {
1839 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
1840 ssl->state++;
1841 return( 0 );
1842 }
1843
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001844#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1845 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1846 {
1847 /* TODO: Support identity hints */
1848 *(p++) = 0x00;
1849 *(p++) = 0x00;
1850
1851 n += 2;
1852 }
1853#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1854
1855#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1856 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1857 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1858 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00001859 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001860 /*
1861 * Ephemeral DH parameters:
1862 *
1863 * struct {
1864 * opaque dh_p<1..2^16-1>;
1865 * opaque dh_g<1..2^16-1>;
1866 * opaque dh_Ys<1..2^16-1>;
1867 * } ServerDHParams;
1868 */
1869 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
1870 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
1871 {
1872 SSL_DEBUG_RET( 1, "mpi_copy", ret );
1873 return( ret );
1874 }
Paul Bakker48916f92012-09-16 19:57:18 +00001875
Paul Bakker41c83d32013-03-20 14:39:14 +01001876 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
1877 mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001878 p,
1879 &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001880 {
1881 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
1882 return( ret );
1883 }
1884
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001885 dig_signed = p;
1886 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001887
1888 p += len;
1889 n += len;
1890
Paul Bakker41c83d32013-03-20 14:39:14 +01001891 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
1892 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1893 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1894 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
1895 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001896#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1897 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001898
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001899#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1900 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1901 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1902 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001903 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001904 /*
1905 * Ephemeral ECDH parameters:
1906 *
1907 * struct {
1908 * ECParameters curve_params;
1909 * ECPoint public;
1910 * } ServerECDHParams;
1911 */
Paul Bakker41c83d32013-03-20 14:39:14 +01001912 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
1913 ssl->handshake->ec_curve ) ) != 0 )
1914 {
1915 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
1916 return( ret );
1917 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001918
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001919 SSL_DEBUG_MSG( 2, ( "ECDH curve size: %d",
1920 (int) ssl->handshake->ecdh_ctx.grp.nbits ) );
1921
Paul Bakker41c83d32013-03-20 14:39:14 +01001922 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001923 &len,
1924 p,
Paul Bakker41c83d32013-03-20 14:39:14 +01001925 1000, ssl->f_rng, ssl->p_rng ) ) != 0 )
1926 {
1927 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
1928 return( ret );
1929 }
1930
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001931 dig_signed = p;
1932 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001933
1934 p += len;
1935 n += len;
1936
Paul Bakker41c83d32013-03-20 14:39:14 +01001937 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
1938 }
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001939#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1940 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001941
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001942#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001943 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1944 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001945 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001946 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1947 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001948 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001949 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001950 unsigned int hashlen = 0;
1951 unsigned char hash[64];
1952 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00001953
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001954 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001955 * Choose hash algorithm. NONE means MD5 + SHA1 here.
1956 */
Paul Bakker577e0062013-08-28 11:57:20 +02001957#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001958 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
1959 {
1960 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
1961
1962 if( md_alg == POLARSSL_MD_NONE )
1963 {
1964 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1965 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1966 }
1967 }
Paul Bakker577e0062013-08-28 11:57:20 +02001968 else
1969#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001970#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1971 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker577e0062013-08-28 11:57:20 +02001972 if ( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001973 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
1974 {
1975 md_alg = POLARSSL_MD_SHA1;
1976 }
1977 else
Paul Bakker577e0062013-08-28 11:57:20 +02001978#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001979 {
1980 md_alg = POLARSSL_MD_NONE;
1981 }
1982
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001983 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001984 * Compute the hash to be signed
1985 */
Paul Bakker577e0062013-08-28 11:57:20 +02001986#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1987 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001988 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00001989 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001990 md5_context md5;
1991 sha1_context sha1;
1992
1993 /*
1994 * digitally-signed struct {
1995 * opaque md5_hash[16];
1996 * opaque sha_hash[20];
1997 * };
1998 *
1999 * md5_hash
2000 * MD5(ClientHello.random + ServerHello.random
2001 * + ServerParams);
2002 * sha_hash
2003 * SHA(ClientHello.random + ServerHello.random
2004 * + ServerParams);
2005 */
2006 md5_starts( &md5 );
2007 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002008 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002009 md5_finish( &md5, hash );
2010
2011 sha1_starts( &sha1 );
2012 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002013 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002014 sha1_finish( &sha1, hash + 16 );
2015
2016 hashlen = 36;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002017 }
2018 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002019#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2020 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002021#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2022 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002023 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002024 {
2025 md_context_t ctx;
2026
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002027 /* Info from md_alg will be used instead */
2028 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002029
2030 /*
2031 * digitally-signed struct {
2032 * opaque client_random[32];
2033 * opaque server_random[32];
2034 * ServerDHParams params;
2035 * };
2036 */
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002037 if( ( ret = md_init_ctx( &ctx, md_info_from_type(md_alg) ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002038 {
2039 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2040 return( ret );
2041 }
2042
2043 md_starts( &ctx );
2044 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002045 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002046 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02002047
2048 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
2049 {
2050 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
2051 return( ret );
2052 }
2053
Paul Bakker23f36802012-09-28 14:15:14 +00002054 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002055 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002056#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2057 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002058 {
2059 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002060 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02002061 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002062
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002063 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2064 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002065
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002066 /*
2067 * Make the signature
2068 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02002069 if( ssl->pk_key == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002070 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002071 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2072 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002073 }
Paul Bakker23f36802012-09-28 14:15:14 +00002074
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002075#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002076 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2077 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002078 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002079 *(p++) = ssl_sig_from_pk( ssl->pk_key );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002080
2081 n += 2;
2082 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002083#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002084
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002085 if( ( ret = pk_sign( ssl->pk_key, md_alg, hash, hashlen,
2086 p + 2 , &signature_len,
2087 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002088 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002089 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002090 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002091 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002092
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002093 *(p++) = (unsigned char)( signature_len >> 8 );
2094 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002095 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002096
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002097 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002098
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002099 p += signature_len;
2100 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002101 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002102#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002103 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2104 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002105
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002106 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002107 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2108 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2109
2110 ssl->state++;
2111
2112 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2113 {
2114 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2115 return( ret );
2116 }
2117
2118 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2119
2120 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002121}
2122
2123static int ssl_write_server_hello_done( ssl_context *ssl )
2124{
2125 int ret;
2126
2127 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2128
2129 ssl->out_msglen = 4;
2130 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2131 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2132
2133 ssl->state++;
2134
2135 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2136 {
2137 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2138 return( ret );
2139 }
2140
2141 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2142
2143 return( 0 );
2144}
2145
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002146#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2147 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2148static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2149 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002150{
2151 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002152 size_t n;
2153
2154 /*
2155 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2156 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002157 if( *p + 2 > end )
2158 {
2159 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2160 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2161 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002162
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002163 n = ( (*p)[0] << 8 ) | (*p)[1];
2164 *p += 2;
2165
2166 if( n < 1 || n > ssl->handshake->dhm_ctx.len || *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002167 {
2168 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2169 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2170 }
2171
2172 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002173 *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002174 {
2175 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2176 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2177 }
2178
2179 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2180
Paul Bakker70df2fb2013-04-17 17:19:09 +02002181 return( ret );
2182}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002183#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2184 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002185
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02002186#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2187 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002188static int ssl_parse_client_ecdh_public( ssl_context *ssl )
2189{
2190 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002191 size_t n;
2192
2193 /*
2194 * Receive client public key and calculate premaster
2195 */
2196 n = ssl->in_msg[3];
2197
2198 if( n < 1 || n > mpi_size( &ssl->handshake->ecdh_ctx.grp.P ) * 2 + 2 ||
2199 n + 4 != ssl->in_hslen )
2200 {
2201 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2202 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2203 }
2204
2205 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2206 ssl->in_msg + 4, n ) ) != 0 )
2207 {
2208 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2209 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2210 }
2211
2212 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2213
Paul Bakker70df2fb2013-04-17 17:19:09 +02002214 return( ret );
2215}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02002216#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2217 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002218
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002219#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002220static int ssl_parse_encrypted_pms_secret( ssl_context *ssl )
2221{
2222 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2223 size_t i, n = 0;
2224
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002225 if( ! pk_can_do( ssl->pk_key, POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002226 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002227 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002228 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2229 }
2230
2231 /*
2232 * Decrypt the premaster using own private RSA key
2233 */
2234 i = 4;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002235 n = pk_get_len( ssl->pk_key );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002236 ssl->handshake->pmslen = 48;
2237
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002238#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2239 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002240 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2241 {
2242 i += 2;
2243 if( ssl->in_msg[4] != ( ( n >> 8 ) & 0xFF ) ||
2244 ssl->in_msg[5] != ( ( n ) & 0xFF ) )
2245 {
2246 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2247 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2248 }
2249 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002250#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002251
2252 if( ssl->in_hslen != i + n )
2253 {
2254 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2255 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2256 }
2257
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002258 ret = pk_decrypt( ssl->pk_key,
2259 ssl->in_msg + i, n,
2260 ssl->handshake->premaster, &ssl->handshake->pmslen,
2261 sizeof(ssl->handshake->premaster),
2262 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002263
2264 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Paul Bakker2fbefde2013-06-29 16:01:15 +02002265 ssl->handshake->premaster[0] != ssl->handshake->max_major_ver ||
2266 ssl->handshake->premaster[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002267 {
2268 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2269
2270 /*
2271 * Protection against Bleichenbacher's attack:
2272 * invalid PKCS#1 v1.5 padding must not cause
2273 * the connection to end immediately; instead,
2274 * send a bad_record_mac later in the handshake.
2275 */
2276 ssl->handshake->pmslen = 48;
2277
2278 ret = ssl->f_rng( ssl->p_rng, ssl->handshake->premaster,
2279 ssl->handshake->pmslen );
2280 if( ret != 0 )
2281 return( ret );
2282 }
2283
2284 return( ret );
2285}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002286#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002287
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002288#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2289 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2290static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2291 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002292{
Paul Bakker6db455e2013-09-18 17:29:31 +02002293 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002294 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002295
Paul Bakker6db455e2013-09-18 17:29:31 +02002296 if( ssl->f_psk == NULL &&
2297 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2298 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002299 {
2300 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2301 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2302 }
2303
2304 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002305 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002306 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002307 if( *p + 2 > end )
2308 {
2309 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2310 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2311 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002312
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002313 n = ( (*p)[0] << 8 ) | (*p)[1];
2314 *p += 2;
2315
2316 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002317 {
2318 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2319 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2320 }
2321
Paul Bakker6db455e2013-09-18 17:29:31 +02002322 if( ssl->f_psk != NULL )
2323 {
2324 if( ( ret != ssl->f_psk( ssl->p_psk, ssl, *p, n ) ) != 0 )
2325 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2326 }
2327
2328 if( ret == 0 )
2329 {
2330 if( n != ssl->psk_identity_len ||
2331 memcmp( ssl->psk_identity, *p, n ) != 0 )
2332 {
2333 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2334 }
2335 }
2336
2337 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002338 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002339 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002340 if( ( ret = ssl_send_alert_message( ssl,
2341 SSL_ALERT_LEVEL_FATAL,
2342 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2343 {
2344 return( ret );
2345 }
2346
2347 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002348 }
2349
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002350 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002351 ret = 0;
2352
Paul Bakkerfbb17802013-04-17 19:10:21 +02002353 return( ret );
2354}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002355#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
2356 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002357
Paul Bakker5121ce52009-01-03 21:22:43 +00002358static int ssl_parse_client_key_exchange( ssl_context *ssl )
2359{
Paul Bakker23986e52011-04-24 08:57:21 +00002360 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002361 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002362
Paul Bakker41c83d32013-03-20 14:39:14 +01002363 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002364
2365 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2366
2367 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2368 {
2369 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2370 return( ret );
2371 }
2372
2373 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2374 {
2375 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002376 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002377 }
2378
2379 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2380 {
2381 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002382 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002383 }
2384
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002385#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002386 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002387 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002388 unsigned char *p = ssl->in_msg + 4;
2389 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2390
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002391 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002392 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002393 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2394 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002395 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002396
2397 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
2398
Manuel Pégourié-Gonnard032c34e2013-09-07 13:06:27 +02002399 /* No blinding needed for DHE, but will be needed for fixed DH! */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002400 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2401 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002402 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002403 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002404 {
2405 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2406 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2407 }
2408
2409 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002410 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002411 else
2412#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002413#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2414 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2415 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2416 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002417 {
2418 if( ( ret = ssl_parse_client_ecdh_public( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002419 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002420 SSL_DEBUG_RET( 1, ( "ssl_parse_client_ecdh_public" ), ret );
2421 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002422 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002423
2424 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2425 &ssl->handshake->pmslen,
2426 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002427 POLARSSL_MPI_MAX_SIZE,
2428 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002429 {
2430 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2431 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2432 }
2433
2434 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002435 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002436 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002437#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2438 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002439#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2440 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002441 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002442 unsigned char *p = ssl->in_msg + 4;
2443 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2444
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002445 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002446 {
2447 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2448 return( ret );
2449 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002450
2451 // Set up the premaster secret
2452 //
2453 p = ssl->handshake->premaster;
2454 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2455 *(p++) = (unsigned char)( ssl->psk_len );
2456 p += ssl->psk_len;
2457
2458 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2459 *(p++) = (unsigned char)( ssl->psk_len );
2460 memcpy( p, ssl->psk, ssl->psk_len );
2461 p += ssl->psk_len;
2462
2463 ssl->handshake->pmslen = 4 + 2 * ssl->psk_len;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002464 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002465 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002466#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
2467#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2468 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2469 {
2470 size_t n;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002471 unsigned char *p = ssl->in_msg + 4;
2472 unsigned char *end = ssl->in_msg + ssl->in_msglen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002473
2474 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2475 {
2476 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2477 return( ret );
2478 }
2479 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2480 {
2481 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2482 return( ret );
2483 }
2484
2485 // Set up the premaster secret
2486 //
2487 p = ssl->handshake->premaster;
2488 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len >> 8 );
2489 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len );
2490
Paul Bakker577e0062013-08-28 11:57:20 +02002491 n = ssl->handshake->dhm_ctx.len;
2492
Manuel Pégourié-Gonnard032c34e2013-09-07 13:06:27 +02002493 /* No blinding needed since this is ephemeral DHM */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002494 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002495 p, &n, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002496 {
2497 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2498 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2499 }
2500
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002501 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2502
2503 p += ssl->handshake->dhm_ctx.len;
2504
2505 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2506 *(p++) = (unsigned char)( ssl->psk_len );
2507 memcpy( p, ssl->psk, ssl->psk_len );
2508 p += ssl->psk_len;
2509
2510 ssl->handshake->pmslen = 4 + ssl->handshake->dhm_ctx.len + ssl->psk_len;
2511 }
2512 else
2513#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
2514#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2515 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002516 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002517 if( ( ret = ssl_parse_encrypted_pms_secret( ssl ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002518 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002519 SSL_DEBUG_RET( 1, ( "ssl_parse_client_ecdh_public" ), ret );
2520 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002521 }
2522 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002523 else
2524#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2525 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002526 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002527 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2528 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002529
Paul Bakkerff60ee62010-03-16 21:09:09 +00002530 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2531 {
2532 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2533 return( ret );
2534 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002535
Paul Bakker5121ce52009-01-03 21:22:43 +00002536 ssl->state++;
2537
2538 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2539
2540 return( 0 );
2541}
2542
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002543#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2544 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002545 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2546 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002547static int ssl_parse_certificate_verify( ssl_context *ssl )
2548{
Paul Bakkered27a042013-04-18 22:46:23 +02002549 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002550 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002551
2552 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2553
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002554 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2555 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002556 {
2557 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2558 ssl->state++;
2559 return( 0 );
2560 }
2561
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002562 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002563 return( ret );
2564}
2565#else
2566static int ssl_parse_certificate_verify( ssl_context *ssl )
2567{
2568 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002569 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002570 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002571 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002572 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02002573#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002574 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02002575#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002576 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002577 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2578
2579 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2580
2581 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2582 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2583 {
2584 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2585 ssl->state++;
2586 return( 0 );
2587 }
2588
Paul Bakkered27a042013-04-18 22:46:23 +02002589 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002590 {
2591 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2592 ssl->state++;
2593 return( 0 );
2594 }
2595
Paul Bakker48916f92012-09-16 19:57:18 +00002596 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002597
2598 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2599 {
2600 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2601 return( ret );
2602 }
2603
2604 ssl->state++;
2605
2606 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2607 {
2608 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002609 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002610 }
2611
2612 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
2613 {
2614 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002615 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002616 }
2617
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002618 /*
2619 * 0 . 0 handshake type
2620 * 1 . 3 handshake length
2621 * 4 . 5 sig alg (TLS 1.2 only)
2622 * 4+n . 5+n signature length (n = sa_len)
2623 * 6+n . 6+n+m signature (m = sig_len)
2624 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002625
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002626#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2627 defined(POLARSSL_SSL_PROTO_TLS1_1)
2628 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002629 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002630 sa_len = 0;
2631
Paul Bakkerc70b9822013-04-07 22:00:46 +02002632 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002633 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002634
2635 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
2636 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
2637 POLARSSL_PK_ECDSA ) )
2638 {
2639 hash_start += 16;
2640 hashlen -= 16;
2641 md_alg = POLARSSL_MD_SHA1;
2642 }
Paul Bakker926af752012-11-23 13:38:07 +01002643 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002644 else
2645#endif
Paul Bakker577e0062013-08-28 11:57:20 +02002646#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2647 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002648 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002649 sa_len = 2;
2650
Paul Bakker5121ce52009-01-03 21:22:43 +00002651 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002652 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00002653 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002654 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002656 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2657 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01002658 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2659 }
2660
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002661 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01002662
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002663 /* Info from md_alg will be used instead */
2664 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002665
2666 /*
2667 * Signature
2668 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002669 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
2670 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002671 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002672 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2673 " for verify message" ) );
2674 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002675 }
2676
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002677 /*
2678 * Check the certificate's key type matches the signature alg
2679 */
2680 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
2681 {
2682 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
2683 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2684 }
Paul Bakker577e0062013-08-28 11:57:20 +02002685 }
2686 else
2687#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2688 {
2689 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002690 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002691 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002692
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002693 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01002694
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002695 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002696 {
2697 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002698 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002699 }
2700
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002701 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002702 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002703 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002704 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002705 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002706 return( ret );
2707 }
2708
2709 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
2710
Paul Bakkered27a042013-04-18 22:46:23 +02002711 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002712}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002713#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2714 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2715 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002716
Paul Bakkera503a632013-08-14 13:48:06 +02002717#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002718static int ssl_write_new_session_ticket( ssl_context *ssl )
2719{
2720 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002721 size_t tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002722
2723 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
2724
2725 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2726 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
2727
2728 /*
2729 * struct {
2730 * uint32 ticket_lifetime_hint;
2731 * opaque ticket<0..2^16-1>;
2732 * } NewSessionTicket;
2733 *
2734 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
2735 * 8 . 9 ticket_len (n)
2736 * 10 . 9+n ticket content
2737 */
2738 ssl->out_msg[4] = 0x00;
2739 ssl->out_msg[5] = 0x00;
2740 ssl->out_msg[6] = 0x00;
2741 ssl->out_msg[7] = 0x00;
2742
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02002743 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
2744 {
2745 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
2746 tlen = 0;
2747 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002748
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002749 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
2750 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002751
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002752 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002753
2754 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2755 {
2756 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2757 return( ret );
2758 }
2759
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002760 /* No need to remember writing a NewSessionTicket any more */
2761 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002762
2763 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
2764
2765 return( 0 );
2766}
Paul Bakkera503a632013-08-14 13:48:06 +02002767#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002768
Paul Bakker5121ce52009-01-03 21:22:43 +00002769/*
Paul Bakker1961b702013-01-25 14:49:24 +01002770 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002771 */
Paul Bakker1961b702013-01-25 14:49:24 +01002772int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002773{
2774 int ret = 0;
2775
Paul Bakker1961b702013-01-25 14:49:24 +01002776 if( ssl->state == SSL_HANDSHAKE_OVER )
2777 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002778
Paul Bakker1961b702013-01-25 14:49:24 +01002779 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
2780
2781 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2782 return( ret );
2783
2784 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002785 {
Paul Bakker1961b702013-01-25 14:49:24 +01002786 case SSL_HELLO_REQUEST:
2787 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002788 break;
2789
Paul Bakker1961b702013-01-25 14:49:24 +01002790 /*
2791 * <== ClientHello
2792 */
2793 case SSL_CLIENT_HELLO:
2794 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002795 break;
Paul Bakker1961b702013-01-25 14:49:24 +01002796
2797 /*
2798 * ==> ServerHello
2799 * Certificate
2800 * ( ServerKeyExchange )
2801 * ( CertificateRequest )
2802 * ServerHelloDone
2803 */
2804 case SSL_SERVER_HELLO:
2805 ret = ssl_write_server_hello( ssl );
2806 break;
2807
2808 case SSL_SERVER_CERTIFICATE:
2809 ret = ssl_write_certificate( ssl );
2810 break;
2811
2812 case SSL_SERVER_KEY_EXCHANGE:
2813 ret = ssl_write_server_key_exchange( ssl );
2814 break;
2815
2816 case SSL_CERTIFICATE_REQUEST:
2817 ret = ssl_write_certificate_request( ssl );
2818 break;
2819
2820 case SSL_SERVER_HELLO_DONE:
2821 ret = ssl_write_server_hello_done( ssl );
2822 break;
2823
2824 /*
2825 * <== ( Certificate/Alert )
2826 * ClientKeyExchange
2827 * ( CertificateVerify )
2828 * ChangeCipherSpec
2829 * Finished
2830 */
2831 case SSL_CLIENT_CERTIFICATE:
2832 ret = ssl_parse_certificate( ssl );
2833 break;
2834
2835 case SSL_CLIENT_KEY_EXCHANGE:
2836 ret = ssl_parse_client_key_exchange( ssl );
2837 break;
2838
2839 case SSL_CERTIFICATE_VERIFY:
2840 ret = ssl_parse_certificate_verify( ssl );
2841 break;
2842
2843 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2844 ret = ssl_parse_change_cipher_spec( ssl );
2845 break;
2846
2847 case SSL_CLIENT_FINISHED:
2848 ret = ssl_parse_finished( ssl );
2849 break;
2850
2851 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002852 * ==> ( NewSessionTicket )
2853 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002854 * Finished
2855 */
2856 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002857#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002858 if( ssl->handshake->new_session_ticket != 0 )
2859 ret = ssl_write_new_session_ticket( ssl );
2860 else
Paul Bakkera503a632013-08-14 13:48:06 +02002861#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002862 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002863 break;
2864
2865 case SSL_SERVER_FINISHED:
2866 ret = ssl_write_finished( ssl );
2867 break;
2868
2869 case SSL_FLUSH_BUFFERS:
2870 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2871 ssl->state = SSL_HANDSHAKE_WRAPUP;
2872 break;
2873
2874 case SSL_HANDSHAKE_WRAPUP:
2875 ssl_handshake_wrapup( ssl );
2876 break;
2877
2878 default:
2879 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2880 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002881 }
2882
Paul Bakker5121ce52009-01-03 21:22:43 +00002883 return( ret );
2884}
Paul Bakker5121ce52009-01-03 21:22:43 +00002885#endif