blob: ffd754e36cdc13dec9a6e0cbd80b3435fd6e9754 [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 {
445 if( p[1] != SSL_SIG_RSA )
Paul Bakker8611e732012-10-30 07:52:29 +0000446 {
447 sig_alg_list_size -= 2;
448 p += 2;
Paul Bakker23f36802012-09-28 14:15:14 +0000449 continue;
Paul Bakker8611e732012-10-30 07:52:29 +0000450 }
Paul Bakker9e36f042013-06-30 14:34:05 +0200451#if defined(POLARSSL_SHA512_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000452 if( p[0] == SSL_HASH_SHA512 )
453 {
454 ssl->handshake->sig_alg = SSL_HASH_SHA512;
455 break;
456 }
457 if( p[0] == SSL_HASH_SHA384 )
458 {
459 ssl->handshake->sig_alg = SSL_HASH_SHA384;
460 break;
461 }
462#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200463#if defined(POLARSSL_SHA256_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000464 if( p[0] == SSL_HASH_SHA256 )
465 {
466 ssl->handshake->sig_alg = SSL_HASH_SHA256;
467 break;
468 }
469 if( p[0] == SSL_HASH_SHA224 )
470 {
471 ssl->handshake->sig_alg = SSL_HASH_SHA224;
472 break;
473 }
474#endif
475 if( p[0] == SSL_HASH_SHA1 )
476 {
477 ssl->handshake->sig_alg = SSL_HASH_SHA1;
478 break;
479 }
480 if( p[0] == SSL_HASH_MD5 )
481 {
482 ssl->handshake->sig_alg = SSL_HASH_MD5;
483 break;
484 }
485
486 sig_alg_list_size -= 2;
487 p += 2;
488 }
489
490 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
491 ssl->handshake->sig_alg ) );
492
493 return( 0 );
494}
495
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200496#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200497static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
498 const unsigned char *buf,
499 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100500{
501 size_t list_size;
502 const unsigned char *p;
503
504 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
505 if( list_size + 2 != len ||
506 list_size % 2 != 0 )
507 {
508 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
509 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
510 }
511
512 p = buf + 2;
513 while( list_size > 0 )
514 {
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200515#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
516 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP192R1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100517 {
518 ssl->handshake->ec_curve = p[1];
519 return( 0 );
520 }
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200521#endif
522#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
523 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP224R1 )
524 {
525 ssl->handshake->ec_curve = p[1];
526 return( 0 );
527 }
528#endif
529#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
530 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP256R1 )
531 {
532 ssl->handshake->ec_curve = p[1];
533 return( 0 );
534 }
535#endif
536#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
537 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP384R1 )
538 {
539 ssl->handshake->ec_curve = p[1];
540 return( 0 );
541 }
542#endif
543#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
544 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP521R1 )
545 {
546 ssl->handshake->ec_curve = p[1];
547 return( 0 );
548 }
549#endif
Paul Bakker41c83d32013-03-20 14:39:14 +0100550
551 list_size -= 2;
552 p += 2;
553 }
554
555 return( 0 );
556}
557
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200558static int ssl_parse_supported_point_formats( ssl_context *ssl,
559 const unsigned char *buf,
560 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100561{
562 size_t list_size;
563 const unsigned char *p;
564
565 list_size = buf[0];
566 if( list_size + 1 != len )
567 {
568 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
569 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
570 }
571
572 p = buf + 2;
573 while( list_size > 0 )
574 {
575 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
576 p[0] == POLARSSL_ECP_PF_COMPRESSED )
577 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200578 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200579 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100580 return( 0 );
581 }
582
583 list_size--;
584 p++;
585 }
586
587 return( 0 );
588}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200589#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100590
Paul Bakker05decb22013-08-15 13:33:48 +0200591#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200592static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
593 const unsigned char *buf,
594 size_t len )
595{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200596 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200597 {
598 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
599 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
600 }
601
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200602 ssl->session_negotiate->mfl_code = buf[0];
603
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200604 return( 0 );
605}
Paul Bakker05decb22013-08-15 13:33:48 +0200606#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200607
Paul Bakker1f2bc622013-08-15 13:45:55 +0200608#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200609static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
610 const unsigned char *buf,
611 size_t len )
612{
613 if( len != 0 )
614 {
615 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
616 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
617 }
618
619 ((void) buf);
620
621 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
622
623 return( 0 );
624}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200625#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200626
Paul Bakkera503a632013-08-14 13:48:06 +0200627#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200628static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200629 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200630 size_t len )
631{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200632 int ret;
633
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200634 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
635 return( 0 );
636
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200637 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200638 ssl->handshake->new_session_ticket = 1;
639
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200640 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
641
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200642 if( len == 0 )
643 return( 0 );
644
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200645 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
646 {
647 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
648 return( 0 );
649 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200650
651 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200652 * Failures are ok: just ignore the ticket and proceed.
653 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200654 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
655 {
656 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200657 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200658 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200659
660 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
661
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200662 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200663
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200664 /* Don't send a new ticket after all, this one is OK */
665 ssl->handshake->new_session_ticket = 0;
666
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200667 return( 0 );
668}
Paul Bakkera503a632013-08-14 13:48:06 +0200669#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200670
Paul Bakker78a8c712013-03-06 17:01:52 +0100671#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
672static int ssl_parse_client_hello_v2( ssl_context *ssl )
673{
674 int ret;
675 unsigned int i, j;
676 size_t n;
677 unsigned int ciph_len, sess_len, chal_len;
678 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200679 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200680 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100681
682 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
683
684 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
685 {
686 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
687
688 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
689 return( ret );
690
691 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
692 }
693
694 buf = ssl->in_hdr;
695
696 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
697
698 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
699 buf[2] ) );
700 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
701 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
702 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
703 buf[3], buf[4] ) );
704
705 /*
706 * SSLv2 Client Hello
707 *
708 * Record layer:
709 * 0 . 1 message length
710 *
711 * SSL layer:
712 * 2 . 2 message type
713 * 3 . 4 protocol version
714 */
715 if( buf[2] != SSL_HS_CLIENT_HELLO ||
716 buf[3] != SSL_MAJOR_VERSION_3 )
717 {
718 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
719 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
720 }
721
722 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
723
724 if( n < 17 || n > 512 )
725 {
726 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
727 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
728 }
729
730 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200731 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
732 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100733
734 if( ssl->minor_ver < ssl->min_minor_ver )
735 {
736 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
737 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
738 ssl->min_major_ver, ssl->min_minor_ver ) );
739
740 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
741 SSL_ALERT_MSG_PROTOCOL_VERSION );
742 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
743 }
744
Paul Bakker2fbefde2013-06-29 16:01:15 +0200745 ssl->handshake->max_major_ver = buf[3];
746 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100747
748 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
749 {
750 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
751 return( ret );
752 }
753
754 ssl->handshake->update_checksum( ssl, buf + 2, n );
755
756 buf = ssl->in_msg;
757 n = ssl->in_left - 5;
758
759 /*
760 * 0 . 1 ciphersuitelist length
761 * 2 . 3 session id length
762 * 4 . 5 challenge length
763 * 6 . .. ciphersuitelist
764 * .. . .. session id
765 * .. . .. challenge
766 */
767 SSL_DEBUG_BUF( 4, "record contents", buf, n );
768
769 ciph_len = ( buf[0] << 8 ) | buf[1];
770 sess_len = ( buf[2] << 8 ) | buf[3];
771 chal_len = ( buf[4] << 8 ) | buf[5];
772
773 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
774 ciph_len, sess_len, chal_len ) );
775
776 /*
777 * Make sure each parameter length is valid
778 */
779 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
780 {
781 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
782 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
783 }
784
785 if( sess_len > 32 )
786 {
787 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
788 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
789 }
790
791 if( chal_len < 8 || chal_len > 32 )
792 {
793 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
794 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
795 }
796
797 if( n != 6 + ciph_len + sess_len + chal_len )
798 {
799 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
800 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
801 }
802
803 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
804 buf + 6, ciph_len );
805 SSL_DEBUG_BUF( 3, "client hello, session id",
806 buf + 6 + ciph_len, sess_len );
807 SSL_DEBUG_BUF( 3, "client hello, challenge",
808 buf + 6 + ciph_len + sess_len, chal_len );
809
810 p = buf + 6 + ciph_len;
811 ssl->session_negotiate->length = sess_len;
812 memset( ssl->session_negotiate->id, 0, sizeof( ssl->session_negotiate->id ) );
813 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
814
815 p += sess_len;
816 memset( ssl->handshake->randbytes, 0, 64 );
817 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
818
819 /*
820 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
821 */
822 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
823 {
824 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
825 {
826 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
827 if( ssl->renegotiation == SSL_RENEGOTIATION )
828 {
829 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
830
831 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
832 return( ret );
833
834 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
835 }
836 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
837 break;
838 }
839 }
840
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200841 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
842 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +0100843 {
844 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
845 {
Paul Bakker41c83d32013-03-20 14:39:14 +0100846 // Only allow non-ECC ciphersuites as we do not have extensions
847 //
Paul Bakker59c28a22013-06-29 15:33:42 +0200848 if( p[0] == 0 && p[1] == 0 &&
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200849 ( ( ciphersuites[i] >> 8 ) & 0xFF ) == 0 &&
850 p[2] == ( ciphersuites[i] & 0xFF ) )
Paul Bakker59c28a22013-06-29 15:33:42 +0200851 {
852 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
853
854 if( ciphersuite_info == NULL )
855 {
856 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %02x not found",
857 ciphersuites[i] ) );
858 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
859 }
860
Paul Bakker2fbefde2013-06-29 16:01:15 +0200861 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
862 ciphersuite_info->max_minor_ver < ssl->minor_ver )
863 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +0200864
Paul Bakker78a8c712013-03-06 17:01:52 +0100865 goto have_ciphersuite_v2;
Paul Bakker59c28a22013-06-29 15:33:42 +0200866 }
Paul Bakker78a8c712013-03-06 17:01:52 +0100867 }
868 }
869
870 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
871
872 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
873
874have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200875 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +0200876 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +0100877 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +0100878
879 /*
880 * SSLv2 Client Hello relevant renegotiation security checks
881 */
882 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
883 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
884 {
885 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
886
887 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
888 return( ret );
889
890 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
891 }
892
893 ssl->in_left = 0;
894 ssl->state++;
895
896 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
897
898 return( 0 );
899}
900#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
901
Paul Bakker5121ce52009-01-03 21:22:43 +0000902static int ssl_parse_client_hello( ssl_context *ssl )
903{
Paul Bakker23986e52011-04-24 08:57:21 +0000904 int ret;
905 unsigned int i, j;
906 size_t n;
907 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +0000908 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +0000909 unsigned int ext_len = 0;
910 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000911 int renegotiation_info_seen = 0;
912 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200913 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +0100914 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +0200915 pk_type_t pk_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +0000916
917 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
918
Paul Bakker48916f92012-09-16 19:57:18 +0000919 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
920 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000921 {
922 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
923 return( ret );
924 }
925
926 buf = ssl->in_hdr;
927
Paul Bakker78a8c712013-03-06 17:01:52 +0100928#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
929 if( ( buf[0] & 0x80 ) != 0 )
930 return ssl_parse_client_hello_v2( ssl );
931#endif
932
Paul Bakkerec636f32012-09-09 19:17:02 +0000933 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
934
935 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
936 buf[0] ) );
937 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
938 ( buf[3] << 8 ) | buf[4] ) );
939 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
940 buf[1], buf[2] ) );
941
942 /*
943 * SSLv3 Client Hello
944 *
945 * Record layer:
946 * 0 . 0 message type
947 * 1 . 2 protocol version
948 * 3 . 4 message length
949 */
950 if( buf[0] != SSL_MSG_HANDSHAKE ||
951 buf[1] != SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000952 {
Paul Bakkerec636f32012-09-09 19:17:02 +0000953 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
954 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
955 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000956
Paul Bakkerec636f32012-09-09 19:17:02 +0000957 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000958
Manuel Pégourié-Gonnard72882b22013-08-02 13:36:00 +0200959 if( n < 45 || n > 2048 )
Paul Bakkerec636f32012-09-09 19:17:02 +0000960 {
961 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
962 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
963 }
964
Paul Bakker48916f92012-09-16 19:57:18 +0000965 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
966 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +0000967 {
968 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
969 return( ret );
970 }
971
972 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +0000973 if( !ssl->renegotiation )
974 n = ssl->in_left - 5;
975 else
976 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +0000977
Paul Bakker48916f92012-09-16 19:57:18 +0000978 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +0000979
980 /*
981 * SSL layer:
982 * 0 . 0 handshake type
983 * 1 . 3 handshake length
984 * 4 . 5 protocol version
985 * 6 . 9 UNIX time()
986 * 10 . 37 random bytes
987 * 38 . 38 session id length
988 * 39 . 38+x session id
989 * 39+x . 40+x ciphersuitelist length
990 * 41+x . .. ciphersuitelist
991 * .. . .. compression alg.
992 * .. . .. extensions
993 */
994 SSL_DEBUG_BUF( 4, "record contents", buf, n );
995
996 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
997 buf[0] ) );
998 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
999 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1000 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1001 buf[4], buf[5] ) );
1002
1003 /*
1004 * Check the handshake type and protocol version
1005 */
1006 if( buf[0] != SSL_HS_CLIENT_HELLO ||
1007 buf[4] != SSL_MAJOR_VERSION_3 )
1008 {
1009 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1010 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1011 }
1012
1013 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +02001014 ssl->minor_ver = ( buf[5] <= ssl->max_minor_ver )
1015 ? buf[5] : ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001016
Paul Bakker1d29fb52012-09-28 13:28:45 +00001017 if( ssl->minor_ver < ssl->min_minor_ver )
1018 {
1019 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
1020 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001021 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001022
1023 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1024 SSL_ALERT_MSG_PROTOCOL_VERSION );
1025
1026 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1027 }
1028
Paul Bakker2fbefde2013-06-29 16:01:15 +02001029 ssl->handshake->max_major_ver = buf[4];
1030 ssl->handshake->max_minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001031
Paul Bakker48916f92012-09-16 19:57:18 +00001032 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001033
1034 /*
1035 * Check the handshake message length
1036 */
1037 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1038 {
1039 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1040 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1041 }
1042
1043 /*
1044 * Check the session length
1045 */
1046 sess_len = buf[38];
1047
1048 if( sess_len > 32 )
1049 {
1050 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1051 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1052 }
1053
Paul Bakker48916f92012-09-16 19:57:18 +00001054 ssl->session_negotiate->length = sess_len;
1055 memset( ssl->session_negotiate->id, 0,
1056 sizeof( ssl->session_negotiate->id ) );
1057 memcpy( ssl->session_negotiate->id, buf + 39,
1058 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001059
1060 /*
1061 * Check the ciphersuitelist length
1062 */
1063 ciph_len = ( buf[39 + sess_len] << 8 )
1064 | ( buf[40 + sess_len] );
1065
1066 if( ciph_len < 2 || ciph_len > 256 || ( ciph_len % 2 ) != 0 )
1067 {
1068 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1069 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1070 }
1071
1072 /*
1073 * Check the compression algorithms length
1074 */
1075 comp_len = buf[41 + sess_len + ciph_len];
1076
1077 if( comp_len < 1 || comp_len > 16 )
1078 {
1079 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1080 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1081 }
1082
Paul Bakker48916f92012-09-16 19:57:18 +00001083 /*
1084 * Check the extension length
1085 */
1086 if( n > 42 + sess_len + ciph_len + comp_len )
1087 {
1088 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1089 | ( buf[43 + sess_len + ciph_len + comp_len] );
1090
1091 if( ( ext_len > 0 && ext_len < 4 ) ||
1092 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1093 {
1094 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1095 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1096 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1097 }
1098 }
1099
1100 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001101#if defined(POLARSSL_ZLIB_SUPPORT)
1102 for( i = 0; i < comp_len; ++i )
1103 {
Paul Bakker48916f92012-09-16 19:57:18 +00001104 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001105 {
Paul Bakker48916f92012-09-16 19:57:18 +00001106 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001107 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001108 }
1109 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001110#endif
1111
Paul Bakkerec636f32012-09-09 19:17:02 +00001112 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1113 buf + 6, 32 );
1114 SSL_DEBUG_BUF( 3, "client hello, session id",
1115 buf + 38, sess_len );
1116 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1117 buf + 41 + sess_len, ciph_len );
1118 SSL_DEBUG_BUF( 3, "client hello, compression",
1119 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001120
Paul Bakkerec636f32012-09-09 19:17:02 +00001121 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001122 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1123 */
1124 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1125 {
1126 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1127 {
1128 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1129 if( ssl->renegotiation == SSL_RENEGOTIATION )
1130 {
1131 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001132
1133 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1134 return( ret );
1135
Paul Bakker48916f92012-09-16 19:57:18 +00001136 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1137 }
1138 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1139 break;
1140 }
1141 }
1142
Paul Bakker48916f92012-09-16 19:57:18 +00001143 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001144
1145 while( ext_len )
1146 {
1147 unsigned int ext_id = ( ( ext[0] << 8 )
1148 | ( ext[1] ) );
1149 unsigned int ext_size = ( ( ext[2] << 8 )
1150 | ( ext[3] ) );
1151
1152 if( ext_size + 4 > ext_len )
1153 {
1154 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1155 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1156 }
1157 switch( ext_id )
1158 {
Paul Bakker5701cdc2012-09-27 21:49:42 +00001159 case TLS_EXT_SERVERNAME:
1160 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1161 if( ssl->f_sni == NULL )
1162 break;
1163
1164 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1165 if( ret != 0 )
1166 return( ret );
1167 break;
1168
Paul Bakker48916f92012-09-16 19:57:18 +00001169 case TLS_EXT_RENEGOTIATION_INFO:
1170 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1171 renegotiation_info_seen = 1;
1172
Paul Bakker23f36802012-09-28 14:15:14 +00001173 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1174 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001175 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001176 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001177
Paul Bakker23f36802012-09-28 14:15:14 +00001178 case TLS_EXT_SIG_ALG:
1179 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1180 if( ssl->renegotiation == SSL_RENEGOTIATION )
1181 break;
1182
1183 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1184 if( ret != 0 )
1185 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001186 break;
1187
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001188#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001189 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1190 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1191
1192 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1193 if( ret != 0 )
1194 return( ret );
1195 break;
1196
1197 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1198 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1199
1200 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1201 if( ret != 0 )
1202 return( ret );
1203 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001204#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001205
Paul Bakker05decb22013-08-15 13:33:48 +02001206#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001207 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1208 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1209
1210 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1211 if( ret != 0 )
1212 return( ret );
1213 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001214#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001215
Paul Bakker1f2bc622013-08-15 13:45:55 +02001216#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001217 case TLS_EXT_TRUNCATED_HMAC:
1218 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1219
1220 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1221 if( ret != 0 )
1222 return( ret );
1223 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001224#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001225
Paul Bakkera503a632013-08-14 13:48:06 +02001226#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001227 case TLS_EXT_SESSION_TICKET:
1228 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1229
1230 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1231 if( ret != 0 )
1232 return( ret );
1233 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001234#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001235
Paul Bakker48916f92012-09-16 19:57:18 +00001236 default:
1237 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1238 ext_id ) );
1239 }
1240
1241 ext_len -= 4 + ext_size;
1242 ext += 4 + ext_size;
1243
1244 if( ext_len > 0 && ext_len < 4 )
1245 {
1246 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1247 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1248 }
1249 }
1250
1251 /*
1252 * Renegotiation security checks
1253 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001254 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1255 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1256 {
1257 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1258 handshake_failure = 1;
1259 }
1260 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1261 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1262 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001263 {
1264 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001265 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001266 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001267 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1268 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1269 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001270 {
1271 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001272 handshake_failure = 1;
1273 }
1274 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1275 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1276 renegotiation_info_seen == 1 )
1277 {
1278 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1279 handshake_failure = 1;
1280 }
1281
1282 if( handshake_failure == 1 )
1283 {
1284 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1285 return( ret );
1286
Paul Bakker48916f92012-09-16 19:57:18 +00001287 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1288 }
Paul Bakker380da532012-04-18 16:10:25 +00001289
Paul Bakker41c83d32013-03-20 14:39:14 +01001290 /*
1291 * Search for a matching ciphersuite
1292 * (At the end because we need information from the EC-based extensions)
1293 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001294 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
1295 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001296 {
1297 for( j = 0, p = buf + 41 + sess_len; j < ciph_len;
1298 j += 2, p += 2 )
1299 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001300 if( p[0] == ( ( ciphersuites[i] >> 8 ) & 0xFF ) &&
1301 p[1] == ( ( ciphersuites[i] ) & 0xFF ) )
Paul Bakker41c83d32013-03-20 14:39:14 +01001302 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001303 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
Paul Bakker41c83d32013-03-20 14:39:14 +01001304
1305 if( ciphersuite_info == NULL )
1306 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001307 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found",
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001308 ciphersuites[i] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001309 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1310 }
1311
Paul Bakker2fbefde2013-06-29 16:01:15 +02001312 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
1313 ciphersuite_info->max_minor_ver < ssl->minor_ver )
1314 continue;
1315
Paul Bakker5fd49172013-08-19 13:29:26 +02001316#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001317 if( ( ciphersuite_info->flags & POLARSSL_CIPHERSUITE_EC ) &&
1318 ssl->handshake->ec_curve == 0 )
1319 continue;
Paul Bakker5fd49172013-08-19 13:29:26 +02001320#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001321
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001322 /* If ciphersuite requires us to have a private key of a
1323 * certain type, make sure we do */
1324 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
1325 if( pk_alg != POLARSSL_PK_NONE &&
1326 ( ssl->pk_key == NULL ||
1327 ! pk_can_do( ssl->pk_key, pk_alg ) ) )
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001328 continue;
1329
Paul Bakker41c83d32013-03-20 14:39:14 +01001330 goto have_ciphersuite;
1331 }
1332 }
1333 }
1334
1335 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1336
1337 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1338 return( ret );
1339
1340 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1341
1342have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001343 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001344 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1345 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1346
Paul Bakker5121ce52009-01-03 21:22:43 +00001347 ssl->in_left = 0;
1348 ssl->state++;
1349
1350 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1351
1352 return( 0 );
1353}
1354
Paul Bakker1f2bc622013-08-15 13:45:55 +02001355#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001356static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1357 unsigned char *buf,
1358 size_t *olen )
1359{
1360 unsigned char *p = buf;
1361
1362 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1363 {
1364 *olen = 0;
1365 return;
1366 }
1367
1368 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1369
1370 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1371 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1372
1373 *p++ = 0x00;
1374 *p++ = 0x00;
1375
1376 *olen = 4;
1377}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001378#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001379
Paul Bakkera503a632013-08-14 13:48:06 +02001380#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001381static void ssl_write_session_ticket_ext( ssl_context *ssl,
1382 unsigned char *buf,
1383 size_t *olen )
1384{
1385 unsigned char *p = buf;
1386
1387 if( ssl->handshake->new_session_ticket == 0 )
1388 {
1389 *olen = 0;
1390 return;
1391 }
1392
1393 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1394
1395 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1396 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1397
1398 *p++ = 0x00;
1399 *p++ = 0x00;
1400
1401 *olen = 4;
1402}
Paul Bakkera503a632013-08-14 13:48:06 +02001403#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001404
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001405static void ssl_write_renegotiation_ext( ssl_context *ssl,
1406 unsigned char *buf,
1407 size_t *olen )
1408{
1409 unsigned char *p = buf;
1410
1411 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1412 {
1413 *olen = 0;
1414 return;
1415 }
1416
1417 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1418
1419 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1420 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1421
1422 *p++ = 0x00;
1423 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1424 *p++ = ssl->verify_data_len * 2 & 0xFF;
1425
1426 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1427 p += ssl->verify_data_len;
1428 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1429 p += ssl->verify_data_len;
1430
1431 *olen = 5 + ssl->verify_data_len * 2;
1432}
1433
Paul Bakker05decb22013-08-15 13:33:48 +02001434#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001435static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1436 unsigned char *buf,
1437 size_t *olen )
1438{
1439 unsigned char *p = buf;
1440
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001441 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1442 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001443 *olen = 0;
1444 return;
1445 }
1446
1447 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1448
1449 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1450 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1451
1452 *p++ = 0x00;
1453 *p++ = 1;
1454
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001455 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001456
1457 *olen = 5;
1458}
Paul Bakker05decb22013-08-15 13:33:48 +02001459#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001460
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001461#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001462static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1463 unsigned char *buf,
1464 size_t *olen )
1465{
1466 unsigned char *p = buf;
1467 ((void) ssl);
1468
1469 *olen = 0;
1470
1471 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1472
1473 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1474 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1475
1476 *p++ = 0x00;
1477 *p++ = 2;
1478
1479 *p++ = 1;
1480 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1481
1482 *olen = 6;
1483}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001484#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001485
Paul Bakker5121ce52009-01-03 21:22:43 +00001486static int ssl_write_server_hello( ssl_context *ssl )
1487{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001488#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001489 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001490#endif
Paul Bakkera3d195c2011-11-27 21:07:34 +00001491 int ret, n;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001492 size_t olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001493 unsigned char *buf, *p;
1494
1495 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1496
1497 /*
1498 * 0 . 0 handshake type
1499 * 1 . 3 handshake length
1500 * 4 . 5 protocol version
1501 * 6 . 9 UNIX time()
1502 * 10 . 37 random bytes
1503 */
1504 buf = ssl->out_msg;
1505 p = buf + 4;
1506
1507 *p++ = (unsigned char) ssl->major_ver;
1508 *p++ = (unsigned char) ssl->minor_ver;
1509
1510 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1511 buf[4], buf[5] ) );
1512
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001513#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001514 t = time( NULL );
1515 *p++ = (unsigned char)( t >> 24 );
1516 *p++ = (unsigned char)( t >> 16 );
1517 *p++ = (unsigned char)( t >> 8 );
1518 *p++ = (unsigned char)( t );
1519
1520 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001521#else
1522 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1523 return( ret );
1524
1525 p += 4;
1526#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001527
Paul Bakkera3d195c2011-11-27 21:07:34 +00001528 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1529 return( ret );
1530
1531 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001532
Paul Bakker48916f92012-09-16 19:57:18 +00001533 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001534
1535 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1536
1537 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001538 * Resume is 0 by default, see ssl_handshake_init().
1539 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1540 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001541 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001542 if( ssl->handshake->resume == 0 &&
1543 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001544 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001545 ssl->f_get_cache != NULL &&
1546 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1547 {
1548 ssl->handshake->resume = 1;
1549 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001550
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001551 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001552 {
1553 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001554 * New session, create a new session id,
1555 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001556 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001557 ssl->state++;
1558
Paul Bakkera503a632013-08-14 13:48:06 +02001559#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001560 if( ssl->handshake->new_session_ticket == 0 )
1561 {
1562 ssl->session_negotiate->length = n = 32;
1563 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001564 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001565 return( ret );
1566 }
1567 else
1568 {
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001569 ssl->session_negotiate->length = n = 0;
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001570 memset( ssl->session_negotiate->id, 0, 32 );
1571 }
Paul Bakkera503a632013-08-14 13:48:06 +02001572#else
1573 ssl->session_negotiate->length = n = 32;
1574 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
1575 n ) ) != 0 )
1576 return( ret );
1577#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001578 }
1579 else
1580 {
1581 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001582 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001583 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001584 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001585 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001586
1587 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1588 {
1589 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1590 return( ret );
1591 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001592 }
1593
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001594 /*
1595 * 38 . 38 session id length
1596 * 39 . 38+n session id
1597 * 39+n . 40+n chosen ciphersuite
1598 * 41+n . 41+n chosen compression alg.
1599 * 42+n . 43+n extensions length
1600 * 44+n . 43+n+m extensions
1601 */
1602 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001603 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1604 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001605
1606 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1607 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1608 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001609 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001610
Paul Bakker48916f92012-09-16 19:57:18 +00001611 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1612 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1613 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001614
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001615 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: 0x%04X",
Paul Bakker48916f92012-09-16 19:57:18 +00001616 ssl->session_negotiate->ciphersuite ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001617 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001618 ssl->session_negotiate->compression ) );
1619
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001620 /*
1621 * First write extensions, then the total length
1622 */
1623 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1624 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001625
Paul Bakker05decb22013-08-15 13:33:48 +02001626#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001627 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1628 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001629#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001630
Paul Bakker1f2bc622013-08-15 13:45:55 +02001631#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001632 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1633 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001634#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001635
Paul Bakkera503a632013-08-14 13:48:06 +02001636#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001637 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1638 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001639#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001640
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001641#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001642 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1643 ext_len += olen;
1644#endif
1645
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001646 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001647
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001648 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1649 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1650 p += ext_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001651
1652 ssl->out_msglen = p - buf;
1653 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1654 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1655
1656 ret = ssl_write_record( ssl );
1657
1658 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1659
1660 return( ret );
1661}
1662
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001663#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1664 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
1665 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001666static int ssl_write_certificate_request( ssl_context *ssl )
1667{
Paul Bakkered27a042013-04-18 22:46:23 +02001668 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1669 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001670
1671 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1672
1673 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1674 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1675 {
1676 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1677 ssl->state++;
1678 return( 0 );
1679 }
1680
1681 return( ret );
1682}
1683#else
1684static int ssl_write_certificate_request( ssl_context *ssl )
1685{
1686 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1687 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001688 size_t dn_size, total_dn_size; /* excluding length bytes */
1689 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00001690 unsigned char *buf, *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001691 const x509_cert *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001692
1693 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1694
1695 ssl->state++;
1696
Paul Bakkerfbb17802013-04-17 19:10:21 +02001697 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001698 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001699 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001700 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001701 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001702 return( 0 );
1703 }
1704
1705 /*
1706 * 0 . 0 handshake type
1707 * 1 . 3 handshake length
1708 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01001709 * 5 .. m-1 cert types
1710 * m .. m+1 sig alg length (TLS 1.2 only)
1711 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001712 * n .. n+1 length of all DNs
1713 * n+2 .. n+3 length of DN 1
1714 * n+4 .. ... Distinguished Name #1
1715 * ... .. ... length of DN 2, etc.
1716 */
1717 buf = ssl->out_msg;
1718 p = buf + 4;
1719
1720 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001721 * Supported certificate types
1722 *
1723 * ClientCertificateType certificate_types<1..2^8-1>;
1724 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00001725 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001726 ct_len = 0;
1727
1728#if defined(POLARSSL_RSA_C)
1729 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
1730#endif
1731#if defined(POLARSSL_ECDSA_C)
1732 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
1733#endif
1734
1735 p[0] = ct_len++;
1736 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01001737
1738 /*
1739 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01001740 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001741 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
1742 *
1743 * struct {
1744 * HashAlgorithm hash;
1745 * SignatureAlgorithm signature;
1746 * } SignatureAndHashAlgorithm;
1747 *
1748 * enum { (255) } HashAlgorithm;
1749 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01001750 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001751 sa_len = 0;
Paul Bakker21dca692013-01-03 11:41:08 +01001752 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01001753 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001754 /*
1755 * Only use current running hash algorithm that is already required
1756 * for requested ciphersuite.
1757 */
Paul Bakker926af752012-11-23 13:38:07 +01001758 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
1759
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001760 if( ssl->transform_negotiate->ciphersuite_info->mac ==
1761 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01001762 {
1763 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
1764 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02001765
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001766 /*
1767 * Supported signature algorithms
1768 */
1769#if defined(POLARSSL_RSA_C)
1770 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1771 p[2 + sa_len++] = SSL_SIG_RSA;
1772#endif
1773#if defined(POLARSSL_ECDSA_C)
1774 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1775 p[2 + sa_len++] = SSL_SIG_ECDSA;
1776#endif
Paul Bakker926af752012-11-23 13:38:07 +01001777
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001778 p[0] = (unsigned char)( sa_len >> 8 );
1779 p[1] = (unsigned char)( sa_len );
1780 sa_len += 2;
1781 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01001782 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001783
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001784 /*
1785 * DistinguishedName certificate_authorities<0..2^16-1>;
1786 * opaque DistinguishedName<1..2^16-1>;
1787 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001788 p += 2;
1789 crt = ssl->ca_chain;
1790
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001791 total_dn_size = 0;
Paul Bakker29087132010-03-21 21:03:34 +00001792 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001793 {
1794 if( p - buf > 4096 )
1795 break;
1796
Paul Bakker926af752012-11-23 13:38:07 +01001797 dn_size = crt->subject_raw.len;
1798 *p++ = (unsigned char)( dn_size >> 8 );
1799 *p++ = (unsigned char)( dn_size );
1800 memcpy( p, crt->subject_raw.p, dn_size );
1801 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001802
Paul Bakker926af752012-11-23 13:38:07 +01001803 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
1804
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001805 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01001806 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00001807 }
1808
Paul Bakker926af752012-11-23 13:38:07 +01001809 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001810 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1811 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001812 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
1813 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001814
1815 ret = ssl_write_record( ssl );
1816
1817 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
1818
1819 return( ret );
1820}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001821#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
1822 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
1823 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001824
Paul Bakker41c83d32013-03-20 14:39:14 +01001825static int ssl_write_server_key_exchange( ssl_context *ssl )
1826{
Paul Bakker23986e52011-04-24 08:57:21 +00001827 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001828 size_t n = 0, len;
Paul Bakker23f36802012-09-28 14:15:14 +00001829 unsigned char hash[64];
Paul Bakkerc70b9822013-04-07 22:00:46 +02001830 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001831 unsigned int hashlen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001832 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001833 unsigned char *dig_signed = p;
1834 size_t dig_signed_len = 0;
Paul Bakker41c83d32013-03-20 14:39:14 +01001835
1836 const ssl_ciphersuite_t *ciphersuite_info;
1837 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001838
1839 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
1840
Paul Bakker41c83d32013-03-20 14:39:14 +01001841 if( ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_RSA &&
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001842 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_ECDHE_RSA &&
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001843 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA &&
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001844 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001845 {
1846 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
1847 ssl->state++;
1848 return( 0 );
1849 }
1850
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001851#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1852 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1853 {
1854 /* TODO: Support identity hints */
1855 *(p++) = 0x00;
1856 *(p++) = 0x00;
1857
1858 n += 2;
1859 }
1860#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1861
1862#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1863 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1864 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1865 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00001866 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001867 /*
1868 * Ephemeral DH parameters:
1869 *
1870 * struct {
1871 * opaque dh_p<1..2^16-1>;
1872 * opaque dh_g<1..2^16-1>;
1873 * opaque dh_Ys<1..2^16-1>;
1874 * } ServerDHParams;
1875 */
1876 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
1877 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
1878 {
1879 SSL_DEBUG_RET( 1, "mpi_copy", ret );
1880 return( ret );
1881 }
Paul Bakker48916f92012-09-16 19:57:18 +00001882
Paul Bakker41c83d32013-03-20 14:39:14 +01001883 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
1884 mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001885 p,
1886 &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001887 {
1888 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
1889 return( ret );
1890 }
1891
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001892 dig_signed = p;
1893 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001894
1895 p += len;
1896 n += len;
1897
Paul Bakker41c83d32013-03-20 14:39:14 +01001898 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
1899 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1900 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1901 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
1902 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001903#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1904 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001905
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001906#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1907 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1908 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1909 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001910 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001911 /*
1912 * Ephemeral ECDH parameters:
1913 *
1914 * struct {
1915 * ECParameters curve_params;
1916 * ECPoint public;
1917 * } ServerECDHParams;
1918 */
Paul Bakker41c83d32013-03-20 14:39:14 +01001919 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
1920 ssl->handshake->ec_curve ) ) != 0 )
1921 {
1922 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
1923 return( ret );
1924 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001925
Paul Bakker41c83d32013-03-20 14:39:14 +01001926 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001927 &len,
1928 p,
Paul Bakker41c83d32013-03-20 14:39:14 +01001929 1000, ssl->f_rng, ssl->p_rng ) ) != 0 )
1930 {
1931 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
1932 return( ret );
1933 }
1934
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001935 dig_signed = p;
1936 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001937
1938 p += len;
1939 n += len;
1940
Paul Bakker41c83d32013-03-20 14:39:14 +01001941 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
1942 }
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001943#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1944 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001945
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001946#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001947 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1948 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001949 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001950 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1951 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001952 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001953 size_t signature_len = 0;
Paul Bakker23f36802012-09-28 14:15:14 +00001954
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001955 /*
1956 * Compute the hash to be signed
1957 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001958 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker23f36802012-09-28 14:15:14 +00001959 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001960 md5_context md5;
1961 sha1_context sha1;
1962
1963 /*
1964 * digitally-signed struct {
1965 * opaque md5_hash[16];
1966 * opaque sha_hash[20];
1967 * };
1968 *
1969 * md5_hash
1970 * MD5(ClientHello.random + ServerHello.random
1971 * + ServerParams);
1972 * sha_hash
1973 * SHA(ClientHello.random + ServerHello.random
1974 * + ServerParams);
1975 */
1976 md5_starts( &md5 );
1977 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001978 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001979 md5_finish( &md5, hash );
1980
1981 sha1_starts( &sha1 );
1982 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001983 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001984 sha1_finish( &sha1, hash + 16 );
1985
1986 hashlen = 36;
1987 md_alg = POLARSSL_MD_NONE;
1988 }
1989 else
1990 {
1991 md_context_t ctx;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001992 const md_info_t *md_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001993
1994 /*
1995 * digitally-signed struct {
1996 * opaque client_random[32];
1997 * opaque server_random[32];
1998 * ServerDHParams params;
1999 * };
2000 */
2001 switch( ssl->handshake->sig_alg )
2002 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002003#if defined(POLARSSL_MD5_C)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002004 case SSL_HASH_MD5:
2005 md_alg = POLARSSL_MD_MD5;
2006 break;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002007#endif
2008#if defined(POLARSSL_SHA1_C)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002009 case SSL_HASH_SHA1:
2010 md_alg = POLARSSL_MD_SHA1;
2011 break;
Paul Bakker23f36802012-09-28 14:15:14 +00002012#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002013#if defined(POLARSSL_SHA256_C)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002014 case SSL_HASH_SHA224:
2015 md_alg = POLARSSL_MD_SHA224;
2016 break;
2017 case SSL_HASH_SHA256:
2018 md_alg = POLARSSL_MD_SHA256;
2019 break;
Paul Bakker23f36802012-09-28 14:15:14 +00002020#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002021#if defined(POLARSSL_SHA512_C)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002022 case SSL_HASH_SHA384:
2023 md_alg = POLARSSL_MD_SHA384;
2024 break;
2025 case SSL_HASH_SHA512:
2026 md_alg = POLARSSL_MD_SHA512;
2027 break;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002028#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002029 default:
2030 /* Should never happen */
2031 return( -1 );
2032 }
2033
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002034 if( ( md_info = md_info_from_type( md_alg ) ) == NULL )
2035 {
2036 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2037 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2038 }
2039
2040 hashlen = md_info->size;
2041
2042 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002043 {
2044 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2045 return( ret );
2046 }
2047
2048 md_starts( &ctx );
2049 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002050 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002051 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02002052
2053 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
2054 {
2055 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
2056 return( ret );
2057 }
2058
Paul Bakker23f36802012-09-28 14:15:14 +00002059 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002060
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002061 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
2062
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002063 /*
2064 * Make the signature
2065 */
2066 if( ssl->pk_key == NULL || ssl->pk_key->pk_info == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002067 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002068 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2069 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002070 }
2071
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002072#if defined(POLARSSL_RSA_C)
2073 if( ssl->rsa_key != NULL )
2074 {
2075 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2076 {
2077 *(p++) = ssl->handshake->sig_alg;
2078 *(p++) = SSL_SIG_RSA;
2079
2080 n += 2;
2081 }
2082
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02002083 if( ssl->rsa_use_alt )
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002084 {
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02002085 if( ( ret = ssl->rsa_sign( ssl->rsa_key, ssl->f_rng,
2086 ssl->p_rng, RSA_PRIVATE, md_alg, hashlen,
2087 hash, p + 2 ) ) != 0 )
2088 {
2089 SSL_DEBUG_RET( 1, "rsa_sign", ret );
2090 return( ret );
2091 }
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002092
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02002093 signature_len = ssl->rsa_key_len( ssl->rsa_key );
2094 }
2095 else
2096 {
2097 if( ( ret = pk_sign( ssl->pk_key, md_alg, hash, hashlen,
2098 p + 2 , &signature_len,
2099 ssl->f_rng, ssl->p_rng ) ) != 0 )
2100 {
2101 SSL_DEBUG_RET( 1, "pk_sign", ret );
2102 return( ret );
2103 }
2104 }
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002105 }
2106 else
2107#endif /* POLARSSL_RSA_C */
2108#if defined(POLARSSL_ECDSA_C)
2109 if( pk_can_do( ssl->pk_key, POLARSSL_PK_ECDSA ) )
2110 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002111 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2112 {
2113 *(p++) = ssl->handshake->sig_alg;
2114 *(p++) = SSL_SIG_ECDSA;
2115
2116 n += 2;
2117 }
2118
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02002119 if( ( ret = pk_sign( ssl->pk_key, md_alg, hash, hashlen,
2120 p + 2 , &signature_len,
2121 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard583b6082013-08-20 16:58:13 +02002122 {
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02002123 SSL_DEBUG_RET( 1, "pk_sign", ret );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002124 return( ret );
2125 }
2126 }
2127 else
2128#endif /* POLARSSL_ECDSA_C */
2129 /* should never happen */
2130 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2131
2132 *(p++) = (unsigned char)( signature_len >> 8 );
2133 *(p++) = (unsigned char)( signature_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002134 n += 2;
2135
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002136 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002137
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002138 p += signature_len;
2139 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002140 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002141#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002142 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2143 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002144
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002145 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002146 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2147 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2148
2149 ssl->state++;
2150
2151 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2152 {
2153 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2154 return( ret );
2155 }
2156
2157 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2158
2159 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002160}
2161
2162static int ssl_write_server_hello_done( ssl_context *ssl )
2163{
2164 int ret;
2165
2166 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2167
2168 ssl->out_msglen = 4;
2169 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2170 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2171
2172 ssl->state++;
2173
2174 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2175 {
2176 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2177 return( ret );
2178 }
2179
2180 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2181
2182 return( 0 );
2183}
2184
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002185#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2186 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2187static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2188 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002189{
2190 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002191 size_t n;
2192
2193 /*
2194 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2195 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002196 if( *p + 2 > end )
2197 {
2198 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2199 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2200 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002201
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002202 n = ( (*p)[0] << 8 ) | (*p)[1];
2203 *p += 2;
2204
2205 if( n < 1 || n > ssl->handshake->dhm_ctx.len || *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002206 {
2207 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2208 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2209 }
2210
2211 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002212 *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002213 {
2214 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2215 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2216 }
2217
2218 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2219
Paul Bakker70df2fb2013-04-17 17:19:09 +02002220 return( ret );
2221}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002222#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2223 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002224
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002225#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002226static int ssl_parse_client_ecdh_public( ssl_context *ssl )
2227{
2228 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002229 size_t n;
2230
2231 /*
2232 * Receive client public key and calculate premaster
2233 */
2234 n = ssl->in_msg[3];
2235
2236 if( n < 1 || n > mpi_size( &ssl->handshake->ecdh_ctx.grp.P ) * 2 + 2 ||
2237 n + 4 != ssl->in_hslen )
2238 {
2239 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2240 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2241 }
2242
2243 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2244 ssl->in_msg + 4, n ) ) != 0 )
2245 {
2246 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2247 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2248 }
2249
2250 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2251
Paul Bakker70df2fb2013-04-17 17:19:09 +02002252 return( ret );
2253}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002254#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002255
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002256#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002257static int ssl_parse_encrypted_pms_secret( ssl_context *ssl )
2258{
2259 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2260 size_t i, n = 0;
2261
2262 if( ssl->rsa_key == NULL )
2263 {
2264 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2265 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2266 }
2267
2268 /*
2269 * Decrypt the premaster using own private RSA key
2270 */
2271 i = 4;
2272 if( ssl->rsa_key )
2273 n = ssl->rsa_key_len( ssl->rsa_key );
2274 ssl->handshake->pmslen = 48;
2275
2276 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2277 {
2278 i += 2;
2279 if( ssl->in_msg[4] != ( ( n >> 8 ) & 0xFF ) ||
2280 ssl->in_msg[5] != ( ( n ) & 0xFF ) )
2281 {
2282 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2283 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2284 }
2285 }
2286
2287 if( ssl->in_hslen != i + n )
2288 {
2289 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2290 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2291 }
2292
2293 if( ssl->rsa_key ) {
2294 ret = ssl->rsa_decrypt( ssl->rsa_key, RSA_PRIVATE,
2295 &ssl->handshake->pmslen,
2296 ssl->in_msg + i,
2297 ssl->handshake->premaster,
2298 sizeof(ssl->handshake->premaster) );
2299 }
2300
2301 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Paul Bakker2fbefde2013-06-29 16:01:15 +02002302 ssl->handshake->premaster[0] != ssl->handshake->max_major_ver ||
2303 ssl->handshake->premaster[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002304 {
2305 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2306
2307 /*
2308 * Protection against Bleichenbacher's attack:
2309 * invalid PKCS#1 v1.5 padding must not cause
2310 * the connection to end immediately; instead,
2311 * send a bad_record_mac later in the handshake.
2312 */
2313 ssl->handshake->pmslen = 48;
2314
2315 ret = ssl->f_rng( ssl->p_rng, ssl->handshake->premaster,
2316 ssl->handshake->pmslen );
2317 if( ret != 0 )
2318 return( ret );
2319 }
2320
2321 return( ret );
2322}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002323#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002324
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002325#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2326 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2327static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2328 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002329{
2330 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002331 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002332
2333 if( ssl->psk == NULL || ssl->psk_identity == NULL ||
2334 ssl->psk_identity_len == 0 || ssl->psk_len == 0 )
2335 {
2336 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2337 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2338 }
2339
2340 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002341 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002342 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002343 if( *p + 2 > end )
2344 {
2345 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2346 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2347 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002348
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002349 n = ( (*p)[0] << 8 ) | (*p)[1];
2350 *p += 2;
2351
2352 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002353 {
2354 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2355 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2356 }
2357
2358 if( n != ssl->psk_identity_len ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002359 memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002360 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002361 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002362 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2363 }
2364
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002365 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002366 ret = 0;
2367
Paul Bakkerfbb17802013-04-17 19:10:21 +02002368 return( ret );
2369}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002370#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
2371 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002372
Paul Bakker5121ce52009-01-03 21:22:43 +00002373static int ssl_parse_client_key_exchange( ssl_context *ssl )
2374{
Paul Bakker23986e52011-04-24 08:57:21 +00002375 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002376 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002377 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002378
Paul Bakker41c83d32013-03-20 14:39:14 +01002379 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002380
2381 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2382
2383 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2384 {
2385 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2386 return( ret );
2387 }
2388
2389 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2390 {
2391 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002392 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002393 }
2394
2395 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2396 {
2397 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002398 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002399 }
2400
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002401 p = ssl->in_msg + 4;
2402 end = ssl->in_msg + ssl->in_msglen;
2403
2404#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002405 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002406 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002407 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002408 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002409 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2410 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002411 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002412
2413 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
2414
2415 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2416 ssl->handshake->premaster,
2417 &ssl->handshake->pmslen ) ) != 0 )
2418 {
2419 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2420 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2421 }
2422
2423 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002424 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002425 else
2426#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002427#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2428 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2429 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2430 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002431 {
2432 if( ( ret = ssl_parse_client_ecdh_public( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002433 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002434 SSL_DEBUG_RET( 1, ( "ssl_parse_client_ecdh_public" ), ret );
2435 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002436 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002437
2438 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2439 &ssl->handshake->pmslen,
2440 ssl->handshake->premaster,
2441 POLARSSL_MPI_MAX_SIZE ) ) != 0 )
2442 {
2443 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2444 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2445 }
2446
2447 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002448 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002449 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002450#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2451 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002452#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2453 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002454 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002455 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002456 {
2457 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2458 return( ret );
2459 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002460
2461 // Set up the premaster secret
2462 //
2463 p = ssl->handshake->premaster;
2464 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2465 *(p++) = (unsigned char)( ssl->psk_len );
2466 p += ssl->psk_len;
2467
2468 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2469 *(p++) = (unsigned char)( ssl->psk_len );
2470 memcpy( p, ssl->psk, ssl->psk_len );
2471 p += ssl->psk_len;
2472
2473 ssl->handshake->pmslen = 4 + 2 * ssl->psk_len;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002474 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002475 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002476#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
2477#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2478 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2479 {
2480 size_t n;
2481
2482 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2483 {
2484 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2485 return( ret );
2486 }
2487 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2488 {
2489 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2490 return( ret );
2491 }
2492
2493 // Set up the premaster secret
2494 //
2495 p = ssl->handshake->premaster;
2496 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len >> 8 );
2497 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len );
2498
2499 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2500 p, &n ) ) != 0 )
2501 {
2502 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2503 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2504 }
2505
2506 if( n != ssl->handshake->dhm_ctx.len )
2507 {
2508 SSL_DEBUG_MSG( 1, ( "dhm_calc_secret result smaller than DHM" ) );
2509 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2510 }
2511
2512 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2513
2514 p += ssl->handshake->dhm_ctx.len;
2515
2516 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2517 *(p++) = (unsigned char)( ssl->psk_len );
2518 memcpy( p, ssl->psk, ssl->psk_len );
2519 p += ssl->psk_len;
2520
2521 ssl->handshake->pmslen = 4 + ssl->handshake->dhm_ctx.len + ssl->psk_len;
2522 }
2523 else
2524#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
2525#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2526 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002527 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002528 if( ( ret = ssl_parse_encrypted_pms_secret( ssl ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002529 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002530 SSL_DEBUG_RET( 1, ( "ssl_parse_client_ecdh_public" ), ret );
2531 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002532 }
2533 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002534 else
2535#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2536 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002537 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002538 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2539 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002540
Paul Bakkerff60ee62010-03-16 21:09:09 +00002541 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2542 {
2543 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2544 return( ret );
2545 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002546
Paul Bakker5121ce52009-01-03 21:22:43 +00002547 ssl->state++;
2548
2549 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2550
2551 return( 0 );
2552}
2553
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002554#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2555 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2556 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002557static int ssl_parse_certificate_verify( ssl_context *ssl )
2558{
Paul Bakkered27a042013-04-18 22:46:23 +02002559 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002560 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002561
2562 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2563
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002564 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2565 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002566 {
2567 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2568 ssl->state++;
2569 return( 0 );
2570 }
2571
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002572 return( ret );
2573}
2574#else
2575static int ssl_parse_certificate_verify( ssl_context *ssl )
2576{
2577 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002578 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002579 unsigned char hash[48];
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002580 size_t hashlen;
2581 pk_type_t pk_alg;
2582 md_type_t md_alg;
2583 const md_info_t *md_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002584 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2585
2586 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2587
2588 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2589 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2590 {
2591 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2592 ssl->state++;
2593 return( 0 );
2594 }
2595
Paul Bakkered27a042013-04-18 22:46:23 +02002596 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002597 {
2598 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2599 ssl->state++;
2600 return( 0 );
2601 }
2602
Paul Bakker48916f92012-09-16 19:57:18 +00002603 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002604
2605 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2606 {
2607 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2608 return( ret );
2609 }
2610
2611 ssl->state++;
2612
2613 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2614 {
2615 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002616 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002617 }
2618
2619 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
2620 {
2621 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002622 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002623 }
2624
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002625 /*
2626 * 0 . 0 handshake type
2627 * 1 . 3 handshake length
2628 * 4 . 5 sig alg (TLS 1.2 only)
2629 * 4+n . 5+n signature length (n = sa_len)
2630 * 6+n . 6+n+m signature (m = sig_len)
2631 */
2632
2633 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002634 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002635 sa_len = 0;
2636
2637 md_alg = POLARSSL_MD_NONE;
2638 hashlen = 36;
2639 }
2640 else
2641 {
2642 sa_len = 2;
2643
Paul Bakker926af752012-11-23 13:38:07 +01002644 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002645 * Hash: as server we know we either have SSL_HASH_SHA384 or
Paul Bakker926af752012-11-23 13:38:07 +01002646 * SSL_HASH_SHA256
2647 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002648 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker926af752012-11-23 13:38:07 +01002649 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002650 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2651 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01002652 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2653 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002654
Paul Bakker926af752012-11-23 13:38:07 +01002655 if( ssl->handshake->verify_sig_alg == SSL_HASH_SHA384 )
Paul Bakkerc70b9822013-04-07 22:00:46 +02002656 md_alg = POLARSSL_MD_SHA384;
Paul Bakker926af752012-11-23 13:38:07 +01002657 else
Paul Bakkerc70b9822013-04-07 22:00:46 +02002658 md_alg = POLARSSL_MD_SHA256;
Paul Bakker926af752012-11-23 13:38:07 +01002659
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002660 /*
2661 * Get hashlen from MD
2662 */
2663 if( ( md_info = md_info_from_type( md_alg ) ) == NULL )
2664 {
2665 SSL_DEBUG_MSG( 1, ( "requested hash not available " ) );
2666 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2667 }
2668 hashlen = md_info->size;
2669
2670 /*
2671 * Signature
2672 */
2673 switch( ssl->in_msg[5] )
2674 {
2675#if defined(POLARSSL_RSA_C)
2676 case SSL_SIG_RSA:
2677 pk_alg = POLARSSL_PK_RSA;
2678 break;
2679#endif
2680
2681#if defined(POLARSSL_ECDSA_C)
2682 case SSL_SIG_ECDSA:
2683 pk_alg = POLARSSL_PK_ECDSA;
2684 break;
2685#endif
2686
2687 default:
2688 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2689 " for verify message" ) );
2690 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2691 }
2692
2693
2694 /*
2695 * Check the certificate's key type matches the signature alg
2696 */
2697 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
2698 {
2699 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
2700 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2701 }
2702
Paul Bakker926af752012-11-23 13:38:07 +01002703 }
2704
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002705 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002706
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002707 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002708 {
2709 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002710 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002711 }
2712
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002713 ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
2714 md_alg, hash, hashlen,
2715 ssl->in_msg + 6 + sa_len, sig_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002716 if( ret != 0 )
2717 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002718 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002719 return( ret );
2720 }
2721
2722 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
2723
Paul Bakkered27a042013-04-18 22:46:23 +02002724 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002725}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002726#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2727 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2728 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002729
Paul Bakkera503a632013-08-14 13:48:06 +02002730#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002731static int ssl_write_new_session_ticket( ssl_context *ssl )
2732{
2733 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002734 size_t tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002735
2736 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
2737
2738 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2739 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
2740
2741 /*
2742 * struct {
2743 * uint32 ticket_lifetime_hint;
2744 * opaque ticket<0..2^16-1>;
2745 * } NewSessionTicket;
2746 *
2747 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
2748 * 8 . 9 ticket_len (n)
2749 * 10 . 9+n ticket content
2750 */
2751 ssl->out_msg[4] = 0x00;
2752 ssl->out_msg[5] = 0x00;
2753 ssl->out_msg[6] = 0x00;
2754 ssl->out_msg[7] = 0x00;
2755
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02002756 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
2757 {
2758 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
2759 tlen = 0;
2760 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002761
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002762 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
2763 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002764
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002765 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002766
2767 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2768 {
2769 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2770 return( ret );
2771 }
2772
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002773 /* No need to remember writing a NewSessionTicket any more */
2774 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002775
2776 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
2777
2778 return( 0 );
2779}
Paul Bakkera503a632013-08-14 13:48:06 +02002780#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002781
Paul Bakker5121ce52009-01-03 21:22:43 +00002782/*
Paul Bakker1961b702013-01-25 14:49:24 +01002783 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002784 */
Paul Bakker1961b702013-01-25 14:49:24 +01002785int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002786{
2787 int ret = 0;
2788
Paul Bakker1961b702013-01-25 14:49:24 +01002789 if( ssl->state == SSL_HANDSHAKE_OVER )
2790 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002791
Paul Bakker1961b702013-01-25 14:49:24 +01002792 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
2793
2794 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2795 return( ret );
2796
2797 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002798 {
Paul Bakker1961b702013-01-25 14:49:24 +01002799 case SSL_HELLO_REQUEST:
2800 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002801 break;
2802
Paul Bakker1961b702013-01-25 14:49:24 +01002803 /*
2804 * <== ClientHello
2805 */
2806 case SSL_CLIENT_HELLO:
2807 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002808 break;
Paul Bakker1961b702013-01-25 14:49:24 +01002809
2810 /*
2811 * ==> ServerHello
2812 * Certificate
2813 * ( ServerKeyExchange )
2814 * ( CertificateRequest )
2815 * ServerHelloDone
2816 */
2817 case SSL_SERVER_HELLO:
2818 ret = ssl_write_server_hello( ssl );
2819 break;
2820
2821 case SSL_SERVER_CERTIFICATE:
2822 ret = ssl_write_certificate( ssl );
2823 break;
2824
2825 case SSL_SERVER_KEY_EXCHANGE:
2826 ret = ssl_write_server_key_exchange( ssl );
2827 break;
2828
2829 case SSL_CERTIFICATE_REQUEST:
2830 ret = ssl_write_certificate_request( ssl );
2831 break;
2832
2833 case SSL_SERVER_HELLO_DONE:
2834 ret = ssl_write_server_hello_done( ssl );
2835 break;
2836
2837 /*
2838 * <== ( Certificate/Alert )
2839 * ClientKeyExchange
2840 * ( CertificateVerify )
2841 * ChangeCipherSpec
2842 * Finished
2843 */
2844 case SSL_CLIENT_CERTIFICATE:
2845 ret = ssl_parse_certificate( ssl );
2846 break;
2847
2848 case SSL_CLIENT_KEY_EXCHANGE:
2849 ret = ssl_parse_client_key_exchange( ssl );
2850 break;
2851
2852 case SSL_CERTIFICATE_VERIFY:
2853 ret = ssl_parse_certificate_verify( ssl );
2854 break;
2855
2856 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2857 ret = ssl_parse_change_cipher_spec( ssl );
2858 break;
2859
2860 case SSL_CLIENT_FINISHED:
2861 ret = ssl_parse_finished( ssl );
2862 break;
2863
2864 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002865 * ==> ( NewSessionTicket )
2866 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002867 * Finished
2868 */
2869 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002870#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002871 if( ssl->handshake->new_session_ticket != 0 )
2872 ret = ssl_write_new_session_ticket( ssl );
2873 else
Paul Bakkera503a632013-08-14 13:48:06 +02002874#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002875 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002876 break;
2877
2878 case SSL_SERVER_FINISHED:
2879 ret = ssl_write_finished( ssl );
2880 break;
2881
2882 case SSL_FLUSH_BUFFERS:
2883 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2884 ssl->state = SSL_HANDSHAKE_WRAPUP;
2885 break;
2886
2887 case SSL_HANDSHAKE_WRAPUP:
2888 ssl_handshake_wrapup( ssl );
2889 break;
2890
2891 default:
2892 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2893 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002894 }
2895
Paul Bakker5121ce52009-01-03 21:22:43 +00002896 return( ret );
2897}
Paul Bakker5121ce52009-01-03 21:22:43 +00002898#endif