blob: 8efd57e82fc232b9debba9fad1b74049d3476036 [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;
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020065#if defined(POLARSSL_X509_PARSE_C)
66 size_t cert_len;
67#endif /* POLARSSL_X509_PARSE_C */
68
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
76#if defined(POLARSSL_X509_PARSE_C)
77 ((ssl_session *) buf)->peer_cert = NULL;
78
79 if( session->peer_cert == NULL )
80 cert_len = 0;
81 else
82 cert_len = session->peer_cert->raw.len;
83
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020084 if( left < 3 + cert_len )
85 return( -1 );
86
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020087 *p++ = (unsigned char)( cert_len >> 16 & 0xFF );
88 *p++ = (unsigned char)( cert_len >> 8 & 0xFF );
89 *p++ = (unsigned char)( cert_len & 0xFF );
90
91 if( session->peer_cert != NULL )
92 memcpy( p, session->peer_cert->raw.p, cert_len );
93
94 p += cert_len;
95#endif /* POLARSSL_X509_PARSE_C */
96
97 *olen = p - buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020098
99 return( 0 );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200100}
101
102/*
103 * Unserialise session, see ssl_save_session()
104 */
105static int ssl_load_session( ssl_session *session,
106 const unsigned char *buf, size_t len )
107{
108 int ret;
109 const unsigned char *p = buf;
110 const unsigned char * const end = buf + len;
111#if defined(POLARSSL_X509_PARSE_C)
112 size_t cert_len;
113#endif /* POLARSSL_X509_PARSE_C */
114
115 if( p + sizeof( ssl_session ) > end )
116 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
117
118 memcpy( session, p, sizeof( ssl_session ) );
119 p += sizeof( ssl_session );
120
121#if defined(POLARSSL_X509_PARSE_C)
122 if( p + 3 > end )
123 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
124
125 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
126 p += 3;
127
128 if( cert_len == 0 )
129 {
130 session->peer_cert = NULL;
131 }
132 else
133 {
134 if( p + cert_len > end )
135 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
136
137 session->peer_cert = polarssl_malloc( cert_len );
138
139 if( session->peer_cert == NULL )
140 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
141
142 memset( session->peer_cert, 0, sizeof( x509_cert ) );
143
144 if( ( ret = x509parse_crt( session->peer_cert, p, cert_len ) ) != 0 )
145 {
146 polarssl_free( session->peer_cert );
147 free( session->peer_cert );
148 session->peer_cert = NULL;
149 return( ret );
150 }
151
152 p += cert_len;
153 }
154#endif /* POLARSSL_X509_PARSE_C */
155
156 if( p != end )
157 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
158
159 return( 0 );
160}
161
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200162/*
163 * Create session ticket, secured as recommended in RFC 5077 section 4:
164 *
165 * struct {
166 * opaque key_name[16];
167 * opaque iv[16];
168 * opaque encrypted_state<0..2^16-1>;
169 * opaque mac[32];
170 * } ticket;
171 *
172 * (the internal state structure differs, however).
173 */
174static int ssl_write_ticket( ssl_context *ssl, size_t *tlen )
175{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200176 int ret;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200177 unsigned char * const start = ssl->out_msg + 10;
178 unsigned char *p = start;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200179 unsigned char *state;
180 unsigned char iv[16];
181 size_t clear_len, enc_len, pad_len, i;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200182
Manuel Pégourié-Gonnard0a201712013-08-23 16:25:16 +0200183 *tlen = 0;
184
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200185 if( ssl->ticket_keys == NULL )
186 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
187
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200188 /* Write key name */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200189 memcpy( p, ssl->ticket_keys->key_name, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200190 p += 16;
191
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200192 /* Generate and write IV (with a copy for aes_crypt) */
193 if( ( ret = ssl->f_rng( ssl->p_rng, p, 16 ) ) != 0 )
194 return( ret );
195 memcpy( iv, p, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200196 p += 16;
197
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200198 /*
199 * Dump session state
200 *
201 * After the session state itself, we still need room for 16 bytes of
202 * padding and 32 bytes of MAC, so there's only so much room left
203 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200204 state = p + 2;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200205 if( ssl_save_session( ssl->session_negotiate, state,
206 SSL_MAX_CONTENT_LEN - (state - ssl->out_ctr) - 48,
207 &clear_len ) != 0 )
208 {
209 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
210 }
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200211 SSL_DEBUG_BUF( 3, "session ticket cleartext", state, clear_len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200212
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200213 /* Apply PKCS padding */
214 pad_len = 16 - clear_len % 16;
215 enc_len = clear_len + pad_len;
216 for( i = clear_len; i < enc_len; i++ )
217 state[i] = (unsigned char) pad_len;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200218
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200219 /* Encrypt */
220 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->enc, AES_ENCRYPT,
221 enc_len, iv, state, state ) ) != 0 )
222 {
223 return( ret );
224 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200225
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200226 /* Write length */
227 *p++ = (unsigned char)( ( enc_len >> 8 ) & 0xFF );
228 *p++ = (unsigned char)( ( enc_len ) & 0xFF );
229 p = state + enc_len;
230
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200231 /* Compute and write MAC( key_name + iv + enc_state_len + enc_state ) */
232 sha256_hmac( ssl->ticket_keys->mac_key, 16, start, p - start, p, 0 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200233 p += 32;
234
235 *tlen = p - start;
236
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200237 SSL_DEBUG_BUF( 3, "session ticket structure", start, *tlen );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200238
239 return( 0 );
240}
241
242/*
243 * Load session ticket (see ssl_write_ticket for structure)
244 */
245static int ssl_parse_ticket( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200246 unsigned char *buf,
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200247 size_t len )
248{
249 int ret;
250 ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200251 unsigned char *key_name = buf;
252 unsigned char *iv = buf + 16;
253 unsigned char *enc_len_p = iv + 16;
254 unsigned char *ticket = enc_len_p + 2;
255 unsigned char *mac;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200256 unsigned char computed_mac[16];
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200257 size_t enc_len, clear_len, i;
258 unsigned char pad_len;
259
260 SSL_DEBUG_BUF( 3, "session ticket structure", buf, len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200261
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200262 if( len < 34 || ssl->ticket_keys == NULL )
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200263 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
264
265 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
266 mac = ticket + enc_len;
267
268 if( len != enc_len + 66 )
269 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
270
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200271 /* Check name */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200272 if( memcmp( key_name, ssl->ticket_keys->key_name, 16 ) != 0 )
273 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200274
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200275 /* Check mac */
276 sha256_hmac( ssl->ticket_keys->mac_key, 16, buf, len - 32,
277 computed_mac, 0 );
278 ret = 0;
279 for( i = 0; i < 32; i++ )
280 if( mac[i] != computed_mac[i] )
281 ret = POLARSSL_ERR_SSL_INVALID_MAC;
282 if( ret != 0 )
283 return( ret );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200284
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200285 /* Decrypt */
286 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->dec, AES_DECRYPT,
287 enc_len, iv, ticket, ticket ) ) != 0 )
288 {
289 return( ret );
290 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200291
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200292 /* Check PKCS padding */
293 pad_len = ticket[enc_len - 1];
294
295 ret = 0;
296 for( i = 2; i < pad_len; i++ )
297 if( ticket[enc_len - i] != pad_len )
298 ret = POLARSSL_ERR_SSL_BAD_INPUT_DATA;
299 if( ret != 0 )
300 return( ret );
301
302 clear_len = enc_len - pad_len;
303
304 SSL_DEBUG_BUF( 3, "session ticket cleartext", ticket, clear_len );
305
306 /* Actually load session */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200307 if( ( ret = ssl_load_session( &session, ticket, clear_len ) ) != 0 )
308 {
309 SSL_DEBUG_MSG( 1, ( "failed to parse ticket content" ) );
310 memset( &session, 0, sizeof( ssl_session ) );
311 return( ret );
312 }
313
Paul Bakker606b4ba2013-08-14 16:52:14 +0200314#if defined(POLARSSL_HAVE_TIME)
315 /* Check if still valid */
316 if( (int) ( time( NULL) - session.start ) > ssl->ticket_lifetime )
317 {
318 SSL_DEBUG_MSG( 1, ( "session ticket expired" ) );
319 memset( &session, 0, sizeof( ssl_session ) );
320 return( POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED );
321 }
322#endif
323
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200324 /*
325 * Keep the session ID sent by the client, since we MUST send it back to
326 * inform him we're accepting the ticket (RFC 5077 section 3.4)
327 */
328 session.length = ssl->session_negotiate->length;
329 memcpy( &session.id, ssl->session_negotiate->id, session.length );
330
331 ssl_session_free( ssl->session_negotiate );
332 memcpy( ssl->session_negotiate, &session, sizeof( ssl_session ) );
333 memset( &session, 0, sizeof( ssl_session ) );
334
335 return( 0 );
336}
Paul Bakkera503a632013-08-14 13:48:06 +0200337#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200338
Paul Bakker5701cdc2012-09-27 21:49:42 +0000339static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000340 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000341 size_t len )
342{
343 int ret;
344 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000345 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000346
347 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
348 if( servername_list_size + 2 != len )
349 {
350 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
351 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
352 }
353
354 p = buf + 2;
355 while( servername_list_size > 0 )
356 {
357 hostname_len = ( ( p[1] << 8 ) | p[2] );
358 if( hostname_len + 3 > servername_list_size )
359 {
360 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
361 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
362 }
363
364 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
365 {
366 ret = ssl->f_sni( ssl->p_sni, ssl, p + 3, hostname_len );
367 if( ret != 0 )
368 {
369 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
370 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
371 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
372 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000373 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000374 }
375
376 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000377 p += hostname_len + 3;
378 }
379
380 if( servername_list_size != 0 )
381 {
382 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
383 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000384 }
385
386 return( 0 );
387}
388
Paul Bakker48916f92012-09-16 19:57:18 +0000389static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000390 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000391 size_t len )
392{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000393 int ret;
394
Paul Bakker48916f92012-09-16 19:57:18 +0000395 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
396 {
397 if( len != 1 || buf[0] != 0x0 )
398 {
399 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000400
401 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
402 return( ret );
403
Paul Bakker48916f92012-09-16 19:57:18 +0000404 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
405 }
406
407 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
408 }
409 else
410 {
411 if( len != 1 + ssl->verify_data_len ||
412 buf[0] != ssl->verify_data_len ||
413 memcmp( buf + 1, ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
414 {
415 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000416
417 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
418 return( ret );
419
Paul Bakker48916f92012-09-16 19:57:18 +0000420 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
421 }
422 }
423
424 return( 0 );
425}
426
Paul Bakker23f36802012-09-28 14:15:14 +0000427static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
428 const unsigned char *buf,
429 size_t len )
430{
431 size_t sig_alg_list_size;
432 const unsigned char *p;
433
434 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
435 if( sig_alg_list_size + 2 != len ||
436 sig_alg_list_size %2 != 0 )
437 {
438 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
439 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
440 }
441
442 p = buf + 2;
443 while( sig_alg_list_size > 0 )
444 {
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200445 /*
446 * For now, just ignore signature algorithm and rely on offered
447 * ciphersuites only. To be fixed later.
448 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200449#if defined(POLARSSL_SHA512_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000450 if( p[0] == SSL_HASH_SHA512 )
451 {
452 ssl->handshake->sig_alg = SSL_HASH_SHA512;
453 break;
454 }
455 if( p[0] == SSL_HASH_SHA384 )
456 {
457 ssl->handshake->sig_alg = SSL_HASH_SHA384;
458 break;
459 }
460#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200461#if defined(POLARSSL_SHA256_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000462 if( p[0] == SSL_HASH_SHA256 )
463 {
464 ssl->handshake->sig_alg = SSL_HASH_SHA256;
465 break;
466 }
467 if( p[0] == SSL_HASH_SHA224 )
468 {
469 ssl->handshake->sig_alg = SSL_HASH_SHA224;
470 break;
471 }
472#endif
473 if( p[0] == SSL_HASH_SHA1 )
474 {
475 ssl->handshake->sig_alg = SSL_HASH_SHA1;
476 break;
477 }
478 if( p[0] == SSL_HASH_MD5 )
479 {
480 ssl->handshake->sig_alg = SSL_HASH_MD5;
481 break;
482 }
483
484 sig_alg_list_size -= 2;
485 p += 2;
486 }
487
488 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
489 ssl->handshake->sig_alg ) );
490
491 return( 0 );
492}
493
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200494#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200495static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
496 const unsigned char *buf,
497 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100498{
499 size_t list_size;
500 const unsigned char *p;
501
502 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
503 if( list_size + 2 != len ||
504 list_size % 2 != 0 )
505 {
506 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
507 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
508 }
509
510 p = buf + 2;
511 while( list_size > 0 )
512 {
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200513#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
514 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP192R1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100515 {
516 ssl->handshake->ec_curve = p[1];
517 return( 0 );
518 }
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200519#endif
520#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
521 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP224R1 )
522 {
523 ssl->handshake->ec_curve = p[1];
524 return( 0 );
525 }
526#endif
527#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
528 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP256R1 )
529 {
530 ssl->handshake->ec_curve = p[1];
531 return( 0 );
532 }
533#endif
534#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
535 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP384R1 )
536 {
537 ssl->handshake->ec_curve = p[1];
538 return( 0 );
539 }
540#endif
541#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
542 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP521R1 )
543 {
544 ssl->handshake->ec_curve = p[1];
545 return( 0 );
546 }
547#endif
Paul Bakker41c83d32013-03-20 14:39:14 +0100548
549 list_size -= 2;
550 p += 2;
551 }
552
553 return( 0 );
554}
555
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200556static int ssl_parse_supported_point_formats( ssl_context *ssl,
557 const unsigned char *buf,
558 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100559{
560 size_t list_size;
561 const unsigned char *p;
562
563 list_size = buf[0];
564 if( list_size + 1 != len )
565 {
566 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
567 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
568 }
569
570 p = buf + 2;
571 while( list_size > 0 )
572 {
573 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
574 p[0] == POLARSSL_ECP_PF_COMPRESSED )
575 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200576 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200577 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100578 return( 0 );
579 }
580
581 list_size--;
582 p++;
583 }
584
585 return( 0 );
586}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200587#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100588
Paul Bakker05decb22013-08-15 13:33:48 +0200589#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200590static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
591 const unsigned char *buf,
592 size_t len )
593{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200594 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200595 {
596 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
597 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
598 }
599
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200600 ssl->session_negotiate->mfl_code = buf[0];
601
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200602 return( 0 );
603}
Paul Bakker05decb22013-08-15 13:33:48 +0200604#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200605
Paul Bakker1f2bc622013-08-15 13:45:55 +0200606#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200607static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
608 const unsigned char *buf,
609 size_t len )
610{
611 if( len != 0 )
612 {
613 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
614 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
615 }
616
617 ((void) buf);
618
619 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
620
621 return( 0 );
622}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200623#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200624
Paul Bakkera503a632013-08-14 13:48:06 +0200625#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200626static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200627 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200628 size_t len )
629{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200630 int ret;
631
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200632 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
633 return( 0 );
634
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200635 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200636 ssl->handshake->new_session_ticket = 1;
637
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200638 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
639
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200640 if( len == 0 )
641 return( 0 );
642
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200643 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
644 {
645 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
646 return( 0 );
647 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200648
649 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200650 * Failures are ok: just ignore the ticket and proceed.
651 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200652 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
653 {
654 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200655 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200656 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200657
658 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
659
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200660 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200661
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200662 /* Don't send a new ticket after all, this one is OK */
663 ssl->handshake->new_session_ticket = 0;
664
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200665 return( 0 );
666}
Paul Bakkera503a632013-08-14 13:48:06 +0200667#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200668
Paul Bakker78a8c712013-03-06 17:01:52 +0100669#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
670static int ssl_parse_client_hello_v2( ssl_context *ssl )
671{
672 int ret;
673 unsigned int i, j;
674 size_t n;
675 unsigned int ciph_len, sess_len, chal_len;
676 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200677 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200678 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100679
680 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
681
682 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
683 {
684 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
685
686 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
687 return( ret );
688
689 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
690 }
691
692 buf = ssl->in_hdr;
693
694 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
695
696 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
697 buf[2] ) );
698 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
699 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
700 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
701 buf[3], buf[4] ) );
702
703 /*
704 * SSLv2 Client Hello
705 *
706 * Record layer:
707 * 0 . 1 message length
708 *
709 * SSL layer:
710 * 2 . 2 message type
711 * 3 . 4 protocol version
712 */
713 if( buf[2] != SSL_HS_CLIENT_HELLO ||
714 buf[3] != SSL_MAJOR_VERSION_3 )
715 {
716 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
717 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
718 }
719
720 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
721
722 if( n < 17 || n > 512 )
723 {
724 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
725 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
726 }
727
728 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200729 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
730 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100731
732 if( ssl->minor_ver < ssl->min_minor_ver )
733 {
734 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
735 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
736 ssl->min_major_ver, ssl->min_minor_ver ) );
737
738 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
739 SSL_ALERT_MSG_PROTOCOL_VERSION );
740 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
741 }
742
Paul Bakker2fbefde2013-06-29 16:01:15 +0200743 ssl->handshake->max_major_ver = buf[3];
744 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100745
746 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
747 {
748 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
749 return( ret );
750 }
751
752 ssl->handshake->update_checksum( ssl, buf + 2, n );
753
754 buf = ssl->in_msg;
755 n = ssl->in_left - 5;
756
757 /*
758 * 0 . 1 ciphersuitelist length
759 * 2 . 3 session id length
760 * 4 . 5 challenge length
761 * 6 . .. ciphersuitelist
762 * .. . .. session id
763 * .. . .. challenge
764 */
765 SSL_DEBUG_BUF( 4, "record contents", buf, n );
766
767 ciph_len = ( buf[0] << 8 ) | buf[1];
768 sess_len = ( buf[2] << 8 ) | buf[3];
769 chal_len = ( buf[4] << 8 ) | buf[5];
770
771 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
772 ciph_len, sess_len, chal_len ) );
773
774 /*
775 * Make sure each parameter length is valid
776 */
777 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
778 {
779 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
780 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
781 }
782
783 if( sess_len > 32 )
784 {
785 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
786 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
787 }
788
789 if( chal_len < 8 || chal_len > 32 )
790 {
791 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
792 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
793 }
794
795 if( n != 6 + ciph_len + sess_len + chal_len )
796 {
797 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
798 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
799 }
800
801 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
802 buf + 6, ciph_len );
803 SSL_DEBUG_BUF( 3, "client hello, session id",
804 buf + 6 + ciph_len, sess_len );
805 SSL_DEBUG_BUF( 3, "client hello, challenge",
806 buf + 6 + ciph_len + sess_len, chal_len );
807
808 p = buf + 6 + ciph_len;
809 ssl->session_negotiate->length = sess_len;
810 memset( ssl->session_negotiate->id, 0, sizeof( ssl->session_negotiate->id ) );
811 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
812
813 p += sess_len;
814 memset( ssl->handshake->randbytes, 0, 64 );
815 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
816
817 /*
818 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
819 */
820 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
821 {
822 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
823 {
824 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
825 if( ssl->renegotiation == SSL_RENEGOTIATION )
826 {
827 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
828
829 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
830 return( ret );
831
832 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
833 }
834 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
835 break;
836 }
837 }
838
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200839 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
840 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +0100841 {
842 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
843 {
Paul Bakker41c83d32013-03-20 14:39:14 +0100844 // Only allow non-ECC ciphersuites as we do not have extensions
845 //
Paul Bakker59c28a22013-06-29 15:33:42 +0200846 if( p[0] == 0 && p[1] == 0 &&
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200847 ( ( ciphersuites[i] >> 8 ) & 0xFF ) == 0 &&
848 p[2] == ( ciphersuites[i] & 0xFF ) )
Paul Bakker59c28a22013-06-29 15:33:42 +0200849 {
850 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
851
852 if( ciphersuite_info == NULL )
853 {
854 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %02x not found",
855 ciphersuites[i] ) );
856 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
857 }
858
Paul Bakker2fbefde2013-06-29 16:01:15 +0200859 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
860 ciphersuite_info->max_minor_ver < ssl->minor_ver )
861 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +0200862
Paul Bakker78a8c712013-03-06 17:01:52 +0100863 goto have_ciphersuite_v2;
Paul Bakker59c28a22013-06-29 15:33:42 +0200864 }
Paul Bakker78a8c712013-03-06 17:01:52 +0100865 }
866 }
867
868 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
869
870 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
871
872have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200873 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +0200874 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +0100875 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +0100876
877 /*
878 * SSLv2 Client Hello relevant renegotiation security checks
879 */
880 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
881 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
882 {
883 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
884
885 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
886 return( ret );
887
888 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
889 }
890
891 ssl->in_left = 0;
892 ssl->state++;
893
894 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
895
896 return( 0 );
897}
898#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
899
Paul Bakker5121ce52009-01-03 21:22:43 +0000900static int ssl_parse_client_hello( ssl_context *ssl )
901{
Paul Bakker23986e52011-04-24 08:57:21 +0000902 int ret;
903 unsigned int i, j;
904 size_t n;
905 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +0000906 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +0000907 unsigned int ext_len = 0;
908 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000909 int renegotiation_info_seen = 0;
910 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200911 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +0100912 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +0200913 pk_type_t pk_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +0000914
915 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
916
Paul Bakker48916f92012-09-16 19:57:18 +0000917 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
918 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000919 {
920 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
921 return( ret );
922 }
923
924 buf = ssl->in_hdr;
925
Paul Bakker78a8c712013-03-06 17:01:52 +0100926#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
927 if( ( buf[0] & 0x80 ) != 0 )
928 return ssl_parse_client_hello_v2( ssl );
929#endif
930
Paul Bakkerec636f32012-09-09 19:17:02 +0000931 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
932
933 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
934 buf[0] ) );
935 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
936 ( buf[3] << 8 ) | buf[4] ) );
937 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
938 buf[1], buf[2] ) );
939
940 /*
941 * SSLv3 Client Hello
942 *
943 * Record layer:
944 * 0 . 0 message type
945 * 1 . 2 protocol version
946 * 3 . 4 message length
947 */
948 if( buf[0] != SSL_MSG_HANDSHAKE ||
949 buf[1] != SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000950 {
Paul Bakkerec636f32012-09-09 19:17:02 +0000951 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
952 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
953 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000954
Paul Bakkerec636f32012-09-09 19:17:02 +0000955 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000956
Manuel Pégourié-Gonnard72882b22013-08-02 13:36:00 +0200957 if( n < 45 || n > 2048 )
Paul Bakkerec636f32012-09-09 19:17:02 +0000958 {
959 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
960 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
961 }
962
Paul Bakker48916f92012-09-16 19:57:18 +0000963 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
964 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +0000965 {
966 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
967 return( ret );
968 }
969
970 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +0000971 if( !ssl->renegotiation )
972 n = ssl->in_left - 5;
973 else
974 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +0000975
Paul Bakker48916f92012-09-16 19:57:18 +0000976 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +0000977
978 /*
979 * SSL layer:
980 * 0 . 0 handshake type
981 * 1 . 3 handshake length
982 * 4 . 5 protocol version
983 * 6 . 9 UNIX time()
984 * 10 . 37 random bytes
985 * 38 . 38 session id length
986 * 39 . 38+x session id
987 * 39+x . 40+x ciphersuitelist length
988 * 41+x . .. ciphersuitelist
989 * .. . .. compression alg.
990 * .. . .. extensions
991 */
992 SSL_DEBUG_BUF( 4, "record contents", buf, n );
993
994 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
995 buf[0] ) );
996 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
997 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
998 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
999 buf[4], buf[5] ) );
1000
1001 /*
1002 * Check the handshake type and protocol version
1003 */
1004 if( buf[0] != SSL_HS_CLIENT_HELLO ||
1005 buf[4] != SSL_MAJOR_VERSION_3 )
1006 {
1007 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1008 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1009 }
1010
1011 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +02001012 ssl->minor_ver = ( buf[5] <= ssl->max_minor_ver )
1013 ? buf[5] : ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001014
Paul Bakker1d29fb52012-09-28 13:28:45 +00001015 if( ssl->minor_ver < ssl->min_minor_ver )
1016 {
1017 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
1018 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001019 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001020
1021 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1022 SSL_ALERT_MSG_PROTOCOL_VERSION );
1023
1024 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1025 }
1026
Paul Bakker2fbefde2013-06-29 16:01:15 +02001027 ssl->handshake->max_major_ver = buf[4];
1028 ssl->handshake->max_minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001029
Paul Bakker48916f92012-09-16 19:57:18 +00001030 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001031
1032 /*
1033 * Check the handshake message length
1034 */
1035 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1036 {
1037 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1038 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1039 }
1040
1041 /*
1042 * Check the session length
1043 */
1044 sess_len = buf[38];
1045
1046 if( sess_len > 32 )
1047 {
1048 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1049 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1050 }
1051
Paul Bakker48916f92012-09-16 19:57:18 +00001052 ssl->session_negotiate->length = sess_len;
1053 memset( ssl->session_negotiate->id, 0,
1054 sizeof( ssl->session_negotiate->id ) );
1055 memcpy( ssl->session_negotiate->id, buf + 39,
1056 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001057
1058 /*
1059 * Check the ciphersuitelist length
1060 */
1061 ciph_len = ( buf[39 + sess_len] << 8 )
1062 | ( buf[40 + sess_len] );
1063
1064 if( ciph_len < 2 || ciph_len > 256 || ( ciph_len % 2 ) != 0 )
1065 {
1066 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1067 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1068 }
1069
1070 /*
1071 * Check the compression algorithms length
1072 */
1073 comp_len = buf[41 + sess_len + ciph_len];
1074
1075 if( comp_len < 1 || comp_len > 16 )
1076 {
1077 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1078 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1079 }
1080
Paul Bakker48916f92012-09-16 19:57:18 +00001081 /*
1082 * Check the extension length
1083 */
1084 if( n > 42 + sess_len + ciph_len + comp_len )
1085 {
1086 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1087 | ( buf[43 + sess_len + ciph_len + comp_len] );
1088
1089 if( ( ext_len > 0 && ext_len < 4 ) ||
1090 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1091 {
1092 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1093 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1094 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1095 }
1096 }
1097
1098 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001099#if defined(POLARSSL_ZLIB_SUPPORT)
1100 for( i = 0; i < comp_len; ++i )
1101 {
Paul Bakker48916f92012-09-16 19:57:18 +00001102 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001103 {
Paul Bakker48916f92012-09-16 19:57:18 +00001104 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001105 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001106 }
1107 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001108#endif
1109
Paul Bakkerec636f32012-09-09 19:17:02 +00001110 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1111 buf + 6, 32 );
1112 SSL_DEBUG_BUF( 3, "client hello, session id",
1113 buf + 38, sess_len );
1114 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1115 buf + 41 + sess_len, ciph_len );
1116 SSL_DEBUG_BUF( 3, "client hello, compression",
1117 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001118
Paul Bakkerec636f32012-09-09 19:17:02 +00001119 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001120 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1121 */
1122 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1123 {
1124 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1125 {
1126 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1127 if( ssl->renegotiation == SSL_RENEGOTIATION )
1128 {
1129 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001130
1131 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1132 return( ret );
1133
Paul Bakker48916f92012-09-16 19:57:18 +00001134 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1135 }
1136 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1137 break;
1138 }
1139 }
1140
Paul Bakker48916f92012-09-16 19:57:18 +00001141 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001142
1143 while( ext_len )
1144 {
1145 unsigned int ext_id = ( ( ext[0] << 8 )
1146 | ( ext[1] ) );
1147 unsigned int ext_size = ( ( ext[2] << 8 )
1148 | ( ext[3] ) );
1149
1150 if( ext_size + 4 > ext_len )
1151 {
1152 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1153 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1154 }
1155 switch( ext_id )
1156 {
Paul Bakker5701cdc2012-09-27 21:49:42 +00001157 case TLS_EXT_SERVERNAME:
1158 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1159 if( ssl->f_sni == NULL )
1160 break;
1161
1162 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1163 if( ret != 0 )
1164 return( ret );
1165 break;
1166
Paul Bakker48916f92012-09-16 19:57:18 +00001167 case TLS_EXT_RENEGOTIATION_INFO:
1168 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1169 renegotiation_info_seen = 1;
1170
Paul Bakker23f36802012-09-28 14:15:14 +00001171 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1172 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001173 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001174 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001175
Paul Bakker23f36802012-09-28 14:15:14 +00001176 case TLS_EXT_SIG_ALG:
1177 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1178 if( ssl->renegotiation == SSL_RENEGOTIATION )
1179 break;
1180
1181 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1182 if( ret != 0 )
1183 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001184 break;
1185
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001186#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001187 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1188 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1189
1190 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1191 if( ret != 0 )
1192 return( ret );
1193 break;
1194
1195 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1196 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1197
1198 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1199 if( ret != 0 )
1200 return( ret );
1201 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001202#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001203
Paul Bakker05decb22013-08-15 13:33:48 +02001204#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001205 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1206 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1207
1208 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1209 if( ret != 0 )
1210 return( ret );
1211 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001212#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001213
Paul Bakker1f2bc622013-08-15 13:45:55 +02001214#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001215 case TLS_EXT_TRUNCATED_HMAC:
1216 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1217
1218 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1219 if( ret != 0 )
1220 return( ret );
1221 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001222#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001223
Paul Bakkera503a632013-08-14 13:48:06 +02001224#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001225 case TLS_EXT_SESSION_TICKET:
1226 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1227
1228 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1229 if( ret != 0 )
1230 return( ret );
1231 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001232#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001233
Paul Bakker48916f92012-09-16 19:57:18 +00001234 default:
1235 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1236 ext_id ) );
1237 }
1238
1239 ext_len -= 4 + ext_size;
1240 ext += 4 + ext_size;
1241
1242 if( ext_len > 0 && ext_len < 4 )
1243 {
1244 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1245 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1246 }
1247 }
1248
1249 /*
1250 * Renegotiation security checks
1251 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001252 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1253 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1254 {
1255 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1256 handshake_failure = 1;
1257 }
1258 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1259 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1260 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001261 {
1262 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001263 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001264 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001265 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1266 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1267 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001268 {
1269 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001270 handshake_failure = 1;
1271 }
1272 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1273 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1274 renegotiation_info_seen == 1 )
1275 {
1276 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1277 handshake_failure = 1;
1278 }
1279
1280 if( handshake_failure == 1 )
1281 {
1282 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1283 return( ret );
1284
Paul Bakker48916f92012-09-16 19:57:18 +00001285 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1286 }
Paul Bakker380da532012-04-18 16:10:25 +00001287
Paul Bakker41c83d32013-03-20 14:39:14 +01001288 /*
1289 * Search for a matching ciphersuite
1290 * (At the end because we need information from the EC-based extensions)
1291 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001292 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
1293 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001294 {
1295 for( j = 0, p = buf + 41 + sess_len; j < ciph_len;
1296 j += 2, p += 2 )
1297 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001298 if( p[0] == ( ( ciphersuites[i] >> 8 ) & 0xFF ) &&
1299 p[1] == ( ( ciphersuites[i] ) & 0xFF ) )
Paul Bakker41c83d32013-03-20 14:39:14 +01001300 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001301 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
Paul Bakker41c83d32013-03-20 14:39:14 +01001302
1303 if( ciphersuite_info == NULL )
1304 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001305 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found",
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001306 ciphersuites[i] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001307 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1308 }
1309
Paul Bakker2fbefde2013-06-29 16:01:15 +02001310 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
1311 ciphersuite_info->max_minor_ver < ssl->minor_ver )
1312 continue;
1313
Paul Bakker5fd49172013-08-19 13:29:26 +02001314#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001315 if( ( ciphersuite_info->flags & POLARSSL_CIPHERSUITE_EC ) &&
1316 ssl->handshake->ec_curve == 0 )
1317 continue;
Paul Bakker5fd49172013-08-19 13:29:26 +02001318#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001319
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001320 /* If ciphersuite requires us to have a private key of a
1321 * certain type, make sure we do */
1322 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
1323 if( pk_alg != POLARSSL_PK_NONE &&
1324 ( ssl->pk_key == NULL ||
1325 ! pk_can_do( ssl->pk_key, pk_alg ) ) )
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001326 continue;
1327
Paul Bakker41c83d32013-03-20 14:39:14 +01001328 goto have_ciphersuite;
1329 }
1330 }
1331 }
1332
1333 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1334
1335 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1336 return( ret );
1337
1338 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1339
1340have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001341 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001342 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1343 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1344
Paul Bakker5121ce52009-01-03 21:22:43 +00001345 ssl->in_left = 0;
1346 ssl->state++;
1347
1348 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1349
1350 return( 0 );
1351}
1352
Paul Bakker1f2bc622013-08-15 13:45:55 +02001353#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001354static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1355 unsigned char *buf,
1356 size_t *olen )
1357{
1358 unsigned char *p = buf;
1359
1360 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1361 {
1362 *olen = 0;
1363 return;
1364 }
1365
1366 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1367
1368 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1369 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1370
1371 *p++ = 0x00;
1372 *p++ = 0x00;
1373
1374 *olen = 4;
1375}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001376#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001377
Paul Bakkera503a632013-08-14 13:48:06 +02001378#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001379static void ssl_write_session_ticket_ext( ssl_context *ssl,
1380 unsigned char *buf,
1381 size_t *olen )
1382{
1383 unsigned char *p = buf;
1384
1385 if( ssl->handshake->new_session_ticket == 0 )
1386 {
1387 *olen = 0;
1388 return;
1389 }
1390
1391 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1392
1393 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1394 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1395
1396 *p++ = 0x00;
1397 *p++ = 0x00;
1398
1399 *olen = 4;
1400}
Paul Bakkera503a632013-08-14 13:48:06 +02001401#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001402
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001403static void ssl_write_renegotiation_ext( ssl_context *ssl,
1404 unsigned char *buf,
1405 size_t *olen )
1406{
1407 unsigned char *p = buf;
1408
1409 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1410 {
1411 *olen = 0;
1412 return;
1413 }
1414
1415 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1416
1417 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1418 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1419
1420 *p++ = 0x00;
1421 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1422 *p++ = ssl->verify_data_len * 2 & 0xFF;
1423
1424 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1425 p += ssl->verify_data_len;
1426 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1427 p += ssl->verify_data_len;
1428
1429 *olen = 5 + ssl->verify_data_len * 2;
1430}
1431
Paul Bakker05decb22013-08-15 13:33:48 +02001432#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001433static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1434 unsigned char *buf,
1435 size_t *olen )
1436{
1437 unsigned char *p = buf;
1438
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001439 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1440 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001441 *olen = 0;
1442 return;
1443 }
1444
1445 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1446
1447 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1448 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1449
1450 *p++ = 0x00;
1451 *p++ = 1;
1452
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001453 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001454
1455 *olen = 5;
1456}
Paul Bakker05decb22013-08-15 13:33:48 +02001457#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001458
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001459#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001460static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1461 unsigned char *buf,
1462 size_t *olen )
1463{
1464 unsigned char *p = buf;
1465 ((void) ssl);
1466
1467 *olen = 0;
1468
1469 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1470
1471 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1472 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1473
1474 *p++ = 0x00;
1475 *p++ = 2;
1476
1477 *p++ = 1;
1478 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1479
1480 *olen = 6;
1481}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001482#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001483
Paul Bakker5121ce52009-01-03 21:22:43 +00001484static int ssl_write_server_hello( ssl_context *ssl )
1485{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001486#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001487 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001488#endif
Paul Bakkera3d195c2011-11-27 21:07:34 +00001489 int ret, n;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001490 size_t olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001491 unsigned char *buf, *p;
1492
1493 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1494
1495 /*
1496 * 0 . 0 handshake type
1497 * 1 . 3 handshake length
1498 * 4 . 5 protocol version
1499 * 6 . 9 UNIX time()
1500 * 10 . 37 random bytes
1501 */
1502 buf = ssl->out_msg;
1503 p = buf + 4;
1504
1505 *p++ = (unsigned char) ssl->major_ver;
1506 *p++ = (unsigned char) ssl->minor_ver;
1507
1508 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1509 buf[4], buf[5] ) );
1510
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001511#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001512 t = time( NULL );
1513 *p++ = (unsigned char)( t >> 24 );
1514 *p++ = (unsigned char)( t >> 16 );
1515 *p++ = (unsigned char)( t >> 8 );
1516 *p++ = (unsigned char)( t );
1517
1518 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001519#else
1520 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1521 return( ret );
1522
1523 p += 4;
1524#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001525
Paul Bakkera3d195c2011-11-27 21:07:34 +00001526 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1527 return( ret );
1528
1529 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001530
Paul Bakker48916f92012-09-16 19:57:18 +00001531 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001532
1533 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1534
1535 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001536 * Resume is 0 by default, see ssl_handshake_init().
1537 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1538 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001539 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001540 if( ssl->handshake->resume == 0 &&
1541 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001542 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001543 ssl->f_get_cache != NULL &&
1544 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1545 {
1546 ssl->handshake->resume = 1;
1547 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001548
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001549 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001550 {
1551 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001552 * New session, create a new session id,
1553 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001554 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001555 ssl->state++;
1556
Paul Bakkera503a632013-08-14 13:48:06 +02001557#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001558 if( ssl->handshake->new_session_ticket == 0 )
1559 {
1560 ssl->session_negotiate->length = n = 32;
1561 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001562 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001563 return( ret );
1564 }
1565 else
1566 {
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001567 ssl->session_negotiate->length = n = 0;
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001568 memset( ssl->session_negotiate->id, 0, 32 );
1569 }
Paul Bakkera503a632013-08-14 13:48:06 +02001570#else
1571 ssl->session_negotiate->length = n = 32;
1572 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
1573 n ) ) != 0 )
1574 return( ret );
1575#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001576 }
1577 else
1578 {
1579 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001580 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001581 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001582 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001583 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001584
1585 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1586 {
1587 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1588 return( ret );
1589 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001590 }
1591
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001592 /*
1593 * 38 . 38 session id length
1594 * 39 . 38+n session id
1595 * 39+n . 40+n chosen ciphersuite
1596 * 41+n . 41+n chosen compression alg.
1597 * 42+n . 43+n extensions length
1598 * 44+n . 43+n+m extensions
1599 */
1600 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001601 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1602 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001603
1604 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1605 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1606 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001607 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001608
Paul Bakker48916f92012-09-16 19:57:18 +00001609 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1610 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1611 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001612
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001613 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: 0x%04X",
Paul Bakker48916f92012-09-16 19:57:18 +00001614 ssl->session_negotiate->ciphersuite ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001615 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001616 ssl->session_negotiate->compression ) );
1617
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001618 /*
1619 * First write extensions, then the total length
1620 */
1621 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1622 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001623
Paul Bakker05decb22013-08-15 13:33:48 +02001624#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001625 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1626 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001627#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001628
Paul Bakker1f2bc622013-08-15 13:45:55 +02001629#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001630 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1631 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001632#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001633
Paul Bakkera503a632013-08-14 13:48:06 +02001634#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001635 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1636 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001637#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001638
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001639#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001640 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1641 ext_len += olen;
1642#endif
1643
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001644 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001645
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001646 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1647 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1648 p += ext_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001649
1650 ssl->out_msglen = p - buf;
1651 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1652 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1653
1654 ret = ssl_write_record( ssl );
1655
1656 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1657
1658 return( ret );
1659}
1660
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001661#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1662 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
1663 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001664static int ssl_write_certificate_request( ssl_context *ssl )
1665{
Paul Bakkered27a042013-04-18 22:46:23 +02001666 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1667 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001668
1669 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1670
1671 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1672 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1673 {
1674 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1675 ssl->state++;
1676 return( 0 );
1677 }
1678
1679 return( ret );
1680}
1681#else
1682static int ssl_write_certificate_request( ssl_context *ssl )
1683{
1684 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1685 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001686 size_t dn_size, total_dn_size; /* excluding length bytes */
1687 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00001688 unsigned char *buf, *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001689 const x509_cert *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001690
1691 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1692
1693 ssl->state++;
1694
Paul Bakkerfbb17802013-04-17 19:10:21 +02001695 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001696 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001697 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001698 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001699 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001700 return( 0 );
1701 }
1702
1703 /*
1704 * 0 . 0 handshake type
1705 * 1 . 3 handshake length
1706 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01001707 * 5 .. m-1 cert types
1708 * m .. m+1 sig alg length (TLS 1.2 only)
1709 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001710 * n .. n+1 length of all DNs
1711 * n+2 .. n+3 length of DN 1
1712 * n+4 .. ... Distinguished Name #1
1713 * ... .. ... length of DN 2, etc.
1714 */
1715 buf = ssl->out_msg;
1716 p = buf + 4;
1717
1718 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001719 * Supported certificate types
1720 *
1721 * ClientCertificateType certificate_types<1..2^8-1>;
1722 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00001723 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001724 ct_len = 0;
1725
1726#if defined(POLARSSL_RSA_C)
1727 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
1728#endif
1729#if defined(POLARSSL_ECDSA_C)
1730 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
1731#endif
1732
1733 p[0] = ct_len++;
1734 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01001735
1736 /*
1737 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01001738 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001739 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
1740 *
1741 * struct {
1742 * HashAlgorithm hash;
1743 * SignatureAlgorithm signature;
1744 * } SignatureAndHashAlgorithm;
1745 *
1746 * enum { (255) } HashAlgorithm;
1747 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01001748 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001749 sa_len = 0;
Paul Bakker21dca692013-01-03 11:41:08 +01001750 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01001751 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001752 /*
1753 * Only use current running hash algorithm that is already required
1754 * for requested ciphersuite.
1755 */
Paul Bakker926af752012-11-23 13:38:07 +01001756 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
1757
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001758 if( ssl->transform_negotiate->ciphersuite_info->mac ==
1759 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01001760 {
1761 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
1762 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02001763
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001764 /*
1765 * Supported signature algorithms
1766 */
1767#if defined(POLARSSL_RSA_C)
1768 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1769 p[2 + sa_len++] = SSL_SIG_RSA;
1770#endif
1771#if defined(POLARSSL_ECDSA_C)
1772 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1773 p[2 + sa_len++] = SSL_SIG_ECDSA;
1774#endif
Paul Bakker926af752012-11-23 13:38:07 +01001775
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001776 p[0] = (unsigned char)( sa_len >> 8 );
1777 p[1] = (unsigned char)( sa_len );
1778 sa_len += 2;
1779 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01001780 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001781
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001782 /*
1783 * DistinguishedName certificate_authorities<0..2^16-1>;
1784 * opaque DistinguishedName<1..2^16-1>;
1785 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001786 p += 2;
1787 crt = ssl->ca_chain;
1788
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001789 total_dn_size = 0;
Paul Bakker29087132010-03-21 21:03:34 +00001790 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001791 {
1792 if( p - buf > 4096 )
1793 break;
1794
Paul Bakker926af752012-11-23 13:38:07 +01001795 dn_size = crt->subject_raw.len;
1796 *p++ = (unsigned char)( dn_size >> 8 );
1797 *p++ = (unsigned char)( dn_size );
1798 memcpy( p, crt->subject_raw.p, dn_size );
1799 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001800
Paul Bakker926af752012-11-23 13:38:07 +01001801 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
1802
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001803 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01001804 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00001805 }
1806
Paul Bakker926af752012-11-23 13:38:07 +01001807 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001808 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1809 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001810 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
1811 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001812
1813 ret = ssl_write_record( ssl );
1814
1815 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
1816
1817 return( ret );
1818}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001819#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
1820 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
1821 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001822
Paul Bakker41c83d32013-03-20 14:39:14 +01001823static int ssl_write_server_key_exchange( ssl_context *ssl )
1824{
Paul Bakker23986e52011-04-24 08:57:21 +00001825 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001826 size_t n = 0, len;
Paul Bakker23f36802012-09-28 14:15:14 +00001827 unsigned char hash[64];
Paul Bakkerc70b9822013-04-07 22:00:46 +02001828 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001829 unsigned int hashlen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001830 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001831 unsigned char *dig_signed = p;
1832 size_t dig_signed_len = 0;
Paul Bakker41c83d32013-03-20 14:39:14 +01001833
1834 const ssl_ciphersuite_t *ciphersuite_info;
1835 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001836
1837 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
1838
Paul Bakker41c83d32013-03-20 14:39:14 +01001839 if( ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_RSA &&
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001840 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_ECDHE_RSA &&
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001841 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA &&
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001842 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001843 {
1844 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
1845 ssl->state++;
1846 return( 0 );
1847 }
1848
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001849#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1850 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1851 {
1852 /* TODO: Support identity hints */
1853 *(p++) = 0x00;
1854 *(p++) = 0x00;
1855
1856 n += 2;
1857 }
1858#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1859
1860#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1861 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1862 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1863 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00001864 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001865 /*
1866 * Ephemeral DH parameters:
1867 *
1868 * struct {
1869 * opaque dh_p<1..2^16-1>;
1870 * opaque dh_g<1..2^16-1>;
1871 * opaque dh_Ys<1..2^16-1>;
1872 * } ServerDHParams;
1873 */
1874 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
1875 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
1876 {
1877 SSL_DEBUG_RET( 1, "mpi_copy", ret );
1878 return( ret );
1879 }
Paul Bakker48916f92012-09-16 19:57:18 +00001880
Paul Bakker41c83d32013-03-20 14:39:14 +01001881 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
1882 mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001883 p,
1884 &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001885 {
1886 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
1887 return( ret );
1888 }
1889
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001890 dig_signed = p;
1891 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001892
1893 p += len;
1894 n += len;
1895
Paul Bakker41c83d32013-03-20 14:39:14 +01001896 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
1897 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1898 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1899 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
1900 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001901#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1902 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001903
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001904#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1905 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1906 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1907 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001908 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001909 /*
1910 * Ephemeral ECDH parameters:
1911 *
1912 * struct {
1913 * ECParameters curve_params;
1914 * ECPoint public;
1915 * } ServerECDHParams;
1916 */
Paul Bakker41c83d32013-03-20 14:39:14 +01001917 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
1918 ssl->handshake->ec_curve ) ) != 0 )
1919 {
1920 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
1921 return( ret );
1922 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001923
Paul Bakker41c83d32013-03-20 14:39:14 +01001924 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001925 &len,
1926 p,
Paul Bakker41c83d32013-03-20 14:39:14 +01001927 1000, ssl->f_rng, ssl->p_rng ) ) != 0 )
1928 {
1929 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
1930 return( ret );
1931 }
1932
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001933 dig_signed = p;
1934 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001935
1936 p += len;
1937 n += len;
1938
Paul Bakker41c83d32013-03-20 14:39:14 +01001939 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
1940 }
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001941#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1942 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001943
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001944#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001945 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1946 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001947 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001948 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1949 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001950 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001951 size_t signature_len = 0;
Paul Bakker23f36802012-09-28 14:15:14 +00001952
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001953 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001954 * Choose hash algorithm. NONE means MD5 + SHA1 here.
1955 */
1956 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
1957 {
1958 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
1959
1960 if( md_alg == POLARSSL_MD_NONE )
1961 {
1962 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1963 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1964 }
1965 }
1966 else if ( ciphersuite_info->key_exchange ==
1967 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
1968 {
1969 md_alg = POLARSSL_MD_SHA1;
1970 }
1971 else
1972 {
1973 md_alg = POLARSSL_MD_NONE;
1974 }
1975
1976
1977 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001978 * Compute the hash to be signed
1979 */
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001980 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00001981 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001982 md5_context md5;
1983 sha1_context sha1;
1984
1985 /*
1986 * digitally-signed struct {
1987 * opaque md5_hash[16];
1988 * opaque sha_hash[20];
1989 * };
1990 *
1991 * md5_hash
1992 * MD5(ClientHello.random + ServerHello.random
1993 * + ServerParams);
1994 * sha_hash
1995 * SHA(ClientHello.random + ServerHello.random
1996 * + ServerParams);
1997 */
1998 md5_starts( &md5 );
1999 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002000 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002001 md5_finish( &md5, hash );
2002
2003 sha1_starts( &sha1 );
2004 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002005 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002006 sha1_finish( &sha1, hash + 16 );
2007
2008 hashlen = 36;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002009 }
2010 else
2011 {
2012 md_context_t ctx;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002013
2014 /* Info from md_alg will be used instead */
2015 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002016
2017 /*
2018 * digitally-signed struct {
2019 * opaque client_random[32];
2020 * opaque server_random[32];
2021 * ServerDHParams params;
2022 * };
2023 */
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002024 if( ( ret = md_init_ctx( &ctx, md_info_from_type(md_alg) ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002025 {
2026 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2027 return( ret );
2028 }
2029
2030 md_starts( &ctx );
2031 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002032 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002033 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02002034
2035 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
2036 {
2037 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
2038 return( ret );
2039 }
2040
Paul Bakker23f36802012-09-28 14:15:14 +00002041 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002042
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002043 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
2044
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002045 /*
2046 * Make the signature
2047 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02002048 if( ssl->pk_key == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002049 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002050 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2051 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002052 }
2053
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002054 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002055 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002056 *(p++) = ssl->handshake->sig_alg;
2057 *(p++) = ssl_sig_from_pk( ssl->pk_key );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002058
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002059 n += 2;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002060 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002061
2062 if( ( ret = pk_sign( ssl->pk_key, md_alg, hash, hashlen,
2063 p + 2 , &signature_len,
2064 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002065 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002066 SSL_DEBUG_RET( 1, "pk_sign", ret );
2067 return( ret );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002068 }
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002069
2070 *(p++) = (unsigned char)( signature_len >> 8 );
2071 *(p++) = (unsigned char)( signature_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002072 n += 2;
2073
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002074 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002075
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002076 p += signature_len;
2077 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002078 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002079#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002080 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2081 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002082
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002083 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002084 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2085 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2086
2087 ssl->state++;
2088
2089 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2090 {
2091 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2092 return( ret );
2093 }
2094
2095 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2096
2097 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002098}
2099
2100static int ssl_write_server_hello_done( ssl_context *ssl )
2101{
2102 int ret;
2103
2104 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2105
2106 ssl->out_msglen = 4;
2107 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2108 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2109
2110 ssl->state++;
2111
2112 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2113 {
2114 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2115 return( ret );
2116 }
2117
2118 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2119
2120 return( 0 );
2121}
2122
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002123#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2124 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2125static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2126 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002127{
2128 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002129 size_t n;
2130
2131 /*
2132 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2133 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002134 if( *p + 2 > end )
2135 {
2136 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2137 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2138 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002139
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002140 n = ( (*p)[0] << 8 ) | (*p)[1];
2141 *p += 2;
2142
2143 if( n < 1 || n > ssl->handshake->dhm_ctx.len || *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002144 {
2145 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2146 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2147 }
2148
2149 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002150 *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002151 {
2152 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2153 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2154 }
2155
2156 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2157
Paul Bakker70df2fb2013-04-17 17:19:09 +02002158 return( ret );
2159}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002160#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2161 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002162
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02002163#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2164 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002165static int ssl_parse_client_ecdh_public( ssl_context *ssl )
2166{
2167 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002168 size_t n;
2169
2170 /*
2171 * Receive client public key and calculate premaster
2172 */
2173 n = ssl->in_msg[3];
2174
2175 if( n < 1 || n > mpi_size( &ssl->handshake->ecdh_ctx.grp.P ) * 2 + 2 ||
2176 n + 4 != ssl->in_hslen )
2177 {
2178 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2179 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2180 }
2181
2182 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2183 ssl->in_msg + 4, n ) ) != 0 )
2184 {
2185 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2186 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2187 }
2188
2189 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2190
Paul Bakker70df2fb2013-04-17 17:19:09 +02002191 return( ret );
2192}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02002193#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2194 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002195
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002196#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002197static int ssl_parse_encrypted_pms_secret( ssl_context *ssl )
2198{
2199 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2200 size_t i, n = 0;
2201
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002202 if( ! pk_can_do( ssl->pk_key, POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002203 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002204 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002205 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2206 }
2207
2208 /*
2209 * Decrypt the premaster using own private RSA key
2210 */
2211 i = 4;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002212 n = pk_get_len( ssl->pk_key );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002213 ssl->handshake->pmslen = 48;
2214
2215 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2216 {
2217 i += 2;
2218 if( ssl->in_msg[4] != ( ( n >> 8 ) & 0xFF ) ||
2219 ssl->in_msg[5] != ( ( n ) & 0xFF ) )
2220 {
2221 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2222 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2223 }
2224 }
2225
2226 if( ssl->in_hslen != i + n )
2227 {
2228 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2229 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2230 }
2231
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002232 ret = pk_decrypt( ssl->pk_key,
2233 ssl->in_msg + i, n,
2234 ssl->handshake->premaster, &ssl->handshake->pmslen,
2235 sizeof(ssl->handshake->premaster),
2236 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002237
2238 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Paul Bakker2fbefde2013-06-29 16:01:15 +02002239 ssl->handshake->premaster[0] != ssl->handshake->max_major_ver ||
2240 ssl->handshake->premaster[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002241 {
2242 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2243
2244 /*
2245 * Protection against Bleichenbacher's attack:
2246 * invalid PKCS#1 v1.5 padding must not cause
2247 * the connection to end immediately; instead,
2248 * send a bad_record_mac later in the handshake.
2249 */
2250 ssl->handshake->pmslen = 48;
2251
2252 ret = ssl->f_rng( ssl->p_rng, ssl->handshake->premaster,
2253 ssl->handshake->pmslen );
2254 if( ret != 0 )
2255 return( ret );
2256 }
2257
2258 return( ret );
2259}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002260#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002261
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002262#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2263 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2264static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2265 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002266{
2267 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002268 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002269
2270 if( ssl->psk == NULL || ssl->psk_identity == NULL ||
2271 ssl->psk_identity_len == 0 || ssl->psk_len == 0 )
2272 {
2273 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2274 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2275 }
2276
2277 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002278 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002279 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002280 if( *p + 2 > end )
2281 {
2282 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2283 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2284 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002285
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002286 n = ( (*p)[0] << 8 ) | (*p)[1];
2287 *p += 2;
2288
2289 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002290 {
2291 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2292 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2293 }
2294
2295 if( n != ssl->psk_identity_len ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002296 memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002297 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002298 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002299 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2300 }
2301
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002302 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002303 ret = 0;
2304
Paul Bakkerfbb17802013-04-17 19:10:21 +02002305 return( ret );
2306}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002307#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
2308 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002309
Paul Bakker5121ce52009-01-03 21:22:43 +00002310static int ssl_parse_client_key_exchange( ssl_context *ssl )
2311{
Paul Bakker23986e52011-04-24 08:57:21 +00002312 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002313 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002314 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002315
Paul Bakker41c83d32013-03-20 14:39:14 +01002316 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002317
2318 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2319
2320 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2321 {
2322 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2323 return( ret );
2324 }
2325
2326 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2327 {
2328 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002329 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002330 }
2331
2332 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2333 {
2334 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002335 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002336 }
2337
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002338 p = ssl->in_msg + 4;
2339 end = ssl->in_msg + ssl->in_msglen;
2340
2341#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002342 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002343 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002344 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002345 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002346 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2347 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002348 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002349
2350 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
2351
2352 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2353 ssl->handshake->premaster,
2354 &ssl->handshake->pmslen ) ) != 0 )
2355 {
2356 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2357 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2358 }
2359
2360 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002361 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002362 else
2363#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002364#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2365 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2366 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2367 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002368 {
2369 if( ( ret = ssl_parse_client_ecdh_public( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002370 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002371 SSL_DEBUG_RET( 1, ( "ssl_parse_client_ecdh_public" ), ret );
2372 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002373 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002374
2375 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2376 &ssl->handshake->pmslen,
2377 ssl->handshake->premaster,
2378 POLARSSL_MPI_MAX_SIZE ) ) != 0 )
2379 {
2380 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2381 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2382 }
2383
2384 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002385 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002386 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002387#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2388 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002389#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2390 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002391 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002392 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002393 {
2394 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2395 return( ret );
2396 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002397
2398 // Set up the premaster secret
2399 //
2400 p = ssl->handshake->premaster;
2401 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2402 *(p++) = (unsigned char)( ssl->psk_len );
2403 p += ssl->psk_len;
2404
2405 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2406 *(p++) = (unsigned char)( ssl->psk_len );
2407 memcpy( p, ssl->psk, ssl->psk_len );
2408 p += ssl->psk_len;
2409
2410 ssl->handshake->pmslen = 4 + 2 * ssl->psk_len;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002411 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002412 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002413#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
2414#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2415 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2416 {
2417 size_t n;
2418
2419 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2420 {
2421 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2422 return( ret );
2423 }
2424 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2425 {
2426 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2427 return( ret );
2428 }
2429
2430 // Set up the premaster secret
2431 //
2432 p = ssl->handshake->premaster;
2433 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len >> 8 );
2434 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len );
2435
2436 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2437 p, &n ) ) != 0 )
2438 {
2439 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2440 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2441 }
2442
2443 if( n != ssl->handshake->dhm_ctx.len )
2444 {
2445 SSL_DEBUG_MSG( 1, ( "dhm_calc_secret result smaller than DHM" ) );
2446 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2447 }
2448
2449 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2450
2451 p += ssl->handshake->dhm_ctx.len;
2452
2453 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2454 *(p++) = (unsigned char)( ssl->psk_len );
2455 memcpy( p, ssl->psk, ssl->psk_len );
2456 p += ssl->psk_len;
2457
2458 ssl->handshake->pmslen = 4 + ssl->handshake->dhm_ctx.len + ssl->psk_len;
2459 }
2460 else
2461#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
2462#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2463 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002464 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002465 if( ( ret = ssl_parse_encrypted_pms_secret( ssl ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002466 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002467 SSL_DEBUG_RET( 1, ( "ssl_parse_client_ecdh_public" ), ret );
2468 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002469 }
2470 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002471 else
2472#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2473 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002474 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002475 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2476 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002477
Paul Bakkerff60ee62010-03-16 21:09:09 +00002478 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2479 {
2480 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2481 return( ret );
2482 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002483
Paul Bakker5121ce52009-01-03 21:22:43 +00002484 ssl->state++;
2485
2486 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2487
2488 return( 0 );
2489}
2490
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002491#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2492 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2493 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002494static int ssl_parse_certificate_verify( ssl_context *ssl )
2495{
Paul Bakkered27a042013-04-18 22:46:23 +02002496 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002497 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002498
2499 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2500
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002501 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2502 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002503 {
2504 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2505 ssl->state++;
2506 return( 0 );
2507 }
2508
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002509 return( ret );
2510}
2511#else
2512static int ssl_parse_certificate_verify( ssl_context *ssl )
2513{
2514 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002515 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002516 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002517 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002518 size_t hashlen;
2519 pk_type_t pk_alg;
2520 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002521 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2522
2523 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2524
2525 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2526 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2527 {
2528 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2529 ssl->state++;
2530 return( 0 );
2531 }
2532
Paul Bakkered27a042013-04-18 22:46:23 +02002533 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002534 {
2535 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2536 ssl->state++;
2537 return( 0 );
2538 }
2539
Paul Bakker48916f92012-09-16 19:57:18 +00002540 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002541
2542 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2543 {
2544 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2545 return( ret );
2546 }
2547
2548 ssl->state++;
2549
2550 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2551 {
2552 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002553 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002554 }
2555
2556 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
2557 {
2558 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002559 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002560 }
2561
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002562 /*
2563 * 0 . 0 handshake type
2564 * 1 . 3 handshake length
2565 * 4 . 5 sig alg (TLS 1.2 only)
2566 * 4+n . 5+n signature length (n = sa_len)
2567 * 6+n . 6+n+m signature (m = sig_len)
2568 */
2569
2570 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002571 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002572 sa_len = 0;
2573
2574 md_alg = POLARSSL_MD_NONE;
2575 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002576
2577 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
2578 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
2579 POLARSSL_PK_ECDSA ) )
2580 {
2581 hash_start += 16;
2582 hashlen -= 16;
2583 md_alg = POLARSSL_MD_SHA1;
2584 }
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002585 }
2586 else
2587 {
2588 sa_len = 2;
2589
Paul Bakker926af752012-11-23 13:38:07 +01002590 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002591 * Hash
Paul Bakker926af752012-11-23 13:38:07 +01002592 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002593 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker926af752012-11-23 13:38:07 +01002594 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002595 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2596 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01002597 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2598 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002599
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002600 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01002601
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002602 /* Info from md_alg will be used instead */
2603 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002604
2605 /*
2606 * Signature
2607 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002608 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
2609 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002610 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002611 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2612 " for verify message" ) );
2613 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002614 }
2615
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002616 /*
2617 * Check the certificate's key type matches the signature alg
2618 */
2619 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
2620 {
2621 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
2622 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2623 }
2624
Paul Bakker926af752012-11-23 13:38:07 +01002625 }
2626
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002627 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002628
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002629 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002630 {
2631 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002632 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002633 }
2634
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002635 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002636 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002637 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002638 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002639 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002640 return( ret );
2641 }
2642
2643 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
2644
Paul Bakkered27a042013-04-18 22:46:23 +02002645 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002646}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002647#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2648 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2649 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002650
Paul Bakkera503a632013-08-14 13:48:06 +02002651#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002652static int ssl_write_new_session_ticket( ssl_context *ssl )
2653{
2654 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002655 size_t tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002656
2657 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
2658
2659 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2660 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
2661
2662 /*
2663 * struct {
2664 * uint32 ticket_lifetime_hint;
2665 * opaque ticket<0..2^16-1>;
2666 * } NewSessionTicket;
2667 *
2668 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
2669 * 8 . 9 ticket_len (n)
2670 * 10 . 9+n ticket content
2671 */
2672 ssl->out_msg[4] = 0x00;
2673 ssl->out_msg[5] = 0x00;
2674 ssl->out_msg[6] = 0x00;
2675 ssl->out_msg[7] = 0x00;
2676
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02002677 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
2678 {
2679 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
2680 tlen = 0;
2681 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002682
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002683 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
2684 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002685
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002686 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002687
2688 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2689 {
2690 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2691 return( ret );
2692 }
2693
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002694 /* No need to remember writing a NewSessionTicket any more */
2695 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002696
2697 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
2698
2699 return( 0 );
2700}
Paul Bakkera503a632013-08-14 13:48:06 +02002701#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002702
Paul Bakker5121ce52009-01-03 21:22:43 +00002703/*
Paul Bakker1961b702013-01-25 14:49:24 +01002704 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002705 */
Paul Bakker1961b702013-01-25 14:49:24 +01002706int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002707{
2708 int ret = 0;
2709
Paul Bakker1961b702013-01-25 14:49:24 +01002710 if( ssl->state == SSL_HANDSHAKE_OVER )
2711 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002712
Paul Bakker1961b702013-01-25 14:49:24 +01002713 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
2714
2715 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2716 return( ret );
2717
2718 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002719 {
Paul Bakker1961b702013-01-25 14:49:24 +01002720 case SSL_HELLO_REQUEST:
2721 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002722 break;
2723
Paul Bakker1961b702013-01-25 14:49:24 +01002724 /*
2725 * <== ClientHello
2726 */
2727 case SSL_CLIENT_HELLO:
2728 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002729 break;
Paul Bakker1961b702013-01-25 14:49:24 +01002730
2731 /*
2732 * ==> ServerHello
2733 * Certificate
2734 * ( ServerKeyExchange )
2735 * ( CertificateRequest )
2736 * ServerHelloDone
2737 */
2738 case SSL_SERVER_HELLO:
2739 ret = ssl_write_server_hello( ssl );
2740 break;
2741
2742 case SSL_SERVER_CERTIFICATE:
2743 ret = ssl_write_certificate( ssl );
2744 break;
2745
2746 case SSL_SERVER_KEY_EXCHANGE:
2747 ret = ssl_write_server_key_exchange( ssl );
2748 break;
2749
2750 case SSL_CERTIFICATE_REQUEST:
2751 ret = ssl_write_certificate_request( ssl );
2752 break;
2753
2754 case SSL_SERVER_HELLO_DONE:
2755 ret = ssl_write_server_hello_done( ssl );
2756 break;
2757
2758 /*
2759 * <== ( Certificate/Alert )
2760 * ClientKeyExchange
2761 * ( CertificateVerify )
2762 * ChangeCipherSpec
2763 * Finished
2764 */
2765 case SSL_CLIENT_CERTIFICATE:
2766 ret = ssl_parse_certificate( ssl );
2767 break;
2768
2769 case SSL_CLIENT_KEY_EXCHANGE:
2770 ret = ssl_parse_client_key_exchange( ssl );
2771 break;
2772
2773 case SSL_CERTIFICATE_VERIFY:
2774 ret = ssl_parse_certificate_verify( ssl );
2775 break;
2776
2777 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2778 ret = ssl_parse_change_cipher_spec( ssl );
2779 break;
2780
2781 case SSL_CLIENT_FINISHED:
2782 ret = ssl_parse_finished( ssl );
2783 break;
2784
2785 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002786 * ==> ( NewSessionTicket )
2787 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002788 * Finished
2789 */
2790 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002791#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002792 if( ssl->handshake->new_session_ticket != 0 )
2793 ret = ssl_write_new_session_ticket( ssl );
2794 else
Paul Bakkera503a632013-08-14 13:48:06 +02002795#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002796 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002797 break;
2798
2799 case SSL_SERVER_FINISHED:
2800 ret = ssl_write_finished( ssl );
2801 break;
2802
2803 case SSL_FLUSH_BUFFERS:
2804 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2805 ssl->state = SSL_HANDSHAKE_WRAPUP;
2806 break;
2807
2808 case SSL_HANDSHAKE_WRAPUP:
2809 ssl_handshake_wrapup( ssl );
2810 break;
2811
2812 default:
2813 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2814 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002815 }
2816
Paul Bakker5121ce52009-01-03 21:22:43 +00002817 return( ret );
2818}
Paul Bakker5121ce52009-01-03 21:22:43 +00002819#endif