blob: e291c53d11eb8ebd5d570908294e67b461db5f9d [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)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200341/*
342 * Wrapper around f_sni, allowing use of
343 * ssl_set_own_cert() but making it act on ssl->hanshake->key_cert instead.
344 */
345static int ssl_sni_wrapper( ssl_context *ssl,
346 const unsigned char* name, size_t len )
347{
348 int ret;
349 ssl_key_cert *key_cert_ori = ssl->key_cert;
350
351 ssl->key_cert = NULL;
352 ret = ssl->f_sni( ssl->p_sni, ssl, name, len );
353 ssl->handshake->key_cert = ssl->key_cert;
354 ssl->handshake->free_key_cert = 1;
355
356 ssl->key_cert = key_cert_ori;
357
358 return( ret );
359}
360
Paul Bakker5701cdc2012-09-27 21:49:42 +0000361static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000362 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000363 size_t len )
364{
365 int ret;
366 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000367 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000368
369 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
370 if( servername_list_size + 2 != len )
371 {
372 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
373 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
374 }
375
376 p = buf + 2;
377 while( servername_list_size > 0 )
378 {
379 hostname_len = ( ( p[1] << 8 ) | p[2] );
380 if( hostname_len + 3 > servername_list_size )
381 {
382 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
383 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
384 }
385
386 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
387 {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200388 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000389 if( ret != 0 )
390 {
391 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
392 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
393 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
394 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000395 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000396 }
397
398 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000399 p += hostname_len + 3;
400 }
401
402 if( servername_list_size != 0 )
403 {
404 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
405 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000406 }
407
408 return( 0 );
409}
Paul Bakker0be444a2013-08-27 21:55:01 +0200410#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000411
Paul Bakker48916f92012-09-16 19:57:18 +0000412static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000413 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000414 size_t len )
415{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000416 int ret;
417
Paul Bakker48916f92012-09-16 19:57:18 +0000418 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
419 {
420 if( len != 1 || buf[0] != 0x0 )
421 {
422 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000423
424 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
425 return( ret );
426
Paul Bakker48916f92012-09-16 19:57:18 +0000427 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
428 }
429
430 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
431 }
432 else
433 {
434 if( len != 1 + ssl->verify_data_len ||
435 buf[0] != ssl->verify_data_len ||
436 memcmp( buf + 1, ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
437 {
438 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000439
440 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
441 return( ret );
442
Paul Bakker48916f92012-09-16 19:57:18 +0000443 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
444 }
445 }
446
447 return( 0 );
448}
449
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200450#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +0000451static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
452 const unsigned char *buf,
453 size_t len )
454{
455 size_t sig_alg_list_size;
456 const unsigned char *p;
457
458 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
459 if( sig_alg_list_size + 2 != len ||
460 sig_alg_list_size %2 != 0 )
461 {
462 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
463 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
464 }
465
466 p = buf + 2;
467 while( sig_alg_list_size > 0 )
468 {
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200469 /*
470 * For now, just ignore signature algorithm and rely on offered
471 * ciphersuites only. To be fixed later.
472 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200473#if defined(POLARSSL_SHA512_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000474 if( p[0] == SSL_HASH_SHA512 )
475 {
476 ssl->handshake->sig_alg = SSL_HASH_SHA512;
477 break;
478 }
479 if( p[0] == SSL_HASH_SHA384 )
480 {
481 ssl->handshake->sig_alg = SSL_HASH_SHA384;
482 break;
483 }
484#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200485#if defined(POLARSSL_SHA256_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000486 if( p[0] == SSL_HASH_SHA256 )
487 {
488 ssl->handshake->sig_alg = SSL_HASH_SHA256;
489 break;
490 }
491 if( p[0] == SSL_HASH_SHA224 )
492 {
493 ssl->handshake->sig_alg = SSL_HASH_SHA224;
494 break;
495 }
496#endif
497 if( p[0] == SSL_HASH_SHA1 )
498 {
499 ssl->handshake->sig_alg = SSL_HASH_SHA1;
500 break;
501 }
502 if( p[0] == SSL_HASH_MD5 )
503 {
504 ssl->handshake->sig_alg = SSL_HASH_MD5;
505 break;
506 }
507
508 sig_alg_list_size -= 2;
509 p += 2;
510 }
511
512 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
513 ssl->handshake->sig_alg ) );
514
515 return( 0 );
516}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200517#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker23f36802012-09-28 14:15:14 +0000518
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200519#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200520static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
521 const unsigned char *buf,
522 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100523{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200524 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100525 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200526 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100527
528 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
529 if( list_size + 2 != len ||
530 list_size % 2 != 0 )
531 {
532 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
533 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
534 }
535
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200536 /* Don't allow our peer to make use allocated too much memory,
537 * and leave room for a final 0 */
538 our_size = list_size / 2 + 1;
539 if( our_size > POLARSSL_ECP_DP_MAX )
540 our_size = POLARSSL_ECP_DP_MAX;
541
542 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
543 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
544
545 memset( curves, 0, our_size * sizeof( *curves ) );
546 ssl->handshake->curves = curves;
547
Paul Bakker41c83d32013-03-20 14:39:14 +0100548 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200549 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100550 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200551 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200552
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200553 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100554 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200555 *curves++ = curve_info;
556 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100557 }
558
559 list_size -= 2;
560 p += 2;
561 }
562
563 return( 0 );
564}
565
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200566static int ssl_parse_supported_point_formats( ssl_context *ssl,
567 const unsigned char *buf,
568 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100569{
570 size_t list_size;
571 const unsigned char *p;
572
573 list_size = buf[0];
574 if( list_size + 1 != len )
575 {
576 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
577 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
578 }
579
580 p = buf + 2;
581 while( list_size > 0 )
582 {
583 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
584 p[0] == POLARSSL_ECP_PF_COMPRESSED )
585 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200586 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200587 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100588 return( 0 );
589 }
590
591 list_size--;
592 p++;
593 }
594
595 return( 0 );
596}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200597#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100598
Paul Bakker05decb22013-08-15 13:33:48 +0200599#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200600static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
601 const unsigned char *buf,
602 size_t len )
603{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200604 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200605 {
606 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
607 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
608 }
609
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200610 ssl->session_negotiate->mfl_code = buf[0];
611
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200612 return( 0 );
613}
Paul Bakker05decb22013-08-15 13:33:48 +0200614#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200615
Paul Bakker1f2bc622013-08-15 13:45:55 +0200616#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200617static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
618 const unsigned char *buf,
619 size_t len )
620{
621 if( len != 0 )
622 {
623 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
624 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
625 }
626
627 ((void) buf);
628
629 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
630
631 return( 0 );
632}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200633#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200634
Paul Bakkera503a632013-08-14 13:48:06 +0200635#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200636static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200637 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200638 size_t len )
639{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200640 int ret;
641
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200642 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
643 return( 0 );
644
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200645 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200646 ssl->handshake->new_session_ticket = 1;
647
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200648 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
649
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200650 if( len == 0 )
651 return( 0 );
652
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200653 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
654 {
655 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
656 return( 0 );
657 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200658
659 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200660 * Failures are ok: just ignore the ticket and proceed.
661 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200662 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
663 {
664 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200665 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200666 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200667
668 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
669
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200670 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200671
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200672 /* Don't send a new ticket after all, this one is OK */
673 ssl->handshake->new_session_ticket = 0;
674
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200675 return( 0 );
676}
Paul Bakkera503a632013-08-14 13:48:06 +0200677#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200678
Paul Bakker78a8c712013-03-06 17:01:52 +0100679#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
680static int ssl_parse_client_hello_v2( ssl_context *ssl )
681{
682 int ret;
683 unsigned int i, j;
684 size_t n;
685 unsigned int ciph_len, sess_len, chal_len;
686 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200687 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200688 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100689
690 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
691
692 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
693 {
694 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
695
696 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
697 return( ret );
698
699 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
700 }
701
702 buf = ssl->in_hdr;
703
704 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
705
706 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
707 buf[2] ) );
708 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
709 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
710 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
711 buf[3], buf[4] ) );
712
713 /*
714 * SSLv2 Client Hello
715 *
716 * Record layer:
717 * 0 . 1 message length
718 *
719 * SSL layer:
720 * 2 . 2 message type
721 * 3 . 4 protocol version
722 */
723 if( buf[2] != SSL_HS_CLIENT_HELLO ||
724 buf[3] != SSL_MAJOR_VERSION_3 )
725 {
726 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
727 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
728 }
729
730 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
731
732 if( n < 17 || n > 512 )
733 {
734 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
735 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
736 }
737
738 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200739 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
740 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100741
742 if( ssl->minor_ver < ssl->min_minor_ver )
743 {
744 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
745 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
746 ssl->min_major_ver, ssl->min_minor_ver ) );
747
748 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
749 SSL_ALERT_MSG_PROTOCOL_VERSION );
750 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
751 }
752
Paul Bakker2fbefde2013-06-29 16:01:15 +0200753 ssl->handshake->max_major_ver = buf[3];
754 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100755
756 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
757 {
758 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
759 return( ret );
760 }
761
762 ssl->handshake->update_checksum( ssl, buf + 2, n );
763
764 buf = ssl->in_msg;
765 n = ssl->in_left - 5;
766
767 /*
768 * 0 . 1 ciphersuitelist length
769 * 2 . 3 session id length
770 * 4 . 5 challenge length
771 * 6 . .. ciphersuitelist
772 * .. . .. session id
773 * .. . .. challenge
774 */
775 SSL_DEBUG_BUF( 4, "record contents", buf, n );
776
777 ciph_len = ( buf[0] << 8 ) | buf[1];
778 sess_len = ( buf[2] << 8 ) | buf[3];
779 chal_len = ( buf[4] << 8 ) | buf[5];
780
781 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
782 ciph_len, sess_len, chal_len ) );
783
784 /*
785 * Make sure each parameter length is valid
786 */
787 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
788 {
789 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
790 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
791 }
792
793 if( sess_len > 32 )
794 {
795 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
796 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
797 }
798
799 if( chal_len < 8 || chal_len > 32 )
800 {
801 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
802 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
803 }
804
805 if( n != 6 + ciph_len + sess_len + chal_len )
806 {
807 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
808 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
809 }
810
811 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
812 buf + 6, ciph_len );
813 SSL_DEBUG_BUF( 3, "client hello, session id",
814 buf + 6 + ciph_len, sess_len );
815 SSL_DEBUG_BUF( 3, "client hello, challenge",
816 buf + 6 + ciph_len + sess_len, chal_len );
817
818 p = buf + 6 + ciph_len;
819 ssl->session_negotiate->length = sess_len;
820 memset( ssl->session_negotiate->id, 0, sizeof( ssl->session_negotiate->id ) );
821 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
822
823 p += sess_len;
824 memset( ssl->handshake->randbytes, 0, 64 );
825 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
826
827 /*
828 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
829 */
830 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
831 {
832 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
833 {
834 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
835 if( ssl->renegotiation == SSL_RENEGOTIATION )
836 {
837 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
838
839 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
840 return( ret );
841
842 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
843 }
844 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
845 break;
846 }
847 }
848
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200849 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
850 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +0100851 {
852 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
853 {
Paul Bakker41c83d32013-03-20 14:39:14 +0100854 // Only allow non-ECC ciphersuites as we do not have extensions
855 //
Paul Bakker59c28a22013-06-29 15:33:42 +0200856 if( p[0] == 0 && p[1] == 0 &&
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200857 ( ( ciphersuites[i] >> 8 ) & 0xFF ) == 0 &&
858 p[2] == ( ciphersuites[i] & 0xFF ) )
Paul Bakker59c28a22013-06-29 15:33:42 +0200859 {
860 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
861
862 if( ciphersuite_info == NULL )
863 {
864 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %02x not found",
865 ciphersuites[i] ) );
866 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
867 }
868
Paul Bakker2fbefde2013-06-29 16:01:15 +0200869 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
870 ciphersuite_info->max_minor_ver < ssl->minor_ver )
871 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +0200872
Paul Bakker78a8c712013-03-06 17:01:52 +0100873 goto have_ciphersuite_v2;
Paul Bakker59c28a22013-06-29 15:33:42 +0200874 }
Paul Bakker78a8c712013-03-06 17:01:52 +0100875 }
876 }
877
878 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
879
880 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
881
882have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200883 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +0200884 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +0100885 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +0100886
887 /*
888 * SSLv2 Client Hello relevant renegotiation security checks
889 */
890 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
891 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
892 {
893 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
894
895 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
896 return( ret );
897
898 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
899 }
900
901 ssl->in_left = 0;
902 ssl->state++;
903
904 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
905
906 return( 0 );
907}
908#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
909
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200910#if defined(POLARSSL_X509_CRT_PARSE_C)
911#if defined(POLARSSL_ECDSA_C)
912static int ssl_key_matches_curves( pk_context *pk,
913 const ecp_curve_info **curves )
914{
915 const ecp_curve_info **crv = curves;
916 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
917
918 while( *crv != NULL )
919 {
920 if( (*crv)->grp_id == grp_id )
921 return( 1 );
922 crv++;
923 }
924
925 return( 0 );
926}
927#endif /* POLARSSL_ECDSA_C */
928
929/*
930 * Try picking a certificate for this ciphersuite,
931 * return 0 on success and -1 on failure.
932 */
933static int ssl_pick_cert( ssl_context *ssl,
934 const ssl_ciphersuite_t * ciphersuite_info )
935{
936 ssl_key_cert *cur;
937 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
938
939 if( pk_alg == POLARSSL_PK_NONE )
940 return( 0 );
941
942 for( cur = ssl->key_cert; cur != NULL; cur = cur->next )
943 {
944 if( ! pk_can_do( cur->key, pk_alg ) )
945 continue;
946
947#if defined(POLARSSL_ECDSA_C)
948 if( pk_alg == POLARSSL_PK_ECDSA )
949 {
950 if( ssl_key_matches_curves( cur->key, ssl->handshake->curves ) )
951 break;
952 }
953 else
954#endif
955 break;
956 }
957
958 if( cur == NULL )
959 return( -1 );
960
961 ssl->handshake->key_cert = cur;
962 return( 0 );
963}
964#endif /* POLARSSL_X509_CRT_PARSE_C */
965
Paul Bakker5121ce52009-01-03 21:22:43 +0000966static int ssl_parse_client_hello( ssl_context *ssl )
967{
Paul Bakker23986e52011-04-24 08:57:21 +0000968 int ret;
969 unsigned int i, j;
970 size_t n;
971 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +0000972 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +0000973 unsigned int ext_len = 0;
974 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000975 int renegotiation_info_seen = 0;
976 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200977 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +0100978 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000979
980 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
981
Paul Bakker48916f92012-09-16 19:57:18 +0000982 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
983 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000984 {
985 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
986 return( ret );
987 }
988
989 buf = ssl->in_hdr;
990
Paul Bakker78a8c712013-03-06 17:01:52 +0100991#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
992 if( ( buf[0] & 0x80 ) != 0 )
993 return ssl_parse_client_hello_v2( ssl );
994#endif
995
Paul Bakkerec636f32012-09-09 19:17:02 +0000996 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
997
998 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
999 buf[0] ) );
1000 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1001 ( buf[3] << 8 ) | buf[4] ) );
1002 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
1003 buf[1], buf[2] ) );
1004
1005 /*
1006 * SSLv3 Client Hello
1007 *
1008 * Record layer:
1009 * 0 . 0 message type
1010 * 1 . 2 protocol version
1011 * 3 . 4 message length
1012 */
1013 if( buf[0] != SSL_MSG_HANDSHAKE ||
1014 buf[1] != SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001015 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001016 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1017 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1018 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001019
Paul Bakkerec636f32012-09-09 19:17:02 +00001020 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
Manuel Pégourié-Gonnard72882b22013-08-02 13:36:00 +02001022 if( n < 45 || n > 2048 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001023 {
1024 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1025 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1026 }
1027
Paul Bakker48916f92012-09-16 19:57:18 +00001028 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1029 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001030 {
1031 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1032 return( ret );
1033 }
1034
1035 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +00001036 if( !ssl->renegotiation )
1037 n = ssl->in_left - 5;
1038 else
1039 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +00001040
Paul Bakker48916f92012-09-16 19:57:18 +00001041 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +00001042
1043 /*
1044 * SSL layer:
1045 * 0 . 0 handshake type
1046 * 1 . 3 handshake length
1047 * 4 . 5 protocol version
1048 * 6 . 9 UNIX time()
1049 * 10 . 37 random bytes
1050 * 38 . 38 session id length
1051 * 39 . 38+x session id
1052 * 39+x . 40+x ciphersuitelist length
1053 * 41+x . .. ciphersuitelist
1054 * .. . .. compression alg.
1055 * .. . .. extensions
1056 */
1057 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1058
1059 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1060 buf[0] ) );
1061 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1062 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1063 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1064 buf[4], buf[5] ) );
1065
1066 /*
1067 * Check the handshake type and protocol version
1068 */
1069 if( buf[0] != SSL_HS_CLIENT_HELLO ||
1070 buf[4] != SSL_MAJOR_VERSION_3 )
1071 {
1072 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1073 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1074 }
1075
1076 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +02001077 ssl->minor_ver = ( buf[5] <= ssl->max_minor_ver )
1078 ? buf[5] : ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001079
Paul Bakker1d29fb52012-09-28 13:28:45 +00001080 if( ssl->minor_ver < ssl->min_minor_ver )
1081 {
1082 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
1083 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001084 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001085
1086 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1087 SSL_ALERT_MSG_PROTOCOL_VERSION );
1088
1089 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1090 }
1091
Paul Bakker2fbefde2013-06-29 16:01:15 +02001092 ssl->handshake->max_major_ver = buf[4];
1093 ssl->handshake->max_minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001094
Paul Bakker48916f92012-09-16 19:57:18 +00001095 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001096
1097 /*
1098 * Check the handshake message length
1099 */
1100 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1101 {
1102 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1103 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1104 }
1105
1106 /*
1107 * Check the session length
1108 */
1109 sess_len = buf[38];
1110
1111 if( sess_len > 32 )
1112 {
1113 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1114 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1115 }
1116
Paul Bakker48916f92012-09-16 19:57:18 +00001117 ssl->session_negotiate->length = sess_len;
1118 memset( ssl->session_negotiate->id, 0,
1119 sizeof( ssl->session_negotiate->id ) );
1120 memcpy( ssl->session_negotiate->id, buf + 39,
1121 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001122
1123 /*
1124 * Check the ciphersuitelist length
1125 */
1126 ciph_len = ( buf[39 + sess_len] << 8 )
1127 | ( buf[40 + sess_len] );
1128
1129 if( ciph_len < 2 || ciph_len > 256 || ( ciph_len % 2 ) != 0 )
1130 {
1131 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1132 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1133 }
1134
1135 /*
1136 * Check the compression algorithms length
1137 */
1138 comp_len = buf[41 + sess_len + ciph_len];
1139
1140 if( comp_len < 1 || comp_len > 16 )
1141 {
1142 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1143 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1144 }
1145
Paul Bakker48916f92012-09-16 19:57:18 +00001146 /*
1147 * Check the extension length
1148 */
1149 if( n > 42 + sess_len + ciph_len + comp_len )
1150 {
1151 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1152 | ( buf[43 + sess_len + ciph_len + comp_len] );
1153
1154 if( ( ext_len > 0 && ext_len < 4 ) ||
1155 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1156 {
1157 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1158 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1159 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1160 }
1161 }
1162
1163 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001164#if defined(POLARSSL_ZLIB_SUPPORT)
1165 for( i = 0; i < comp_len; ++i )
1166 {
Paul Bakker48916f92012-09-16 19:57:18 +00001167 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001168 {
Paul Bakker48916f92012-09-16 19:57:18 +00001169 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001170 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001171 }
1172 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001173#endif
1174
Paul Bakkerec636f32012-09-09 19:17:02 +00001175 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1176 buf + 6, 32 );
1177 SSL_DEBUG_BUF( 3, "client hello, session id",
1178 buf + 38, sess_len );
1179 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1180 buf + 41 + sess_len, ciph_len );
1181 SSL_DEBUG_BUF( 3, "client hello, compression",
1182 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001183
Paul Bakkerec636f32012-09-09 19:17:02 +00001184 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001185 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1186 */
1187 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1188 {
1189 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1190 {
1191 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1192 if( ssl->renegotiation == SSL_RENEGOTIATION )
1193 {
1194 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001195
1196 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1197 return( ret );
1198
Paul Bakker48916f92012-09-16 19:57:18 +00001199 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1200 }
1201 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1202 break;
1203 }
1204 }
1205
Paul Bakker48916f92012-09-16 19:57:18 +00001206 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001207
1208 while( ext_len )
1209 {
1210 unsigned int ext_id = ( ( ext[0] << 8 )
1211 | ( ext[1] ) );
1212 unsigned int ext_size = ( ( ext[2] << 8 )
1213 | ( ext[3] ) );
1214
1215 if( ext_size + 4 > ext_len )
1216 {
1217 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1218 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1219 }
1220 switch( ext_id )
1221 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001222#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001223 case TLS_EXT_SERVERNAME:
1224 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1225 if( ssl->f_sni == NULL )
1226 break;
1227
1228 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1229 if( ret != 0 )
1230 return( ret );
1231 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001232#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001233
Paul Bakker48916f92012-09-16 19:57:18 +00001234 case TLS_EXT_RENEGOTIATION_INFO:
1235 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1236 renegotiation_info_seen = 1;
1237
Paul Bakker23f36802012-09-28 14:15:14 +00001238 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1239 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001240 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001241 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001242
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001243#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001244 case TLS_EXT_SIG_ALG:
1245 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1246 if( ssl->renegotiation == SSL_RENEGOTIATION )
1247 break;
1248
1249 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1250 if( ret != 0 )
1251 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001252 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001253#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001254
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001255#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001256 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1257 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1258
1259 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1260 if( ret != 0 )
1261 return( ret );
1262 break;
1263
1264 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1265 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1266
1267 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1268 if( ret != 0 )
1269 return( ret );
1270 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001271#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001272
Paul Bakker05decb22013-08-15 13:33:48 +02001273#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001274 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1275 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1276
1277 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1278 if( ret != 0 )
1279 return( ret );
1280 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001281#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001282
Paul Bakker1f2bc622013-08-15 13:45:55 +02001283#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001284 case TLS_EXT_TRUNCATED_HMAC:
1285 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1286
1287 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1288 if( ret != 0 )
1289 return( ret );
1290 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001291#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001292
Paul Bakkera503a632013-08-14 13:48:06 +02001293#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001294 case TLS_EXT_SESSION_TICKET:
1295 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1296
1297 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1298 if( ret != 0 )
1299 return( ret );
1300 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001301#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001302
Paul Bakker48916f92012-09-16 19:57:18 +00001303 default:
1304 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1305 ext_id ) );
1306 }
1307
1308 ext_len -= 4 + ext_size;
1309 ext += 4 + ext_size;
1310
1311 if( ext_len > 0 && ext_len < 4 )
1312 {
1313 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1314 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1315 }
1316 }
1317
1318 /*
1319 * Renegotiation security checks
1320 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001321 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1322 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1323 {
1324 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1325 handshake_failure = 1;
1326 }
1327 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1328 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1329 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001330 {
1331 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001332 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001333 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001334 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1335 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1336 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001337 {
1338 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001339 handshake_failure = 1;
1340 }
1341 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1342 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1343 renegotiation_info_seen == 1 )
1344 {
1345 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1346 handshake_failure = 1;
1347 }
1348
1349 if( handshake_failure == 1 )
1350 {
1351 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1352 return( ret );
1353
Paul Bakker48916f92012-09-16 19:57:18 +00001354 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1355 }
Paul Bakker380da532012-04-18 16:10:25 +00001356
Paul Bakker41c83d32013-03-20 14:39:14 +01001357 /*
1358 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001359 * (At the end because we need information from the EC-based extensions
1360 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001361 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001362 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
1363 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001364 {
1365 for( j = 0, p = buf + 41 + sess_len; j < ciph_len;
1366 j += 2, p += 2 )
1367 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001368 if( p[0] == ( ( ciphersuites[i] >> 8 ) & 0xFF ) &&
1369 p[1] == ( ( ciphersuites[i] ) & 0xFF ) )
Paul Bakker41c83d32013-03-20 14:39:14 +01001370 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001371 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
Paul Bakker41c83d32013-03-20 14:39:14 +01001372
1373 if( ciphersuite_info == NULL )
1374 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001375 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found",
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001376 ciphersuites[i] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001377 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1378 }
1379
Paul Bakker2fbefde2013-06-29 16:01:15 +02001380 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
1381 ciphersuite_info->max_minor_ver < ssl->minor_ver )
1382 continue;
1383
Paul Bakker5fd49172013-08-19 13:29:26 +02001384#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001385 if( ssl_ciphersuite_uses_ec( ciphersuite_info ) &&
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02001386 ssl->handshake->curves[0] == NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +01001387 continue;
Paul Bakker5fd49172013-08-19 13:29:26 +02001388#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001389
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001390#if defined(POLARSSL_X509_CRT_PARSE_C)
1391 /*
1392 * Final check: if ciphersuite requires us to have a
1393 * certificate/key of a particular type:
1394 * - select the appropriate certificate if we have one, or
1395 * - try the next ciphersuite if we don't
1396 * This must be done last since we modify the key_cert list.
1397 */
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02001398 if( ssl_pick_cert( ssl, ciphersuite_info ) != 0 )
1399 continue;
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02001400#endif
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001401
Paul Bakker41c83d32013-03-20 14:39:14 +01001402 goto have_ciphersuite;
1403 }
1404 }
1405 }
1406
1407 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1408
1409 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1410 return( ret );
1411
1412 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1413
1414have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001415 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001416 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1417 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1418
Paul Bakker5121ce52009-01-03 21:22:43 +00001419 ssl->in_left = 0;
1420 ssl->state++;
1421
1422 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1423
1424 return( 0 );
1425}
1426
Paul Bakker1f2bc622013-08-15 13:45:55 +02001427#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001428static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1429 unsigned char *buf,
1430 size_t *olen )
1431{
1432 unsigned char *p = buf;
1433
1434 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1435 {
1436 *olen = 0;
1437 return;
1438 }
1439
1440 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1441
1442 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1443 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1444
1445 *p++ = 0x00;
1446 *p++ = 0x00;
1447
1448 *olen = 4;
1449}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001450#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001451
Paul Bakkera503a632013-08-14 13:48:06 +02001452#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001453static void ssl_write_session_ticket_ext( ssl_context *ssl,
1454 unsigned char *buf,
1455 size_t *olen )
1456{
1457 unsigned char *p = buf;
1458
1459 if( ssl->handshake->new_session_ticket == 0 )
1460 {
1461 *olen = 0;
1462 return;
1463 }
1464
1465 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1466
1467 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1468 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1469
1470 *p++ = 0x00;
1471 *p++ = 0x00;
1472
1473 *olen = 4;
1474}
Paul Bakkera503a632013-08-14 13:48:06 +02001475#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001476
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001477static void ssl_write_renegotiation_ext( ssl_context *ssl,
1478 unsigned char *buf,
1479 size_t *olen )
1480{
1481 unsigned char *p = buf;
1482
1483 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1484 {
1485 *olen = 0;
1486 return;
1487 }
1488
1489 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1490
1491 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1492 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1493
1494 *p++ = 0x00;
1495 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1496 *p++ = ssl->verify_data_len * 2 & 0xFF;
1497
1498 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1499 p += ssl->verify_data_len;
1500 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1501 p += ssl->verify_data_len;
1502
1503 *olen = 5 + ssl->verify_data_len * 2;
1504}
1505
Paul Bakker05decb22013-08-15 13:33:48 +02001506#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001507static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1508 unsigned char *buf,
1509 size_t *olen )
1510{
1511 unsigned char *p = buf;
1512
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001513 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1514 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001515 *olen = 0;
1516 return;
1517 }
1518
1519 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1520
1521 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1522 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1523
1524 *p++ = 0x00;
1525 *p++ = 1;
1526
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001527 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001528
1529 *olen = 5;
1530}
Paul Bakker05decb22013-08-15 13:33:48 +02001531#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001532
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001533#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001534static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1535 unsigned char *buf,
1536 size_t *olen )
1537{
1538 unsigned char *p = buf;
1539 ((void) ssl);
1540
1541 *olen = 0;
1542
1543 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1544
1545 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1546 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1547
1548 *p++ = 0x00;
1549 *p++ = 2;
1550
1551 *p++ = 1;
1552 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1553
1554 *olen = 6;
1555}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001556#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001557
Paul Bakker5121ce52009-01-03 21:22:43 +00001558static int ssl_write_server_hello( ssl_context *ssl )
1559{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001560#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001561 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001562#endif
Paul Bakkera3d195c2011-11-27 21:07:34 +00001563 int ret, n;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001564 size_t olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001565 unsigned char *buf, *p;
1566
1567 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1568
1569 /*
1570 * 0 . 0 handshake type
1571 * 1 . 3 handshake length
1572 * 4 . 5 protocol version
1573 * 6 . 9 UNIX time()
1574 * 10 . 37 random bytes
1575 */
1576 buf = ssl->out_msg;
1577 p = buf + 4;
1578
1579 *p++ = (unsigned char) ssl->major_ver;
1580 *p++ = (unsigned char) ssl->minor_ver;
1581
1582 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1583 buf[4], buf[5] ) );
1584
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001585#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001586 t = time( NULL );
1587 *p++ = (unsigned char)( t >> 24 );
1588 *p++ = (unsigned char)( t >> 16 );
1589 *p++ = (unsigned char)( t >> 8 );
1590 *p++ = (unsigned char)( t );
1591
1592 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001593#else
1594 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1595 return( ret );
1596
1597 p += 4;
1598#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001599
Paul Bakkera3d195c2011-11-27 21:07:34 +00001600 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1601 return( ret );
1602
1603 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001604
Paul Bakker48916f92012-09-16 19:57:18 +00001605 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001606
1607 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1608
1609 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001610 * Resume is 0 by default, see ssl_handshake_init().
1611 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1612 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001613 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001614 if( ssl->handshake->resume == 0 &&
1615 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001616 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001617 ssl->f_get_cache != NULL &&
1618 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1619 {
1620 ssl->handshake->resume = 1;
1621 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001622
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001623 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001624 {
1625 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001626 * New session, create a new session id,
1627 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001628 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001629 ssl->state++;
1630
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001631#if defined(POLARSSL_HAVE_TIME)
1632 ssl->session_negotiate->start = time( NULL );
1633#endif
1634
Paul Bakkera503a632013-08-14 13:48:06 +02001635#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001636 if( ssl->handshake->new_session_ticket != 0 )
1637 {
1638 ssl->session_negotiate->length = n = 0;
1639 memset( ssl->session_negotiate->id, 0, 32 );
1640 }
1641 else
1642#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001643 {
1644 ssl->session_negotiate->length = n = 32;
1645 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001646 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001647 return( ret );
1648 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001649 }
1650 else
1651 {
1652 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001653 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001654 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001655 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001656 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001657
1658 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1659 {
1660 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1661 return( ret );
1662 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001663 }
1664
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001665 /*
1666 * 38 . 38 session id length
1667 * 39 . 38+n session id
1668 * 39+n . 40+n chosen ciphersuite
1669 * 41+n . 41+n chosen compression alg.
1670 * 42+n . 43+n extensions length
1671 * 44+n . 43+n+m extensions
1672 */
1673 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001674 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1675 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001676
1677 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1678 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1679 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001680 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001681
Paul Bakker48916f92012-09-16 19:57:18 +00001682 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1683 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1684 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001685
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001686 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
1687 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001688 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001689 ssl->session_negotiate->compression ) );
1690
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001691 /*
1692 * First write extensions, then the total length
1693 */
1694 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1695 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001696
Paul Bakker05decb22013-08-15 13:33:48 +02001697#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001698 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1699 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001700#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001701
Paul Bakker1f2bc622013-08-15 13:45:55 +02001702#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001703 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1704 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001705#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001706
Paul Bakkera503a632013-08-14 13:48:06 +02001707#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001708 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1709 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001710#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001711
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001712#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001713 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1714 ext_len += olen;
1715#endif
1716
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001717 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001718
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001719 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1720 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1721 p += ext_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001722
1723 ssl->out_msglen = p - buf;
1724 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1725 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1726
1727 ret = ssl_write_record( ssl );
1728
1729 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1730
1731 return( ret );
1732}
1733
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001734#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1735 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001736 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1737 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001738static int ssl_write_certificate_request( ssl_context *ssl )
1739{
Paul Bakkered27a042013-04-18 22:46:23 +02001740 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1741 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001742
1743 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1744
1745 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1746 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1747 {
1748 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1749 ssl->state++;
1750 return( 0 );
1751 }
1752
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001753 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001754 return( ret );
1755}
1756#else
1757static int ssl_write_certificate_request( ssl_context *ssl )
1758{
1759 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1760 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001761 size_t dn_size, total_dn_size; /* excluding length bytes */
1762 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00001763 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001764 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001765
1766 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1767
1768 ssl->state++;
1769
Paul Bakkerfbb17802013-04-17 19:10:21 +02001770 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001771 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001772 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001773 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001774 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001775 return( 0 );
1776 }
1777
1778 /*
1779 * 0 . 0 handshake type
1780 * 1 . 3 handshake length
1781 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01001782 * 5 .. m-1 cert types
1783 * m .. m+1 sig alg length (TLS 1.2 only)
1784 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001785 * n .. n+1 length of all DNs
1786 * n+2 .. n+3 length of DN 1
1787 * n+4 .. ... Distinguished Name #1
1788 * ... .. ... length of DN 2, etc.
1789 */
1790 buf = ssl->out_msg;
1791 p = buf + 4;
1792
1793 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001794 * Supported certificate types
1795 *
1796 * ClientCertificateType certificate_types<1..2^8-1>;
1797 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00001798 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001799 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01001800
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001801#if defined(POLARSSL_RSA_C)
1802 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
1803#endif
1804#if defined(POLARSSL_ECDSA_C)
1805 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
1806#endif
1807
1808 p[0] = ct_len++;
1809 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01001810
Paul Bakker577e0062013-08-28 11:57:20 +02001811 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001812#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01001813 /*
1814 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01001815 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001816 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
1817 *
1818 * struct {
1819 * HashAlgorithm hash;
1820 * SignatureAlgorithm signature;
1821 * } SignatureAndHashAlgorithm;
1822 *
1823 * enum { (255) } HashAlgorithm;
1824 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01001825 */
Paul Bakker21dca692013-01-03 11:41:08 +01001826 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01001827 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001828 /*
1829 * Only use current running hash algorithm that is already required
1830 * for requested ciphersuite.
1831 */
Paul Bakker926af752012-11-23 13:38:07 +01001832 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
1833
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001834 if( ssl->transform_negotiate->ciphersuite_info->mac ==
1835 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01001836 {
1837 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
1838 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02001839
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001840 /*
1841 * Supported signature algorithms
1842 */
1843#if defined(POLARSSL_RSA_C)
1844 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1845 p[2 + sa_len++] = SSL_SIG_RSA;
1846#endif
1847#if defined(POLARSSL_ECDSA_C)
1848 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1849 p[2 + sa_len++] = SSL_SIG_ECDSA;
1850#endif
Paul Bakker926af752012-11-23 13:38:07 +01001851
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001852 p[0] = (unsigned char)( sa_len >> 8 );
1853 p[1] = (unsigned char)( sa_len );
1854 sa_len += 2;
1855 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01001856 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001857#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001858
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001859 /*
1860 * DistinguishedName certificate_authorities<0..2^16-1>;
1861 * opaque DistinguishedName<1..2^16-1>;
1862 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001863 p += 2;
1864 crt = ssl->ca_chain;
1865
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001866 total_dn_size = 0;
Paul Bakker29087132010-03-21 21:03:34 +00001867 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001868 {
1869 if( p - buf > 4096 )
1870 break;
1871
Paul Bakker926af752012-11-23 13:38:07 +01001872 dn_size = crt->subject_raw.len;
1873 *p++ = (unsigned char)( dn_size >> 8 );
1874 *p++ = (unsigned char)( dn_size );
1875 memcpy( p, crt->subject_raw.p, dn_size );
1876 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001877
Paul Bakker926af752012-11-23 13:38:07 +01001878 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
1879
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001880 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01001881 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00001882 }
1883
Paul Bakker926af752012-11-23 13:38:07 +01001884 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001885 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1886 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001887 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
1888 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001889
1890 ret = ssl_write_record( ssl );
1891
1892 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
1893
1894 return( ret );
1895}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001896#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
1897 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
1898 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001899
Paul Bakker41c83d32013-03-20 14:39:14 +01001900static int ssl_write_server_key_exchange( ssl_context *ssl )
1901{
Paul Bakker23986e52011-04-24 08:57:21 +00001902 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001903 size_t n = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001904 const ssl_ciphersuite_t *ciphersuite_info;
1905
1906#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1907 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
1908 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1909 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001910 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001911 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001912 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001913 ((void) dig_signed);
1914 ((void) dig_signed_len);
1915#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001916
Paul Bakker41c83d32013-03-20 14:39:14 +01001917 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001918
1919 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
1920
Paul Bakker41c83d32013-03-20 14:39:14 +01001921 if( ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_RSA &&
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001922 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_ECDHE_RSA &&
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001923 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA &&
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001924 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001925 {
1926 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
1927 ssl->state++;
1928 return( 0 );
1929 }
1930
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001931#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1932 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1933 {
1934 /* TODO: Support identity hints */
1935 *(p++) = 0x00;
1936 *(p++) = 0x00;
1937
1938 n += 2;
1939 }
1940#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1941
1942#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1943 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1944 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1945 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00001946 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001947 /*
1948 * Ephemeral DH parameters:
1949 *
1950 * struct {
1951 * opaque dh_p<1..2^16-1>;
1952 * opaque dh_g<1..2^16-1>;
1953 * opaque dh_Ys<1..2^16-1>;
1954 * } ServerDHParams;
1955 */
1956 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
1957 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
1958 {
1959 SSL_DEBUG_RET( 1, "mpi_copy", ret );
1960 return( ret );
1961 }
Paul Bakker48916f92012-09-16 19:57:18 +00001962
Paul Bakker41c83d32013-03-20 14:39:14 +01001963 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
1964 mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001965 p,
1966 &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001967 {
1968 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
1969 return( ret );
1970 }
1971
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001972 dig_signed = p;
1973 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001974
1975 p += len;
1976 n += len;
1977
Paul Bakker41c83d32013-03-20 14:39:14 +01001978 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
1979 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1980 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1981 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
1982 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001983#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1984 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001985
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001986#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1987 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1988 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1989 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001990 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001991 /*
1992 * Ephemeral ECDH parameters:
1993 *
1994 * struct {
1995 * ECParameters curve_params;
1996 * ECPoint public;
1997 * } ServerECDHParams;
1998 */
Paul Bakker41c83d32013-03-20 14:39:14 +01001999 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02002000 ssl->handshake->curves[0]->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002001 {
2002 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2003 return( ret );
2004 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002005
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02002006 SSL_DEBUG_MSG( 2, ( "ECDH curve size: %d",
2007 (int) ssl->handshake->ecdh_ctx.grp.nbits ) );
2008
Paul Bakker41c83d32013-03-20 14:39:14 +01002009 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002010 &len,
2011 p,
Paul Bakker41c83d32013-03-20 14:39:14 +01002012 1000, ssl->f_rng, ssl->p_rng ) ) != 0 )
2013 {
2014 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2015 return( ret );
2016 }
2017
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002018 dig_signed = p;
2019 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002020
2021 p += len;
2022 n += len;
2023
Paul Bakker41c83d32013-03-20 14:39:14 +01002024 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2025 }
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002026#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2027 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002028
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002029#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002030 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2031 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002032 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002033 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2034 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002035 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002036 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002037 unsigned int hashlen = 0;
2038 unsigned char hash[64];
2039 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002040
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002041 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002042 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2043 */
Paul Bakker577e0062013-08-28 11:57:20 +02002044#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002045 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2046 {
2047 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2048
2049 if( md_alg == POLARSSL_MD_NONE )
2050 {
2051 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2052 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2053 }
2054 }
Paul Bakker577e0062013-08-28 11:57:20 +02002055 else
2056#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002057#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2058 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker577e0062013-08-28 11:57:20 +02002059 if ( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002060 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2061 {
2062 md_alg = POLARSSL_MD_SHA1;
2063 }
2064 else
Paul Bakker577e0062013-08-28 11:57:20 +02002065#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002066 {
2067 md_alg = POLARSSL_MD_NONE;
2068 }
2069
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002070 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002071 * Compute the hash to be signed
2072 */
Paul Bakker577e0062013-08-28 11:57:20 +02002073#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2074 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002075 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002076 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002077 md5_context md5;
2078 sha1_context sha1;
2079
2080 /*
2081 * digitally-signed struct {
2082 * opaque md5_hash[16];
2083 * opaque sha_hash[20];
2084 * };
2085 *
2086 * md5_hash
2087 * MD5(ClientHello.random + ServerHello.random
2088 * + ServerParams);
2089 * sha_hash
2090 * SHA(ClientHello.random + ServerHello.random
2091 * + ServerParams);
2092 */
2093 md5_starts( &md5 );
2094 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002095 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002096 md5_finish( &md5, hash );
2097
2098 sha1_starts( &sha1 );
2099 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002100 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002101 sha1_finish( &sha1, hash + 16 );
2102
2103 hashlen = 36;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002104 }
2105 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002106#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2107 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002108#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2109 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002110 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002111 {
2112 md_context_t ctx;
2113
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002114 /* Info from md_alg will be used instead */
2115 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002116
2117 /*
2118 * digitally-signed struct {
2119 * opaque client_random[32];
2120 * opaque server_random[32];
2121 * ServerDHParams params;
2122 * };
2123 */
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002124 if( ( ret = md_init_ctx( &ctx, md_info_from_type(md_alg) ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002125 {
2126 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2127 return( ret );
2128 }
2129
2130 md_starts( &ctx );
2131 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002132 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002133 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02002134
2135 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
2136 {
2137 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
2138 return( ret );
2139 }
2140
Paul Bakker23f36802012-09-28 14:15:14 +00002141 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002142 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002143#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2144 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002145 {
2146 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002147 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02002148 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002149
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002150 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2151 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002152
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002153 /*
2154 * Make the signature
2155 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002156 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002157 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002158 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2159 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002160 }
Paul Bakker23f36802012-09-28 14:15:14 +00002161
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002162#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002163 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2164 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002165 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002166 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002167
2168 n += 2;
2169 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002170#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002171
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002172 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002173 p + 2 , &signature_len,
2174 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002175 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002176 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002177 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002178 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002179
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002180 *(p++) = (unsigned char)( signature_len >> 8 );
2181 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002182 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002183
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002184 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002185
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002186 p += signature_len;
2187 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002188 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002189#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002190 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2191 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002192
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002193 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002194 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2195 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2196
2197 ssl->state++;
2198
2199 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2200 {
2201 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2202 return( ret );
2203 }
2204
2205 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2206
2207 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002208}
2209
2210static int ssl_write_server_hello_done( ssl_context *ssl )
2211{
2212 int ret;
2213
2214 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2215
2216 ssl->out_msglen = 4;
2217 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2218 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2219
2220 ssl->state++;
2221
2222 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2223 {
2224 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2225 return( ret );
2226 }
2227
2228 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2229
2230 return( 0 );
2231}
2232
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002233#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2234 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2235static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2236 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002237{
2238 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002239 size_t n;
2240
2241 /*
2242 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2243 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002244 if( *p + 2 > end )
2245 {
2246 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2247 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2248 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002249
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002250 n = ( (*p)[0] << 8 ) | (*p)[1];
2251 *p += 2;
2252
2253 if( n < 1 || n > ssl->handshake->dhm_ctx.len || *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002254 {
2255 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2256 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2257 }
2258
2259 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002260 *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002261 {
2262 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2263 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2264 }
2265
2266 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2267
Paul Bakker70df2fb2013-04-17 17:19:09 +02002268 return( ret );
2269}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002270#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2271 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002272
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02002273#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2274 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002275static int ssl_parse_client_ecdh_public( ssl_context *ssl )
2276{
2277 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002278 size_t n;
2279
2280 /*
2281 * Receive client public key and calculate premaster
2282 */
2283 n = ssl->in_msg[3];
2284
2285 if( n < 1 || n > mpi_size( &ssl->handshake->ecdh_ctx.grp.P ) * 2 + 2 ||
2286 n + 4 != ssl->in_hslen )
2287 {
2288 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2289 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2290 }
2291
2292 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2293 ssl->in_msg + 4, n ) ) != 0 )
2294 {
2295 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2296 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2297 }
2298
2299 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2300
Paul Bakker70df2fb2013-04-17 17:19:09 +02002301 return( ret );
2302}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02002303#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2304 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002305
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002306#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002307static int ssl_parse_encrypted_pms_secret( ssl_context *ssl )
2308{
2309 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2310 size_t i, n = 0;
2311
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002312 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002313 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002314 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002315 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2316 }
2317
2318 /*
2319 * Decrypt the premaster using own private RSA key
2320 */
2321 i = 4;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002322 n = pk_get_len( ssl_own_key( ssl ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002323 ssl->handshake->pmslen = 48;
2324
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002325#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2326 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002327 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2328 {
2329 i += 2;
2330 if( ssl->in_msg[4] != ( ( n >> 8 ) & 0xFF ) ||
2331 ssl->in_msg[5] != ( ( n ) & 0xFF ) )
2332 {
2333 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2334 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2335 }
2336 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002337#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002338
2339 if( ssl->in_hslen != i + n )
2340 {
2341 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2342 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2343 }
2344
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002345 ret = pk_decrypt( ssl_own_key( ssl ),
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002346 ssl->in_msg + i, n,
2347 ssl->handshake->premaster, &ssl->handshake->pmslen,
2348 sizeof(ssl->handshake->premaster),
2349 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002350
2351 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Paul Bakker2fbefde2013-06-29 16:01:15 +02002352 ssl->handshake->premaster[0] != ssl->handshake->max_major_ver ||
2353 ssl->handshake->premaster[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002354 {
2355 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2356
2357 /*
2358 * Protection against Bleichenbacher's attack:
2359 * invalid PKCS#1 v1.5 padding must not cause
2360 * the connection to end immediately; instead,
2361 * send a bad_record_mac later in the handshake.
2362 */
2363 ssl->handshake->pmslen = 48;
2364
2365 ret = ssl->f_rng( ssl->p_rng, ssl->handshake->premaster,
2366 ssl->handshake->pmslen );
2367 if( ret != 0 )
2368 return( ret );
2369 }
2370
2371 return( ret );
2372}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002373#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002374
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002375#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2376 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2377static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2378 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002379{
Paul Bakker6db455e2013-09-18 17:29:31 +02002380 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002381 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002382
Paul Bakker6db455e2013-09-18 17:29:31 +02002383 if( ssl->f_psk == NULL &&
2384 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2385 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002386 {
2387 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2388 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2389 }
2390
2391 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002392 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002393 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002394 if( *p + 2 > end )
2395 {
2396 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2397 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2398 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002399
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002400 n = ( (*p)[0] << 8 ) | (*p)[1];
2401 *p += 2;
2402
2403 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002404 {
2405 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2406 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2407 }
2408
Paul Bakker6db455e2013-09-18 17:29:31 +02002409 if( ssl->f_psk != NULL )
2410 {
2411 if( ( ret != ssl->f_psk( ssl->p_psk, ssl, *p, n ) ) != 0 )
2412 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2413 }
2414
2415 if( ret == 0 )
2416 {
2417 if( n != ssl->psk_identity_len ||
2418 memcmp( ssl->psk_identity, *p, n ) != 0 )
2419 {
2420 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2421 }
2422 }
2423
2424 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002425 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002426 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002427 if( ( ret = ssl_send_alert_message( ssl,
2428 SSL_ALERT_LEVEL_FATAL,
2429 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2430 {
2431 return( ret );
2432 }
2433
2434 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002435 }
2436
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002437 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002438 ret = 0;
2439
Paul Bakkerfbb17802013-04-17 19:10:21 +02002440 return( ret );
2441}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002442#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
2443 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002444
Paul Bakker5121ce52009-01-03 21:22:43 +00002445static int ssl_parse_client_key_exchange( ssl_context *ssl )
2446{
Paul Bakker23986e52011-04-24 08:57:21 +00002447 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002448 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002449
Paul Bakker41c83d32013-03-20 14:39:14 +01002450 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002451
2452 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2453
2454 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2455 {
2456 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2457 return( ret );
2458 }
2459
2460 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2461 {
2462 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002463 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002464 }
2465
2466 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2467 {
2468 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002469 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002470 }
2471
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002472#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002473 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002474 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002475 unsigned char *p = ssl->in_msg + 4;
2476 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2477
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002478 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002479 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002480 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2481 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002482 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002483
2484 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
2485
Manuel Pégourié-Gonnard032c34e2013-09-07 13:06:27 +02002486 /* No blinding needed for DHE, but will be needed for fixed DH! */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002487 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2488 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002489 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002490 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002491 {
2492 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2493 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2494 }
2495
2496 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002497 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002498 else
2499#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002500#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2501 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2502 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2503 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002504 {
2505 if( ( ret = ssl_parse_client_ecdh_public( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002506 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002507 SSL_DEBUG_RET( 1, ( "ssl_parse_client_ecdh_public" ), ret );
2508 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002509 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002510
2511 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2512 &ssl->handshake->pmslen,
2513 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002514 POLARSSL_MPI_MAX_SIZE,
2515 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002516 {
2517 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2518 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2519 }
2520
2521 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002522 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002523 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002524#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2525 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002526#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2527 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002528 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002529 unsigned char *p = ssl->in_msg + 4;
2530 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2531
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002532 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002533 {
2534 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2535 return( ret );
2536 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002537
2538 // Set up the premaster secret
2539 //
2540 p = ssl->handshake->premaster;
2541 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2542 *(p++) = (unsigned char)( ssl->psk_len );
2543 p += ssl->psk_len;
2544
2545 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2546 *(p++) = (unsigned char)( ssl->psk_len );
2547 memcpy( p, ssl->psk, ssl->psk_len );
2548 p += ssl->psk_len;
2549
2550 ssl->handshake->pmslen = 4 + 2 * ssl->psk_len;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002551 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002552 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002553#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
2554#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2555 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2556 {
2557 size_t n;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002558 unsigned char *p = ssl->in_msg + 4;
2559 unsigned char *end = ssl->in_msg + ssl->in_msglen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002560
2561 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2562 {
2563 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2564 return( ret );
2565 }
2566 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2567 {
2568 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2569 return( ret );
2570 }
2571
2572 // Set up the premaster secret
2573 //
2574 p = ssl->handshake->premaster;
2575 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len >> 8 );
2576 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len );
2577
Paul Bakker577e0062013-08-28 11:57:20 +02002578 n = ssl->handshake->dhm_ctx.len;
2579
Manuel Pégourié-Gonnard032c34e2013-09-07 13:06:27 +02002580 /* No blinding needed since this is ephemeral DHM */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002581 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002582 p, &n, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002583 {
2584 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2585 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2586 }
2587
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002588 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2589
2590 p += ssl->handshake->dhm_ctx.len;
2591
2592 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2593 *(p++) = (unsigned char)( ssl->psk_len );
2594 memcpy( p, ssl->psk, ssl->psk_len );
2595 p += ssl->psk_len;
2596
2597 ssl->handshake->pmslen = 4 + ssl->handshake->dhm_ctx.len + ssl->psk_len;
2598 }
2599 else
2600#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
2601#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2602 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002603 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002604 if( ( ret = ssl_parse_encrypted_pms_secret( ssl ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002605 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002606 SSL_DEBUG_RET( 1, ( "ssl_parse_client_ecdh_public" ), ret );
2607 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002608 }
2609 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002610 else
2611#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2612 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002613 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002614 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2615 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002616
Paul Bakkerff60ee62010-03-16 21:09:09 +00002617 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2618 {
2619 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2620 return( ret );
2621 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002622
Paul Bakker5121ce52009-01-03 21:22:43 +00002623 ssl->state++;
2624
2625 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2626
2627 return( 0 );
2628}
2629
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002630#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2631 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002632 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2633 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002634static int ssl_parse_certificate_verify( ssl_context *ssl )
2635{
Paul Bakkered27a042013-04-18 22:46:23 +02002636 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002637 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002638
2639 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2640
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002641 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2642 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002643 {
2644 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2645 ssl->state++;
2646 return( 0 );
2647 }
2648
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002649 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002650 return( ret );
2651}
2652#else
2653static int ssl_parse_certificate_verify( ssl_context *ssl )
2654{
2655 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002656 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002657 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002658 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002659 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02002660#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002661 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02002662#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002663 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002664 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2665
2666 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2667
2668 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2669 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2670 {
2671 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2672 ssl->state++;
2673 return( 0 );
2674 }
2675
Paul Bakkered27a042013-04-18 22:46:23 +02002676 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002677 {
2678 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2679 ssl->state++;
2680 return( 0 );
2681 }
2682
Paul Bakker48916f92012-09-16 19:57:18 +00002683 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002684
2685 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2686 {
2687 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2688 return( ret );
2689 }
2690
2691 ssl->state++;
2692
2693 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2694 {
2695 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002696 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002697 }
2698
2699 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
2700 {
2701 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002702 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002703 }
2704
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002705 /*
2706 * 0 . 0 handshake type
2707 * 1 . 3 handshake length
2708 * 4 . 5 sig alg (TLS 1.2 only)
2709 * 4+n . 5+n signature length (n = sa_len)
2710 * 6+n . 6+n+m signature (m = sig_len)
2711 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002712
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002713#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2714 defined(POLARSSL_SSL_PROTO_TLS1_1)
2715 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002716 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002717 sa_len = 0;
2718
Paul Bakkerc70b9822013-04-07 22:00:46 +02002719 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002720 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002721
2722 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
2723 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
2724 POLARSSL_PK_ECDSA ) )
2725 {
2726 hash_start += 16;
2727 hashlen -= 16;
2728 md_alg = POLARSSL_MD_SHA1;
2729 }
Paul Bakker926af752012-11-23 13:38:07 +01002730 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002731 else
2732#endif
Paul Bakker577e0062013-08-28 11:57:20 +02002733#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2734 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002735 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002736 sa_len = 2;
2737
Paul Bakker5121ce52009-01-03 21:22:43 +00002738 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002739 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00002740 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002741 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002742 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002743 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2744 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01002745 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2746 }
2747
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002748 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01002749
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002750 /* Info from md_alg will be used instead */
2751 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002752
2753 /*
2754 * Signature
2755 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002756 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
2757 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002758 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002759 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2760 " for verify message" ) );
2761 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002762 }
2763
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002764 /*
2765 * Check the certificate's key type matches the signature alg
2766 */
2767 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
2768 {
2769 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
2770 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2771 }
Paul Bakker577e0062013-08-28 11:57:20 +02002772 }
2773 else
2774#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2775 {
2776 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002777 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002778 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002779
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002780 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01002781
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002782 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002783 {
2784 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002785 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002786 }
2787
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002788 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002789 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002790 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002791 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002792 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002793 return( ret );
2794 }
2795
2796 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
2797
Paul Bakkered27a042013-04-18 22:46:23 +02002798 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002799}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002800#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2801 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2802 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002803
Paul Bakkera503a632013-08-14 13:48:06 +02002804#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002805static int ssl_write_new_session_ticket( ssl_context *ssl )
2806{
2807 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002808 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002809 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002810
2811 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
2812
2813 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2814 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
2815
2816 /*
2817 * struct {
2818 * uint32 ticket_lifetime_hint;
2819 * opaque ticket<0..2^16-1>;
2820 * } NewSessionTicket;
2821 *
2822 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
2823 * 8 . 9 ticket_len (n)
2824 * 10 . 9+n ticket content
2825 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002826
2827 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
2828 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
2829 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
2830 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002831
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02002832 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
2833 {
2834 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
2835 tlen = 0;
2836 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002837
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002838 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
2839 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002840
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002841 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002842
2843 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2844 {
2845 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2846 return( ret );
2847 }
2848
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002849 /* No need to remember writing a NewSessionTicket any more */
2850 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002851
2852 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
2853
2854 return( 0 );
2855}
Paul Bakkera503a632013-08-14 13:48:06 +02002856#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002857
Paul Bakker5121ce52009-01-03 21:22:43 +00002858/*
Paul Bakker1961b702013-01-25 14:49:24 +01002859 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002860 */
Paul Bakker1961b702013-01-25 14:49:24 +01002861int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002862{
2863 int ret = 0;
2864
Paul Bakker1961b702013-01-25 14:49:24 +01002865 if( ssl->state == SSL_HANDSHAKE_OVER )
2866 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002867
Paul Bakker1961b702013-01-25 14:49:24 +01002868 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
2869
2870 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2871 return( ret );
2872
2873 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002874 {
Paul Bakker1961b702013-01-25 14:49:24 +01002875 case SSL_HELLO_REQUEST:
2876 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002877 break;
2878
Paul Bakker1961b702013-01-25 14:49:24 +01002879 /*
2880 * <== ClientHello
2881 */
2882 case SSL_CLIENT_HELLO:
2883 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002884 break;
Paul Bakker1961b702013-01-25 14:49:24 +01002885
2886 /*
2887 * ==> ServerHello
2888 * Certificate
2889 * ( ServerKeyExchange )
2890 * ( CertificateRequest )
2891 * ServerHelloDone
2892 */
2893 case SSL_SERVER_HELLO:
2894 ret = ssl_write_server_hello( ssl );
2895 break;
2896
2897 case SSL_SERVER_CERTIFICATE:
2898 ret = ssl_write_certificate( ssl );
2899 break;
2900
2901 case SSL_SERVER_KEY_EXCHANGE:
2902 ret = ssl_write_server_key_exchange( ssl );
2903 break;
2904
2905 case SSL_CERTIFICATE_REQUEST:
2906 ret = ssl_write_certificate_request( ssl );
2907 break;
2908
2909 case SSL_SERVER_HELLO_DONE:
2910 ret = ssl_write_server_hello_done( ssl );
2911 break;
2912
2913 /*
2914 * <== ( Certificate/Alert )
2915 * ClientKeyExchange
2916 * ( CertificateVerify )
2917 * ChangeCipherSpec
2918 * Finished
2919 */
2920 case SSL_CLIENT_CERTIFICATE:
2921 ret = ssl_parse_certificate( ssl );
2922 break;
2923
2924 case SSL_CLIENT_KEY_EXCHANGE:
2925 ret = ssl_parse_client_key_exchange( ssl );
2926 break;
2927
2928 case SSL_CERTIFICATE_VERIFY:
2929 ret = ssl_parse_certificate_verify( ssl );
2930 break;
2931
2932 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2933 ret = ssl_parse_change_cipher_spec( ssl );
2934 break;
2935
2936 case SSL_CLIENT_FINISHED:
2937 ret = ssl_parse_finished( ssl );
2938 break;
2939
2940 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002941 * ==> ( NewSessionTicket )
2942 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002943 * Finished
2944 */
2945 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002946#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002947 if( ssl->handshake->new_session_ticket != 0 )
2948 ret = ssl_write_new_session_ticket( ssl );
2949 else
Paul Bakkera503a632013-08-14 13:48:06 +02002950#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002951 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002952 break;
2953
2954 case SSL_SERVER_FINISHED:
2955 ret = ssl_write_finished( ssl );
2956 break;
2957
2958 case SSL_FLUSH_BUFFERS:
2959 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2960 ssl->state = SSL_HANDSHAKE_WRAPUP;
2961 break;
2962
2963 case SSL_HANDSHAKE_WRAPUP:
2964 ssl_handshake_wrapup( ssl );
2965 break;
2966
2967 default:
2968 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2969 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002970 }
2971
Paul Bakker5121ce52009-01-03 21:22:43 +00002972 return( ret );
2973}
Paul Bakker5121ce52009-01-03 21:22:43 +00002974#endif