blob: f13b9c220e5c42df714e238a487d8648cf14f23b [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 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002001 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002002
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002003 if( ( md_info = md_info_from_type( md_alg ) ) == NULL )
2004 {
2005 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2006 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2007 }
2008
2009 hashlen = md_info->size;
2010
2011 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002012 {
2013 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2014 return( ret );
2015 }
2016
2017 md_starts( &ctx );
2018 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002019 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002020 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02002021
2022 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
2023 {
2024 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
2025 return( ret );
2026 }
2027
Paul Bakker23f36802012-09-28 14:15:14 +00002028 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002029
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002030 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
2031
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002032 /*
2033 * Make the signature
2034 */
2035 if( ssl->pk_key == NULL || ssl->pk_key->pk_info == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002036 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002037 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2038 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002039 }
2040
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002041 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002042 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002043 *(p++) = ssl->handshake->sig_alg;
2044 *(p++) = ssl_sig_from_pk( ssl->pk_key );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002045
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002046 n += 2;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002047 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002048
2049 if( ( ret = pk_sign( ssl->pk_key, md_alg, hash, hashlen,
2050 p + 2 , &signature_len,
2051 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002052 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002053 SSL_DEBUG_RET( 1, "pk_sign", ret );
2054 return( ret );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002055 }
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002056
2057 *(p++) = (unsigned char)( signature_len >> 8 );
2058 *(p++) = (unsigned char)( signature_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002059 n += 2;
2060
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002061 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002062
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002063 p += signature_len;
2064 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002065 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002066#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002067 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2068 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002069
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002070 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002071 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2072 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2073
2074 ssl->state++;
2075
2076 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2077 {
2078 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2079 return( ret );
2080 }
2081
2082 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2083
2084 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002085}
2086
2087static int ssl_write_server_hello_done( ssl_context *ssl )
2088{
2089 int ret;
2090
2091 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2092
2093 ssl->out_msglen = 4;
2094 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2095 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2096
2097 ssl->state++;
2098
2099 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2100 {
2101 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2102 return( ret );
2103 }
2104
2105 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2106
2107 return( 0 );
2108}
2109
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002110#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2111 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2112static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2113 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002114{
2115 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002116 size_t n;
2117
2118 /*
2119 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2120 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002121 if( *p + 2 > end )
2122 {
2123 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2124 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2125 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002126
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002127 n = ( (*p)[0] << 8 ) | (*p)[1];
2128 *p += 2;
2129
2130 if( n < 1 || n > ssl->handshake->dhm_ctx.len || *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002131 {
2132 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2133 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2134 }
2135
2136 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002137 *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002138 {
2139 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2140 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2141 }
2142
2143 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2144
Paul Bakker70df2fb2013-04-17 17:19:09 +02002145 return( ret );
2146}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002147#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2148 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002149
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002150#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002151static int ssl_parse_client_ecdh_public( ssl_context *ssl )
2152{
2153 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002154 size_t n;
2155
2156 /*
2157 * Receive client public key and calculate premaster
2158 */
2159 n = ssl->in_msg[3];
2160
2161 if( n < 1 || n > mpi_size( &ssl->handshake->ecdh_ctx.grp.P ) * 2 + 2 ||
2162 n + 4 != ssl->in_hslen )
2163 {
2164 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2165 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2166 }
2167
2168 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2169 ssl->in_msg + 4, n ) ) != 0 )
2170 {
2171 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2172 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2173 }
2174
2175 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2176
Paul Bakker70df2fb2013-04-17 17:19:09 +02002177 return( ret );
2178}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002179#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002180
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002181#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002182static int ssl_parse_encrypted_pms_secret( ssl_context *ssl )
2183{
2184 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2185 size_t i, n = 0;
2186
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002187 if( ! pk_can_do( ssl->pk_key, POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002188 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002189 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002190 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2191 }
2192
2193 /*
2194 * Decrypt the premaster using own private RSA key
2195 */
2196 i = 4;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002197 n = pk_get_len( ssl->pk_key );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002198 ssl->handshake->pmslen = 48;
2199
2200 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2201 {
2202 i += 2;
2203 if( ssl->in_msg[4] != ( ( n >> 8 ) & 0xFF ) ||
2204 ssl->in_msg[5] != ( ( n ) & 0xFF ) )
2205 {
2206 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2207 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2208 }
2209 }
2210
2211 if( ssl->in_hslen != i + n )
2212 {
2213 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2214 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2215 }
2216
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002217 ret = pk_decrypt( ssl->pk_key,
2218 ssl->in_msg + i, n,
2219 ssl->handshake->premaster, &ssl->handshake->pmslen,
2220 sizeof(ssl->handshake->premaster),
2221 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002222
2223 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Paul Bakker2fbefde2013-06-29 16:01:15 +02002224 ssl->handshake->premaster[0] != ssl->handshake->max_major_ver ||
2225 ssl->handshake->premaster[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002226 {
2227 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2228
2229 /*
2230 * Protection against Bleichenbacher's attack:
2231 * invalid PKCS#1 v1.5 padding must not cause
2232 * the connection to end immediately; instead,
2233 * send a bad_record_mac later in the handshake.
2234 */
2235 ssl->handshake->pmslen = 48;
2236
2237 ret = ssl->f_rng( ssl->p_rng, ssl->handshake->premaster,
2238 ssl->handshake->pmslen );
2239 if( ret != 0 )
2240 return( ret );
2241 }
2242
2243 return( ret );
2244}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002245#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002246
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002247#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2248 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2249static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2250 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002251{
2252 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002253 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002254
2255 if( ssl->psk == NULL || ssl->psk_identity == NULL ||
2256 ssl->psk_identity_len == 0 || ssl->psk_len == 0 )
2257 {
2258 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2259 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2260 }
2261
2262 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002263 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002264 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002265 if( *p + 2 > end )
2266 {
2267 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2268 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2269 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002270
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002271 n = ( (*p)[0] << 8 ) | (*p)[1];
2272 *p += 2;
2273
2274 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002275 {
2276 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2277 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2278 }
2279
2280 if( n != ssl->psk_identity_len ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002281 memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002282 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002283 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002284 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2285 }
2286
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002287 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002288 ret = 0;
2289
Paul Bakkerfbb17802013-04-17 19:10:21 +02002290 return( ret );
2291}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002292#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
2293 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002294
Paul Bakker5121ce52009-01-03 21:22:43 +00002295static int ssl_parse_client_key_exchange( ssl_context *ssl )
2296{
Paul Bakker23986e52011-04-24 08:57:21 +00002297 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002298 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002299 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002300
Paul Bakker41c83d32013-03-20 14:39:14 +01002301 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002302
2303 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2304
2305 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2306 {
2307 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2308 return( ret );
2309 }
2310
2311 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2312 {
2313 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002314 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002315 }
2316
2317 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2318 {
2319 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002320 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002321 }
2322
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002323 p = ssl->in_msg + 4;
2324 end = ssl->in_msg + ssl->in_msglen;
2325
2326#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002327 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002328 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002329 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002330 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002331 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2332 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002333 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002334
2335 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
2336
2337 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2338 ssl->handshake->premaster,
2339 &ssl->handshake->pmslen ) ) != 0 )
2340 {
2341 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2342 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2343 }
2344
2345 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002346 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002347 else
2348#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002349#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2350 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2351 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2352 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002353 {
2354 if( ( ret = ssl_parse_client_ecdh_public( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002355 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002356 SSL_DEBUG_RET( 1, ( "ssl_parse_client_ecdh_public" ), ret );
2357 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002358 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002359
2360 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2361 &ssl->handshake->pmslen,
2362 ssl->handshake->premaster,
2363 POLARSSL_MPI_MAX_SIZE ) ) != 0 )
2364 {
2365 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2366 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2367 }
2368
2369 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002370 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002371 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002372#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2373 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002374#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2375 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002376 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002377 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002378 {
2379 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2380 return( ret );
2381 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002382
2383 // Set up the premaster secret
2384 //
2385 p = ssl->handshake->premaster;
2386 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2387 *(p++) = (unsigned char)( ssl->psk_len );
2388 p += ssl->psk_len;
2389
2390 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2391 *(p++) = (unsigned char)( ssl->psk_len );
2392 memcpy( p, ssl->psk, ssl->psk_len );
2393 p += ssl->psk_len;
2394
2395 ssl->handshake->pmslen = 4 + 2 * ssl->psk_len;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002396 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002397 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002398#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
2399#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2400 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2401 {
2402 size_t n;
2403
2404 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2405 {
2406 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2407 return( ret );
2408 }
2409 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2410 {
2411 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2412 return( ret );
2413 }
2414
2415 // Set up the premaster secret
2416 //
2417 p = ssl->handshake->premaster;
2418 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len >> 8 );
2419 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len );
2420
2421 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2422 p, &n ) ) != 0 )
2423 {
2424 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2425 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2426 }
2427
2428 if( n != ssl->handshake->dhm_ctx.len )
2429 {
2430 SSL_DEBUG_MSG( 1, ( "dhm_calc_secret result smaller than DHM" ) );
2431 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2432 }
2433
2434 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2435
2436 p += ssl->handshake->dhm_ctx.len;
2437
2438 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2439 *(p++) = (unsigned char)( ssl->psk_len );
2440 memcpy( p, ssl->psk, ssl->psk_len );
2441 p += ssl->psk_len;
2442
2443 ssl->handshake->pmslen = 4 + ssl->handshake->dhm_ctx.len + ssl->psk_len;
2444 }
2445 else
2446#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
2447#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2448 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002449 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002450 if( ( ret = ssl_parse_encrypted_pms_secret( ssl ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002451 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002452 SSL_DEBUG_RET( 1, ( "ssl_parse_client_ecdh_public" ), ret );
2453 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002454 }
2455 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002456 else
2457#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2458 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002459 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002460 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2461 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002462
Paul Bakkerff60ee62010-03-16 21:09:09 +00002463 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2464 {
2465 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2466 return( ret );
2467 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002468
Paul Bakker5121ce52009-01-03 21:22:43 +00002469 ssl->state++;
2470
2471 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2472
2473 return( 0 );
2474}
2475
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002476#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2477 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2478 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002479static int ssl_parse_certificate_verify( ssl_context *ssl )
2480{
Paul Bakkered27a042013-04-18 22:46:23 +02002481 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002482 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002483
2484 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2485
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002486 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2487 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002488 {
2489 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2490 ssl->state++;
2491 return( 0 );
2492 }
2493
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002494 return( ret );
2495}
2496#else
2497static int ssl_parse_certificate_verify( ssl_context *ssl )
2498{
2499 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002500 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002501 unsigned char hash[48];
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002502 size_t hashlen;
2503 pk_type_t pk_alg;
2504 md_type_t md_alg;
2505 const md_info_t *md_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002506 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2507
2508 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2509
2510 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2511 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2512 {
2513 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2514 ssl->state++;
2515 return( 0 );
2516 }
2517
Paul Bakkered27a042013-04-18 22:46:23 +02002518 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002519 {
2520 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2521 ssl->state++;
2522 return( 0 );
2523 }
2524
Paul Bakker48916f92012-09-16 19:57:18 +00002525 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002526
2527 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2528 {
2529 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2530 return( ret );
2531 }
2532
2533 ssl->state++;
2534
2535 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2536 {
2537 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002538 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002539 }
2540
2541 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
2542 {
2543 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002544 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002545 }
2546
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002547 /*
2548 * 0 . 0 handshake type
2549 * 1 . 3 handshake length
2550 * 4 . 5 sig alg (TLS 1.2 only)
2551 * 4+n . 5+n signature length (n = sa_len)
2552 * 6+n . 6+n+m signature (m = sig_len)
2553 */
2554
2555 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002556 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002557 sa_len = 0;
2558
2559 md_alg = POLARSSL_MD_NONE;
2560 hashlen = 36;
2561 }
2562 else
2563 {
2564 sa_len = 2;
2565
Paul Bakker926af752012-11-23 13:38:07 +01002566 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002567 * Hash
Paul Bakker926af752012-11-23 13:38:07 +01002568 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002569 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker926af752012-11-23 13:38:07 +01002570 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002571 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2572 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01002573 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2574 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002575
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002576 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01002577
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002578 /*
2579 * Get hashlen from MD
2580 */
2581 if( ( md_info = md_info_from_type( md_alg ) ) == NULL )
2582 {
2583 SSL_DEBUG_MSG( 1, ( "requested hash not available " ) );
2584 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2585 }
2586 hashlen = md_info->size;
2587
2588 /*
2589 * Signature
2590 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002591 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
2592 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002593 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002594 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2595 " for verify message" ) );
2596 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002597 }
2598
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002599 /*
2600 * Check the certificate's key type matches the signature alg
2601 */
2602 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
2603 {
2604 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
2605 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2606 }
2607
Paul Bakker926af752012-11-23 13:38:07 +01002608 }
2609
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002610 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002611
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002612 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002613 {
2614 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002615 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002616 }
2617
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002618 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
2619 md_alg, hash, hashlen,
2620 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002621 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002622 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002623 return( ret );
2624 }
2625
2626 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
2627
Paul Bakkered27a042013-04-18 22:46:23 +02002628 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002629}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002630#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2631 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2632 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002633
Paul Bakkera503a632013-08-14 13:48:06 +02002634#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002635static int ssl_write_new_session_ticket( ssl_context *ssl )
2636{
2637 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002638 size_t tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002639
2640 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
2641
2642 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2643 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
2644
2645 /*
2646 * struct {
2647 * uint32 ticket_lifetime_hint;
2648 * opaque ticket<0..2^16-1>;
2649 * } NewSessionTicket;
2650 *
2651 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
2652 * 8 . 9 ticket_len (n)
2653 * 10 . 9+n ticket content
2654 */
2655 ssl->out_msg[4] = 0x00;
2656 ssl->out_msg[5] = 0x00;
2657 ssl->out_msg[6] = 0x00;
2658 ssl->out_msg[7] = 0x00;
2659
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02002660 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
2661 {
2662 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
2663 tlen = 0;
2664 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002665
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002666 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
2667 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002668
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002669 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002670
2671 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2672 {
2673 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2674 return( ret );
2675 }
2676
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002677 /* No need to remember writing a NewSessionTicket any more */
2678 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002679
2680 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
2681
2682 return( 0 );
2683}
Paul Bakkera503a632013-08-14 13:48:06 +02002684#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002685
Paul Bakker5121ce52009-01-03 21:22:43 +00002686/*
Paul Bakker1961b702013-01-25 14:49:24 +01002687 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002688 */
Paul Bakker1961b702013-01-25 14:49:24 +01002689int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002690{
2691 int ret = 0;
2692
Paul Bakker1961b702013-01-25 14:49:24 +01002693 if( ssl->state == SSL_HANDSHAKE_OVER )
2694 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002695
Paul Bakker1961b702013-01-25 14:49:24 +01002696 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
2697
2698 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2699 return( ret );
2700
2701 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002702 {
Paul Bakker1961b702013-01-25 14:49:24 +01002703 case SSL_HELLO_REQUEST:
2704 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002705 break;
2706
Paul Bakker1961b702013-01-25 14:49:24 +01002707 /*
2708 * <== ClientHello
2709 */
2710 case SSL_CLIENT_HELLO:
2711 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002712 break;
Paul Bakker1961b702013-01-25 14:49:24 +01002713
2714 /*
2715 * ==> ServerHello
2716 * Certificate
2717 * ( ServerKeyExchange )
2718 * ( CertificateRequest )
2719 * ServerHelloDone
2720 */
2721 case SSL_SERVER_HELLO:
2722 ret = ssl_write_server_hello( ssl );
2723 break;
2724
2725 case SSL_SERVER_CERTIFICATE:
2726 ret = ssl_write_certificate( ssl );
2727 break;
2728
2729 case SSL_SERVER_KEY_EXCHANGE:
2730 ret = ssl_write_server_key_exchange( ssl );
2731 break;
2732
2733 case SSL_CERTIFICATE_REQUEST:
2734 ret = ssl_write_certificate_request( ssl );
2735 break;
2736
2737 case SSL_SERVER_HELLO_DONE:
2738 ret = ssl_write_server_hello_done( ssl );
2739 break;
2740
2741 /*
2742 * <== ( Certificate/Alert )
2743 * ClientKeyExchange
2744 * ( CertificateVerify )
2745 * ChangeCipherSpec
2746 * Finished
2747 */
2748 case SSL_CLIENT_CERTIFICATE:
2749 ret = ssl_parse_certificate( ssl );
2750 break;
2751
2752 case SSL_CLIENT_KEY_EXCHANGE:
2753 ret = ssl_parse_client_key_exchange( ssl );
2754 break;
2755
2756 case SSL_CERTIFICATE_VERIFY:
2757 ret = ssl_parse_certificate_verify( ssl );
2758 break;
2759
2760 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2761 ret = ssl_parse_change_cipher_spec( ssl );
2762 break;
2763
2764 case SSL_CLIENT_FINISHED:
2765 ret = ssl_parse_finished( ssl );
2766 break;
2767
2768 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002769 * ==> ( NewSessionTicket )
2770 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002771 * Finished
2772 */
2773 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002774#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002775 if( ssl->handshake->new_session_ticket != 0 )
2776 ret = ssl_write_new_session_ticket( ssl );
2777 else
Paul Bakkera503a632013-08-14 13:48:06 +02002778#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002779 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002780 break;
2781
2782 case SSL_SERVER_FINISHED:
2783 ret = ssl_write_finished( ssl );
2784 break;
2785
2786 case SSL_FLUSH_BUFFERS:
2787 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2788 ssl->state = SSL_HANDSHAKE_WRAPUP;
2789 break;
2790
2791 case SSL_HANDSHAKE_WRAPUP:
2792 ssl_handshake_wrapup( ssl );
2793 break;
2794
2795 default:
2796 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2797 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002798 }
2799
Paul Bakker5121ce52009-01-03 21:22:43 +00002800 return( ret );
2801}
Paul Bakker5121ce52009-01-03 21:22:43 +00002802#endif