blob: 0c491b417473cd54aedb0847be71880d276270be [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 ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001753 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1754 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001755 {
1756 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1757 ssl->state++;
1758 return( 0 );
1759 }
1760
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001761 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001762 return( ret );
1763}
1764#else
1765static int ssl_write_certificate_request( ssl_context *ssl )
1766{
1767 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1768 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001769 size_t dn_size, total_dn_size; /* excluding length bytes */
1770 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00001771 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001772 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001773
1774 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1775
1776 ssl->state++;
1777
Paul Bakkerfbb17802013-04-17 19:10:21 +02001778 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001779 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001780 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001781 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001782 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001783 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001784 return( 0 );
1785 }
1786
1787 /*
1788 * 0 . 0 handshake type
1789 * 1 . 3 handshake length
1790 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01001791 * 5 .. m-1 cert types
1792 * m .. m+1 sig alg length (TLS 1.2 only)
1793 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001794 * n .. n+1 length of all DNs
1795 * n+2 .. n+3 length of DN 1
1796 * n+4 .. ... Distinguished Name #1
1797 * ... .. ... length of DN 2, etc.
1798 */
1799 buf = ssl->out_msg;
1800 p = buf + 4;
1801
1802 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001803 * Supported certificate types
1804 *
1805 * ClientCertificateType certificate_types<1..2^8-1>;
1806 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00001807 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001808 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01001809
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001810#if defined(POLARSSL_RSA_C)
1811 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
1812#endif
1813#if defined(POLARSSL_ECDSA_C)
1814 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
1815#endif
1816
1817 p[0] = ct_len++;
1818 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01001819
Paul Bakker577e0062013-08-28 11:57:20 +02001820 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001821#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01001822 /*
1823 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01001824 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001825 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
1826 *
1827 * struct {
1828 * HashAlgorithm hash;
1829 * SignatureAlgorithm signature;
1830 * } SignatureAndHashAlgorithm;
1831 *
1832 * enum { (255) } HashAlgorithm;
1833 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01001834 */
Paul Bakker21dca692013-01-03 11:41:08 +01001835 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01001836 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001837 /*
1838 * Only use current running hash algorithm that is already required
1839 * for requested ciphersuite.
1840 */
Paul Bakker926af752012-11-23 13:38:07 +01001841 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
1842
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001843 if( ssl->transform_negotiate->ciphersuite_info->mac ==
1844 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01001845 {
1846 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
1847 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02001848
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001849 /*
1850 * Supported signature algorithms
1851 */
1852#if defined(POLARSSL_RSA_C)
1853 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1854 p[2 + sa_len++] = SSL_SIG_RSA;
1855#endif
1856#if defined(POLARSSL_ECDSA_C)
1857 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1858 p[2 + sa_len++] = SSL_SIG_ECDSA;
1859#endif
Paul Bakker926af752012-11-23 13:38:07 +01001860
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001861 p[0] = (unsigned char)( sa_len >> 8 );
1862 p[1] = (unsigned char)( sa_len );
1863 sa_len += 2;
1864 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01001865 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001866#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001867
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001868 /*
1869 * DistinguishedName certificate_authorities<0..2^16-1>;
1870 * opaque DistinguishedName<1..2^16-1>;
1871 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001872 p += 2;
1873 crt = ssl->ca_chain;
1874
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001875 total_dn_size = 0;
Paul Bakker29087132010-03-21 21:03:34 +00001876 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001877 {
1878 if( p - buf > 4096 )
1879 break;
1880
Paul Bakker926af752012-11-23 13:38:07 +01001881 dn_size = crt->subject_raw.len;
1882 *p++ = (unsigned char)( dn_size >> 8 );
1883 *p++ = (unsigned char)( dn_size );
1884 memcpy( p, crt->subject_raw.p, dn_size );
1885 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001886
Paul Bakker926af752012-11-23 13:38:07 +01001887 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
1888
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001889 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01001890 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00001891 }
1892
Paul Bakker926af752012-11-23 13:38:07 +01001893 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001894 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1895 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001896 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
1897 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001898
1899 ret = ssl_write_record( ssl );
1900
1901 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
1902
1903 return( ret );
1904}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001905#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
1906 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
1907 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001908
Paul Bakker41c83d32013-03-20 14:39:14 +01001909static int ssl_write_server_key_exchange( ssl_context *ssl )
1910{
Paul Bakker23986e52011-04-24 08:57:21 +00001911 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001912 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001913 const ssl_ciphersuite_t *ciphersuite_info =
1914 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001915
1916#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1917 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
1918 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001919 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02001920 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001921 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001922 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001923 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001924 ((void) dig_signed);
1925 ((void) dig_signed_len);
1926#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001927
Paul Bakker5121ce52009-01-03 21:22:43 +00001928 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
1929
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001930 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
1931 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1932 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001933 {
1934 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
1935 ssl->state++;
1936 return( 0 );
1937 }
1938
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001939#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
1940 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1941 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1942 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001943 {
1944 /* TODO: Support identity hints */
1945 *(p++) = 0x00;
1946 *(p++) = 0x00;
1947
1948 n += 2;
1949 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001950#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
1951 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001952
1953#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1954 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1955 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1956 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00001957 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001958 /*
1959 * Ephemeral DH parameters:
1960 *
1961 * struct {
1962 * opaque dh_p<1..2^16-1>;
1963 * opaque dh_g<1..2^16-1>;
1964 * opaque dh_Ys<1..2^16-1>;
1965 * } ServerDHParams;
1966 */
1967 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
1968 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
1969 {
1970 SSL_DEBUG_RET( 1, "mpi_copy", ret );
1971 return( ret );
1972 }
Paul Bakker48916f92012-09-16 19:57:18 +00001973
Paul Bakker41c83d32013-03-20 14:39:14 +01001974 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
1975 mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001976 p,
1977 &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001978 {
1979 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
1980 return( ret );
1981 }
1982
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001983 dig_signed = p;
1984 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001985
1986 p += len;
1987 n += len;
1988
Paul Bakker41c83d32013-03-20 14:39:14 +01001989 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
1990 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1991 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1992 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
1993 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001994#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1995 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001996
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001997#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001998 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1999 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2000
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002001 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002002 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2003 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002004 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002005 /*
2006 * Ephemeral ECDH parameters:
2007 *
2008 * struct {
2009 * ECParameters curve_params;
2010 * ECPoint public;
2011 * } ServerECDHParams;
2012 */
Paul Bakker41c83d32013-03-20 14:39:14 +01002013 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02002014 ssl->handshake->curves[0]->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002015 {
2016 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2017 return( ret );
2018 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002019
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02002020 SSL_DEBUG_MSG( 2, ( "ECDH curve size: %d",
2021 (int) ssl->handshake->ecdh_ctx.grp.nbits ) );
2022
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002023 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2024 p, SSL_MAX_CONTENT_LEN - n,
2025 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002026 {
2027 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2028 return( ret );
2029 }
2030
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002031 dig_signed = p;
2032 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002033
2034 p += len;
2035 n += len;
2036
Paul Bakker41c83d32013-03-20 14:39:14 +01002037 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2038 }
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002039#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002040 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2041 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002042
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002043#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002044 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2045 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002046 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002047 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2048 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002049 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002050 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002051 unsigned int hashlen = 0;
2052 unsigned char hash[64];
2053 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002054
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002055 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002056 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2057 */
Paul Bakker577e0062013-08-28 11:57:20 +02002058#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002059 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2060 {
2061 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2062
2063 if( md_alg == POLARSSL_MD_NONE )
2064 {
2065 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2066 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2067 }
2068 }
Paul Bakker577e0062013-08-28 11:57:20 +02002069 else
2070#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002071#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2072 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker577e0062013-08-28 11:57:20 +02002073 if ( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002074 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2075 {
2076 md_alg = POLARSSL_MD_SHA1;
2077 }
2078 else
Paul Bakker577e0062013-08-28 11:57:20 +02002079#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002080 {
2081 md_alg = POLARSSL_MD_NONE;
2082 }
2083
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002084 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002085 * Compute the hash to be signed
2086 */
Paul Bakker577e0062013-08-28 11:57:20 +02002087#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2088 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002089 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002090 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002091 md5_context md5;
2092 sha1_context sha1;
2093
2094 /*
2095 * digitally-signed struct {
2096 * opaque md5_hash[16];
2097 * opaque sha_hash[20];
2098 * };
2099 *
2100 * md5_hash
2101 * MD5(ClientHello.random + ServerHello.random
2102 * + ServerParams);
2103 * sha_hash
2104 * SHA(ClientHello.random + ServerHello.random
2105 * + ServerParams);
2106 */
2107 md5_starts( &md5 );
2108 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002109 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002110 md5_finish( &md5, hash );
2111
2112 sha1_starts( &sha1 );
2113 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002114 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002115 sha1_finish( &sha1, hash + 16 );
2116
2117 hashlen = 36;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002118 }
2119 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002120#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2121 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002122#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2123 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002124 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002125 {
2126 md_context_t ctx;
2127
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002128 /* Info from md_alg will be used instead */
2129 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002130
2131 /*
2132 * digitally-signed struct {
2133 * opaque client_random[32];
2134 * opaque server_random[32];
2135 * ServerDHParams params;
2136 * };
2137 */
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002138 if( ( ret = md_init_ctx( &ctx, md_info_from_type(md_alg) ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002139 {
2140 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2141 return( ret );
2142 }
2143
2144 md_starts( &ctx );
2145 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002146 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002147 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02002148
2149 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
2150 {
2151 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
2152 return( ret );
2153 }
2154
Paul Bakker23f36802012-09-28 14:15:14 +00002155 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002156 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002157#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2158 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002159 {
2160 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002161 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02002162 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002163
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002164 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2165 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002166
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002167 /*
2168 * Make the signature
2169 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002170 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002171 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002172 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2173 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002174 }
Paul Bakker23f36802012-09-28 14:15:14 +00002175
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002176#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002177 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2178 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002179 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002180 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002181
2182 n += 2;
2183 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002184#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002185
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002186 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002187 p + 2 , &signature_len,
2188 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002189 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002190 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002191 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002192 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002193
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002194 *(p++) = (unsigned char)( signature_len >> 8 );
2195 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002196 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002197
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002198 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002199
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002200 p += signature_len;
2201 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002202 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002203#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002204 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2205 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002206
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002207 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002208 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2209 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2210
2211 ssl->state++;
2212
2213 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2214 {
2215 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2216 return( ret );
2217 }
2218
2219 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2220
2221 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002222}
2223
2224static int ssl_write_server_hello_done( ssl_context *ssl )
2225{
2226 int ret;
2227
2228 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2229
2230 ssl->out_msglen = 4;
2231 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2232 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2233
2234 ssl->state++;
2235
2236 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2237 {
2238 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2239 return( ret );
2240 }
2241
2242 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2243
2244 return( 0 );
2245}
2246
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002247#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2248 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2249static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2250 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002251{
2252 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002253 size_t n;
2254
2255 /*
2256 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2257 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002258 if( *p + 2 > end )
2259 {
2260 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2261 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2262 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002263
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002264 n = ( (*p)[0] << 8 ) | (*p)[1];
2265 *p += 2;
2266
2267 if( n < 1 || n > ssl->handshake->dhm_ctx.len || *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002268 {
2269 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2270 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2271 }
2272
2273 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002274 *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002275 {
2276 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2277 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2278 }
2279
2280 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2281
Paul Bakker70df2fb2013-04-17 17:19:09 +02002282 return( ret );
2283}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002284#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2285 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002286
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002287#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2288 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002289static int ssl_parse_encrypted_pms( ssl_context *ssl,
2290 const unsigned char *p,
2291 const unsigned char *end,
2292 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002293{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002294 int ret;
2295 size_t len = pk_get_len( ssl_own_key( ssl ) );
2296 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002297
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002298 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002299 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002300 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002301 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2302 }
2303
2304 /*
2305 * Decrypt the premaster using own private RSA key
2306 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002307#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2308 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002309 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2310 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002311 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2312 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002313 {
2314 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2315 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2316 }
2317 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002318#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002319
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002320 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002321 {
2322 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2323 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2324 }
2325
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002326 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2327 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002328 sizeof(ssl->handshake->premaster),
2329 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002330
2331 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002332 pms[0] != ssl->handshake->max_major_ver ||
2333 pms[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002334 {
2335 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2336
2337 /*
2338 * Protection against Bleichenbacher's attack:
2339 * invalid PKCS#1 v1.5 padding must not cause
2340 * the connection to end immediately; instead,
2341 * send a bad_record_mac later in the handshake.
2342 */
2343 ssl->handshake->pmslen = 48;
2344
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002345 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002346 if( ret != 0 )
2347 return( ret );
2348 }
2349
2350 return( ret );
2351}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002352#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2353 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002354
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002355#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002356static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2357 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002358{
Paul Bakker6db455e2013-09-18 17:29:31 +02002359 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002360 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002361
Paul Bakker6db455e2013-09-18 17:29:31 +02002362 if( ssl->f_psk == NULL &&
2363 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2364 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002365 {
2366 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2367 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2368 }
2369
2370 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002371 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002372 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002373 if( *p + 2 > end )
2374 {
2375 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2376 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2377 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002378
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002379 n = ( (*p)[0] << 8 ) | (*p)[1];
2380 *p += 2;
2381
2382 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002383 {
2384 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2385 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2386 }
2387
Paul Bakker6db455e2013-09-18 17:29:31 +02002388 if( ssl->f_psk != NULL )
2389 {
2390 if( ( ret != ssl->f_psk( ssl->p_psk, ssl, *p, n ) ) != 0 )
2391 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2392 }
2393
2394 if( ret == 0 )
2395 {
2396 if( n != ssl->psk_identity_len ||
2397 memcmp( ssl->psk_identity, *p, n ) != 0 )
2398 {
2399 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2400 }
2401 }
2402
2403 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002404 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002405 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002406 if( ( ret = ssl_send_alert_message( ssl,
2407 SSL_ALERT_LEVEL_FATAL,
2408 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2409 {
2410 return( ret );
2411 }
2412
2413 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002414 }
2415
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002416 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002417 ret = 0;
2418
Paul Bakkerfbb17802013-04-17 19:10:21 +02002419 return( ret );
2420}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002421#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002422
Paul Bakker5121ce52009-01-03 21:22:43 +00002423static int ssl_parse_client_key_exchange( ssl_context *ssl )
2424{
Paul Bakker23986e52011-04-24 08:57:21 +00002425 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002426 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002427
Paul Bakker41c83d32013-03-20 14:39:14 +01002428 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002429
2430 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2431
2432 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2433 {
2434 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2435 return( ret );
2436 }
2437
2438 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2439 {
2440 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002441 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002442 }
2443
2444 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2445 {
2446 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002447 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002448 }
2449
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002450#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002451 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002452 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002453 unsigned char *p = ssl->in_msg + 4;
2454 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2455
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002456 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002457 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002458 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2459 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002460 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002461
2462 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
2463
2464 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2465 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002466 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002467 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002468 {
2469 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2470 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2471 }
2472
2473 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002474 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002475 else
2476#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002477#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2478 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2479 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2480 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002481 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002482 size_t n = ssl->in_msg[3];
2483
2484 if( n < 1 || n > mpi_size( &ssl->handshake->ecdh_ctx.grp.P ) * 2 + 2 ||
2485 n + 4 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002486 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002487 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2488 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002489 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002490
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002491 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2492 ssl->in_msg + 4, n ) ) != 0 )
2493 {
2494 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2495 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2496 }
2497
2498 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2499
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002500 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2501 &ssl->handshake->pmslen,
2502 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002503 POLARSSL_MPI_MAX_SIZE,
2504 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002505 {
2506 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2507 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2508 }
2509
2510 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002511 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002512 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002513#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2514 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002515#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2516 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002517 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002518 unsigned char *p = ssl->in_msg + 4;
2519 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2520
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002521 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002522 {
2523 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2524 return( ret );
2525 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002526
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002527 if( ( ret = ssl_psk_derive_premaster( ssl,
2528 ciphersuite_info->key_exchange ) ) != 0 )
2529 {
2530 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2531 return( ret );
2532 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002533 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002534 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002535#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002536#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2537 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2538 {
2539 unsigned char *p = ssl->in_msg + 4;
2540 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2541
2542 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2543 {
2544 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2545 return( ret );
2546 }
2547
2548 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
2549 {
2550 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
2551 return( ret );
2552 }
2553
2554 if( ( ret = ssl_psk_derive_premaster( ssl,
2555 ciphersuite_info->key_exchange ) ) != 0 )
2556 {
2557 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2558 return( ret );
2559 }
2560 }
2561 else
2562#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002563#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2564 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2565 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002566 unsigned char *p = ssl->in_msg + 4;
2567 unsigned char *end = ssl->in_msg + ssl->in_msglen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002568
2569 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2570 {
2571 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2572 return( ret );
2573 }
2574 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2575 {
2576 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2577 return( ret );
2578 }
2579
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002580 if( ( ret = ssl_psk_derive_premaster( ssl,
2581 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002582 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002583 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2584 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002585 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002586 }
2587 else
2588#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002589#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2590 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2591 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002592 unsigned char *p = ssl->in_msg + 4;
2593 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2594
2595 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2596 {
2597 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2598 return( ret );
2599 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002600
2601 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2602 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002603 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002604 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2605 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002606 }
2607
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002608 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2609
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002610 if( ( ret = ssl_psk_derive_premaster( ssl,
2611 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002612 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002613 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002614 return( ret );
2615 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002616 }
2617 else
2618#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002619#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2620 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002621 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002622 if( ( ret = ssl_parse_encrypted_pms( ssl,
2623 ssl->in_msg + 4,
2624 ssl->in_msg + ssl->in_msglen,
2625 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002626 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002627 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_ecrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002628 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002629 }
2630 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002631 else
2632#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2633 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002634 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002635 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2636 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002637
Paul Bakkerff60ee62010-03-16 21:09:09 +00002638 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2639 {
2640 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2641 return( ret );
2642 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002643
Paul Bakker5121ce52009-01-03 21:22:43 +00002644 ssl->state++;
2645
2646 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2647
2648 return( 0 );
2649}
2650
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002651#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2652 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002653 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2654 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002655static int ssl_parse_certificate_verify( ssl_context *ssl )
2656{
Paul Bakkered27a042013-04-18 22:46:23 +02002657 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002658 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002659
2660 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2661
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002662 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002663 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002664 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002665 {
2666 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2667 ssl->state++;
2668 return( 0 );
2669 }
2670
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002671 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002672 return( ret );
2673}
2674#else
2675static int ssl_parse_certificate_verify( ssl_context *ssl )
2676{
2677 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002678 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002679 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002680 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002681 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02002682#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002683 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02002684#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002685 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002686 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2687
2688 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2689
2690 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002691 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002692 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2693 {
2694 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2695 ssl->state++;
2696 return( 0 );
2697 }
2698
Paul Bakkered27a042013-04-18 22:46:23 +02002699 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002700 {
2701 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2702 ssl->state++;
2703 return( 0 );
2704 }
2705
Paul Bakker48916f92012-09-16 19:57:18 +00002706 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002707
2708 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2709 {
2710 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2711 return( ret );
2712 }
2713
2714 ssl->state++;
2715
2716 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2717 {
2718 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002719 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002720 }
2721
2722 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
2723 {
2724 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002725 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002726 }
2727
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002728 /*
2729 * 0 . 0 handshake type
2730 * 1 . 3 handshake length
2731 * 4 . 5 sig alg (TLS 1.2 only)
2732 * 4+n . 5+n signature length (n = sa_len)
2733 * 6+n . 6+n+m signature (m = sig_len)
2734 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002735
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002736#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2737 defined(POLARSSL_SSL_PROTO_TLS1_1)
2738 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002739 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002740 sa_len = 0;
2741
Paul Bakkerc70b9822013-04-07 22:00:46 +02002742 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002743 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002744
2745 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
2746 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
2747 POLARSSL_PK_ECDSA ) )
2748 {
2749 hash_start += 16;
2750 hashlen -= 16;
2751 md_alg = POLARSSL_MD_SHA1;
2752 }
Paul Bakker926af752012-11-23 13:38:07 +01002753 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002754 else
2755#endif
Paul Bakker577e0062013-08-28 11:57:20 +02002756#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2757 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002758 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002759 sa_len = 2;
2760
Paul Bakker5121ce52009-01-03 21:22:43 +00002761 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002762 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00002763 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002764 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002765 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002766 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2767 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01002768 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2769 }
2770
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002771 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01002772
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002773 /* Info from md_alg will be used instead */
2774 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002775
2776 /*
2777 * Signature
2778 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002779 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
2780 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002781 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002782 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2783 " for verify message" ) );
2784 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002785 }
2786
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002787 /*
2788 * Check the certificate's key type matches the signature alg
2789 */
2790 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
2791 {
2792 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
2793 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2794 }
Paul Bakker577e0062013-08-28 11:57:20 +02002795 }
2796 else
2797#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2798 {
2799 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002800 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002801 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002802
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002803 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01002804
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002805 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002806 {
2807 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002808 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002809 }
2810
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002811 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002812 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002813 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002814 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002815 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002816 return( ret );
2817 }
2818
2819 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
2820
Paul Bakkered27a042013-04-18 22:46:23 +02002821 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002822}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002823#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2824 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2825 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002826
Paul Bakkera503a632013-08-14 13:48:06 +02002827#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002828static int ssl_write_new_session_ticket( ssl_context *ssl )
2829{
2830 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002831 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002832 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002833
2834 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
2835
2836 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2837 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
2838
2839 /*
2840 * struct {
2841 * uint32 ticket_lifetime_hint;
2842 * opaque ticket<0..2^16-1>;
2843 * } NewSessionTicket;
2844 *
2845 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
2846 * 8 . 9 ticket_len (n)
2847 * 10 . 9+n ticket content
2848 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002849
2850 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
2851 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
2852 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
2853 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002854
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02002855 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
2856 {
2857 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
2858 tlen = 0;
2859 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002860
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002861 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
2862 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002863
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002864 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002865
2866 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2867 {
2868 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2869 return( ret );
2870 }
2871
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002872 /* No need to remember writing a NewSessionTicket any more */
2873 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002874
2875 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
2876
2877 return( 0 );
2878}
Paul Bakkera503a632013-08-14 13:48:06 +02002879#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002880
Paul Bakker5121ce52009-01-03 21:22:43 +00002881/*
Paul Bakker1961b702013-01-25 14:49:24 +01002882 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002883 */
Paul Bakker1961b702013-01-25 14:49:24 +01002884int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002885{
2886 int ret = 0;
2887
Paul Bakker1961b702013-01-25 14:49:24 +01002888 if( ssl->state == SSL_HANDSHAKE_OVER )
2889 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002890
Paul Bakker1961b702013-01-25 14:49:24 +01002891 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
2892
2893 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2894 return( ret );
2895
2896 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002897 {
Paul Bakker1961b702013-01-25 14:49:24 +01002898 case SSL_HELLO_REQUEST:
2899 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002900 break;
2901
Paul Bakker1961b702013-01-25 14:49:24 +01002902 /*
2903 * <== ClientHello
2904 */
2905 case SSL_CLIENT_HELLO:
2906 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002907 break;
Paul Bakker1961b702013-01-25 14:49:24 +01002908
2909 /*
2910 * ==> ServerHello
2911 * Certificate
2912 * ( ServerKeyExchange )
2913 * ( CertificateRequest )
2914 * ServerHelloDone
2915 */
2916 case SSL_SERVER_HELLO:
2917 ret = ssl_write_server_hello( ssl );
2918 break;
2919
2920 case SSL_SERVER_CERTIFICATE:
2921 ret = ssl_write_certificate( ssl );
2922 break;
2923
2924 case SSL_SERVER_KEY_EXCHANGE:
2925 ret = ssl_write_server_key_exchange( ssl );
2926 break;
2927
2928 case SSL_CERTIFICATE_REQUEST:
2929 ret = ssl_write_certificate_request( ssl );
2930 break;
2931
2932 case SSL_SERVER_HELLO_DONE:
2933 ret = ssl_write_server_hello_done( ssl );
2934 break;
2935
2936 /*
2937 * <== ( Certificate/Alert )
2938 * ClientKeyExchange
2939 * ( CertificateVerify )
2940 * ChangeCipherSpec
2941 * Finished
2942 */
2943 case SSL_CLIENT_CERTIFICATE:
2944 ret = ssl_parse_certificate( ssl );
2945 break;
2946
2947 case SSL_CLIENT_KEY_EXCHANGE:
2948 ret = ssl_parse_client_key_exchange( ssl );
2949 break;
2950
2951 case SSL_CERTIFICATE_VERIFY:
2952 ret = ssl_parse_certificate_verify( ssl );
2953 break;
2954
2955 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2956 ret = ssl_parse_change_cipher_spec( ssl );
2957 break;
2958
2959 case SSL_CLIENT_FINISHED:
2960 ret = ssl_parse_finished( ssl );
2961 break;
2962
2963 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002964 * ==> ( NewSessionTicket )
2965 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002966 * Finished
2967 */
2968 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002969#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002970 if( ssl->handshake->new_session_ticket != 0 )
2971 ret = ssl_write_new_session_ticket( ssl );
2972 else
Paul Bakkera503a632013-08-14 13:48:06 +02002973#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002974 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002975 break;
2976
2977 case SSL_SERVER_FINISHED:
2978 ret = ssl_write_finished( ssl );
2979 break;
2980
2981 case SSL_FLUSH_BUFFERS:
2982 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2983 ssl->state = SSL_HANDSHAKE_WRAPUP;
2984 break;
2985
2986 case SSL_HANDSHAKE_WRAPUP:
2987 ssl_handshake_wrapup( ssl );
2988 break;
2989
2990 default:
2991 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2992 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002993 }
2994
Paul Bakker5121ce52009-01-03 21:22:43 +00002995 return( ret );
2996}
Paul Bakker5121ce52009-01-03 21:22:43 +00002997#endif