blob: 47e3e272c6019ba2762722441d1fd655bde5aac8 [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 &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001309 ( ssl_own_key( ssl ) == NULL ||
1310 ! pk_can_do( ssl_own_key( ssl ), 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
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001543#if defined(POLARSSL_HAVE_TIME)
1544 ssl->session_negotiate->start = time( NULL );
1545#endif
1546
Paul Bakkera503a632013-08-14 13:48:06 +02001547#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001548 if( ssl->handshake->new_session_ticket != 0 )
1549 {
1550 ssl->session_negotiate->length = n = 0;
1551 memset( ssl->session_negotiate->id, 0, 32 );
1552 }
1553 else
1554#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001555 {
1556 ssl->session_negotiate->length = n = 32;
1557 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001558 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001559 return( ret );
1560 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001561 }
1562 else
1563 {
1564 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001565 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001566 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001567 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001568 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001569
1570 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1571 {
1572 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1573 return( ret );
1574 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001575 }
1576
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001577 /*
1578 * 38 . 38 session id length
1579 * 39 . 38+n session id
1580 * 39+n . 40+n chosen ciphersuite
1581 * 41+n . 41+n chosen compression alg.
1582 * 42+n . 43+n extensions length
1583 * 44+n . 43+n+m extensions
1584 */
1585 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001586 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1587 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001588
1589 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1590 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1591 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001592 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001593
Paul Bakker48916f92012-09-16 19:57:18 +00001594 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1595 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1596 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001597
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001598 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
1599 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001600 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001601 ssl->session_negotiate->compression ) );
1602
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001603 /*
1604 * First write extensions, then the total length
1605 */
1606 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1607 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001608
Paul Bakker05decb22013-08-15 13:33:48 +02001609#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001610 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1611 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001612#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001613
Paul Bakker1f2bc622013-08-15 13:45:55 +02001614#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001615 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1616 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001617#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001618
Paul Bakkera503a632013-08-14 13:48:06 +02001619#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001620 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1621 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001622#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001623
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001624#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001625 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1626 ext_len += olen;
1627#endif
1628
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001629 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001630
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001631 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1632 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1633 p += ext_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001634
1635 ssl->out_msglen = p - buf;
1636 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1637 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1638
1639 ret = ssl_write_record( ssl );
1640
1641 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1642
1643 return( ret );
1644}
1645
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001646#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1647 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001648 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1649 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001650static int ssl_write_certificate_request( ssl_context *ssl )
1651{
Paul Bakkered27a042013-04-18 22:46:23 +02001652 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1653 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001654
1655 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1656
1657 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1658 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1659 {
1660 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1661 ssl->state++;
1662 return( 0 );
1663 }
1664
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001665 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001666 return( ret );
1667}
1668#else
1669static int ssl_write_certificate_request( ssl_context *ssl )
1670{
1671 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1672 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001673 size_t dn_size, total_dn_size; /* excluding length bytes */
1674 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00001675 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001676 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001677
1678 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1679
1680 ssl->state++;
1681
Paul Bakkerfbb17802013-04-17 19:10:21 +02001682 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001683 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001684 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001685 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001686 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001687 return( 0 );
1688 }
1689
1690 /*
1691 * 0 . 0 handshake type
1692 * 1 . 3 handshake length
1693 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01001694 * 5 .. m-1 cert types
1695 * m .. m+1 sig alg length (TLS 1.2 only)
1696 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001697 * n .. n+1 length of all DNs
1698 * n+2 .. n+3 length of DN 1
1699 * n+4 .. ... Distinguished Name #1
1700 * ... .. ... length of DN 2, etc.
1701 */
1702 buf = ssl->out_msg;
1703 p = buf + 4;
1704
1705 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001706 * Supported certificate types
1707 *
1708 * ClientCertificateType certificate_types<1..2^8-1>;
1709 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00001710 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001711 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01001712
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001713#if defined(POLARSSL_RSA_C)
1714 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
1715#endif
1716#if defined(POLARSSL_ECDSA_C)
1717 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
1718#endif
1719
1720 p[0] = ct_len++;
1721 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01001722
Paul Bakker577e0062013-08-28 11:57:20 +02001723 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001724#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01001725 /*
1726 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01001727 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001728 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
1729 *
1730 * struct {
1731 * HashAlgorithm hash;
1732 * SignatureAlgorithm signature;
1733 * } SignatureAndHashAlgorithm;
1734 *
1735 * enum { (255) } HashAlgorithm;
1736 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01001737 */
Paul Bakker21dca692013-01-03 11:41:08 +01001738 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01001739 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001740 /*
1741 * Only use current running hash algorithm that is already required
1742 * for requested ciphersuite.
1743 */
Paul Bakker926af752012-11-23 13:38:07 +01001744 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
1745
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001746 if( ssl->transform_negotiate->ciphersuite_info->mac ==
1747 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01001748 {
1749 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
1750 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02001751
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001752 /*
1753 * Supported signature algorithms
1754 */
1755#if defined(POLARSSL_RSA_C)
1756 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1757 p[2 + sa_len++] = SSL_SIG_RSA;
1758#endif
1759#if defined(POLARSSL_ECDSA_C)
1760 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1761 p[2 + sa_len++] = SSL_SIG_ECDSA;
1762#endif
Paul Bakker926af752012-11-23 13:38:07 +01001763
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001764 p[0] = (unsigned char)( sa_len >> 8 );
1765 p[1] = (unsigned char)( sa_len );
1766 sa_len += 2;
1767 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01001768 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001769#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001770
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001771 /*
1772 * DistinguishedName certificate_authorities<0..2^16-1>;
1773 * opaque DistinguishedName<1..2^16-1>;
1774 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001775 p += 2;
1776 crt = ssl->ca_chain;
1777
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001778 total_dn_size = 0;
Paul Bakker29087132010-03-21 21:03:34 +00001779 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001780 {
1781 if( p - buf > 4096 )
1782 break;
1783
Paul Bakker926af752012-11-23 13:38:07 +01001784 dn_size = crt->subject_raw.len;
1785 *p++ = (unsigned char)( dn_size >> 8 );
1786 *p++ = (unsigned char)( dn_size );
1787 memcpy( p, crt->subject_raw.p, dn_size );
1788 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001789
Paul Bakker926af752012-11-23 13:38:07 +01001790 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
1791
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001792 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01001793 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00001794 }
1795
Paul Bakker926af752012-11-23 13:38:07 +01001796 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001797 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1798 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001799 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
1800 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001801
1802 ret = ssl_write_record( ssl );
1803
1804 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
1805
1806 return( ret );
1807}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001808#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
1809 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
1810 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001811
Paul Bakker41c83d32013-03-20 14:39:14 +01001812static int ssl_write_server_key_exchange( ssl_context *ssl )
1813{
Paul Bakker23986e52011-04-24 08:57:21 +00001814 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001815 size_t n = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001816 const ssl_ciphersuite_t *ciphersuite_info;
1817
1818#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1819 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
1820 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1821 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001822 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001823 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001824 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001825 ((void) dig_signed);
1826 ((void) dig_signed_len);
1827#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001828
Paul Bakker41c83d32013-03-20 14:39:14 +01001829 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001830
1831 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
1832
Paul Bakker41c83d32013-03-20 14:39:14 +01001833 if( ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_RSA &&
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001834 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_ECDHE_RSA &&
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001835 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA &&
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001836 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001837 {
1838 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
1839 ssl->state++;
1840 return( 0 );
1841 }
1842
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001843#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1844 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1845 {
1846 /* TODO: Support identity hints */
1847 *(p++) = 0x00;
1848 *(p++) = 0x00;
1849
1850 n += 2;
1851 }
1852#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1853
1854#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1855 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1856 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1857 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00001858 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001859 /*
1860 * Ephemeral DH parameters:
1861 *
1862 * struct {
1863 * opaque dh_p<1..2^16-1>;
1864 * opaque dh_g<1..2^16-1>;
1865 * opaque dh_Ys<1..2^16-1>;
1866 * } ServerDHParams;
1867 */
1868 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
1869 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
1870 {
1871 SSL_DEBUG_RET( 1, "mpi_copy", ret );
1872 return( ret );
1873 }
Paul Bakker48916f92012-09-16 19:57:18 +00001874
Paul Bakker41c83d32013-03-20 14:39:14 +01001875 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
1876 mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001877 p,
1878 &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001879 {
1880 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
1881 return( ret );
1882 }
1883
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001884 dig_signed = p;
1885 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001886
1887 p += len;
1888 n += len;
1889
Paul Bakker41c83d32013-03-20 14:39:14 +01001890 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
1891 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1892 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1893 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
1894 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001895#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1896 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001897
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001898#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1899 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1900 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1901 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001902 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001903 /*
1904 * Ephemeral ECDH parameters:
1905 *
1906 * struct {
1907 * ECParameters curve_params;
1908 * ECPoint public;
1909 * } ServerECDHParams;
1910 */
Paul Bakker41c83d32013-03-20 14:39:14 +01001911 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
1912 ssl->handshake->ec_curve ) ) != 0 )
1913 {
1914 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
1915 return( ret );
1916 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001917
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001918 SSL_DEBUG_MSG( 2, ( "ECDH curve size: %d",
1919 (int) ssl->handshake->ecdh_ctx.grp.nbits ) );
1920
Paul Bakker41c83d32013-03-20 14:39:14 +01001921 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001922 &len,
1923 p,
Paul Bakker41c83d32013-03-20 14:39:14 +01001924 1000, ssl->f_rng, ssl->p_rng ) ) != 0 )
1925 {
1926 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
1927 return( ret );
1928 }
1929
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001930 dig_signed = p;
1931 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001932
1933 p += len;
1934 n += len;
1935
Paul Bakker41c83d32013-03-20 14:39:14 +01001936 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
1937 }
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001938#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1939 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001940
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001941#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001942 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1943 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001944 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001945 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1946 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001947 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001948 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001949 unsigned int hashlen = 0;
1950 unsigned char hash[64];
1951 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00001952
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001953 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001954 * Choose hash algorithm. NONE means MD5 + SHA1 here.
1955 */
Paul Bakker577e0062013-08-28 11:57:20 +02001956#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001957 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
1958 {
1959 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
1960
1961 if( md_alg == POLARSSL_MD_NONE )
1962 {
1963 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1964 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1965 }
1966 }
Paul Bakker577e0062013-08-28 11:57:20 +02001967 else
1968#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001969#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1970 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker577e0062013-08-28 11:57:20 +02001971 if ( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001972 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
1973 {
1974 md_alg = POLARSSL_MD_SHA1;
1975 }
1976 else
Paul Bakker577e0062013-08-28 11:57:20 +02001977#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001978 {
1979 md_alg = POLARSSL_MD_NONE;
1980 }
1981
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001982 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001983 * Compute the hash to be signed
1984 */
Paul Bakker577e0062013-08-28 11:57:20 +02001985#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1986 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001987 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00001988 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001989 md5_context md5;
1990 sha1_context sha1;
1991
1992 /*
1993 * digitally-signed struct {
1994 * opaque md5_hash[16];
1995 * opaque sha_hash[20];
1996 * };
1997 *
1998 * md5_hash
1999 * MD5(ClientHello.random + ServerHello.random
2000 * + ServerParams);
2001 * sha_hash
2002 * SHA(ClientHello.random + ServerHello.random
2003 * + ServerParams);
2004 */
2005 md5_starts( &md5 );
2006 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002007 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002008 md5_finish( &md5, hash );
2009
2010 sha1_starts( &sha1 );
2011 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002012 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002013 sha1_finish( &sha1, hash + 16 );
2014
2015 hashlen = 36;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002016 }
2017 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002018#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2019 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002020#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2021 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002022 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002023 {
2024 md_context_t ctx;
2025
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002026 /* Info from md_alg will be used instead */
2027 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002028
2029 /*
2030 * digitally-signed struct {
2031 * opaque client_random[32];
2032 * opaque server_random[32];
2033 * ServerDHParams params;
2034 * };
2035 */
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002036 if( ( ret = md_init_ctx( &ctx, md_info_from_type(md_alg) ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002037 {
2038 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2039 return( ret );
2040 }
2041
2042 md_starts( &ctx );
2043 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002044 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002045 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02002046
2047 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
2048 {
2049 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
2050 return( ret );
2051 }
2052
Paul Bakker23f36802012-09-28 14:15:14 +00002053 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002054 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002055#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2056 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002057 {
2058 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002059 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02002060 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002061
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002062 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2063 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002064
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002065 /*
2066 * Make the signature
2067 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002068 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002069 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002070 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2071 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002072 }
Paul Bakker23f36802012-09-28 14:15:14 +00002073
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002074#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002075 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2076 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002077 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002078 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002079
2080 n += 2;
2081 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002082#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002083
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002084 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002085 p + 2 , &signature_len,
2086 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002087 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002088 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002089 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002090 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002091
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002092 *(p++) = (unsigned char)( signature_len >> 8 );
2093 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002094 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002095
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002096 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002097
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002098 p += signature_len;
2099 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002100 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002101#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002102 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2103 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002104
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002105 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002106 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2107 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2108
2109 ssl->state++;
2110
2111 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2112 {
2113 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2114 return( ret );
2115 }
2116
2117 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2118
2119 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002120}
2121
2122static int ssl_write_server_hello_done( ssl_context *ssl )
2123{
2124 int ret;
2125
2126 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2127
2128 ssl->out_msglen = 4;
2129 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2130 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2131
2132 ssl->state++;
2133
2134 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2135 {
2136 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2137 return( ret );
2138 }
2139
2140 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2141
2142 return( 0 );
2143}
2144
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002145#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2146 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2147static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2148 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002149{
2150 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002151 size_t n;
2152
2153 /*
2154 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2155 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002156 if( *p + 2 > end )
2157 {
2158 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2159 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2160 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002161
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002162 n = ( (*p)[0] << 8 ) | (*p)[1];
2163 *p += 2;
2164
2165 if( n < 1 || n > ssl->handshake->dhm_ctx.len || *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002166 {
2167 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2168 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2169 }
2170
2171 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002172 *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002173 {
2174 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2175 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2176 }
2177
2178 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2179
Paul Bakker70df2fb2013-04-17 17:19:09 +02002180 return( ret );
2181}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002182#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2183 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002184
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02002185#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2186 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002187static int ssl_parse_client_ecdh_public( ssl_context *ssl )
2188{
2189 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002190 size_t n;
2191
2192 /*
2193 * Receive client public key and calculate premaster
2194 */
2195 n = ssl->in_msg[3];
2196
2197 if( n < 1 || n > mpi_size( &ssl->handshake->ecdh_ctx.grp.P ) * 2 + 2 ||
2198 n + 4 != ssl->in_hslen )
2199 {
2200 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2201 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2202 }
2203
2204 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2205 ssl->in_msg + 4, n ) ) != 0 )
2206 {
2207 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2208 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2209 }
2210
2211 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2212
Paul Bakker70df2fb2013-04-17 17:19:09 +02002213 return( ret );
2214}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02002215#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2216 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002217
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002218#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002219static int ssl_parse_encrypted_pms_secret( ssl_context *ssl )
2220{
2221 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2222 size_t i, n = 0;
2223
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002224 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002225 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002226 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002227 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2228 }
2229
2230 /*
2231 * Decrypt the premaster using own private RSA key
2232 */
2233 i = 4;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002234 n = pk_get_len( ssl_own_key( ssl ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002235 ssl->handshake->pmslen = 48;
2236
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002237#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2238 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002239 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2240 {
2241 i += 2;
2242 if( ssl->in_msg[4] != ( ( n >> 8 ) & 0xFF ) ||
2243 ssl->in_msg[5] != ( ( n ) & 0xFF ) )
2244 {
2245 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2246 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2247 }
2248 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002249#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002250
2251 if( ssl->in_hslen != i + n )
2252 {
2253 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2254 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2255 }
2256
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002257 ret = pk_decrypt( ssl_own_key( ssl ),
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002258 ssl->in_msg + i, n,
2259 ssl->handshake->premaster, &ssl->handshake->pmslen,
2260 sizeof(ssl->handshake->premaster),
2261 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002262
2263 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Paul Bakker2fbefde2013-06-29 16:01:15 +02002264 ssl->handshake->premaster[0] != ssl->handshake->max_major_ver ||
2265 ssl->handshake->premaster[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002266 {
2267 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2268
2269 /*
2270 * Protection against Bleichenbacher's attack:
2271 * invalid PKCS#1 v1.5 padding must not cause
2272 * the connection to end immediately; instead,
2273 * send a bad_record_mac later in the handshake.
2274 */
2275 ssl->handshake->pmslen = 48;
2276
2277 ret = ssl->f_rng( ssl->p_rng, ssl->handshake->premaster,
2278 ssl->handshake->pmslen );
2279 if( ret != 0 )
2280 return( ret );
2281 }
2282
2283 return( ret );
2284}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002285#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002286
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002287#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2288 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2289static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2290 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002291{
Paul Bakker6db455e2013-09-18 17:29:31 +02002292 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002293 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002294
Paul Bakker6db455e2013-09-18 17:29:31 +02002295 if( ssl->f_psk == NULL &&
2296 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2297 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002298 {
2299 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2300 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2301 }
2302
2303 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002304 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002305 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002306 if( *p + 2 > end )
2307 {
2308 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2309 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2310 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002311
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002312 n = ( (*p)[0] << 8 ) | (*p)[1];
2313 *p += 2;
2314
2315 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002316 {
2317 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2318 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2319 }
2320
Paul Bakker6db455e2013-09-18 17:29:31 +02002321 if( ssl->f_psk != NULL )
2322 {
2323 if( ( ret != ssl->f_psk( ssl->p_psk, ssl, *p, n ) ) != 0 )
2324 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2325 }
2326
2327 if( ret == 0 )
2328 {
2329 if( n != ssl->psk_identity_len ||
2330 memcmp( ssl->psk_identity, *p, n ) != 0 )
2331 {
2332 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2333 }
2334 }
2335
2336 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002337 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002338 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002339 if( ( ret = ssl_send_alert_message( ssl,
2340 SSL_ALERT_LEVEL_FATAL,
2341 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2342 {
2343 return( ret );
2344 }
2345
2346 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002347 }
2348
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002349 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002350 ret = 0;
2351
Paul Bakkerfbb17802013-04-17 19:10:21 +02002352 return( ret );
2353}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002354#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
2355 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002356
Paul Bakker5121ce52009-01-03 21:22:43 +00002357static int ssl_parse_client_key_exchange( ssl_context *ssl )
2358{
Paul Bakker23986e52011-04-24 08:57:21 +00002359 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002360 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002361
Paul Bakker41c83d32013-03-20 14:39:14 +01002362 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002363
2364 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2365
2366 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2367 {
2368 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2369 return( ret );
2370 }
2371
2372 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2373 {
2374 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002375 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002376 }
2377
2378 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2379 {
2380 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002381 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002382 }
2383
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002384#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002385 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002386 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002387 unsigned char *p = ssl->in_msg + 4;
2388 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2389
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002390 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002391 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002392 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2393 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002394 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002395
2396 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
2397
Manuel Pégourié-Gonnard032c34e2013-09-07 13:06:27 +02002398 /* No blinding needed for DHE, but will be needed for fixed DH! */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002399 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2400 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002401 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002402 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002403 {
2404 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2405 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2406 }
2407
2408 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002409 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002410 else
2411#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002412#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2413 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2414 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2415 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002416 {
2417 if( ( ret = ssl_parse_client_ecdh_public( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002418 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002419 SSL_DEBUG_RET( 1, ( "ssl_parse_client_ecdh_public" ), ret );
2420 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002421 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002422
2423 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2424 &ssl->handshake->pmslen,
2425 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002426 POLARSSL_MPI_MAX_SIZE,
2427 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002428 {
2429 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2430 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2431 }
2432
2433 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002434 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002435 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002436#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2437 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002438#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2439 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002440 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002441 unsigned char *p = ssl->in_msg + 4;
2442 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2443
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002444 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002445 {
2446 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2447 return( ret );
2448 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002449
2450 // Set up the premaster secret
2451 //
2452 p = ssl->handshake->premaster;
2453 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2454 *(p++) = (unsigned char)( ssl->psk_len );
2455 p += ssl->psk_len;
2456
2457 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2458 *(p++) = (unsigned char)( ssl->psk_len );
2459 memcpy( p, ssl->psk, ssl->psk_len );
2460 p += ssl->psk_len;
2461
2462 ssl->handshake->pmslen = 4 + 2 * ssl->psk_len;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002463 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002464 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002465#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
2466#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2467 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2468 {
2469 size_t n;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002470 unsigned char *p = ssl->in_msg + 4;
2471 unsigned char *end = ssl->in_msg + ssl->in_msglen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002472
2473 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2474 {
2475 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2476 return( ret );
2477 }
2478 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2479 {
2480 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2481 return( ret );
2482 }
2483
2484 // Set up the premaster secret
2485 //
2486 p = ssl->handshake->premaster;
2487 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len >> 8 );
2488 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len );
2489
Paul Bakker577e0062013-08-28 11:57:20 +02002490 n = ssl->handshake->dhm_ctx.len;
2491
Manuel Pégourié-Gonnard032c34e2013-09-07 13:06:27 +02002492 /* No blinding needed since this is ephemeral DHM */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002493 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002494 p, &n, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002495 {
2496 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2497 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2498 }
2499
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002500 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2501
2502 p += ssl->handshake->dhm_ctx.len;
2503
2504 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2505 *(p++) = (unsigned char)( ssl->psk_len );
2506 memcpy( p, ssl->psk, ssl->psk_len );
2507 p += ssl->psk_len;
2508
2509 ssl->handshake->pmslen = 4 + ssl->handshake->dhm_ctx.len + ssl->psk_len;
2510 }
2511 else
2512#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
2513#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2514 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002515 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002516 if( ( ret = ssl_parse_encrypted_pms_secret( ssl ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002517 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002518 SSL_DEBUG_RET( 1, ( "ssl_parse_client_ecdh_public" ), ret );
2519 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002520 }
2521 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002522 else
2523#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2524 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002525 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002526 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2527 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002528
Paul Bakkerff60ee62010-03-16 21:09:09 +00002529 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2530 {
2531 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2532 return( ret );
2533 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002534
Paul Bakker5121ce52009-01-03 21:22:43 +00002535 ssl->state++;
2536
2537 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2538
2539 return( 0 );
2540}
2541
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002542#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2543 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002544 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2545 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002546static int ssl_parse_certificate_verify( ssl_context *ssl )
2547{
Paul Bakkered27a042013-04-18 22:46:23 +02002548 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002549 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002550
2551 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2552
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002553 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2554 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002555 {
2556 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2557 ssl->state++;
2558 return( 0 );
2559 }
2560
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002561 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002562 return( ret );
2563}
2564#else
2565static int ssl_parse_certificate_verify( ssl_context *ssl )
2566{
2567 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002568 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002569 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002570 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002571 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02002572#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002573 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02002574#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002575 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002576 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2577
2578 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2579
2580 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2581 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2582 {
2583 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2584 ssl->state++;
2585 return( 0 );
2586 }
2587
Paul Bakkered27a042013-04-18 22:46:23 +02002588 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002589 {
2590 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2591 ssl->state++;
2592 return( 0 );
2593 }
2594
Paul Bakker48916f92012-09-16 19:57:18 +00002595 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002596
2597 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2598 {
2599 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2600 return( ret );
2601 }
2602
2603 ssl->state++;
2604
2605 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2606 {
2607 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002608 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002609 }
2610
2611 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
2612 {
2613 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002614 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002615 }
2616
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002617 /*
2618 * 0 . 0 handshake type
2619 * 1 . 3 handshake length
2620 * 4 . 5 sig alg (TLS 1.2 only)
2621 * 4+n . 5+n signature length (n = sa_len)
2622 * 6+n . 6+n+m signature (m = sig_len)
2623 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002624
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002625#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2626 defined(POLARSSL_SSL_PROTO_TLS1_1)
2627 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002628 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002629 sa_len = 0;
2630
Paul Bakkerc70b9822013-04-07 22:00:46 +02002631 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002632 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002633
2634 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
2635 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
2636 POLARSSL_PK_ECDSA ) )
2637 {
2638 hash_start += 16;
2639 hashlen -= 16;
2640 md_alg = POLARSSL_MD_SHA1;
2641 }
Paul Bakker926af752012-11-23 13:38:07 +01002642 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002643 else
2644#endif
Paul Bakker577e0062013-08-28 11:57:20 +02002645#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2646 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002647 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002648 sa_len = 2;
2649
Paul Bakker5121ce52009-01-03 21:22:43 +00002650 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002651 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00002652 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002653 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002654 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002655 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2656 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01002657 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2658 }
2659
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002660 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01002661
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002662 /* Info from md_alg will be used instead */
2663 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002664
2665 /*
2666 * Signature
2667 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002668 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
2669 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002670 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002671 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2672 " for verify message" ) );
2673 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002674 }
2675
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002676 /*
2677 * Check the certificate's key type matches the signature alg
2678 */
2679 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
2680 {
2681 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
2682 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2683 }
Paul Bakker577e0062013-08-28 11:57:20 +02002684 }
2685 else
2686#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2687 {
2688 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002689 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002690 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002691
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002692 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01002693
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002694 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002695 {
2696 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002697 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002698 }
2699
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002700 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002701 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002702 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002703 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002704 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002705 return( ret );
2706 }
2707
2708 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
2709
Paul Bakkered27a042013-04-18 22:46:23 +02002710 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002711}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002712#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2713 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2714 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002715
Paul Bakkera503a632013-08-14 13:48:06 +02002716#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002717static int ssl_write_new_session_ticket( ssl_context *ssl )
2718{
2719 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002720 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002721 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
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 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002738
2739 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
2740 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
2741 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
2742 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002743
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02002744 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
2745 {
2746 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
2747 tlen = 0;
2748 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002749
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002750 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
2751 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002752
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002753 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002754
2755 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2756 {
2757 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2758 return( ret );
2759 }
2760
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002761 /* No need to remember writing a NewSessionTicket any more */
2762 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002763
2764 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
2765
2766 return( 0 );
2767}
Paul Bakkera503a632013-08-14 13:48:06 +02002768#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002769
Paul Bakker5121ce52009-01-03 21:22:43 +00002770/*
Paul Bakker1961b702013-01-25 14:49:24 +01002771 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002772 */
Paul Bakker1961b702013-01-25 14:49:24 +01002773int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002774{
2775 int ret = 0;
2776
Paul Bakker1961b702013-01-25 14:49:24 +01002777 if( ssl->state == SSL_HANDSHAKE_OVER )
2778 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002779
Paul Bakker1961b702013-01-25 14:49:24 +01002780 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
2781
2782 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2783 return( ret );
2784
2785 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002786 {
Paul Bakker1961b702013-01-25 14:49:24 +01002787 case SSL_HELLO_REQUEST:
2788 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002789 break;
2790
Paul Bakker1961b702013-01-25 14:49:24 +01002791 /*
2792 * <== ClientHello
2793 */
2794 case SSL_CLIENT_HELLO:
2795 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002796 break;
Paul Bakker1961b702013-01-25 14:49:24 +01002797
2798 /*
2799 * ==> ServerHello
2800 * Certificate
2801 * ( ServerKeyExchange )
2802 * ( CertificateRequest )
2803 * ServerHelloDone
2804 */
2805 case SSL_SERVER_HELLO:
2806 ret = ssl_write_server_hello( ssl );
2807 break;
2808
2809 case SSL_SERVER_CERTIFICATE:
2810 ret = ssl_write_certificate( ssl );
2811 break;
2812
2813 case SSL_SERVER_KEY_EXCHANGE:
2814 ret = ssl_write_server_key_exchange( ssl );
2815 break;
2816
2817 case SSL_CERTIFICATE_REQUEST:
2818 ret = ssl_write_certificate_request( ssl );
2819 break;
2820
2821 case SSL_SERVER_HELLO_DONE:
2822 ret = ssl_write_server_hello_done( ssl );
2823 break;
2824
2825 /*
2826 * <== ( Certificate/Alert )
2827 * ClientKeyExchange
2828 * ( CertificateVerify )
2829 * ChangeCipherSpec
2830 * Finished
2831 */
2832 case SSL_CLIENT_CERTIFICATE:
2833 ret = ssl_parse_certificate( ssl );
2834 break;
2835
2836 case SSL_CLIENT_KEY_EXCHANGE:
2837 ret = ssl_parse_client_key_exchange( ssl );
2838 break;
2839
2840 case SSL_CERTIFICATE_VERIFY:
2841 ret = ssl_parse_certificate_verify( ssl );
2842 break;
2843
2844 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2845 ret = ssl_parse_change_cipher_spec( ssl );
2846 break;
2847
2848 case SSL_CLIENT_FINISHED:
2849 ret = ssl_parse_finished( ssl );
2850 break;
2851
2852 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002853 * ==> ( NewSessionTicket )
2854 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002855 * Finished
2856 */
2857 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002858#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002859 if( ssl->handshake->new_session_ticket != 0 )
2860 ret = ssl_write_new_session_ticket( ssl );
2861 else
Paul Bakkera503a632013-08-14 13:48:06 +02002862#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002863 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002864 break;
2865
2866 case SSL_SERVER_FINISHED:
2867 ret = ssl_write_finished( ssl );
2868 break;
2869
2870 case SSL_FLUSH_BUFFERS:
2871 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2872 ssl->state = SSL_HANDSHAKE_WRAPUP;
2873 break;
2874
2875 case SSL_HANDSHAKE_WRAPUP:
2876 ssl_handshake_wrapup( ssl );
2877 break;
2878
2879 default:
2880 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2881 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002882 }
2883
Paul Bakker5121ce52009-01-03 21:22:43 +00002884 return( ret );
2885}
Paul Bakker5121ce52009-01-03 21:22:43 +00002886#endif