blob: 66ba58a1ab118d4c6b990aa3ec1845f88d614552 [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 if( session->peer_cert == NULL )
78 cert_len = 0;
79 else
80 cert_len = session->peer_cert->raw.len;
81
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020082 if( left < 3 + cert_len )
83 return( -1 );
84
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020085 *p++ = (unsigned char)( cert_len >> 16 & 0xFF );
86 *p++ = (unsigned char)( cert_len >> 8 & 0xFF );
87 *p++ = (unsigned char)( cert_len & 0xFF );
88
89 if( session->peer_cert != NULL )
90 memcpy( p, session->peer_cert->raw.p, cert_len );
91
92 p += cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020094
95 *olen = p - buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020096
97 return( 0 );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020098}
99
100/*
101 * Unserialise session, see ssl_save_session()
102 */
103static int ssl_load_session( ssl_session *session,
104 const unsigned char *buf, size_t len )
105{
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200106 const unsigned char *p = buf;
107 const unsigned char * const end = buf + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200108#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200109 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200110#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200111
112 if( p + sizeof( ssl_session ) > end )
113 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
114
115 memcpy( session, p, sizeof( ssl_session ) );
116 p += sizeof( ssl_session );
117
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200118#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200119 if( p + 3 > end )
120 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
121
122 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
123 p += 3;
124
125 if( cert_len == 0 )
126 {
127 session->peer_cert = NULL;
128 }
129 else
130 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200131 int ret;
132
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200133 if( p + cert_len > end )
134 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
135
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200136 session->peer_cert = polarssl_malloc( sizeof( x509_crt ) );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200137
138 if( session->peer_cert == NULL )
139 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
140
Paul Bakkerb6b09562013-09-18 14:17:41 +0200141 x509_crt_init( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200142
Paul Bakkerddf26b42013-09-18 13:46:23 +0200143 if( ( ret = x509_crt_parse( session->peer_cert, p, cert_len ) ) != 0 )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200144 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200145 x509_crt_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200146 polarssl_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200147 session->peer_cert = NULL;
148 return( ret );
149 }
150
151 p += cert_len;
152 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200153#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200154
155 if( p != end )
156 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
157
158 return( 0 );
159}
160
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200161/*
162 * Create session ticket, secured as recommended in RFC 5077 section 4:
163 *
164 * struct {
165 * opaque key_name[16];
166 * opaque iv[16];
167 * opaque encrypted_state<0..2^16-1>;
168 * opaque mac[32];
169 * } ticket;
170 *
171 * (the internal state structure differs, however).
172 */
173static int ssl_write_ticket( ssl_context *ssl, size_t *tlen )
174{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200175 int ret;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200176 unsigned char * const start = ssl->out_msg + 10;
177 unsigned char *p = start;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200178 unsigned char *state;
179 unsigned char iv[16];
180 size_t clear_len, enc_len, pad_len, i;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200181
Manuel Pégourié-Gonnard0a201712013-08-23 16:25:16 +0200182 *tlen = 0;
183
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200184 if( ssl->ticket_keys == NULL )
185 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
186
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200187 /* Write key name */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200188 memcpy( p, ssl->ticket_keys->key_name, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200189 p += 16;
190
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200191 /* Generate and write IV (with a copy for aes_crypt) */
192 if( ( ret = ssl->f_rng( ssl->p_rng, p, 16 ) ) != 0 )
193 return( ret );
194 memcpy( iv, p, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200195 p += 16;
196
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200197 /*
198 * Dump session state
199 *
200 * After the session state itself, we still need room for 16 bytes of
201 * padding and 32 bytes of MAC, so there's only so much room left
202 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200203 state = p + 2;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200204 if( ssl_save_session( ssl->session_negotiate, state,
205 SSL_MAX_CONTENT_LEN - (state - ssl->out_ctr) - 48,
206 &clear_len ) != 0 )
207 {
208 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
209 }
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200210 SSL_DEBUG_BUF( 3, "session ticket cleartext", state, clear_len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200211
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200212 /* Apply PKCS padding */
213 pad_len = 16 - clear_len % 16;
214 enc_len = clear_len + pad_len;
215 for( i = clear_len; i < enc_len; i++ )
216 state[i] = (unsigned char) pad_len;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200217
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200218 /* Encrypt */
219 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->enc, AES_ENCRYPT,
220 enc_len, iv, state, state ) ) != 0 )
221 {
222 return( ret );
223 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200224
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200225 /* Write length */
226 *p++ = (unsigned char)( ( enc_len >> 8 ) & 0xFF );
227 *p++ = (unsigned char)( ( enc_len ) & 0xFF );
228 p = state + enc_len;
229
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200230 /* Compute and write MAC( key_name + iv + enc_state_len + enc_state ) */
231 sha256_hmac( ssl->ticket_keys->mac_key, 16, start, p - start, p, 0 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200232 p += 32;
233
234 *tlen = p - start;
235
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200236 SSL_DEBUG_BUF( 3, "session ticket structure", start, *tlen );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200237
238 return( 0 );
239}
240
241/*
242 * Load session ticket (see ssl_write_ticket for structure)
243 */
244static int ssl_parse_ticket( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200245 unsigned char *buf,
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200246 size_t len )
247{
248 int ret;
249 ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200250 unsigned char *key_name = buf;
251 unsigned char *iv = buf + 16;
252 unsigned char *enc_len_p = iv + 16;
253 unsigned char *ticket = enc_len_p + 2;
254 unsigned char *mac;
Manuel Pégourié-Gonnard34ced2d2013-09-20 11:37:39 +0200255 unsigned char computed_mac[32];
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200256 size_t enc_len, clear_len, i;
257 unsigned char pad_len;
258
259 SSL_DEBUG_BUF( 3, "session ticket structure", buf, len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200260
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200261 if( len < 34 || ssl->ticket_keys == NULL )
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200262 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
263
264 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
265 mac = ticket + enc_len;
266
267 if( len != enc_len + 66 )
268 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
269
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200270 /* Check name */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200271 if( memcmp( key_name, ssl->ticket_keys->key_name, 16 ) != 0 )
272 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200273
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200274 /* Check mac */
275 sha256_hmac( ssl->ticket_keys->mac_key, 16, buf, len - 32,
276 computed_mac, 0 );
277 ret = 0;
278 for( i = 0; i < 32; i++ )
279 if( mac[i] != computed_mac[i] )
280 ret = POLARSSL_ERR_SSL_INVALID_MAC;
281 if( ret != 0 )
282 return( ret );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200283
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200284 /* Decrypt */
285 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->dec, AES_DECRYPT,
286 enc_len, iv, ticket, ticket ) ) != 0 )
287 {
288 return( ret );
289 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200290
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200291 /* Check PKCS padding */
292 pad_len = ticket[enc_len - 1];
293
294 ret = 0;
295 for( i = 2; i < pad_len; i++ )
296 if( ticket[enc_len - i] != pad_len )
297 ret = POLARSSL_ERR_SSL_BAD_INPUT_DATA;
298 if( ret != 0 )
299 return( ret );
300
301 clear_len = enc_len - pad_len;
302
303 SSL_DEBUG_BUF( 3, "session ticket cleartext", ticket, clear_len );
304
305 /* Actually load session */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200306 if( ( ret = ssl_load_session( &session, ticket, clear_len ) ) != 0 )
307 {
308 SSL_DEBUG_MSG( 1, ( "failed to parse ticket content" ) );
309 memset( &session, 0, sizeof( ssl_session ) );
310 return( ret );
311 }
312
Paul Bakker606b4ba2013-08-14 16:52:14 +0200313#if defined(POLARSSL_HAVE_TIME)
314 /* Check if still valid */
315 if( (int) ( time( NULL) - session.start ) > ssl->ticket_lifetime )
316 {
317 SSL_DEBUG_MSG( 1, ( "session ticket expired" ) );
318 memset( &session, 0, sizeof( ssl_session ) );
319 return( POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED );
320 }
321#endif
322
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200323 /*
324 * Keep the session ID sent by the client, since we MUST send it back to
325 * inform him we're accepting the ticket (RFC 5077 section 3.4)
326 */
327 session.length = ssl->session_negotiate->length;
328 memcpy( &session.id, ssl->session_negotiate->id, session.length );
329
330 ssl_session_free( ssl->session_negotiate );
331 memcpy( ssl->session_negotiate, &session, sizeof( ssl_session ) );
332 memset( &session, 0, sizeof( ssl_session ) );
333
334 return( 0 );
335}
Paul Bakkera503a632013-08-14 13:48:06 +0200336#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200337
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +0100338/*
339 * Write HelloRequest to request renegotiation
340 */
341int ssl_write_hello_request( ssl_context *ssl )
342{
343 int ret;
344
345 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
346
347 ssl->out_msglen = 4;
348 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
349 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
350
351 if( ( ret = ssl_write_record( ssl ) ) != 0 )
352 {
353 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
354 return( ret );
355 }
356
357 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
358
359 return( 0 );
360}
361
Paul Bakker0be444a2013-08-27 21:55:01 +0200362#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200363/*
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200364 * Wrapper around f_sni, allowing use of ssl_set_own_cert() but
365 * making it act on ssl->hanshake->sni_key_cert instead.
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200366 */
367static int ssl_sni_wrapper( ssl_context *ssl,
368 const unsigned char* name, size_t len )
369{
370 int ret;
371 ssl_key_cert *key_cert_ori = ssl->key_cert;
372
373 ssl->key_cert = NULL;
374 ret = ssl->f_sni( ssl->p_sni, ssl, name, len );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200375 ssl->handshake->sni_key_cert = ssl->key_cert;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200376
377 ssl->key_cert = key_cert_ori;
378
379 return( ret );
380}
381
Paul Bakker5701cdc2012-09-27 21:49:42 +0000382static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000383 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000384 size_t len )
385{
386 int ret;
387 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000388 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000389
390 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
391 if( servername_list_size + 2 != len )
392 {
393 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
394 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
395 }
396
397 p = buf + 2;
398 while( servername_list_size > 0 )
399 {
400 hostname_len = ( ( p[1] << 8 ) | p[2] );
401 if( hostname_len + 3 > servername_list_size )
402 {
403 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
404 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
405 }
406
407 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
408 {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200409 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000410 if( ret != 0 )
411 {
412 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
413 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
414 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
415 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000416 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000417 }
418
419 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000420 p += hostname_len + 3;
421 }
422
423 if( servername_list_size != 0 )
424 {
425 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
426 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000427 }
428
429 return( 0 );
430}
Paul Bakker0be444a2013-08-27 21:55:01 +0200431#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000432
Paul Bakker48916f92012-09-16 19:57:18 +0000433static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000434 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000435 size_t len )
436{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000437 int ret;
438
Paul Bakker48916f92012-09-16 19:57:18 +0000439 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
440 {
441 if( len != 1 || buf[0] != 0x0 )
442 {
443 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000444
445 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
446 return( ret );
447
Paul Bakker48916f92012-09-16 19:57:18 +0000448 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
449 }
450
451 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
452 }
453 else
454 {
455 if( len != 1 + ssl->verify_data_len ||
456 buf[0] != ssl->verify_data_len ||
457 memcmp( buf + 1, ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
458 {
459 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000460
461 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
462 return( ret );
463
Paul Bakker48916f92012-09-16 19:57:18 +0000464 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
465 }
466 }
467
468 return( 0 );
469}
470
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200471#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +0000472static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
473 const unsigned char *buf,
474 size_t len )
475{
476 size_t sig_alg_list_size;
477 const unsigned char *p;
478
479 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
480 if( sig_alg_list_size + 2 != len ||
481 sig_alg_list_size %2 != 0 )
482 {
483 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
484 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
485 }
486
487 p = buf + 2;
488 while( sig_alg_list_size > 0 )
489 {
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200490 /*
491 * For now, just ignore signature algorithm and rely on offered
492 * ciphersuites only. To be fixed later.
493 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200494#if defined(POLARSSL_SHA512_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000495 if( p[0] == SSL_HASH_SHA512 )
496 {
497 ssl->handshake->sig_alg = SSL_HASH_SHA512;
498 break;
499 }
500 if( p[0] == SSL_HASH_SHA384 )
501 {
502 ssl->handshake->sig_alg = SSL_HASH_SHA384;
503 break;
504 }
505#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200506#if defined(POLARSSL_SHA256_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000507 if( p[0] == SSL_HASH_SHA256 )
508 {
509 ssl->handshake->sig_alg = SSL_HASH_SHA256;
510 break;
511 }
512 if( p[0] == SSL_HASH_SHA224 )
513 {
514 ssl->handshake->sig_alg = SSL_HASH_SHA224;
515 break;
516 }
517#endif
518 if( p[0] == SSL_HASH_SHA1 )
519 {
520 ssl->handshake->sig_alg = SSL_HASH_SHA1;
521 break;
522 }
523 if( p[0] == SSL_HASH_MD5 )
524 {
525 ssl->handshake->sig_alg = SSL_HASH_MD5;
526 break;
527 }
528
529 sig_alg_list_size -= 2;
530 p += 2;
531 }
532
533 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
534 ssl->handshake->sig_alg ) );
535
536 return( 0 );
537}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200538#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker23f36802012-09-28 14:15:14 +0000539
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200540#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200541static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
542 const unsigned char *buf,
543 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100544{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200545 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100546 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200547 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100548
549 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
550 if( list_size + 2 != len ||
551 list_size % 2 != 0 )
552 {
553 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
554 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
555 }
556
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200557 /* Don't allow our peer to make use allocated too much memory,
558 * and leave room for a final 0 */
559 our_size = list_size / 2 + 1;
560 if( our_size > POLARSSL_ECP_DP_MAX )
561 our_size = POLARSSL_ECP_DP_MAX;
562
563 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
564 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
565
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200566 /* explicit void pointer cast for buggy MS compiler */
567 memset( (void *) curves, 0, our_size * sizeof( *curves ) );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200568 ssl->handshake->curves = curves;
569
Paul Bakker41c83d32013-03-20 14:39:14 +0100570 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200571 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100572 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200573 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200574
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200575 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100576 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200577 *curves++ = curve_info;
578 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100579 }
580
581 list_size -= 2;
582 p += 2;
583 }
584
585 return( 0 );
586}
587
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200588static int ssl_parse_supported_point_formats( ssl_context *ssl,
589 const unsigned char *buf,
590 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100591{
592 size_t list_size;
593 const unsigned char *p;
594
595 list_size = buf[0];
596 if( list_size + 1 != len )
597 {
598 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
599 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
600 }
601
602 p = buf + 2;
603 while( list_size > 0 )
604 {
605 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
606 p[0] == POLARSSL_ECP_PF_COMPRESSED )
607 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200608 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200609 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100610 return( 0 );
611 }
612
613 list_size--;
614 p++;
615 }
616
617 return( 0 );
618}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200619#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100620
Paul Bakker05decb22013-08-15 13:33:48 +0200621#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200622static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
623 const unsigned char *buf,
624 size_t len )
625{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200626 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200627 {
628 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
629 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
630 }
631
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200632 ssl->session_negotiate->mfl_code = buf[0];
633
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200634 return( 0 );
635}
Paul Bakker05decb22013-08-15 13:33:48 +0200636#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200637
Paul Bakker1f2bc622013-08-15 13:45:55 +0200638#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200639static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
640 const unsigned char *buf,
641 size_t len )
642{
643 if( len != 0 )
644 {
645 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
646 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
647 }
648
649 ((void) buf);
650
651 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
652
653 return( 0 );
654}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200655#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200656
Paul Bakkera503a632013-08-14 13:48:06 +0200657#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200658static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200659 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200660 size_t len )
661{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200662 int ret;
663
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200664 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
665 return( 0 );
666
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200667 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200668 ssl->handshake->new_session_ticket = 1;
669
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200670 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
671
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200672 if( len == 0 )
673 return( 0 );
674
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200675 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
676 {
677 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
678 return( 0 );
679 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200680
681 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200682 * Failures are ok: just ignore the ticket and proceed.
683 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200684 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
685 {
686 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200687 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200688 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200689
690 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
691
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200692 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200693
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200694 /* Don't send a new ticket after all, this one is OK */
695 ssl->handshake->new_session_ticket = 0;
696
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200697 return( 0 );
698}
Paul Bakkera503a632013-08-14 13:48:06 +0200699#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200700
Paul Bakker78a8c712013-03-06 17:01:52 +0100701#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
702static int ssl_parse_client_hello_v2( ssl_context *ssl )
703{
704 int ret;
705 unsigned int i, j;
706 size_t n;
707 unsigned int ciph_len, sess_len, chal_len;
708 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200709 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200710 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100711
712 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
713
714 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
715 {
716 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
717
718 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
719 return( ret );
720
721 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
722 }
723
724 buf = ssl->in_hdr;
725
726 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
727
728 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
729 buf[2] ) );
730 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
731 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
732 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
733 buf[3], buf[4] ) );
734
735 /*
736 * SSLv2 Client Hello
737 *
738 * Record layer:
739 * 0 . 1 message length
740 *
741 * SSL layer:
742 * 2 . 2 message type
743 * 3 . 4 protocol version
744 */
745 if( buf[2] != SSL_HS_CLIENT_HELLO ||
746 buf[3] != SSL_MAJOR_VERSION_3 )
747 {
748 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
749 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
750 }
751
752 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
753
754 if( n < 17 || n > 512 )
755 {
756 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
757 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
758 }
759
760 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200761 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
762 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100763
764 if( ssl->minor_ver < ssl->min_minor_ver )
765 {
766 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
767 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
768 ssl->min_major_ver, ssl->min_minor_ver ) );
769
770 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
771 SSL_ALERT_MSG_PROTOCOL_VERSION );
772 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
773 }
774
Paul Bakker2fbefde2013-06-29 16:01:15 +0200775 ssl->handshake->max_major_ver = buf[3];
776 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100777
778 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
779 {
780 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
781 return( ret );
782 }
783
784 ssl->handshake->update_checksum( ssl, buf + 2, n );
785
786 buf = ssl->in_msg;
787 n = ssl->in_left - 5;
788
789 /*
790 * 0 . 1 ciphersuitelist length
791 * 2 . 3 session id length
792 * 4 . 5 challenge length
793 * 6 . .. ciphersuitelist
794 * .. . .. session id
795 * .. . .. challenge
796 */
797 SSL_DEBUG_BUF( 4, "record contents", buf, n );
798
799 ciph_len = ( buf[0] << 8 ) | buf[1];
800 sess_len = ( buf[2] << 8 ) | buf[3];
801 chal_len = ( buf[4] << 8 ) | buf[5];
802
803 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
804 ciph_len, sess_len, chal_len ) );
805
806 /*
807 * Make sure each parameter length is valid
808 */
809 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
810 {
811 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
812 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
813 }
814
815 if( sess_len > 32 )
816 {
817 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
818 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
819 }
820
821 if( chal_len < 8 || chal_len > 32 )
822 {
823 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
824 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
825 }
826
827 if( n != 6 + ciph_len + sess_len + chal_len )
828 {
829 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
830 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
831 }
832
833 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
834 buf + 6, ciph_len );
835 SSL_DEBUG_BUF( 3, "client hello, session id",
836 buf + 6 + ciph_len, sess_len );
837 SSL_DEBUG_BUF( 3, "client hello, challenge",
838 buf + 6 + ciph_len + sess_len, chal_len );
839
840 p = buf + 6 + ciph_len;
841 ssl->session_negotiate->length = sess_len;
842 memset( ssl->session_negotiate->id, 0, sizeof( ssl->session_negotiate->id ) );
843 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
844
845 p += sess_len;
846 memset( ssl->handshake->randbytes, 0, 64 );
847 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
848
849 /*
850 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
851 */
852 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
853 {
854 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
855 {
856 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
857 if( ssl->renegotiation == SSL_RENEGOTIATION )
858 {
859 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
860
861 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
862 return( ret );
863
864 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
865 }
866 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
867 break;
868 }
869 }
870
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200871 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
872 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +0100873 {
874 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
875 {
Paul Bakker41c83d32013-03-20 14:39:14 +0100876 // Only allow non-ECC ciphersuites as we do not have extensions
877 //
Paul Bakker59c28a22013-06-29 15:33:42 +0200878 if( p[0] == 0 && p[1] == 0 &&
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200879 ( ( ciphersuites[i] >> 8 ) & 0xFF ) == 0 &&
880 p[2] == ( ciphersuites[i] & 0xFF ) )
Paul Bakker59c28a22013-06-29 15:33:42 +0200881 {
882 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
883
884 if( ciphersuite_info == NULL )
885 {
886 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %02x not found",
887 ciphersuites[i] ) );
888 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
889 }
890
Paul Bakker2fbefde2013-06-29 16:01:15 +0200891 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
892 ciphersuite_info->max_minor_ver < ssl->minor_ver )
893 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +0200894
Paul Bakker78a8c712013-03-06 17:01:52 +0100895 goto have_ciphersuite_v2;
Paul Bakker59c28a22013-06-29 15:33:42 +0200896 }
Paul Bakker78a8c712013-03-06 17:01:52 +0100897 }
898 }
899
900 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
901
902 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
903
904have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200905 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +0200906 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +0100907 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +0100908
909 /*
910 * SSLv2 Client Hello relevant renegotiation security checks
911 */
912 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
913 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
914 {
915 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
916
917 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
918 return( ret );
919
920 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
921 }
922
923 ssl->in_left = 0;
924 ssl->state++;
925
926 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
927
928 return( 0 );
929}
930#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
931
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200932#if defined(POLARSSL_X509_CRT_PARSE_C)
933#if defined(POLARSSL_ECDSA_C)
934static int ssl_key_matches_curves( pk_context *pk,
935 const ecp_curve_info **curves )
936{
937 const ecp_curve_info **crv = curves;
938 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
939
940 while( *crv != NULL )
941 {
942 if( (*crv)->grp_id == grp_id )
943 return( 1 );
944 crv++;
945 }
946
947 return( 0 );
948}
949#endif /* POLARSSL_ECDSA_C */
950
951/*
952 * Try picking a certificate for this ciphersuite,
953 * return 0 on success and -1 on failure.
954 */
955static int ssl_pick_cert( ssl_context *ssl,
956 const ssl_ciphersuite_t * ciphersuite_info )
957{
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200958 ssl_key_cert *cur, *list;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200959 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
960
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200961#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
962 if( ssl->handshake->sni_key_cert != NULL )
963 list = ssl->handshake->sni_key_cert;
964 else
965#endif
966 list = ssl->handshake->key_cert;
967
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200968 if( pk_alg == POLARSSL_PK_NONE )
969 return( 0 );
970
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200971 for( cur = list; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200972 {
973 if( ! pk_can_do( cur->key, pk_alg ) )
974 continue;
975
976#if defined(POLARSSL_ECDSA_C)
977 if( pk_alg == POLARSSL_PK_ECDSA )
978 {
979 if( ssl_key_matches_curves( cur->key, ssl->handshake->curves ) )
980 break;
981 }
982 else
983#endif
984 break;
985 }
986
987 if( cur == NULL )
988 return( -1 );
989
990 ssl->handshake->key_cert = cur;
991 return( 0 );
992}
993#endif /* POLARSSL_X509_CRT_PARSE_C */
994
Paul Bakker5121ce52009-01-03 21:22:43 +0000995static int ssl_parse_client_hello( ssl_context *ssl )
996{
Paul Bakker23986e52011-04-24 08:57:21 +0000997 int ret;
998 unsigned int i, j;
999 size_t n;
1000 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001001 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001002 unsigned int ext_len = 0;
1003 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001004 int renegotiation_info_seen = 0;
1005 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001006 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001007 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001008
1009 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1010
Paul Bakker48916f92012-09-16 19:57:18 +00001011 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1012 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001013 {
1014 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1015 return( ret );
1016 }
1017
1018 buf = ssl->in_hdr;
1019
Paul Bakker78a8c712013-03-06 17:01:52 +01001020#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1021 if( ( buf[0] & 0x80 ) != 0 )
1022 return ssl_parse_client_hello_v2( ssl );
1023#endif
1024
Paul Bakkerec636f32012-09-09 19:17:02 +00001025 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1026
1027 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1028 buf[0] ) );
1029 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1030 ( buf[3] << 8 ) | buf[4] ) );
1031 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
1032 buf[1], buf[2] ) );
1033
1034 /*
1035 * SSLv3 Client Hello
1036 *
1037 * Record layer:
1038 * 0 . 0 message type
1039 * 1 . 2 protocol version
1040 * 3 . 4 message length
1041 */
1042 if( buf[0] != SSL_MSG_HANDSHAKE ||
1043 buf[1] != SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001044 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001045 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1046 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1047 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001048
Paul Bakkerec636f32012-09-09 19:17:02 +00001049 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +00001050
Manuel Pégourié-Gonnard72882b22013-08-02 13:36:00 +02001051 if( n < 45 || n > 2048 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001052 {
1053 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1054 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1055 }
1056
Paul Bakker48916f92012-09-16 19:57:18 +00001057 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1058 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001059 {
1060 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1061 return( ret );
1062 }
1063
1064 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +00001065 if( !ssl->renegotiation )
1066 n = ssl->in_left - 5;
1067 else
1068 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +00001069
Paul Bakker48916f92012-09-16 19:57:18 +00001070 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +00001071
1072 /*
1073 * SSL layer:
1074 * 0 . 0 handshake type
1075 * 1 . 3 handshake length
1076 * 4 . 5 protocol version
1077 * 6 . 9 UNIX time()
1078 * 10 . 37 random bytes
1079 * 38 . 38 session id length
1080 * 39 . 38+x session id
1081 * 39+x . 40+x ciphersuitelist length
1082 * 41+x . .. ciphersuitelist
1083 * .. . .. compression alg.
1084 * .. . .. extensions
1085 */
1086 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1087
1088 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1089 buf[0] ) );
1090 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1091 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1092 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1093 buf[4], buf[5] ) );
1094
1095 /*
1096 * Check the handshake type and protocol version
1097 */
1098 if( buf[0] != SSL_HS_CLIENT_HELLO ||
1099 buf[4] != SSL_MAJOR_VERSION_3 )
1100 {
1101 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1102 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1103 }
1104
1105 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +02001106 ssl->minor_ver = ( buf[5] <= ssl->max_minor_ver )
1107 ? buf[5] : ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001108
Paul Bakker1d29fb52012-09-28 13:28:45 +00001109 if( ssl->minor_ver < ssl->min_minor_ver )
1110 {
1111 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
1112 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001113 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001114
1115 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1116 SSL_ALERT_MSG_PROTOCOL_VERSION );
1117
1118 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1119 }
1120
Paul Bakker2fbefde2013-06-29 16:01:15 +02001121 ssl->handshake->max_major_ver = buf[4];
1122 ssl->handshake->max_minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001123
Paul Bakker48916f92012-09-16 19:57:18 +00001124 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001125
1126 /*
1127 * Check the handshake message length
1128 */
1129 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1130 {
1131 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1132 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1133 }
1134
1135 /*
1136 * Check the session length
1137 */
1138 sess_len = buf[38];
1139
1140 if( sess_len > 32 )
1141 {
1142 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1143 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1144 }
1145
Paul Bakker48916f92012-09-16 19:57:18 +00001146 ssl->session_negotiate->length = sess_len;
1147 memset( ssl->session_negotiate->id, 0,
1148 sizeof( ssl->session_negotiate->id ) );
1149 memcpy( ssl->session_negotiate->id, buf + 39,
1150 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001151
1152 /*
1153 * Check the ciphersuitelist length
1154 */
1155 ciph_len = ( buf[39 + sess_len] << 8 )
1156 | ( buf[40 + sess_len] );
1157
1158 if( ciph_len < 2 || ciph_len > 256 || ( ciph_len % 2 ) != 0 )
1159 {
1160 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1161 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1162 }
1163
1164 /*
1165 * Check the compression algorithms length
1166 */
1167 comp_len = buf[41 + sess_len + ciph_len];
1168
1169 if( comp_len < 1 || comp_len > 16 )
1170 {
1171 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1172 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1173 }
1174
Paul Bakker48916f92012-09-16 19:57:18 +00001175 /*
1176 * Check the extension length
1177 */
1178 if( n > 42 + sess_len + ciph_len + comp_len )
1179 {
1180 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1181 | ( buf[43 + sess_len + ciph_len + comp_len] );
1182
1183 if( ( ext_len > 0 && ext_len < 4 ) ||
1184 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1185 {
1186 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1187 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1188 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1189 }
1190 }
1191
1192 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001193#if defined(POLARSSL_ZLIB_SUPPORT)
1194 for( i = 0; i < comp_len; ++i )
1195 {
Paul Bakker48916f92012-09-16 19:57:18 +00001196 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001197 {
Paul Bakker48916f92012-09-16 19:57:18 +00001198 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001199 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001200 }
1201 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001202#endif
1203
Paul Bakkerec636f32012-09-09 19:17:02 +00001204 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1205 buf + 6, 32 );
1206 SSL_DEBUG_BUF( 3, "client hello, session id",
1207 buf + 38, sess_len );
1208 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1209 buf + 41 + sess_len, ciph_len );
1210 SSL_DEBUG_BUF( 3, "client hello, compression",
1211 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001212
Paul Bakkerec636f32012-09-09 19:17:02 +00001213 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001214 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1215 */
1216 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1217 {
1218 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1219 {
1220 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1221 if( ssl->renegotiation == SSL_RENEGOTIATION )
1222 {
1223 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001224
1225 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1226 return( ret );
1227
Paul Bakker48916f92012-09-16 19:57:18 +00001228 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1229 }
1230 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1231 break;
1232 }
1233 }
1234
Paul Bakker48916f92012-09-16 19:57:18 +00001235 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001236
1237 while( ext_len )
1238 {
1239 unsigned int ext_id = ( ( ext[0] << 8 )
1240 | ( ext[1] ) );
1241 unsigned int ext_size = ( ( ext[2] << 8 )
1242 | ( ext[3] ) );
1243
1244 if( ext_size + 4 > ext_len )
1245 {
1246 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1247 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1248 }
1249 switch( ext_id )
1250 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001251#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001252 case TLS_EXT_SERVERNAME:
1253 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1254 if( ssl->f_sni == NULL )
1255 break;
1256
1257 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1258 if( ret != 0 )
1259 return( ret );
1260 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001261#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001262
Paul Bakker48916f92012-09-16 19:57:18 +00001263 case TLS_EXT_RENEGOTIATION_INFO:
1264 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1265 renegotiation_info_seen = 1;
1266
Paul Bakker23f36802012-09-28 14:15:14 +00001267 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1268 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001269 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001270 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001271
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001272#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001273 case TLS_EXT_SIG_ALG:
1274 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1275 if( ssl->renegotiation == SSL_RENEGOTIATION )
1276 break;
1277
1278 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1279 if( ret != 0 )
1280 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001281 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001282#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001283
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001284#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001285 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1286 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1287
1288 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1289 if( ret != 0 )
1290 return( ret );
1291 break;
1292
1293 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1294 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001295 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001296
1297 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1298 if( ret != 0 )
1299 return( ret );
1300 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001301#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001302
Paul Bakker05decb22013-08-15 13:33:48 +02001303#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001304 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1305 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1306
1307 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1308 if( ret != 0 )
1309 return( ret );
1310 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001311#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001312
Paul Bakker1f2bc622013-08-15 13:45:55 +02001313#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001314 case TLS_EXT_TRUNCATED_HMAC:
1315 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1316
1317 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1318 if( ret != 0 )
1319 return( ret );
1320 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001321#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001322
Paul Bakkera503a632013-08-14 13:48:06 +02001323#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001324 case TLS_EXT_SESSION_TICKET:
1325 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1326
1327 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1328 if( ret != 0 )
1329 return( ret );
1330 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001331#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001332
Paul Bakker48916f92012-09-16 19:57:18 +00001333 default:
1334 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1335 ext_id ) );
1336 }
1337
1338 ext_len -= 4 + ext_size;
1339 ext += 4 + ext_size;
1340
1341 if( ext_len > 0 && ext_len < 4 )
1342 {
1343 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1344 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1345 }
1346 }
1347
1348 /*
1349 * Renegotiation security checks
1350 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001351 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1352 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1353 {
1354 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1355 handshake_failure = 1;
1356 }
1357 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1358 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1359 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001360 {
1361 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001362 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001363 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001364 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1365 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1366 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001367 {
1368 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001369 handshake_failure = 1;
1370 }
1371 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1372 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1373 renegotiation_info_seen == 1 )
1374 {
1375 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1376 handshake_failure = 1;
1377 }
1378
1379 if( handshake_failure == 1 )
1380 {
1381 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1382 return( ret );
1383
Paul Bakker48916f92012-09-16 19:57:18 +00001384 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1385 }
Paul Bakker380da532012-04-18 16:10:25 +00001386
Paul Bakker41c83d32013-03-20 14:39:14 +01001387 /*
1388 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001389 * (At the end because we need information from the EC-based extensions
1390 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001391 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001392 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
1393 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001394 {
1395 for( j = 0, p = buf + 41 + sess_len; j < ciph_len;
1396 j += 2, p += 2 )
1397 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001398 if( p[0] == ( ( ciphersuites[i] >> 8 ) & 0xFF ) &&
1399 p[1] == ( ( ciphersuites[i] ) & 0xFF ) )
Paul Bakker41c83d32013-03-20 14:39:14 +01001400 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001401 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
Paul Bakker41c83d32013-03-20 14:39:14 +01001402
1403 if( ciphersuite_info == NULL )
1404 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001405 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found",
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001406 ciphersuites[i] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001407 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1408 }
1409
Paul Bakker2fbefde2013-06-29 16:01:15 +02001410 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
1411 ciphersuite_info->max_minor_ver < ssl->minor_ver )
1412 continue;
1413
Paul Bakker5fd49172013-08-19 13:29:26 +02001414#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001415 if( ssl_ciphersuite_uses_ec( ciphersuite_info ) &&
Paul Bakkercaa3af42013-09-26 13:32:43 +02001416 ( ssl->handshake->curves == NULL ||
1417 ssl->handshake->curves[0] == NULL ) )
Paul Bakker41c83d32013-03-20 14:39:14 +01001418 continue;
Paul Bakker5fd49172013-08-19 13:29:26 +02001419#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001420
Manuel Pégourié-Gonnard21ef42f2013-10-27 14:47:25 +01001421#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1422 /* If the ciphersuite requires a pre-shared key and we don't
1423 * have one, skip it now rather than failing later */
1424 if( ssl_ciphersuite_uses_psk( ciphersuite_info ) &&
1425 ssl->f_psk == NULL &&
1426 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
1427 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
1428 continue;
1429#endif
1430
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001431#if defined(POLARSSL_X509_CRT_PARSE_C)
1432 /*
1433 * Final check: if ciphersuite requires us to have a
1434 * certificate/key of a particular type:
1435 * - select the appropriate certificate if we have one, or
1436 * - try the next ciphersuite if we don't
1437 * This must be done last since we modify the key_cert list.
1438 */
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02001439 if( ssl_pick_cert( ssl, ciphersuite_info ) != 0 )
1440 continue;
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02001441#endif
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001442
Paul Bakker41c83d32013-03-20 14:39:14 +01001443 goto have_ciphersuite;
1444 }
1445 }
1446 }
1447
1448 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1449
1450 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1451 return( ret );
1452
1453 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1454
1455have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001456 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001457 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1458 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1459
Paul Bakker5121ce52009-01-03 21:22:43 +00001460 ssl->in_left = 0;
1461 ssl->state++;
1462
1463 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1464
1465 return( 0 );
1466}
1467
Paul Bakker1f2bc622013-08-15 13:45:55 +02001468#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001469static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1470 unsigned char *buf,
1471 size_t *olen )
1472{
1473 unsigned char *p = buf;
1474
1475 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1476 {
1477 *olen = 0;
1478 return;
1479 }
1480
1481 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1482
1483 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1484 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1485
1486 *p++ = 0x00;
1487 *p++ = 0x00;
1488
1489 *olen = 4;
1490}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001491#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001492
Paul Bakkera503a632013-08-14 13:48:06 +02001493#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001494static void ssl_write_session_ticket_ext( ssl_context *ssl,
1495 unsigned char *buf,
1496 size_t *olen )
1497{
1498 unsigned char *p = buf;
1499
1500 if( ssl->handshake->new_session_ticket == 0 )
1501 {
1502 *olen = 0;
1503 return;
1504 }
1505
1506 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1507
1508 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1509 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1510
1511 *p++ = 0x00;
1512 *p++ = 0x00;
1513
1514 *olen = 4;
1515}
Paul Bakkera503a632013-08-14 13:48:06 +02001516#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001517
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001518static void ssl_write_renegotiation_ext( ssl_context *ssl,
1519 unsigned char *buf,
1520 size_t *olen )
1521{
1522 unsigned char *p = buf;
1523
1524 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1525 {
1526 *olen = 0;
1527 return;
1528 }
1529
1530 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1531
1532 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1533 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1534
1535 *p++ = 0x00;
1536 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1537 *p++ = ssl->verify_data_len * 2 & 0xFF;
1538
1539 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1540 p += ssl->verify_data_len;
1541 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1542 p += ssl->verify_data_len;
1543
1544 *olen = 5 + ssl->verify_data_len * 2;
1545}
1546
Paul Bakker05decb22013-08-15 13:33:48 +02001547#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001548static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1549 unsigned char *buf,
1550 size_t *olen )
1551{
1552 unsigned char *p = buf;
1553
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001554 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1555 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001556 *olen = 0;
1557 return;
1558 }
1559
1560 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1561
1562 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1563 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1564
1565 *p++ = 0x00;
1566 *p++ = 1;
1567
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001568 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001569
1570 *olen = 5;
1571}
Paul Bakker05decb22013-08-15 13:33:48 +02001572#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001573
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001574#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001575static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1576 unsigned char *buf,
1577 size_t *olen )
1578{
1579 unsigned char *p = buf;
1580 ((void) ssl);
1581
Paul Bakker677377f2013-10-28 12:54:26 +01001582 if( ( ssl->handshake->cli_exts &
1583 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
1584 {
1585 *olen = 0;
1586 return;
1587 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001588
1589 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1590
1591 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1592 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1593
1594 *p++ = 0x00;
1595 *p++ = 2;
1596
1597 *p++ = 1;
1598 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1599
1600 *olen = 6;
1601}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001602#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001603
Paul Bakker5121ce52009-01-03 21:22:43 +00001604static int ssl_write_server_hello( ssl_context *ssl )
1605{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001606#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001607 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001608#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001609 int ret;
1610 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001611 unsigned char *buf, *p;
1612
1613 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1614
1615 /*
1616 * 0 . 0 handshake type
1617 * 1 . 3 handshake length
1618 * 4 . 5 protocol version
1619 * 6 . 9 UNIX time()
1620 * 10 . 37 random bytes
1621 */
1622 buf = ssl->out_msg;
1623 p = buf + 4;
1624
1625 *p++ = (unsigned char) ssl->major_ver;
1626 *p++ = (unsigned char) ssl->minor_ver;
1627
1628 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1629 buf[4], buf[5] ) );
1630
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001631#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001632 t = time( NULL );
1633 *p++ = (unsigned char)( t >> 24 );
1634 *p++ = (unsigned char)( t >> 16 );
1635 *p++ = (unsigned char)( t >> 8 );
1636 *p++ = (unsigned char)( t );
1637
1638 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001639#else
1640 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1641 return( ret );
1642
1643 p += 4;
1644#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001645
Paul Bakkera3d195c2011-11-27 21:07:34 +00001646 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1647 return( ret );
1648
1649 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001650
Paul Bakker48916f92012-09-16 19:57:18 +00001651 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001652
1653 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1654
1655 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001656 * Resume is 0 by default, see ssl_handshake_init().
1657 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1658 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001659 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001660 if( ssl->handshake->resume == 0 &&
1661 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001662 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001663 ssl->f_get_cache != NULL &&
1664 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1665 {
1666 ssl->handshake->resume = 1;
1667 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001668
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001669 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001670 {
1671 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001672 * New session, create a new session id,
1673 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001674 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001675 ssl->state++;
1676
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001677#if defined(POLARSSL_HAVE_TIME)
1678 ssl->session_negotiate->start = time( NULL );
1679#endif
1680
Paul Bakkera503a632013-08-14 13:48:06 +02001681#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001682 if( ssl->handshake->new_session_ticket != 0 )
1683 {
1684 ssl->session_negotiate->length = n = 0;
1685 memset( ssl->session_negotiate->id, 0, 32 );
1686 }
1687 else
1688#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001689 {
1690 ssl->session_negotiate->length = n = 32;
1691 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001692 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001693 return( ret );
1694 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001695 }
1696 else
1697 {
1698 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001699 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001700 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001701 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001702 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001703
1704 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1705 {
1706 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1707 return( ret );
1708 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001709 }
1710
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001711 /*
1712 * 38 . 38 session id length
1713 * 39 . 38+n session id
1714 * 39+n . 40+n chosen ciphersuite
1715 * 41+n . 41+n chosen compression alg.
1716 * 42+n . 43+n extensions length
1717 * 44+n . 43+n+m extensions
1718 */
1719 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001720 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1721 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001722
1723 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1724 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1725 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001726 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001727
Paul Bakker48916f92012-09-16 19:57:18 +00001728 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1729 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1730 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001731
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001732 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
1733 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001734 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001735 ssl->session_negotiate->compression ) );
1736
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001737 /*
1738 * First write extensions, then the total length
1739 */
1740 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1741 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001742
Paul Bakker05decb22013-08-15 13:33:48 +02001743#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001744 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1745 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001746#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001747
Paul Bakker1f2bc622013-08-15 13:45:55 +02001748#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001749 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1750 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001751#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001752
Paul Bakkera503a632013-08-14 13:48:06 +02001753#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001754 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1755 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001756#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001757
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001758#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001759 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1760 ext_len += olen;
1761#endif
1762
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001763 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001764
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001765 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1766 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1767 p += ext_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001768
1769 ssl->out_msglen = p - buf;
1770 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1771 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1772
1773 ret = ssl_write_record( ssl );
1774
1775 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1776
1777 return( ret );
1778}
1779
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001780#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1781 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001782 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1783 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001784static int ssl_write_certificate_request( ssl_context *ssl )
1785{
Paul Bakkered27a042013-04-18 22:46:23 +02001786 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1787 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001788
1789 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1790
1791 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001792 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1793 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001794 {
1795 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1796 ssl->state++;
1797 return( 0 );
1798 }
1799
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001800 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001801 return( ret );
1802}
1803#else
1804static int ssl_write_certificate_request( ssl_context *ssl )
1805{
1806 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1807 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001808 size_t dn_size, total_dn_size; /* excluding length bytes */
1809 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00001810 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001811 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001812
1813 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1814
1815 ssl->state++;
1816
Paul Bakkerfbb17802013-04-17 19:10:21 +02001817 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001818 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001819 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001820 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001821 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001822 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001823 return( 0 );
1824 }
1825
1826 /*
1827 * 0 . 0 handshake type
1828 * 1 . 3 handshake length
1829 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01001830 * 5 .. m-1 cert types
1831 * m .. m+1 sig alg length (TLS 1.2 only)
1832 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001833 * n .. n+1 length of all DNs
1834 * n+2 .. n+3 length of DN 1
1835 * n+4 .. ... Distinguished Name #1
1836 * ... .. ... length of DN 2, etc.
1837 */
1838 buf = ssl->out_msg;
1839 p = buf + 4;
1840
1841 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001842 * Supported certificate types
1843 *
1844 * ClientCertificateType certificate_types<1..2^8-1>;
1845 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00001846 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001847 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01001848
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001849#if defined(POLARSSL_RSA_C)
1850 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
1851#endif
1852#if defined(POLARSSL_ECDSA_C)
1853 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
1854#endif
1855
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001856 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001857 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01001858
Paul Bakker577e0062013-08-28 11:57:20 +02001859 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001860#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01001861 /*
1862 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01001863 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001864 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
1865 *
1866 * struct {
1867 * HashAlgorithm hash;
1868 * SignatureAlgorithm signature;
1869 * } SignatureAndHashAlgorithm;
1870 *
1871 * enum { (255) } HashAlgorithm;
1872 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01001873 */
Paul Bakker21dca692013-01-03 11:41:08 +01001874 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01001875 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001876 /*
1877 * Only use current running hash algorithm that is already required
1878 * for requested ciphersuite.
1879 */
Paul Bakker926af752012-11-23 13:38:07 +01001880 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
1881
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001882 if( ssl->transform_negotiate->ciphersuite_info->mac ==
1883 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01001884 {
1885 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
1886 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02001887
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001888 /*
1889 * Supported signature algorithms
1890 */
1891#if defined(POLARSSL_RSA_C)
1892 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1893 p[2 + sa_len++] = SSL_SIG_RSA;
1894#endif
1895#if defined(POLARSSL_ECDSA_C)
1896 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1897 p[2 + sa_len++] = SSL_SIG_ECDSA;
1898#endif
Paul Bakker926af752012-11-23 13:38:07 +01001899
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001900 p[0] = (unsigned char)( sa_len >> 8 );
1901 p[1] = (unsigned char)( sa_len );
1902 sa_len += 2;
1903 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01001904 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001905#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001906
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001907 /*
1908 * DistinguishedName certificate_authorities<0..2^16-1>;
1909 * opaque DistinguishedName<1..2^16-1>;
1910 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001911 p += 2;
1912 crt = ssl->ca_chain;
1913
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001914 total_dn_size = 0;
Paul Bakker29087132010-03-21 21:03:34 +00001915 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001916 {
1917 if( p - buf > 4096 )
1918 break;
1919
Paul Bakker926af752012-11-23 13:38:07 +01001920 dn_size = crt->subject_raw.len;
1921 *p++ = (unsigned char)( dn_size >> 8 );
1922 *p++ = (unsigned char)( dn_size );
1923 memcpy( p, crt->subject_raw.p, dn_size );
1924 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001925
Paul Bakker926af752012-11-23 13:38:07 +01001926 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
1927
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001928 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01001929 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00001930 }
1931
Paul Bakker926af752012-11-23 13:38:07 +01001932 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001933 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1934 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001935 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
1936 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001937
1938 ret = ssl_write_record( ssl );
1939
1940 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
1941
1942 return( ret );
1943}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001944#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
1945 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
1946 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001947
Paul Bakker41c83d32013-03-20 14:39:14 +01001948static int ssl_write_server_key_exchange( ssl_context *ssl )
1949{
Paul Bakker23986e52011-04-24 08:57:21 +00001950 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001951 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001952 const ssl_ciphersuite_t *ciphersuite_info =
1953 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001954
1955#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1956 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
1957 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001958 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02001959 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001960 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001961 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001962 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001963 ((void) dig_signed);
1964 ((void) dig_signed_len);
1965#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001966
Paul Bakker5121ce52009-01-03 21:22:43 +00001967 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
1968
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001969 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
1970 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1971 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001972 {
1973 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
1974 ssl->state++;
1975 return( 0 );
1976 }
1977
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001978#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
1979 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1980 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1981 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001982 {
1983 /* TODO: Support identity hints */
1984 *(p++) = 0x00;
1985 *(p++) = 0x00;
1986
1987 n += 2;
1988 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001989#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
1990 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001991
1992#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1993 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1994 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1995 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00001996 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001997 /*
1998 * Ephemeral DH parameters:
1999 *
2000 * struct {
2001 * opaque dh_p<1..2^16-1>;
2002 * opaque dh_g<1..2^16-1>;
2003 * opaque dh_Ys<1..2^16-1>;
2004 * } ServerDHParams;
2005 */
2006 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2007 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2008 {
2009 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2010 return( ret );
2011 }
Paul Bakker48916f92012-09-16 19:57:18 +00002012
Paul Bakker41c83d32013-03-20 14:39:14 +01002013 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002014 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002015 p,
2016 &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002017 {
2018 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2019 return( ret );
2020 }
2021
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002022 dig_signed = p;
2023 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002024
2025 p += len;
2026 n += len;
2027
Paul Bakker41c83d32013-03-20 14:39:14 +01002028 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2029 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2030 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2031 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2032 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002033#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2034 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002035
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002036#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002037 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2038 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2039
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002040 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002041 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2042 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002043 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002044 /*
2045 * Ephemeral ECDH parameters:
2046 *
2047 * struct {
2048 * ECParameters curve_params;
2049 * ECPoint public;
2050 * } ServerECDHParams;
2051 */
Paul Bakker41c83d32013-03-20 14:39:14 +01002052 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02002053 ssl->handshake->curves[0]->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002054 {
2055 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2056 return( ret );
2057 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002058
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02002059 SSL_DEBUG_MSG( 2, ( "ECDH curve size: %d",
2060 (int) ssl->handshake->ecdh_ctx.grp.nbits ) );
2061
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002062 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2063 p, SSL_MAX_CONTENT_LEN - n,
2064 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002065 {
2066 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2067 return( ret );
2068 }
2069
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002070 dig_signed = p;
2071 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002072
2073 p += len;
2074 n += len;
2075
Paul Bakker41c83d32013-03-20 14:39:14 +01002076 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2077 }
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002078#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002079 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2080 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002081
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002082#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002083 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2084 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002085 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002086 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2087 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002088 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002089 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002090 unsigned int hashlen = 0;
2091 unsigned char hash[64];
2092 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002093
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002094 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002095 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2096 */
Paul Bakker577e0062013-08-28 11:57:20 +02002097#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002098 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2099 {
2100 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2101
2102 if( md_alg == POLARSSL_MD_NONE )
2103 {
2104 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2105 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2106 }
2107 }
Paul Bakker577e0062013-08-28 11:57:20 +02002108 else
2109#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002110#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2111 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker577e0062013-08-28 11:57:20 +02002112 if ( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002113 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2114 {
2115 md_alg = POLARSSL_MD_SHA1;
2116 }
2117 else
Paul Bakker577e0062013-08-28 11:57:20 +02002118#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002119 {
2120 md_alg = POLARSSL_MD_NONE;
2121 }
2122
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002123 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002124 * Compute the hash to be signed
2125 */
Paul Bakker577e0062013-08-28 11:57:20 +02002126#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2127 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002128 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002129 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002130 md5_context md5;
2131 sha1_context sha1;
2132
2133 /*
2134 * digitally-signed struct {
2135 * opaque md5_hash[16];
2136 * opaque sha_hash[20];
2137 * };
2138 *
2139 * md5_hash
2140 * MD5(ClientHello.random + ServerHello.random
2141 * + ServerParams);
2142 * sha_hash
2143 * SHA(ClientHello.random + ServerHello.random
2144 * + ServerParams);
2145 */
2146 md5_starts( &md5 );
2147 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002148 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002149 md5_finish( &md5, hash );
2150
2151 sha1_starts( &sha1 );
2152 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002153 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002154 sha1_finish( &sha1, hash + 16 );
2155
2156 hashlen = 36;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002157 }
2158 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002159#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2160 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002161#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2162 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002163 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002164 {
2165 md_context_t ctx;
2166
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002167 /* Info from md_alg will be used instead */
2168 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002169
2170 /*
2171 * digitally-signed struct {
2172 * opaque client_random[32];
2173 * opaque server_random[32];
2174 * ServerDHParams params;
2175 * };
2176 */
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002177 if( ( ret = md_init_ctx( &ctx, md_info_from_type(md_alg) ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002178 {
2179 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2180 return( ret );
2181 }
2182
2183 md_starts( &ctx );
2184 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002185 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002186 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02002187
2188 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
2189 {
2190 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
2191 return( ret );
2192 }
2193
Paul Bakker23f36802012-09-28 14:15:14 +00002194 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002195 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002196#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2197 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002198 {
2199 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002200 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02002201 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002202
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002203 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2204 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002205
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002206 /*
2207 * Make the signature
2208 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002209 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002210 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002211 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2212 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002213 }
Paul Bakker23f36802012-09-28 14:15:14 +00002214
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002215#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002216 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2217 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002218 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002219 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002220
2221 n += 2;
2222 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002223#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002224
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002225 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002226 p + 2 , &signature_len,
2227 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002228 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002229 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002230 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002231 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002232
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002233 *(p++) = (unsigned char)( signature_len >> 8 );
2234 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002235 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002236
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002237 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002238
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002239 p += signature_len;
2240 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002241 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002242#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002243 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2244 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002245
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002246 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002247 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2248 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2249
2250 ssl->state++;
2251
2252 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2253 {
2254 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2255 return( ret );
2256 }
2257
2258 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2259
2260 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002261}
2262
2263static int ssl_write_server_hello_done( ssl_context *ssl )
2264{
2265 int ret;
2266
2267 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2268
2269 ssl->out_msglen = 4;
2270 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2271 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2272
2273 ssl->state++;
2274
2275 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2276 {
2277 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2278 return( ret );
2279 }
2280
2281 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2282
2283 return( 0 );
2284}
2285
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002286#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2287 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2288static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2289 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002290{
2291 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002292 size_t n;
2293
2294 /*
2295 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2296 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002297 if( *p + 2 > end )
2298 {
2299 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2300 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2301 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002302
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002303 n = ( (*p)[0] << 8 ) | (*p)[1];
2304 *p += 2;
2305
2306 if( n < 1 || n > ssl->handshake->dhm_ctx.len || *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002307 {
2308 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2309 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2310 }
2311
2312 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002313 *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002314 {
2315 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2316 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2317 }
2318
2319 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2320
Paul Bakker70df2fb2013-04-17 17:19:09 +02002321 return( ret );
2322}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002323#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2324 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002325
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002326#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2327 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002328static int ssl_parse_encrypted_pms( ssl_context *ssl,
2329 const unsigned char *p,
2330 const unsigned char *end,
2331 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002332{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002333 int ret;
2334 size_t len = pk_get_len( ssl_own_key( ssl ) );
2335 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002336
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002337 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002338 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002339 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002340 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2341 }
2342
2343 /*
2344 * Decrypt the premaster using own private RSA key
2345 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002346#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2347 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002348 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2349 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002350 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2351 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002352 {
2353 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2354 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2355 }
2356 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002357#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002358
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002359 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002360 {
2361 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2362 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2363 }
2364
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002365 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2366 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002367 sizeof(ssl->handshake->premaster),
2368 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002369
2370 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002371 pms[0] != ssl->handshake->max_major_ver ||
2372 pms[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002373 {
2374 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2375
2376 /*
2377 * Protection against Bleichenbacher's attack:
2378 * invalid PKCS#1 v1.5 padding must not cause
2379 * the connection to end immediately; instead,
2380 * send a bad_record_mac later in the handshake.
2381 */
2382 ssl->handshake->pmslen = 48;
2383
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002384 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002385 if( ret != 0 )
2386 return( ret );
2387 }
2388
2389 return( ret );
2390}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002391#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2392 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002393
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002394#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002395static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2396 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002397{
Paul Bakker6db455e2013-09-18 17:29:31 +02002398 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002399 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002400
Paul Bakker6db455e2013-09-18 17:29:31 +02002401 if( ssl->f_psk == NULL &&
2402 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2403 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002404 {
2405 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2406 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2407 }
2408
2409 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002410 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002411 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002412 if( *p + 2 > end )
2413 {
2414 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2415 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2416 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002417
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002418 n = ( (*p)[0] << 8 ) | (*p)[1];
2419 *p += 2;
2420
2421 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002422 {
2423 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2424 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2425 }
2426
Paul Bakker6db455e2013-09-18 17:29:31 +02002427 if( ssl->f_psk != NULL )
2428 {
2429 if( ( ret != ssl->f_psk( ssl->p_psk, ssl, *p, n ) ) != 0 )
2430 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2431 }
2432
2433 if( ret == 0 )
2434 {
2435 if( n != ssl->psk_identity_len ||
2436 memcmp( ssl->psk_identity, *p, n ) != 0 )
2437 {
2438 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2439 }
2440 }
2441
2442 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002443 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002444 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002445 if( ( ret = ssl_send_alert_message( ssl,
2446 SSL_ALERT_LEVEL_FATAL,
2447 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2448 {
2449 return( ret );
2450 }
2451
2452 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002453 }
2454
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002455 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002456 ret = 0;
2457
Paul Bakkerfbb17802013-04-17 19:10:21 +02002458 return( ret );
2459}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002460#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002461
Paul Bakker5121ce52009-01-03 21:22:43 +00002462static int ssl_parse_client_key_exchange( ssl_context *ssl )
2463{
Paul Bakker23986e52011-04-24 08:57:21 +00002464 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002465 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002466
Paul Bakker41c83d32013-03-20 14:39:14 +01002467 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002468
2469 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2470
2471 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2472 {
2473 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2474 return( ret );
2475 }
2476
2477 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2478 {
2479 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002480 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002481 }
2482
2483 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2484 {
2485 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002486 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002487 }
2488
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002489#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002490 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002491 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002492 unsigned char *p = ssl->in_msg + 4;
2493 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2494
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002495 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002496 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002497 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2498 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002499 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002500
2501 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
2502
2503 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2504 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002505 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002506 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002507 {
2508 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2509 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2510 }
2511
2512 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002513 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002514 else
2515#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002516#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2517 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2518 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2519 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002520 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002521 size_t n = ssl->in_msg[3];
2522
2523 if( n < 1 || n > mpi_size( &ssl->handshake->ecdh_ctx.grp.P ) * 2 + 2 ||
2524 n + 4 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002525 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002526 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2527 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002528 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002529
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002530 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2531 ssl->in_msg + 4, n ) ) != 0 )
2532 {
2533 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2534 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2535 }
2536
2537 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2538
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002539 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2540 &ssl->handshake->pmslen,
2541 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002542 POLARSSL_MPI_MAX_SIZE,
2543 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002544 {
2545 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2546 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2547 }
2548
2549 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002550 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002551 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002552#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2553 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002554#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2555 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002556 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002557 unsigned char *p = ssl->in_msg + 4;
2558 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2559
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002560 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002561 {
2562 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2563 return( ret );
2564 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002565
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002566 if( ( ret = ssl_psk_derive_premaster( ssl,
2567 ciphersuite_info->key_exchange ) ) != 0 )
2568 {
2569 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2570 return( ret );
2571 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002572 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002573 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002574#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002575#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2576 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2577 {
2578 unsigned char *p = ssl->in_msg + 4;
2579 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2580
2581 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2582 {
2583 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2584 return( ret );
2585 }
2586
2587 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
2588 {
2589 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
2590 return( ret );
2591 }
2592
2593 if( ( ret = ssl_psk_derive_premaster( ssl,
2594 ciphersuite_info->key_exchange ) ) != 0 )
2595 {
2596 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2597 return( ret );
2598 }
2599 }
2600 else
2601#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002602#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2603 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2604 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002605 unsigned char *p = ssl->in_msg + 4;
2606 unsigned char *end = ssl->in_msg + ssl->in_msglen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002607
2608 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2609 {
2610 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2611 return( ret );
2612 }
2613 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2614 {
2615 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2616 return( ret );
2617 }
2618
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002619 if( ( ret = ssl_psk_derive_premaster( ssl,
2620 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002621 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002622 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2623 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002624 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002625 }
2626 else
2627#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002628#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2629 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2630 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002631 unsigned char *p = ssl->in_msg + 4;
2632 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2633
2634 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2635 {
2636 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2637 return( ret );
2638 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002639
2640 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2641 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002642 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002643 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2644 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002645 }
2646
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002647 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2648
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002649 if( ( ret = ssl_psk_derive_premaster( ssl,
2650 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002651 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002652 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002653 return( ret );
2654 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002655 }
2656 else
2657#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002658#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2659 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002660 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002661 if( ( ret = ssl_parse_encrypted_pms( ssl,
2662 ssl->in_msg + 4,
2663 ssl->in_msg + ssl->in_msglen,
2664 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002665 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002666 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_ecrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002667 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002668 }
2669 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002670 else
2671#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2672 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002673 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002674 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2675 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002676
Paul Bakkerff60ee62010-03-16 21:09:09 +00002677 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2678 {
2679 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2680 return( ret );
2681 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002682
Paul Bakker5121ce52009-01-03 21:22:43 +00002683 ssl->state++;
2684
2685 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2686
2687 return( 0 );
2688}
2689
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002690#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2691 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002692 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2693 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002694static int ssl_parse_certificate_verify( ssl_context *ssl )
2695{
Paul Bakkered27a042013-04-18 22:46:23 +02002696 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002697 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002698
2699 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2700
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002701 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002702 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002703 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002704 {
2705 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2706 ssl->state++;
2707 return( 0 );
2708 }
2709
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002710 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002711 return( ret );
2712}
2713#else
2714static int ssl_parse_certificate_verify( ssl_context *ssl )
2715{
2716 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002717 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002718 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002719 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002720 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02002721#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002722 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02002723#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002724 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002725 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2726
2727 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2728
2729 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002730 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002731 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2732 {
2733 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2734 ssl->state++;
2735 return( 0 );
2736 }
2737
Paul Bakkered27a042013-04-18 22:46:23 +02002738 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002739 {
2740 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2741 ssl->state++;
2742 return( 0 );
2743 }
2744
Paul Bakker48916f92012-09-16 19:57:18 +00002745 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002746
2747 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2748 {
2749 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2750 return( ret );
2751 }
2752
2753 ssl->state++;
2754
2755 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2756 {
2757 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002758 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002759 }
2760
2761 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
2762 {
2763 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002764 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002765 }
2766
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002767 /*
2768 * 0 . 0 handshake type
2769 * 1 . 3 handshake length
2770 * 4 . 5 sig alg (TLS 1.2 only)
2771 * 4+n . 5+n signature length (n = sa_len)
2772 * 6+n . 6+n+m signature (m = sig_len)
2773 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002774
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002775#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2776 defined(POLARSSL_SSL_PROTO_TLS1_1)
2777 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002778 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002779 sa_len = 0;
2780
Paul Bakkerc70b9822013-04-07 22:00:46 +02002781 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002782 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002783
2784 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
2785 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
2786 POLARSSL_PK_ECDSA ) )
2787 {
2788 hash_start += 16;
2789 hashlen -= 16;
2790 md_alg = POLARSSL_MD_SHA1;
2791 }
Paul Bakker926af752012-11-23 13:38:07 +01002792 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002793 else
2794#endif
Paul Bakker577e0062013-08-28 11:57:20 +02002795#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2796 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002797 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002798 sa_len = 2;
2799
Paul Bakker5121ce52009-01-03 21:22:43 +00002800 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002801 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00002802 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002803 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002804 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002805 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2806 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01002807 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2808 }
2809
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002810 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01002811
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002812 /* Info from md_alg will be used instead */
2813 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002814
2815 /*
2816 * Signature
2817 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002818 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
2819 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002820 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002821 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2822 " for verify message" ) );
2823 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002824 }
2825
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002826 /*
2827 * Check the certificate's key type matches the signature alg
2828 */
2829 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
2830 {
2831 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
2832 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2833 }
Paul Bakker577e0062013-08-28 11:57:20 +02002834 }
2835 else
2836#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2837 {
2838 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002839 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002840 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002841
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002842 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01002843
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002844 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002845 {
2846 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002847 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002848 }
2849
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002850 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002851 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002852 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002853 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002854 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002855 return( ret );
2856 }
2857
2858 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
2859
Paul Bakkered27a042013-04-18 22:46:23 +02002860 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002861}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002862#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2863 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2864 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002865
Paul Bakkera503a632013-08-14 13:48:06 +02002866#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002867static int ssl_write_new_session_ticket( ssl_context *ssl )
2868{
2869 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002870 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002871 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002872
2873 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
2874
2875 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2876 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
2877
2878 /*
2879 * struct {
2880 * uint32 ticket_lifetime_hint;
2881 * opaque ticket<0..2^16-1>;
2882 * } NewSessionTicket;
2883 *
2884 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
2885 * 8 . 9 ticket_len (n)
2886 * 10 . 9+n ticket content
2887 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002888
2889 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
2890 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
2891 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
2892 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002893
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02002894 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
2895 {
2896 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
2897 tlen = 0;
2898 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002899
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002900 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
2901 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002902
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002903 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002904
2905 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2906 {
2907 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2908 return( ret );
2909 }
2910
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002911 /* No need to remember writing a NewSessionTicket any more */
2912 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002913
2914 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
2915
2916 return( 0 );
2917}
Paul Bakkera503a632013-08-14 13:48:06 +02002918#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002919
Paul Bakker5121ce52009-01-03 21:22:43 +00002920/*
Paul Bakker1961b702013-01-25 14:49:24 +01002921 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002922 */
Paul Bakker1961b702013-01-25 14:49:24 +01002923int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002924{
2925 int ret = 0;
2926
Paul Bakker1961b702013-01-25 14:49:24 +01002927 if( ssl->state == SSL_HANDSHAKE_OVER )
2928 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002929
Paul Bakker1961b702013-01-25 14:49:24 +01002930 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
2931
2932 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2933 return( ret );
2934
2935 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002936 {
Paul Bakker1961b702013-01-25 14:49:24 +01002937 case SSL_HELLO_REQUEST:
2938 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002939 break;
2940
Paul Bakker1961b702013-01-25 14:49:24 +01002941 /*
2942 * <== ClientHello
2943 */
2944 case SSL_CLIENT_HELLO:
2945 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002946 break;
Paul Bakker1961b702013-01-25 14:49:24 +01002947
2948 /*
2949 * ==> ServerHello
2950 * Certificate
2951 * ( ServerKeyExchange )
2952 * ( CertificateRequest )
2953 * ServerHelloDone
2954 */
2955 case SSL_SERVER_HELLO:
2956 ret = ssl_write_server_hello( ssl );
2957 break;
2958
2959 case SSL_SERVER_CERTIFICATE:
2960 ret = ssl_write_certificate( ssl );
2961 break;
2962
2963 case SSL_SERVER_KEY_EXCHANGE:
2964 ret = ssl_write_server_key_exchange( ssl );
2965 break;
2966
2967 case SSL_CERTIFICATE_REQUEST:
2968 ret = ssl_write_certificate_request( ssl );
2969 break;
2970
2971 case SSL_SERVER_HELLO_DONE:
2972 ret = ssl_write_server_hello_done( ssl );
2973 break;
2974
2975 /*
2976 * <== ( Certificate/Alert )
2977 * ClientKeyExchange
2978 * ( CertificateVerify )
2979 * ChangeCipherSpec
2980 * Finished
2981 */
2982 case SSL_CLIENT_CERTIFICATE:
2983 ret = ssl_parse_certificate( ssl );
2984 break;
2985
2986 case SSL_CLIENT_KEY_EXCHANGE:
2987 ret = ssl_parse_client_key_exchange( ssl );
2988 break;
2989
2990 case SSL_CERTIFICATE_VERIFY:
2991 ret = ssl_parse_certificate_verify( ssl );
2992 break;
2993
2994 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2995 ret = ssl_parse_change_cipher_spec( ssl );
2996 break;
2997
2998 case SSL_CLIENT_FINISHED:
2999 ret = ssl_parse_finished( ssl );
3000 break;
3001
3002 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003003 * ==> ( NewSessionTicket )
3004 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003005 * Finished
3006 */
3007 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003008#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003009 if( ssl->handshake->new_session_ticket != 0 )
3010 ret = ssl_write_new_session_ticket( ssl );
3011 else
Paul Bakkera503a632013-08-14 13:48:06 +02003012#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003013 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003014 break;
3015
3016 case SSL_SERVER_FINISHED:
3017 ret = ssl_write_finished( ssl );
3018 break;
3019
3020 case SSL_FLUSH_BUFFERS:
3021 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3022 ssl->state = SSL_HANDSHAKE_WRAPUP;
3023 break;
3024
3025 case SSL_HANDSHAKE_WRAPUP:
3026 ssl_handshake_wrapup( ssl );
3027 break;
3028
3029 default:
3030 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3031 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003032 }
3033
Paul Bakker5121ce52009-01-03 21:22:43 +00003034 return( ret );
3035}
Paul Bakker5121ce52009-01-03 21:22:43 +00003036#endif