blob: 15d14fa8853411d34ca8928267265d369bb8cafe [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/*
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200342 * Wrapper around f_sni, allowing use of ssl_set_own_cert() but
343 * making it act on ssl->hanshake->sni_key_cert instead.
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200344 */
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 );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200353 ssl->handshake->sni_key_cert = ssl->key_cert;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200354
355 ssl->key_cert = key_cert_ori;
356
357 return( ret );
358}
359
Paul Bakker5701cdc2012-09-27 21:49:42 +0000360static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000361 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000362 size_t len )
363{
364 int ret;
365 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000366 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000367
368 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
369 if( servername_list_size + 2 != len )
370 {
371 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
372 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
373 }
374
375 p = buf + 2;
376 while( servername_list_size > 0 )
377 {
378 hostname_len = ( ( p[1] << 8 ) | p[2] );
379 if( hostname_len + 3 > servername_list_size )
380 {
381 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
382 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
383 }
384
385 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
386 {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200387 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000388 if( ret != 0 )
389 {
390 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
391 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
392 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
393 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000394 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000395 }
396
397 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000398 p += hostname_len + 3;
399 }
400
401 if( servername_list_size != 0 )
402 {
403 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
404 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000405 }
406
407 return( 0 );
408}
Paul Bakker0be444a2013-08-27 21:55:01 +0200409#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000410
Paul Bakker48916f92012-09-16 19:57:18 +0000411static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000412 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000413 size_t len )
414{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000415 int ret;
416
Paul Bakker48916f92012-09-16 19:57:18 +0000417 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
418 {
419 if( len != 1 || buf[0] != 0x0 )
420 {
421 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000422
423 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
424 return( ret );
425
Paul Bakker48916f92012-09-16 19:57:18 +0000426 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
427 }
428
429 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
430 }
431 else
432 {
433 if( len != 1 + ssl->verify_data_len ||
434 buf[0] != ssl->verify_data_len ||
435 memcmp( buf + 1, ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
436 {
437 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000438
439 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
440 return( ret );
441
Paul Bakker48916f92012-09-16 19:57:18 +0000442 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
443 }
444 }
445
446 return( 0 );
447}
448
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200449#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +0000450static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
451 const unsigned char *buf,
452 size_t len )
453{
454 size_t sig_alg_list_size;
455 const unsigned char *p;
456
457 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
458 if( sig_alg_list_size + 2 != len ||
459 sig_alg_list_size %2 != 0 )
460 {
461 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
462 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
463 }
464
465 p = buf + 2;
466 while( sig_alg_list_size > 0 )
467 {
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200468 /*
469 * For now, just ignore signature algorithm and rely on offered
470 * ciphersuites only. To be fixed later.
471 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200472#if defined(POLARSSL_SHA512_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000473 if( p[0] == SSL_HASH_SHA512 )
474 {
475 ssl->handshake->sig_alg = SSL_HASH_SHA512;
476 break;
477 }
478 if( p[0] == SSL_HASH_SHA384 )
479 {
480 ssl->handshake->sig_alg = SSL_HASH_SHA384;
481 break;
482 }
483#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200484#if defined(POLARSSL_SHA256_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000485 if( p[0] == SSL_HASH_SHA256 )
486 {
487 ssl->handshake->sig_alg = SSL_HASH_SHA256;
488 break;
489 }
490 if( p[0] == SSL_HASH_SHA224 )
491 {
492 ssl->handshake->sig_alg = SSL_HASH_SHA224;
493 break;
494 }
495#endif
496 if( p[0] == SSL_HASH_SHA1 )
497 {
498 ssl->handshake->sig_alg = SSL_HASH_SHA1;
499 break;
500 }
501 if( p[0] == SSL_HASH_MD5 )
502 {
503 ssl->handshake->sig_alg = SSL_HASH_MD5;
504 break;
505 }
506
507 sig_alg_list_size -= 2;
508 p += 2;
509 }
510
511 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
512 ssl->handshake->sig_alg ) );
513
514 return( 0 );
515}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200516#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker23f36802012-09-28 14:15:14 +0000517
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200518#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200519static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
520 const unsigned char *buf,
521 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100522{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200523 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100524 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200525 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100526
527 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
528 if( list_size + 2 != len ||
529 list_size % 2 != 0 )
530 {
531 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
532 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
533 }
534
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200535 /* Don't allow our peer to make use allocated too much memory,
536 * and leave room for a final 0 */
537 our_size = list_size / 2 + 1;
538 if( our_size > POLARSSL_ECP_DP_MAX )
539 our_size = POLARSSL_ECP_DP_MAX;
540
541 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
542 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
543
544 memset( curves, 0, our_size * sizeof( *curves ) );
545 ssl->handshake->curves = curves;
546
Paul Bakker41c83d32013-03-20 14:39:14 +0100547 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200548 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100549 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200550 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200551
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200552 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100553 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200554 *curves++ = curve_info;
555 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100556 }
557
558 list_size -= 2;
559 p += 2;
560 }
561
562 return( 0 );
563}
564
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200565static int ssl_parse_supported_point_formats( ssl_context *ssl,
566 const unsigned char *buf,
567 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100568{
569 size_t list_size;
570 const unsigned char *p;
571
572 list_size = buf[0];
573 if( list_size + 1 != len )
574 {
575 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
576 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
577 }
578
579 p = buf + 2;
580 while( list_size > 0 )
581 {
582 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
583 p[0] == POLARSSL_ECP_PF_COMPRESSED )
584 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200585 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200586 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100587 return( 0 );
588 }
589
590 list_size--;
591 p++;
592 }
593
594 return( 0 );
595}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200596#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100597
Paul Bakker05decb22013-08-15 13:33:48 +0200598#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200599static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
600 const unsigned char *buf,
601 size_t len )
602{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200603 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200604 {
605 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
606 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
607 }
608
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200609 ssl->session_negotiate->mfl_code = buf[0];
610
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200611 return( 0 );
612}
Paul Bakker05decb22013-08-15 13:33:48 +0200613#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200614
Paul Bakker1f2bc622013-08-15 13:45:55 +0200615#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200616static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
617 const unsigned char *buf,
618 size_t len )
619{
620 if( len != 0 )
621 {
622 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
623 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
624 }
625
626 ((void) buf);
627
628 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
629
630 return( 0 );
631}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200632#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200633
Paul Bakkera503a632013-08-14 13:48:06 +0200634#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200635static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200636 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200637 size_t len )
638{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200639 int ret;
640
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200641 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
642 return( 0 );
643
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200644 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200645 ssl->handshake->new_session_ticket = 1;
646
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200647 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
648
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200649 if( len == 0 )
650 return( 0 );
651
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200652 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
653 {
654 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
655 return( 0 );
656 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200657
658 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200659 * Failures are ok: just ignore the ticket and proceed.
660 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200661 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
662 {
663 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200664 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200665 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200666
667 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
668
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200669 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200670
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200671 /* Don't send a new ticket after all, this one is OK */
672 ssl->handshake->new_session_ticket = 0;
673
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200674 return( 0 );
675}
Paul Bakkera503a632013-08-14 13:48:06 +0200676#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200677
Paul Bakker78a8c712013-03-06 17:01:52 +0100678#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
679static int ssl_parse_client_hello_v2( ssl_context *ssl )
680{
681 int ret;
682 unsigned int i, j;
683 size_t n;
684 unsigned int ciph_len, sess_len, chal_len;
685 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200686 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200687 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100688
689 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
690
691 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
692 {
693 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
694
695 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
696 return( ret );
697
698 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
699 }
700
701 buf = ssl->in_hdr;
702
703 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
704
705 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
706 buf[2] ) );
707 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
708 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
709 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
710 buf[3], buf[4] ) );
711
712 /*
713 * SSLv2 Client Hello
714 *
715 * Record layer:
716 * 0 . 1 message length
717 *
718 * SSL layer:
719 * 2 . 2 message type
720 * 3 . 4 protocol version
721 */
722 if( buf[2] != SSL_HS_CLIENT_HELLO ||
723 buf[3] != SSL_MAJOR_VERSION_3 )
724 {
725 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
726 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
727 }
728
729 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
730
731 if( n < 17 || n > 512 )
732 {
733 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
734 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
735 }
736
737 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200738 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
739 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100740
741 if( ssl->minor_ver < ssl->min_minor_ver )
742 {
743 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
744 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
745 ssl->min_major_ver, ssl->min_minor_ver ) );
746
747 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
748 SSL_ALERT_MSG_PROTOCOL_VERSION );
749 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
750 }
751
Paul Bakker2fbefde2013-06-29 16:01:15 +0200752 ssl->handshake->max_major_ver = buf[3];
753 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100754
755 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
756 {
757 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
758 return( ret );
759 }
760
761 ssl->handshake->update_checksum( ssl, buf + 2, n );
762
763 buf = ssl->in_msg;
764 n = ssl->in_left - 5;
765
766 /*
767 * 0 . 1 ciphersuitelist length
768 * 2 . 3 session id length
769 * 4 . 5 challenge length
770 * 6 . .. ciphersuitelist
771 * .. . .. session id
772 * .. . .. challenge
773 */
774 SSL_DEBUG_BUF( 4, "record contents", buf, n );
775
776 ciph_len = ( buf[0] << 8 ) | buf[1];
777 sess_len = ( buf[2] << 8 ) | buf[3];
778 chal_len = ( buf[4] << 8 ) | buf[5];
779
780 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
781 ciph_len, sess_len, chal_len ) );
782
783 /*
784 * Make sure each parameter length is valid
785 */
786 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
787 {
788 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
789 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
790 }
791
792 if( sess_len > 32 )
793 {
794 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
795 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
796 }
797
798 if( chal_len < 8 || chal_len > 32 )
799 {
800 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
801 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
802 }
803
804 if( n != 6 + ciph_len + sess_len + chal_len )
805 {
806 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
807 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
808 }
809
810 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
811 buf + 6, ciph_len );
812 SSL_DEBUG_BUF( 3, "client hello, session id",
813 buf + 6 + ciph_len, sess_len );
814 SSL_DEBUG_BUF( 3, "client hello, challenge",
815 buf + 6 + ciph_len + sess_len, chal_len );
816
817 p = buf + 6 + ciph_len;
818 ssl->session_negotiate->length = sess_len;
819 memset( ssl->session_negotiate->id, 0, sizeof( ssl->session_negotiate->id ) );
820 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
821
822 p += sess_len;
823 memset( ssl->handshake->randbytes, 0, 64 );
824 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
825
826 /*
827 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
828 */
829 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
830 {
831 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
832 {
833 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
834 if( ssl->renegotiation == SSL_RENEGOTIATION )
835 {
836 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
837
838 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
839 return( ret );
840
841 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
842 }
843 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
844 break;
845 }
846 }
847
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200848 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
849 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +0100850 {
851 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
852 {
Paul Bakker41c83d32013-03-20 14:39:14 +0100853 // Only allow non-ECC ciphersuites as we do not have extensions
854 //
Paul Bakker59c28a22013-06-29 15:33:42 +0200855 if( p[0] == 0 && p[1] == 0 &&
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200856 ( ( ciphersuites[i] >> 8 ) & 0xFF ) == 0 &&
857 p[2] == ( ciphersuites[i] & 0xFF ) )
Paul Bakker59c28a22013-06-29 15:33:42 +0200858 {
859 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
860
861 if( ciphersuite_info == NULL )
862 {
863 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %02x not found",
864 ciphersuites[i] ) );
865 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
866 }
867
Paul Bakker2fbefde2013-06-29 16:01:15 +0200868 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
869 ciphersuite_info->max_minor_ver < ssl->minor_ver )
870 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +0200871
Paul Bakker78a8c712013-03-06 17:01:52 +0100872 goto have_ciphersuite_v2;
Paul Bakker59c28a22013-06-29 15:33:42 +0200873 }
Paul Bakker78a8c712013-03-06 17:01:52 +0100874 }
875 }
876
877 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
878
879 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
880
881have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200882 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +0200883 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +0100884 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +0100885
886 /*
887 * SSLv2 Client Hello relevant renegotiation security checks
888 */
889 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
890 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
891 {
892 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
893
894 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
895 return( ret );
896
897 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
898 }
899
900 ssl->in_left = 0;
901 ssl->state++;
902
903 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
904
905 return( 0 );
906}
907#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
908
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200909#if defined(POLARSSL_X509_CRT_PARSE_C)
910#if defined(POLARSSL_ECDSA_C)
911static int ssl_key_matches_curves( pk_context *pk,
912 const ecp_curve_info **curves )
913{
914 const ecp_curve_info **crv = curves;
915 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
916
917 while( *crv != NULL )
918 {
919 if( (*crv)->grp_id == grp_id )
920 return( 1 );
921 crv++;
922 }
923
924 return( 0 );
925}
926#endif /* POLARSSL_ECDSA_C */
927
928/*
929 * Try picking a certificate for this ciphersuite,
930 * return 0 on success and -1 on failure.
931 */
932static int ssl_pick_cert( ssl_context *ssl,
933 const ssl_ciphersuite_t * ciphersuite_info )
934{
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200935 ssl_key_cert *cur, *list;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200936 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
937
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200938#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
939 if( ssl->handshake->sni_key_cert != NULL )
940 list = ssl->handshake->sni_key_cert;
941 else
942#endif
943 list = ssl->handshake->key_cert;
944
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200945 if( pk_alg == POLARSSL_PK_NONE )
946 return( 0 );
947
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200948 for( cur = list; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200949 {
950 if( ! pk_can_do( cur->key, pk_alg ) )
951 continue;
952
953#if defined(POLARSSL_ECDSA_C)
954 if( pk_alg == POLARSSL_PK_ECDSA )
955 {
956 if( ssl_key_matches_curves( cur->key, ssl->handshake->curves ) )
957 break;
958 }
959 else
960#endif
961 break;
962 }
963
964 if( cur == NULL )
965 return( -1 );
966
967 ssl->handshake->key_cert = cur;
968 return( 0 );
969}
970#endif /* POLARSSL_X509_CRT_PARSE_C */
971
Paul Bakker5121ce52009-01-03 21:22:43 +0000972static int ssl_parse_client_hello( ssl_context *ssl )
973{
Paul Bakker23986e52011-04-24 08:57:21 +0000974 int ret;
975 unsigned int i, j;
976 size_t n;
977 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +0000978 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +0000979 unsigned int ext_len = 0;
980 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000981 int renegotiation_info_seen = 0;
982 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200983 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +0100984 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000985
986 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
987
Paul Bakker48916f92012-09-16 19:57:18 +0000988 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
989 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000990 {
991 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
992 return( ret );
993 }
994
995 buf = ssl->in_hdr;
996
Paul Bakker78a8c712013-03-06 17:01:52 +0100997#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
998 if( ( buf[0] & 0x80 ) != 0 )
999 return ssl_parse_client_hello_v2( ssl );
1000#endif
1001
Paul Bakkerec636f32012-09-09 19:17:02 +00001002 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1003
1004 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1005 buf[0] ) );
1006 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1007 ( buf[3] << 8 ) | buf[4] ) );
1008 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
1009 buf[1], buf[2] ) );
1010
1011 /*
1012 * SSLv3 Client Hello
1013 *
1014 * Record layer:
1015 * 0 . 0 message type
1016 * 1 . 2 protocol version
1017 * 3 . 4 message length
1018 */
1019 if( buf[0] != SSL_MSG_HANDSHAKE ||
1020 buf[1] != SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001021 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001022 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1023 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1024 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001025
Paul Bakkerec636f32012-09-09 19:17:02 +00001026 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +00001027
Manuel Pégourié-Gonnard72882b22013-08-02 13:36:00 +02001028 if( n < 45 || n > 2048 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001029 {
1030 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1031 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1032 }
1033
Paul Bakker48916f92012-09-16 19:57:18 +00001034 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1035 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001036 {
1037 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1038 return( ret );
1039 }
1040
1041 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +00001042 if( !ssl->renegotiation )
1043 n = ssl->in_left - 5;
1044 else
1045 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +00001046
Paul Bakker48916f92012-09-16 19:57:18 +00001047 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +00001048
1049 /*
1050 * SSL layer:
1051 * 0 . 0 handshake type
1052 * 1 . 3 handshake length
1053 * 4 . 5 protocol version
1054 * 6 . 9 UNIX time()
1055 * 10 . 37 random bytes
1056 * 38 . 38 session id length
1057 * 39 . 38+x session id
1058 * 39+x . 40+x ciphersuitelist length
1059 * 41+x . .. ciphersuitelist
1060 * .. . .. compression alg.
1061 * .. . .. extensions
1062 */
1063 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1064
1065 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1066 buf[0] ) );
1067 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1068 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1069 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1070 buf[4], buf[5] ) );
1071
1072 /*
1073 * Check the handshake type and protocol version
1074 */
1075 if( buf[0] != SSL_HS_CLIENT_HELLO ||
1076 buf[4] != SSL_MAJOR_VERSION_3 )
1077 {
1078 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1079 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1080 }
1081
1082 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +02001083 ssl->minor_ver = ( buf[5] <= ssl->max_minor_ver )
1084 ? buf[5] : ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001085
Paul Bakker1d29fb52012-09-28 13:28:45 +00001086 if( ssl->minor_ver < ssl->min_minor_ver )
1087 {
1088 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
1089 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001090 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001091
1092 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1093 SSL_ALERT_MSG_PROTOCOL_VERSION );
1094
1095 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1096 }
1097
Paul Bakker2fbefde2013-06-29 16:01:15 +02001098 ssl->handshake->max_major_ver = buf[4];
1099 ssl->handshake->max_minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001100
Paul Bakker48916f92012-09-16 19:57:18 +00001101 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001102
1103 /*
1104 * Check the handshake message length
1105 */
1106 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1107 {
1108 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1109 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1110 }
1111
1112 /*
1113 * Check the session length
1114 */
1115 sess_len = buf[38];
1116
1117 if( sess_len > 32 )
1118 {
1119 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1120 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1121 }
1122
Paul Bakker48916f92012-09-16 19:57:18 +00001123 ssl->session_negotiate->length = sess_len;
1124 memset( ssl->session_negotiate->id, 0,
1125 sizeof( ssl->session_negotiate->id ) );
1126 memcpy( ssl->session_negotiate->id, buf + 39,
1127 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001128
1129 /*
1130 * Check the ciphersuitelist length
1131 */
1132 ciph_len = ( buf[39 + sess_len] << 8 )
1133 | ( buf[40 + sess_len] );
1134
1135 if( ciph_len < 2 || ciph_len > 256 || ( ciph_len % 2 ) != 0 )
1136 {
1137 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1138 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1139 }
1140
1141 /*
1142 * Check the compression algorithms length
1143 */
1144 comp_len = buf[41 + sess_len + ciph_len];
1145
1146 if( comp_len < 1 || comp_len > 16 )
1147 {
1148 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1149 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1150 }
1151
Paul Bakker48916f92012-09-16 19:57:18 +00001152 /*
1153 * Check the extension length
1154 */
1155 if( n > 42 + sess_len + ciph_len + comp_len )
1156 {
1157 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1158 | ( buf[43 + sess_len + ciph_len + comp_len] );
1159
1160 if( ( ext_len > 0 && ext_len < 4 ) ||
1161 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1162 {
1163 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1164 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1165 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1166 }
1167 }
1168
1169 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001170#if defined(POLARSSL_ZLIB_SUPPORT)
1171 for( i = 0; i < comp_len; ++i )
1172 {
Paul Bakker48916f92012-09-16 19:57:18 +00001173 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001174 {
Paul Bakker48916f92012-09-16 19:57:18 +00001175 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001176 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001177 }
1178 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001179#endif
1180
Paul Bakkerec636f32012-09-09 19:17:02 +00001181 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1182 buf + 6, 32 );
1183 SSL_DEBUG_BUF( 3, "client hello, session id",
1184 buf + 38, sess_len );
1185 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1186 buf + 41 + sess_len, ciph_len );
1187 SSL_DEBUG_BUF( 3, "client hello, compression",
1188 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001189
Paul Bakkerec636f32012-09-09 19:17:02 +00001190 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001191 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1192 */
1193 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1194 {
1195 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1196 {
1197 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1198 if( ssl->renegotiation == SSL_RENEGOTIATION )
1199 {
1200 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001201
1202 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1203 return( ret );
1204
Paul Bakker48916f92012-09-16 19:57:18 +00001205 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1206 }
1207 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1208 break;
1209 }
1210 }
1211
Paul Bakker48916f92012-09-16 19:57:18 +00001212 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001213
1214 while( ext_len )
1215 {
1216 unsigned int ext_id = ( ( ext[0] << 8 )
1217 | ( ext[1] ) );
1218 unsigned int ext_size = ( ( ext[2] << 8 )
1219 | ( ext[3] ) );
1220
1221 if( ext_size + 4 > ext_len )
1222 {
1223 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1224 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1225 }
1226 switch( ext_id )
1227 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001228#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001229 case TLS_EXT_SERVERNAME:
1230 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1231 if( ssl->f_sni == NULL )
1232 break;
1233
1234 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1235 if( ret != 0 )
1236 return( ret );
1237 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001238#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001239
Paul Bakker48916f92012-09-16 19:57:18 +00001240 case TLS_EXT_RENEGOTIATION_INFO:
1241 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1242 renegotiation_info_seen = 1;
1243
Paul Bakker23f36802012-09-28 14:15:14 +00001244 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1245 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001246 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001247 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001248
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001249#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001250 case TLS_EXT_SIG_ALG:
1251 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1252 if( ssl->renegotiation == SSL_RENEGOTIATION )
1253 break;
1254
1255 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1256 if( ret != 0 )
1257 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001258 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001259#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001260
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001261#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001262 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1263 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1264
1265 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1266 if( ret != 0 )
1267 return( ret );
1268 break;
1269
1270 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1271 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1272
1273 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1274 if( ret != 0 )
1275 return( ret );
1276 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001277#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001278
Paul Bakker05decb22013-08-15 13:33:48 +02001279#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001280 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1281 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1282
1283 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1284 if( ret != 0 )
1285 return( ret );
1286 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001287#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001288
Paul Bakker1f2bc622013-08-15 13:45:55 +02001289#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001290 case TLS_EXT_TRUNCATED_HMAC:
1291 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1292
1293 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1294 if( ret != 0 )
1295 return( ret );
1296 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001297#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001298
Paul Bakkera503a632013-08-14 13:48:06 +02001299#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001300 case TLS_EXT_SESSION_TICKET:
1301 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1302
1303 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1304 if( ret != 0 )
1305 return( ret );
1306 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001307#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001308
Paul Bakker48916f92012-09-16 19:57:18 +00001309 default:
1310 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1311 ext_id ) );
1312 }
1313
1314 ext_len -= 4 + ext_size;
1315 ext += 4 + ext_size;
1316
1317 if( ext_len > 0 && ext_len < 4 )
1318 {
1319 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1320 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1321 }
1322 }
1323
1324 /*
1325 * Renegotiation security checks
1326 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001327 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1328 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1329 {
1330 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1331 handshake_failure = 1;
1332 }
1333 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1334 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1335 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001336 {
1337 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001338 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001339 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001340 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1341 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1342 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001343 {
1344 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001345 handshake_failure = 1;
1346 }
1347 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1348 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1349 renegotiation_info_seen == 1 )
1350 {
1351 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1352 handshake_failure = 1;
1353 }
1354
1355 if( handshake_failure == 1 )
1356 {
1357 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1358 return( ret );
1359
Paul Bakker48916f92012-09-16 19:57:18 +00001360 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1361 }
Paul Bakker380da532012-04-18 16:10:25 +00001362
Paul Bakker41c83d32013-03-20 14:39:14 +01001363 /*
1364 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001365 * (At the end because we need information from the EC-based extensions
1366 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001367 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001368 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
1369 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001370 {
1371 for( j = 0, p = buf + 41 + sess_len; j < ciph_len;
1372 j += 2, p += 2 )
1373 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001374 if( p[0] == ( ( ciphersuites[i] >> 8 ) & 0xFF ) &&
1375 p[1] == ( ( ciphersuites[i] ) & 0xFF ) )
Paul Bakker41c83d32013-03-20 14:39:14 +01001376 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001377 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
Paul Bakker41c83d32013-03-20 14:39:14 +01001378
1379 if( ciphersuite_info == NULL )
1380 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001381 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found",
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001382 ciphersuites[i] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001383 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1384 }
1385
Paul Bakker2fbefde2013-06-29 16:01:15 +02001386 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
1387 ciphersuite_info->max_minor_ver < ssl->minor_ver )
1388 continue;
1389
Paul Bakker5fd49172013-08-19 13:29:26 +02001390#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001391 if( ssl_ciphersuite_uses_ec( ciphersuite_info ) &&
Paul Bakkercaa3af42013-09-26 13:32:43 +02001392 ( ssl->handshake->curves == NULL ||
1393 ssl->handshake->curves[0] == NULL ) )
Paul Bakker41c83d32013-03-20 14:39:14 +01001394 continue;
Paul Bakker5fd49172013-08-19 13:29:26 +02001395#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001396
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001397#if defined(POLARSSL_X509_CRT_PARSE_C)
1398 /*
1399 * Final check: if ciphersuite requires us to have a
1400 * certificate/key of a particular type:
1401 * - select the appropriate certificate if we have one, or
1402 * - try the next ciphersuite if we don't
1403 * This must be done last since we modify the key_cert list.
1404 */
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02001405 if( ssl_pick_cert( ssl, ciphersuite_info ) != 0 )
1406 continue;
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02001407#endif
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001408
Paul Bakker41c83d32013-03-20 14:39:14 +01001409 goto have_ciphersuite;
1410 }
1411 }
1412 }
1413
1414 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1415
1416 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1417 return( ret );
1418
1419 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1420
1421have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001422 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001423 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1424 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1425
Paul Bakker5121ce52009-01-03 21:22:43 +00001426 ssl->in_left = 0;
1427 ssl->state++;
1428
1429 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1430
1431 return( 0 );
1432}
1433
Paul Bakker1f2bc622013-08-15 13:45:55 +02001434#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001435static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1436 unsigned char *buf,
1437 size_t *olen )
1438{
1439 unsigned char *p = buf;
1440
1441 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1442 {
1443 *olen = 0;
1444 return;
1445 }
1446
1447 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1448
1449 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1450 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1451
1452 *p++ = 0x00;
1453 *p++ = 0x00;
1454
1455 *olen = 4;
1456}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001457#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001458
Paul Bakkera503a632013-08-14 13:48:06 +02001459#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001460static void ssl_write_session_ticket_ext( ssl_context *ssl,
1461 unsigned char *buf,
1462 size_t *olen )
1463{
1464 unsigned char *p = buf;
1465
1466 if( ssl->handshake->new_session_ticket == 0 )
1467 {
1468 *olen = 0;
1469 return;
1470 }
1471
1472 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1473
1474 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1475 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1476
1477 *p++ = 0x00;
1478 *p++ = 0x00;
1479
1480 *olen = 4;
1481}
Paul Bakkera503a632013-08-14 13:48:06 +02001482#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001483
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001484static void ssl_write_renegotiation_ext( ssl_context *ssl,
1485 unsigned char *buf,
1486 size_t *olen )
1487{
1488 unsigned char *p = buf;
1489
1490 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1491 {
1492 *olen = 0;
1493 return;
1494 }
1495
1496 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1497
1498 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1499 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1500
1501 *p++ = 0x00;
1502 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1503 *p++ = ssl->verify_data_len * 2 & 0xFF;
1504
1505 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1506 p += ssl->verify_data_len;
1507 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1508 p += ssl->verify_data_len;
1509
1510 *olen = 5 + ssl->verify_data_len * 2;
1511}
1512
Paul Bakker05decb22013-08-15 13:33:48 +02001513#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001514static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1515 unsigned char *buf,
1516 size_t *olen )
1517{
1518 unsigned char *p = buf;
1519
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001520 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1521 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001522 *olen = 0;
1523 return;
1524 }
1525
1526 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1527
1528 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1529 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1530
1531 *p++ = 0x00;
1532 *p++ = 1;
1533
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001534 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001535
1536 *olen = 5;
1537}
Paul Bakker05decb22013-08-15 13:33:48 +02001538#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001539
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001540#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001541static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1542 unsigned char *buf,
1543 size_t *olen )
1544{
1545 unsigned char *p = buf;
1546 ((void) ssl);
1547
1548 *olen = 0;
1549
1550 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1551
1552 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1553 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1554
1555 *p++ = 0x00;
1556 *p++ = 2;
1557
1558 *p++ = 1;
1559 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1560
1561 *olen = 6;
1562}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001563#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001564
Paul Bakker5121ce52009-01-03 21:22:43 +00001565static int ssl_write_server_hello( ssl_context *ssl )
1566{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001567#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001568 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001569#endif
Paul Bakkera3d195c2011-11-27 21:07:34 +00001570 int ret, n;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001571 size_t olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001572 unsigned char *buf, *p;
1573
1574 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1575
1576 /*
1577 * 0 . 0 handshake type
1578 * 1 . 3 handshake length
1579 * 4 . 5 protocol version
1580 * 6 . 9 UNIX time()
1581 * 10 . 37 random bytes
1582 */
1583 buf = ssl->out_msg;
1584 p = buf + 4;
1585
1586 *p++ = (unsigned char) ssl->major_ver;
1587 *p++ = (unsigned char) ssl->minor_ver;
1588
1589 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1590 buf[4], buf[5] ) );
1591
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001592#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001593 t = time( NULL );
1594 *p++ = (unsigned char)( t >> 24 );
1595 *p++ = (unsigned char)( t >> 16 );
1596 *p++ = (unsigned char)( t >> 8 );
1597 *p++ = (unsigned char)( t );
1598
1599 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001600#else
1601 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1602 return( ret );
1603
1604 p += 4;
1605#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001606
Paul Bakkera3d195c2011-11-27 21:07:34 +00001607 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1608 return( ret );
1609
1610 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001611
Paul Bakker48916f92012-09-16 19:57:18 +00001612 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001613
1614 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1615
1616 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001617 * Resume is 0 by default, see ssl_handshake_init().
1618 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1619 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001620 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001621 if( ssl->handshake->resume == 0 &&
1622 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001623 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001624 ssl->f_get_cache != NULL &&
1625 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1626 {
1627 ssl->handshake->resume = 1;
1628 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001629
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001630 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001631 {
1632 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001633 * New session, create a new session id,
1634 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001635 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001636 ssl->state++;
1637
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001638#if defined(POLARSSL_HAVE_TIME)
1639 ssl->session_negotiate->start = time( NULL );
1640#endif
1641
Paul Bakkera503a632013-08-14 13:48:06 +02001642#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001643 if( ssl->handshake->new_session_ticket != 0 )
1644 {
1645 ssl->session_negotiate->length = n = 0;
1646 memset( ssl->session_negotiate->id, 0, 32 );
1647 }
1648 else
1649#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001650 {
1651 ssl->session_negotiate->length = n = 32;
1652 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001653 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001654 return( ret );
1655 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001656 }
1657 else
1658 {
1659 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001660 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001661 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001662 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001663 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001664
1665 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1666 {
1667 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1668 return( ret );
1669 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001670 }
1671
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001672 /*
1673 * 38 . 38 session id length
1674 * 39 . 38+n session id
1675 * 39+n . 40+n chosen ciphersuite
1676 * 41+n . 41+n chosen compression alg.
1677 * 42+n . 43+n extensions length
1678 * 44+n . 43+n+m extensions
1679 */
1680 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001681 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1682 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001683
1684 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1685 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1686 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001687 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001688
Paul Bakker48916f92012-09-16 19:57:18 +00001689 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1690 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1691 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001692
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001693 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
1694 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001695 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001696 ssl->session_negotiate->compression ) );
1697
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001698 /*
1699 * First write extensions, then the total length
1700 */
1701 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1702 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001703
Paul Bakker05decb22013-08-15 13:33:48 +02001704#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001705 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1706 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001707#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001708
Paul Bakker1f2bc622013-08-15 13:45:55 +02001709#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001710 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1711 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001712#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001713
Paul Bakkera503a632013-08-14 13:48:06 +02001714#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001715 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1716 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001717#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001718
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001719#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001720 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1721 ext_len += olen;
1722#endif
1723
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001724 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001725
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001726 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1727 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1728 p += ext_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001729
1730 ssl->out_msglen = p - buf;
1731 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1732 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1733
1734 ret = ssl_write_record( ssl );
1735
1736 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1737
1738 return( ret );
1739}
1740
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001741#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1742 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001743 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1744 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001745static int ssl_write_certificate_request( ssl_context *ssl )
1746{
Paul Bakkered27a042013-04-18 22:46:23 +02001747 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1748 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001749
1750 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1751
1752 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1753 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1754 {
1755 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1756 ssl->state++;
1757 return( 0 );
1758 }
1759
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001760 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001761 return( ret );
1762}
1763#else
1764static int ssl_write_certificate_request( ssl_context *ssl )
1765{
1766 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1767 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001768 size_t dn_size, total_dn_size; /* excluding length bytes */
1769 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00001770 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001771 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001772
1773 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1774
1775 ssl->state++;
1776
Paul Bakkerfbb17802013-04-17 19:10:21 +02001777 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001778 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001779 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001780 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001781 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001782 return( 0 );
1783 }
1784
1785 /*
1786 * 0 . 0 handshake type
1787 * 1 . 3 handshake length
1788 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01001789 * 5 .. m-1 cert types
1790 * m .. m+1 sig alg length (TLS 1.2 only)
1791 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001792 * n .. n+1 length of all DNs
1793 * n+2 .. n+3 length of DN 1
1794 * n+4 .. ... Distinguished Name #1
1795 * ... .. ... length of DN 2, etc.
1796 */
1797 buf = ssl->out_msg;
1798 p = buf + 4;
1799
1800 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001801 * Supported certificate types
1802 *
1803 * ClientCertificateType certificate_types<1..2^8-1>;
1804 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00001805 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001806 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01001807
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001808#if defined(POLARSSL_RSA_C)
1809 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
1810#endif
1811#if defined(POLARSSL_ECDSA_C)
1812 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
1813#endif
1814
1815 p[0] = ct_len++;
1816 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01001817
Paul Bakker577e0062013-08-28 11:57:20 +02001818 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001819#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01001820 /*
1821 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01001822 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001823 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
1824 *
1825 * struct {
1826 * HashAlgorithm hash;
1827 * SignatureAlgorithm signature;
1828 * } SignatureAndHashAlgorithm;
1829 *
1830 * enum { (255) } HashAlgorithm;
1831 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01001832 */
Paul Bakker21dca692013-01-03 11:41:08 +01001833 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01001834 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001835 /*
1836 * Only use current running hash algorithm that is already required
1837 * for requested ciphersuite.
1838 */
Paul Bakker926af752012-11-23 13:38:07 +01001839 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
1840
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001841 if( ssl->transform_negotiate->ciphersuite_info->mac ==
1842 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01001843 {
1844 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
1845 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02001846
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001847 /*
1848 * Supported signature algorithms
1849 */
1850#if defined(POLARSSL_RSA_C)
1851 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1852 p[2 + sa_len++] = SSL_SIG_RSA;
1853#endif
1854#if defined(POLARSSL_ECDSA_C)
1855 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1856 p[2 + sa_len++] = SSL_SIG_ECDSA;
1857#endif
Paul Bakker926af752012-11-23 13:38:07 +01001858
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001859 p[0] = (unsigned char)( sa_len >> 8 );
1860 p[1] = (unsigned char)( sa_len );
1861 sa_len += 2;
1862 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01001863 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001864#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001865
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001866 /*
1867 * DistinguishedName certificate_authorities<0..2^16-1>;
1868 * opaque DistinguishedName<1..2^16-1>;
1869 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001870 p += 2;
1871 crt = ssl->ca_chain;
1872
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001873 total_dn_size = 0;
Paul Bakker29087132010-03-21 21:03:34 +00001874 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001875 {
1876 if( p - buf > 4096 )
1877 break;
1878
Paul Bakker926af752012-11-23 13:38:07 +01001879 dn_size = crt->subject_raw.len;
1880 *p++ = (unsigned char)( dn_size >> 8 );
1881 *p++ = (unsigned char)( dn_size );
1882 memcpy( p, crt->subject_raw.p, dn_size );
1883 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001884
Paul Bakker926af752012-11-23 13:38:07 +01001885 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
1886
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001887 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01001888 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00001889 }
1890
Paul Bakker926af752012-11-23 13:38:07 +01001891 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001892 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1893 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001894 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
1895 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001896
1897 ret = ssl_write_record( ssl );
1898
1899 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
1900
1901 return( ret );
1902}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001903#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
1904 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
1905 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001906
Paul Bakker41c83d32013-03-20 14:39:14 +01001907static int ssl_write_server_key_exchange( ssl_context *ssl )
1908{
Paul Bakker23986e52011-04-24 08:57:21 +00001909 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001910 size_t n = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001911 const ssl_ciphersuite_t *ciphersuite_info;
1912
1913#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1914 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
1915 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1916 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001917 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001918 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001919 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001920 ((void) dig_signed);
1921 ((void) dig_signed_len);
1922#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001923
Paul Bakker41c83d32013-03-20 14:39:14 +01001924 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001925
1926 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
1927
Paul Bakker41c83d32013-03-20 14:39:14 +01001928 if( ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_RSA &&
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001929 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_ECDHE_RSA &&
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001930 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA &&
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001931 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001932 {
1933 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
1934 ssl->state++;
1935 return( 0 );
1936 }
1937
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001938#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1939 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1940 {
1941 /* TODO: Support identity hints */
1942 *(p++) = 0x00;
1943 *(p++) = 0x00;
1944
1945 n += 2;
1946 }
1947#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1948
1949#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1950 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1951 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1952 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00001953 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001954 /*
1955 * Ephemeral DH parameters:
1956 *
1957 * struct {
1958 * opaque dh_p<1..2^16-1>;
1959 * opaque dh_g<1..2^16-1>;
1960 * opaque dh_Ys<1..2^16-1>;
1961 * } ServerDHParams;
1962 */
1963 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
1964 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
1965 {
1966 SSL_DEBUG_RET( 1, "mpi_copy", ret );
1967 return( ret );
1968 }
Paul Bakker48916f92012-09-16 19:57:18 +00001969
Paul Bakker41c83d32013-03-20 14:39:14 +01001970 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
1971 mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001972 p,
1973 &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001974 {
1975 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
1976 return( ret );
1977 }
1978
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001979 dig_signed = p;
1980 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001981
1982 p += len;
1983 n += len;
1984
Paul Bakker41c83d32013-03-20 14:39:14 +01001985 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
1986 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1987 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1988 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
1989 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001990#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1991 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001992
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001993#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1994 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1995 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1996 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001997 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001998 /*
1999 * Ephemeral ECDH parameters:
2000 *
2001 * struct {
2002 * ECParameters curve_params;
2003 * ECPoint public;
2004 * } ServerECDHParams;
2005 */
Paul Bakker41c83d32013-03-20 14:39:14 +01002006 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02002007 ssl->handshake->curves[0]->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002008 {
2009 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2010 return( ret );
2011 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002012
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02002013 SSL_DEBUG_MSG( 2, ( "ECDH curve size: %d",
2014 (int) ssl->handshake->ecdh_ctx.grp.nbits ) );
2015
Paul Bakker41c83d32013-03-20 14:39:14 +01002016 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002017 &len,
2018 p,
Paul Bakker41c83d32013-03-20 14:39:14 +01002019 1000, ssl->f_rng, ssl->p_rng ) ) != 0 )
2020 {
2021 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2022 return( ret );
2023 }
2024
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002025 dig_signed = p;
2026 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002027
2028 p += len;
2029 n += len;
2030
Paul Bakker41c83d32013-03-20 14:39:14 +01002031 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2032 }
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002033#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2034 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002035
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002036#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002037 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2038 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002039 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002040 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2041 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002042 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002043 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002044 unsigned int hashlen = 0;
2045 unsigned char hash[64];
2046 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002047
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002048 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002049 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2050 */
Paul Bakker577e0062013-08-28 11:57:20 +02002051#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002052 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2053 {
2054 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2055
2056 if( md_alg == POLARSSL_MD_NONE )
2057 {
2058 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2059 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2060 }
2061 }
Paul Bakker577e0062013-08-28 11:57:20 +02002062 else
2063#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002064#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2065 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker577e0062013-08-28 11:57:20 +02002066 if ( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002067 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2068 {
2069 md_alg = POLARSSL_MD_SHA1;
2070 }
2071 else
Paul Bakker577e0062013-08-28 11:57:20 +02002072#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002073 {
2074 md_alg = POLARSSL_MD_NONE;
2075 }
2076
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002077 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002078 * Compute the hash to be signed
2079 */
Paul Bakker577e0062013-08-28 11:57:20 +02002080#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2081 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002082 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002083 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002084 md5_context md5;
2085 sha1_context sha1;
2086
2087 /*
2088 * digitally-signed struct {
2089 * opaque md5_hash[16];
2090 * opaque sha_hash[20];
2091 * };
2092 *
2093 * md5_hash
2094 * MD5(ClientHello.random + ServerHello.random
2095 * + ServerParams);
2096 * sha_hash
2097 * SHA(ClientHello.random + ServerHello.random
2098 * + ServerParams);
2099 */
2100 md5_starts( &md5 );
2101 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002102 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002103 md5_finish( &md5, hash );
2104
2105 sha1_starts( &sha1 );
2106 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002107 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002108 sha1_finish( &sha1, hash + 16 );
2109
2110 hashlen = 36;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002111 }
2112 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002113#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2114 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002115#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2116 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002117 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002118 {
2119 md_context_t ctx;
2120
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002121 /* Info from md_alg will be used instead */
2122 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002123
2124 /*
2125 * digitally-signed struct {
2126 * opaque client_random[32];
2127 * opaque server_random[32];
2128 * ServerDHParams params;
2129 * };
2130 */
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002131 if( ( ret = md_init_ctx( &ctx, md_info_from_type(md_alg) ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002132 {
2133 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2134 return( ret );
2135 }
2136
2137 md_starts( &ctx );
2138 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002139 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002140 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02002141
2142 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
2143 {
2144 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
2145 return( ret );
2146 }
2147
Paul Bakker23f36802012-09-28 14:15:14 +00002148 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002149 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002150#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2151 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002152 {
2153 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002154 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02002155 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002156
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002157 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2158 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002159
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002160 /*
2161 * Make the signature
2162 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002163 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002164 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002165 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2166 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002167 }
Paul Bakker23f36802012-09-28 14:15:14 +00002168
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002169#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002170 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2171 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002172 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002173 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002174
2175 n += 2;
2176 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002177#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002178
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002179 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002180 p + 2 , &signature_len,
2181 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002182 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002183 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002184 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002185 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002186
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002187 *(p++) = (unsigned char)( signature_len >> 8 );
2188 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002189 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002190
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002191 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002192
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002193 p += signature_len;
2194 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002195 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002196#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002197 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2198 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002199
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002200 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002201 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2202 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2203
2204 ssl->state++;
2205
2206 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2207 {
2208 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2209 return( ret );
2210 }
2211
2212 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2213
2214 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002215}
2216
2217static int ssl_write_server_hello_done( ssl_context *ssl )
2218{
2219 int ret;
2220
2221 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2222
2223 ssl->out_msglen = 4;
2224 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2225 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2226
2227 ssl->state++;
2228
2229 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2230 {
2231 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2232 return( ret );
2233 }
2234
2235 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2236
2237 return( 0 );
2238}
2239
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002240#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2241 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2242static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2243 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002244{
2245 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002246 size_t n;
2247
2248 /*
2249 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2250 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002251 if( *p + 2 > end )
2252 {
2253 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2254 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2255 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002256
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002257 n = ( (*p)[0] << 8 ) | (*p)[1];
2258 *p += 2;
2259
2260 if( n < 1 || n > ssl->handshake->dhm_ctx.len || *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002261 {
2262 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2263 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2264 }
2265
2266 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002267 *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002268 {
2269 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2270 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2271 }
2272
2273 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2274
Paul Bakker70df2fb2013-04-17 17:19:09 +02002275 return( ret );
2276}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002277#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2278 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002279
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02002280#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2281 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002282static int ssl_parse_client_ecdh_public( ssl_context *ssl )
2283{
2284 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002285 size_t n;
2286
2287 /*
2288 * Receive client public key and calculate premaster
2289 */
2290 n = ssl->in_msg[3];
2291
2292 if( n < 1 || n > mpi_size( &ssl->handshake->ecdh_ctx.grp.P ) * 2 + 2 ||
2293 n + 4 != ssl->in_hslen )
2294 {
2295 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2296 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2297 }
2298
2299 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2300 ssl->in_msg + 4, n ) ) != 0 )
2301 {
2302 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2303 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2304 }
2305
2306 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2307
Paul Bakker70df2fb2013-04-17 17:19:09 +02002308 return( ret );
2309}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02002310#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2311 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002312
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002313#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002314static int ssl_parse_encrypted_pms_secret( ssl_context *ssl )
2315{
2316 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2317 size_t i, n = 0;
2318
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002319 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002320 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002321 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002322 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2323 }
2324
2325 /*
2326 * Decrypt the premaster using own private RSA key
2327 */
2328 i = 4;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002329 n = pk_get_len( ssl_own_key( ssl ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002330 ssl->handshake->pmslen = 48;
2331
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002332#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2333 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002334 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2335 {
2336 i += 2;
2337 if( ssl->in_msg[4] != ( ( n >> 8 ) & 0xFF ) ||
2338 ssl->in_msg[5] != ( ( n ) & 0xFF ) )
2339 {
2340 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2341 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2342 }
2343 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002344#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002345
2346 if( ssl->in_hslen != i + n )
2347 {
2348 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2349 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2350 }
2351
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002352 ret = pk_decrypt( ssl_own_key( ssl ),
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002353 ssl->in_msg + i, n,
2354 ssl->handshake->premaster, &ssl->handshake->pmslen,
2355 sizeof(ssl->handshake->premaster),
2356 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002357
2358 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Paul Bakker2fbefde2013-06-29 16:01:15 +02002359 ssl->handshake->premaster[0] != ssl->handshake->max_major_ver ||
2360 ssl->handshake->premaster[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002361 {
2362 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2363
2364 /*
2365 * Protection against Bleichenbacher's attack:
2366 * invalid PKCS#1 v1.5 padding must not cause
2367 * the connection to end immediately; instead,
2368 * send a bad_record_mac later in the handshake.
2369 */
2370 ssl->handshake->pmslen = 48;
2371
2372 ret = ssl->f_rng( ssl->p_rng, ssl->handshake->premaster,
2373 ssl->handshake->pmslen );
2374 if( ret != 0 )
2375 return( ret );
2376 }
2377
2378 return( ret );
2379}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002380#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002381
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002382#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2383 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2384static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2385 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002386{
Paul Bakker6db455e2013-09-18 17:29:31 +02002387 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002388 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002389
Paul Bakker6db455e2013-09-18 17:29:31 +02002390 if( ssl->f_psk == NULL &&
2391 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2392 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002393 {
2394 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2395 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2396 }
2397
2398 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002399 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002400 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002401 if( *p + 2 > end )
2402 {
2403 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2404 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2405 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002406
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002407 n = ( (*p)[0] << 8 ) | (*p)[1];
2408 *p += 2;
2409
2410 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002411 {
2412 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2413 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2414 }
2415
Paul Bakker6db455e2013-09-18 17:29:31 +02002416 if( ssl->f_psk != NULL )
2417 {
2418 if( ( ret != ssl->f_psk( ssl->p_psk, ssl, *p, n ) ) != 0 )
2419 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2420 }
2421
2422 if( ret == 0 )
2423 {
2424 if( n != ssl->psk_identity_len ||
2425 memcmp( ssl->psk_identity, *p, n ) != 0 )
2426 {
2427 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2428 }
2429 }
2430
2431 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002432 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002433 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002434 if( ( ret = ssl_send_alert_message( ssl,
2435 SSL_ALERT_LEVEL_FATAL,
2436 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2437 {
2438 return( ret );
2439 }
2440
2441 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002442 }
2443
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002444 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002445 ret = 0;
2446
Paul Bakkerfbb17802013-04-17 19:10:21 +02002447 return( ret );
2448}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002449#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
2450 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002451
Paul Bakker5121ce52009-01-03 21:22:43 +00002452static int ssl_parse_client_key_exchange( ssl_context *ssl )
2453{
Paul Bakker23986e52011-04-24 08:57:21 +00002454 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002455 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002456
Paul Bakker41c83d32013-03-20 14:39:14 +01002457 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002458
2459 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2460
2461 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2462 {
2463 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2464 return( ret );
2465 }
2466
2467 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2468 {
2469 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002470 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002471 }
2472
2473 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2474 {
2475 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002476 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002477 }
2478
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002479#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002480 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002481 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002482 unsigned char *p = ssl->in_msg + 4;
2483 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2484
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002485 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002486 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002487 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2488 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002489 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002490
2491 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
2492
Manuel Pégourié-Gonnard032c34e2013-09-07 13:06:27 +02002493 /* No blinding needed for DHE, but will be needed for fixed DH! */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002494 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2495 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002496 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002497 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002498 {
2499 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2500 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2501 }
2502
2503 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002504 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002505 else
2506#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002507#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2508 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2509 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2510 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002511 {
2512 if( ( ret = ssl_parse_client_ecdh_public( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002513 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002514 SSL_DEBUG_RET( 1, ( "ssl_parse_client_ecdh_public" ), ret );
2515 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002516 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002517
2518 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2519 &ssl->handshake->pmslen,
2520 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002521 POLARSSL_MPI_MAX_SIZE,
2522 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002523 {
2524 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2525 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2526 }
2527
2528 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002529 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002530 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002531#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2532 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002533#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2534 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002535 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002536 unsigned char *p = ssl->in_msg + 4;
2537 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2538
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002539 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002540 {
2541 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2542 return( ret );
2543 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002544
2545 // Set up the premaster secret
2546 //
2547 p = ssl->handshake->premaster;
2548 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2549 *(p++) = (unsigned char)( ssl->psk_len );
2550 p += ssl->psk_len;
2551
2552 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2553 *(p++) = (unsigned char)( ssl->psk_len );
2554 memcpy( p, ssl->psk, ssl->psk_len );
2555 p += ssl->psk_len;
2556
2557 ssl->handshake->pmslen = 4 + 2 * ssl->psk_len;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002558 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002559 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002560#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
2561#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2562 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2563 {
2564 size_t n;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002565 unsigned char *p = ssl->in_msg + 4;
2566 unsigned char *end = ssl->in_msg + ssl->in_msglen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002567
2568 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2569 {
2570 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2571 return( ret );
2572 }
2573 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2574 {
2575 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2576 return( ret );
2577 }
2578
2579 // Set up the premaster secret
2580 //
2581 p = ssl->handshake->premaster;
2582 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len >> 8 );
2583 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len );
2584
Paul Bakker577e0062013-08-28 11:57:20 +02002585 n = ssl->handshake->dhm_ctx.len;
2586
Manuel Pégourié-Gonnard032c34e2013-09-07 13:06:27 +02002587 /* No blinding needed since this is ephemeral DHM */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002588 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002589 p, &n, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002590 {
2591 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2592 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2593 }
2594
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002595 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2596
2597 p += ssl->handshake->dhm_ctx.len;
2598
2599 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2600 *(p++) = (unsigned char)( ssl->psk_len );
2601 memcpy( p, ssl->psk, ssl->psk_len );
2602 p += ssl->psk_len;
2603
2604 ssl->handshake->pmslen = 4 + ssl->handshake->dhm_ctx.len + ssl->psk_len;
2605 }
2606 else
2607#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
2608#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2609 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002610 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002611 if( ( ret = ssl_parse_encrypted_pms_secret( ssl ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002612 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002613 SSL_DEBUG_RET( 1, ( "ssl_parse_client_ecdh_public" ), ret );
2614 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002615 }
2616 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002617 else
2618#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2619 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002620 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002621 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2622 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002623
Paul Bakkerff60ee62010-03-16 21:09:09 +00002624 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2625 {
2626 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2627 return( ret );
2628 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002629
Paul Bakker5121ce52009-01-03 21:22:43 +00002630 ssl->state++;
2631
2632 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2633
2634 return( 0 );
2635}
2636
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002637#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2638 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002639 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2640 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002641static int ssl_parse_certificate_verify( ssl_context *ssl )
2642{
Paul Bakkered27a042013-04-18 22:46:23 +02002643 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002644 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002645
2646 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2647
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002648 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2649 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002650 {
2651 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2652 ssl->state++;
2653 return( 0 );
2654 }
2655
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002656 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002657 return( ret );
2658}
2659#else
2660static int ssl_parse_certificate_verify( ssl_context *ssl )
2661{
2662 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002663 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002664 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002665 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002666 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02002667#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002668 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02002669#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002670 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002671 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2672
2673 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2674
2675 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2676 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2677 {
2678 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2679 ssl->state++;
2680 return( 0 );
2681 }
2682
Paul Bakkered27a042013-04-18 22:46:23 +02002683 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002684 {
2685 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2686 ssl->state++;
2687 return( 0 );
2688 }
2689
Paul Bakker48916f92012-09-16 19:57:18 +00002690 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002691
2692 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2693 {
2694 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2695 return( ret );
2696 }
2697
2698 ssl->state++;
2699
2700 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2701 {
2702 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002703 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002704 }
2705
2706 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
2707 {
2708 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002709 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002710 }
2711
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002712 /*
2713 * 0 . 0 handshake type
2714 * 1 . 3 handshake length
2715 * 4 . 5 sig alg (TLS 1.2 only)
2716 * 4+n . 5+n signature length (n = sa_len)
2717 * 6+n . 6+n+m signature (m = sig_len)
2718 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002719
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002720#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2721 defined(POLARSSL_SSL_PROTO_TLS1_1)
2722 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002723 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002724 sa_len = 0;
2725
Paul Bakkerc70b9822013-04-07 22:00:46 +02002726 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002727 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002728
2729 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
2730 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
2731 POLARSSL_PK_ECDSA ) )
2732 {
2733 hash_start += 16;
2734 hashlen -= 16;
2735 md_alg = POLARSSL_MD_SHA1;
2736 }
Paul Bakker926af752012-11-23 13:38:07 +01002737 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002738 else
2739#endif
Paul Bakker577e0062013-08-28 11:57:20 +02002740#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2741 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002742 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002743 sa_len = 2;
2744
Paul Bakker5121ce52009-01-03 21:22:43 +00002745 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002746 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00002747 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002748 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002749 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002750 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2751 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01002752 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2753 }
2754
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002755 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01002756
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002757 /* Info from md_alg will be used instead */
2758 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002759
2760 /*
2761 * Signature
2762 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002763 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
2764 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002765 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002766 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2767 " for verify message" ) );
2768 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002769 }
2770
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002771 /*
2772 * Check the certificate's key type matches the signature alg
2773 */
2774 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
2775 {
2776 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
2777 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2778 }
Paul Bakker577e0062013-08-28 11:57:20 +02002779 }
2780 else
2781#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2782 {
2783 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002784 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002785 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002786
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002787 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01002788
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002789 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002790 {
2791 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002792 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002793 }
2794
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002795 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002796 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002797 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002798 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002799 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002800 return( ret );
2801 }
2802
2803 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
2804
Paul Bakkered27a042013-04-18 22:46:23 +02002805 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002806}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002807#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2808 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2809 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002810
Paul Bakkera503a632013-08-14 13:48:06 +02002811#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002812static int ssl_write_new_session_ticket( ssl_context *ssl )
2813{
2814 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002815 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002816 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002817
2818 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
2819
2820 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2821 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
2822
2823 /*
2824 * struct {
2825 * uint32 ticket_lifetime_hint;
2826 * opaque ticket<0..2^16-1>;
2827 * } NewSessionTicket;
2828 *
2829 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
2830 * 8 . 9 ticket_len (n)
2831 * 10 . 9+n ticket content
2832 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002833
2834 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
2835 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
2836 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
2837 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002838
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02002839 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
2840 {
2841 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
2842 tlen = 0;
2843 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002844
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002845 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
2846 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002847
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002848 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002849
2850 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2851 {
2852 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2853 return( ret );
2854 }
2855
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002856 /* No need to remember writing a NewSessionTicket any more */
2857 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002858
2859 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
2860
2861 return( 0 );
2862}
Paul Bakkera503a632013-08-14 13:48:06 +02002863#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002864
Paul Bakker5121ce52009-01-03 21:22:43 +00002865/*
Paul Bakker1961b702013-01-25 14:49:24 +01002866 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002867 */
Paul Bakker1961b702013-01-25 14:49:24 +01002868int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002869{
2870 int ret = 0;
2871
Paul Bakker1961b702013-01-25 14:49:24 +01002872 if( ssl->state == SSL_HANDSHAKE_OVER )
2873 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002874
Paul Bakker1961b702013-01-25 14:49:24 +01002875 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
2876
2877 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2878 return( ret );
2879
2880 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002881 {
Paul Bakker1961b702013-01-25 14:49:24 +01002882 case SSL_HELLO_REQUEST:
2883 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002884 break;
2885
Paul Bakker1961b702013-01-25 14:49:24 +01002886 /*
2887 * <== ClientHello
2888 */
2889 case SSL_CLIENT_HELLO:
2890 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002891 break;
Paul Bakker1961b702013-01-25 14:49:24 +01002892
2893 /*
2894 * ==> ServerHello
2895 * Certificate
2896 * ( ServerKeyExchange )
2897 * ( CertificateRequest )
2898 * ServerHelloDone
2899 */
2900 case SSL_SERVER_HELLO:
2901 ret = ssl_write_server_hello( ssl );
2902 break;
2903
2904 case SSL_SERVER_CERTIFICATE:
2905 ret = ssl_write_certificate( ssl );
2906 break;
2907
2908 case SSL_SERVER_KEY_EXCHANGE:
2909 ret = ssl_write_server_key_exchange( ssl );
2910 break;
2911
2912 case SSL_CERTIFICATE_REQUEST:
2913 ret = ssl_write_certificate_request( ssl );
2914 break;
2915
2916 case SSL_SERVER_HELLO_DONE:
2917 ret = ssl_write_server_hello_done( ssl );
2918 break;
2919
2920 /*
2921 * <== ( Certificate/Alert )
2922 * ClientKeyExchange
2923 * ( CertificateVerify )
2924 * ChangeCipherSpec
2925 * Finished
2926 */
2927 case SSL_CLIENT_CERTIFICATE:
2928 ret = ssl_parse_certificate( ssl );
2929 break;
2930
2931 case SSL_CLIENT_KEY_EXCHANGE:
2932 ret = ssl_parse_client_key_exchange( ssl );
2933 break;
2934
2935 case SSL_CERTIFICATE_VERIFY:
2936 ret = ssl_parse_certificate_verify( ssl );
2937 break;
2938
2939 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2940 ret = ssl_parse_change_cipher_spec( ssl );
2941 break;
2942
2943 case SSL_CLIENT_FINISHED:
2944 ret = ssl_parse_finished( ssl );
2945 break;
2946
2947 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002948 * ==> ( NewSessionTicket )
2949 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002950 * Finished
2951 */
2952 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002953#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002954 if( ssl->handshake->new_session_ticket != 0 )
2955 ret = ssl_write_new_session_ticket( ssl );
2956 else
Paul Bakkera503a632013-08-14 13:48:06 +02002957#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002958 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002959 break;
2960
2961 case SSL_SERVER_FINISHED:
2962 ret = ssl_write_finished( ssl );
2963 break;
2964
2965 case SSL_FLUSH_BUFFERS:
2966 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2967 ssl->state = SSL_HANDSHAKE_WRAPUP;
2968 break;
2969
2970 case SSL_HANDSHAKE_WRAPUP:
2971 ssl_handshake_wrapup( ssl );
2972 break;
2973
2974 default:
2975 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2976 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002977 }
2978
Paul Bakker5121ce52009-01-03 21:22:43 +00002979 return( ret );
2980}
Paul Bakker5121ce52009-01-03 21:22:43 +00002981#endif