blob: 0ffe886b578075fdd1907448b61a24923c39fbbc [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{
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200108 const unsigned char *p = buf;
109 const unsigned char * const end = buf + len;
110#if defined(POLARSSL_X509_PARSE_C)
111 size_t cert_len;
112#endif /* POLARSSL_X509_PARSE_C */
113
114 if( p + sizeof( ssl_session ) > end )
115 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
116
117 memcpy( session, p, sizeof( ssl_session ) );
118 p += sizeof( ssl_session );
119
120#if defined(POLARSSL_X509_PARSE_C)
121 if( p + 3 > end )
122 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
123
124 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
125 p += 3;
126
127 if( cert_len == 0 )
128 {
129 session->peer_cert = NULL;
130 }
131 else
132 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200133 int ret;
134
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200135 if( p + cert_len > end )
136 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
137
Manuel Pégourié-Gonnard9f5a3c42013-09-07 18:09:43 +0200138 session->peer_cert = polarssl_malloc( sizeof( x509_cert ) );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200139
140 if( session->peer_cert == NULL )
141 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
142
143 memset( session->peer_cert, 0, sizeof( x509_cert ) );
144
145 if( ( ret = x509parse_crt( session->peer_cert, p, cert_len ) ) != 0 )
146 {
Manuel Pégourié-Gonnard9f5a3c42013-09-07 18:09:43 +0200147 x509_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200148 polarssl_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200149 session->peer_cert = NULL;
150 return( ret );
151 }
152
153 p += cert_len;
154 }
155#endif /* POLARSSL_X509_PARSE_C */
156
157 if( p != end )
158 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
159
160 return( 0 );
161}
162
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200163/*
164 * Create session ticket, secured as recommended in RFC 5077 section 4:
165 *
166 * struct {
167 * opaque key_name[16];
168 * opaque iv[16];
169 * opaque encrypted_state<0..2^16-1>;
170 * opaque mac[32];
171 * } ticket;
172 *
173 * (the internal state structure differs, however).
174 */
175static int ssl_write_ticket( ssl_context *ssl, size_t *tlen )
176{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200177 int ret;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200178 unsigned char * const start = ssl->out_msg + 10;
179 unsigned char *p = start;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200180 unsigned char *state;
181 unsigned char iv[16];
182 size_t clear_len, enc_len, pad_len, i;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200183
Manuel Pégourié-Gonnard0a201712013-08-23 16:25:16 +0200184 *tlen = 0;
185
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200186 if( ssl->ticket_keys == NULL )
187 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
188
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200189 /* Write key name */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200190 memcpy( p, ssl->ticket_keys->key_name, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200191 p += 16;
192
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200193 /* Generate and write IV (with a copy for aes_crypt) */
194 if( ( ret = ssl->f_rng( ssl->p_rng, p, 16 ) ) != 0 )
195 return( ret );
196 memcpy( iv, p, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200197 p += 16;
198
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200199 /*
200 * Dump session state
201 *
202 * After the session state itself, we still need room for 16 bytes of
203 * padding and 32 bytes of MAC, so there's only so much room left
204 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200205 state = p + 2;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200206 if( ssl_save_session( ssl->session_negotiate, state,
207 SSL_MAX_CONTENT_LEN - (state - ssl->out_ctr) - 48,
208 &clear_len ) != 0 )
209 {
210 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
211 }
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200212 SSL_DEBUG_BUF( 3, "session ticket cleartext", state, clear_len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200213
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200214 /* Apply PKCS padding */
215 pad_len = 16 - clear_len % 16;
216 enc_len = clear_len + pad_len;
217 for( i = clear_len; i < enc_len; i++ )
218 state[i] = (unsigned char) pad_len;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200219
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200220 /* Encrypt */
221 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->enc, AES_ENCRYPT,
222 enc_len, iv, state, state ) ) != 0 )
223 {
224 return( ret );
225 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200226
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200227 /* Write length */
228 *p++ = (unsigned char)( ( enc_len >> 8 ) & 0xFF );
229 *p++ = (unsigned char)( ( enc_len ) & 0xFF );
230 p = state + enc_len;
231
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200232 /* Compute and write MAC( key_name + iv + enc_state_len + enc_state ) */
233 sha256_hmac( ssl->ticket_keys->mac_key, 16, start, p - start, p, 0 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200234 p += 32;
235
236 *tlen = p - start;
237
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200238 SSL_DEBUG_BUF( 3, "session ticket structure", start, *tlen );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200239
240 return( 0 );
241}
242
243/*
244 * Load session ticket (see ssl_write_ticket for structure)
245 */
246static int ssl_parse_ticket( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200247 unsigned char *buf,
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200248 size_t len )
249{
250 int ret;
251 ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200252 unsigned char *key_name = buf;
253 unsigned char *iv = buf + 16;
254 unsigned char *enc_len_p = iv + 16;
255 unsigned char *ticket = enc_len_p + 2;
256 unsigned char *mac;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200257 unsigned char computed_mac[16];
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200258 size_t enc_len, clear_len, i;
259 unsigned char pad_len;
260
261 SSL_DEBUG_BUF( 3, "session ticket structure", buf, len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200262
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200263 if( len < 34 || ssl->ticket_keys == NULL )
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200264 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
265
266 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
267 mac = ticket + enc_len;
268
269 if( len != enc_len + 66 )
270 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
271
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200272 /* Check name */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200273 if( memcmp( key_name, ssl->ticket_keys->key_name, 16 ) != 0 )
274 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200275
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200276 /* Check mac */
277 sha256_hmac( ssl->ticket_keys->mac_key, 16, buf, len - 32,
278 computed_mac, 0 );
279 ret = 0;
280 for( i = 0; i < 32; i++ )
281 if( mac[i] != computed_mac[i] )
282 ret = POLARSSL_ERR_SSL_INVALID_MAC;
283 if( ret != 0 )
284 return( ret );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200285
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200286 /* Decrypt */
287 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->dec, AES_DECRYPT,
288 enc_len, iv, ticket, ticket ) ) != 0 )
289 {
290 return( ret );
291 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200292
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200293 /* Check PKCS padding */
294 pad_len = ticket[enc_len - 1];
295
296 ret = 0;
297 for( i = 2; i < pad_len; i++ )
298 if( ticket[enc_len - i] != pad_len )
299 ret = POLARSSL_ERR_SSL_BAD_INPUT_DATA;
300 if( ret != 0 )
301 return( ret );
302
303 clear_len = enc_len - pad_len;
304
305 SSL_DEBUG_BUF( 3, "session ticket cleartext", ticket, clear_len );
306
307 /* Actually load session */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200308 if( ( ret = ssl_load_session( &session, ticket, clear_len ) ) != 0 )
309 {
310 SSL_DEBUG_MSG( 1, ( "failed to parse ticket content" ) );
311 memset( &session, 0, sizeof( ssl_session ) );
312 return( ret );
313 }
314
Paul Bakker606b4ba2013-08-14 16:52:14 +0200315#if defined(POLARSSL_HAVE_TIME)
316 /* Check if still valid */
317 if( (int) ( time( NULL) - session.start ) > ssl->ticket_lifetime )
318 {
319 SSL_DEBUG_MSG( 1, ( "session ticket expired" ) );
320 memset( &session, 0, sizeof( ssl_session ) );
321 return( POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED );
322 }
323#endif
324
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200325 /*
326 * Keep the session ID sent by the client, since we MUST send it back to
327 * inform him we're accepting the ticket (RFC 5077 section 3.4)
328 */
329 session.length = ssl->session_negotiate->length;
330 memcpy( &session.id, ssl->session_negotiate->id, session.length );
331
332 ssl_session_free( ssl->session_negotiate );
333 memcpy( ssl->session_negotiate, &session, sizeof( ssl_session ) );
334 memset( &session, 0, sizeof( ssl_session ) );
335
336 return( 0 );
337}
Paul Bakkera503a632013-08-14 13:48:06 +0200338#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200339
Paul Bakker0be444a2013-08-27 21:55:01 +0200340#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +0000341static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000342 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000343 size_t len )
344{
345 int ret;
346 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000347 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000348
349 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
350 if( servername_list_size + 2 != len )
351 {
352 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
353 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
354 }
355
356 p = buf + 2;
357 while( servername_list_size > 0 )
358 {
359 hostname_len = ( ( p[1] << 8 ) | p[2] );
360 if( hostname_len + 3 > servername_list_size )
361 {
362 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
363 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
364 }
365
366 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
367 {
368 ret = ssl->f_sni( ssl->p_sni, ssl, p + 3, hostname_len );
369 if( ret != 0 )
370 {
371 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
372 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
373 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
374 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000375 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000376 }
377
378 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000379 p += hostname_len + 3;
380 }
381
382 if( servername_list_size != 0 )
383 {
384 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
385 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000386 }
387
388 return( 0 );
389}
Paul Bakker0be444a2013-08-27 21:55:01 +0200390#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000391
Paul Bakker48916f92012-09-16 19:57:18 +0000392static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000393 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000394 size_t len )
395{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000396 int ret;
397
Paul Bakker48916f92012-09-16 19:57:18 +0000398 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
399 {
400 if( len != 1 || buf[0] != 0x0 )
401 {
402 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000403
404 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
405 return( ret );
406
Paul Bakker48916f92012-09-16 19:57:18 +0000407 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
408 }
409
410 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
411 }
412 else
413 {
414 if( len != 1 + ssl->verify_data_len ||
415 buf[0] != ssl->verify_data_len ||
416 memcmp( buf + 1, ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
417 {
418 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000419
420 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
421 return( ret );
422
Paul Bakker48916f92012-09-16 19:57:18 +0000423 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
424 }
425 }
426
427 return( 0 );
428}
429
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200430#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +0000431static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
432 const unsigned char *buf,
433 size_t len )
434{
435 size_t sig_alg_list_size;
436 const unsigned char *p;
437
438 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
439 if( sig_alg_list_size + 2 != len ||
440 sig_alg_list_size %2 != 0 )
441 {
442 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
443 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
444 }
445
446 p = buf + 2;
447 while( sig_alg_list_size > 0 )
448 {
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200449 /*
450 * For now, just ignore signature algorithm and rely on offered
451 * ciphersuites only. To be fixed later.
452 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200453#if defined(POLARSSL_SHA512_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000454 if( p[0] == SSL_HASH_SHA512 )
455 {
456 ssl->handshake->sig_alg = SSL_HASH_SHA512;
457 break;
458 }
459 if( p[0] == SSL_HASH_SHA384 )
460 {
461 ssl->handshake->sig_alg = SSL_HASH_SHA384;
462 break;
463 }
464#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200465#if defined(POLARSSL_SHA256_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000466 if( p[0] == SSL_HASH_SHA256 )
467 {
468 ssl->handshake->sig_alg = SSL_HASH_SHA256;
469 break;
470 }
471 if( p[0] == SSL_HASH_SHA224 )
472 {
473 ssl->handshake->sig_alg = SSL_HASH_SHA224;
474 break;
475 }
476#endif
477 if( p[0] == SSL_HASH_SHA1 )
478 {
479 ssl->handshake->sig_alg = SSL_HASH_SHA1;
480 break;
481 }
482 if( p[0] == SSL_HASH_MD5 )
483 {
484 ssl->handshake->sig_alg = SSL_HASH_MD5;
485 break;
486 }
487
488 sig_alg_list_size -= 2;
489 p += 2;
490 }
491
492 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
493 ssl->handshake->sig_alg ) );
494
495 return( 0 );
496}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200497#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker23f36802012-09-28 14:15:14 +0000498
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200499#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200500static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
501 const unsigned char *buf,
502 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100503{
504 size_t list_size;
505 const unsigned char *p;
506
507 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
508 if( list_size + 2 != len ||
509 list_size % 2 != 0 )
510 {
511 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
512 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
513 }
514
515 p = buf + 2;
516 while( list_size > 0 )
517 {
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200518#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
519 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP192R1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100520 {
521 ssl->handshake->ec_curve = p[1];
522 return( 0 );
523 }
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200524#endif
525#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
526 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP224R1 )
527 {
528 ssl->handshake->ec_curve = p[1];
529 return( 0 );
530 }
531#endif
532#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
533 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP256R1 )
534 {
535 ssl->handshake->ec_curve = p[1];
536 return( 0 );
537 }
538#endif
539#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
540 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP384R1 )
541 {
542 ssl->handshake->ec_curve = p[1];
543 return( 0 );
544 }
545#endif
546#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
547 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP521R1 )
548 {
549 ssl->handshake->ec_curve = p[1];
550 return( 0 );
551 }
552#endif
Paul Bakker41c83d32013-03-20 14:39:14 +0100553
554 list_size -= 2;
555 p += 2;
556 }
557
558 return( 0 );
559}
560
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200561static int ssl_parse_supported_point_formats( ssl_context *ssl,
562 const unsigned char *buf,
563 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100564{
565 size_t list_size;
566 const unsigned char *p;
567
568 list_size = buf[0];
569 if( list_size + 1 != len )
570 {
571 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
572 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
573 }
574
575 p = buf + 2;
576 while( list_size > 0 )
577 {
578 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
579 p[0] == POLARSSL_ECP_PF_COMPRESSED )
580 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200581 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200582 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100583 return( 0 );
584 }
585
586 list_size--;
587 p++;
588 }
589
590 return( 0 );
591}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200592#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100593
Paul Bakker05decb22013-08-15 13:33:48 +0200594#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200595static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
596 const unsigned char *buf,
597 size_t len )
598{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200599 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200600 {
601 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
602 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
603 }
604
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200605 ssl->session_negotiate->mfl_code = buf[0];
606
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200607 return( 0 );
608}
Paul Bakker05decb22013-08-15 13:33:48 +0200609#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200610
Paul Bakker1f2bc622013-08-15 13:45:55 +0200611#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200612static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
613 const unsigned char *buf,
614 size_t len )
615{
616 if( len != 0 )
617 {
618 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
619 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
620 }
621
622 ((void) buf);
623
624 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
625
626 return( 0 );
627}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200628#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200629
Paul Bakkera503a632013-08-14 13:48:06 +0200630#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200631static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200632 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200633 size_t len )
634{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200635 int ret;
636
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200637 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
638 return( 0 );
639
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200640 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200641 ssl->handshake->new_session_ticket = 1;
642
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200643 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
644
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200645 if( len == 0 )
646 return( 0 );
647
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200648 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
649 {
650 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
651 return( 0 );
652 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200653
654 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200655 * Failures are ok: just ignore the ticket and proceed.
656 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200657 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
658 {
659 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200660 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200661 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200662
663 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
664
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200665 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200666
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200667 /* Don't send a new ticket after all, this one is OK */
668 ssl->handshake->new_session_ticket = 0;
669
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200670 return( 0 );
671}
Paul Bakkera503a632013-08-14 13:48:06 +0200672#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200673
Paul Bakker78a8c712013-03-06 17:01:52 +0100674#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
675static int ssl_parse_client_hello_v2( ssl_context *ssl )
676{
677 int ret;
678 unsigned int i, j;
679 size_t n;
680 unsigned int ciph_len, sess_len, chal_len;
681 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200682 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200683 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100684
685 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
686
687 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
688 {
689 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
690
691 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
692 return( ret );
693
694 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
695 }
696
697 buf = ssl->in_hdr;
698
699 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
700
701 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
702 buf[2] ) );
703 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
704 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
705 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
706 buf[3], buf[4] ) );
707
708 /*
709 * SSLv2 Client Hello
710 *
711 * Record layer:
712 * 0 . 1 message length
713 *
714 * SSL layer:
715 * 2 . 2 message type
716 * 3 . 4 protocol version
717 */
718 if( buf[2] != SSL_HS_CLIENT_HELLO ||
719 buf[3] != SSL_MAJOR_VERSION_3 )
720 {
721 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
722 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
723 }
724
725 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
726
727 if( n < 17 || n > 512 )
728 {
729 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
730 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
731 }
732
733 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200734 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
735 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100736
737 if( ssl->minor_ver < ssl->min_minor_ver )
738 {
739 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
740 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
741 ssl->min_major_ver, ssl->min_minor_ver ) );
742
743 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
744 SSL_ALERT_MSG_PROTOCOL_VERSION );
745 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
746 }
747
Paul Bakker2fbefde2013-06-29 16:01:15 +0200748 ssl->handshake->max_major_ver = buf[3];
749 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100750
751 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
752 {
753 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
754 return( ret );
755 }
756
757 ssl->handshake->update_checksum( ssl, buf + 2, n );
758
759 buf = ssl->in_msg;
760 n = ssl->in_left - 5;
761
762 /*
763 * 0 . 1 ciphersuitelist length
764 * 2 . 3 session id length
765 * 4 . 5 challenge length
766 * 6 . .. ciphersuitelist
767 * .. . .. session id
768 * .. . .. challenge
769 */
770 SSL_DEBUG_BUF( 4, "record contents", buf, n );
771
772 ciph_len = ( buf[0] << 8 ) | buf[1];
773 sess_len = ( buf[2] << 8 ) | buf[3];
774 chal_len = ( buf[4] << 8 ) | buf[5];
775
776 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
777 ciph_len, sess_len, chal_len ) );
778
779 /*
780 * Make sure each parameter length is valid
781 */
782 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
783 {
784 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
785 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
786 }
787
788 if( sess_len > 32 )
789 {
790 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
791 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
792 }
793
794 if( chal_len < 8 || chal_len > 32 )
795 {
796 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
797 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
798 }
799
800 if( n != 6 + ciph_len + sess_len + chal_len )
801 {
802 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
803 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
804 }
805
806 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
807 buf + 6, ciph_len );
808 SSL_DEBUG_BUF( 3, "client hello, session id",
809 buf + 6 + ciph_len, sess_len );
810 SSL_DEBUG_BUF( 3, "client hello, challenge",
811 buf + 6 + ciph_len + sess_len, chal_len );
812
813 p = buf + 6 + ciph_len;
814 ssl->session_negotiate->length = sess_len;
815 memset( ssl->session_negotiate->id, 0, sizeof( ssl->session_negotiate->id ) );
816 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
817
818 p += sess_len;
819 memset( ssl->handshake->randbytes, 0, 64 );
820 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
821
822 /*
823 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
824 */
825 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
826 {
827 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
828 {
829 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
830 if( ssl->renegotiation == SSL_RENEGOTIATION )
831 {
832 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
833
834 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
835 return( ret );
836
837 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
838 }
839 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
840 break;
841 }
842 }
843
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200844 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
845 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +0100846 {
847 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
848 {
Paul Bakker41c83d32013-03-20 14:39:14 +0100849 // Only allow non-ECC ciphersuites as we do not have extensions
850 //
Paul Bakker59c28a22013-06-29 15:33:42 +0200851 if( p[0] == 0 && p[1] == 0 &&
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200852 ( ( ciphersuites[i] >> 8 ) & 0xFF ) == 0 &&
853 p[2] == ( ciphersuites[i] & 0xFF ) )
Paul Bakker59c28a22013-06-29 15:33:42 +0200854 {
855 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
856
857 if( ciphersuite_info == NULL )
858 {
859 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %02x not found",
860 ciphersuites[i] ) );
861 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
862 }
863
Paul Bakker2fbefde2013-06-29 16:01:15 +0200864 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
865 ciphersuite_info->max_minor_ver < ssl->minor_ver )
866 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +0200867
Paul Bakker78a8c712013-03-06 17:01:52 +0100868 goto have_ciphersuite_v2;
Paul Bakker59c28a22013-06-29 15:33:42 +0200869 }
Paul Bakker78a8c712013-03-06 17:01:52 +0100870 }
871 }
872
873 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
874
875 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
876
877have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200878 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +0200879 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +0100880 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +0100881
882 /*
883 * SSLv2 Client Hello relevant renegotiation security checks
884 */
885 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
886 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
887 {
888 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
889
890 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
891 return( ret );
892
893 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
894 }
895
896 ssl->in_left = 0;
897 ssl->state++;
898
899 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
900
901 return( 0 );
902}
903#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
904
Paul Bakker5121ce52009-01-03 21:22:43 +0000905static int ssl_parse_client_hello( ssl_context *ssl )
906{
Paul Bakker23986e52011-04-24 08:57:21 +0000907 int ret;
908 unsigned int i, j;
909 size_t n;
910 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +0000911 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +0000912 unsigned int ext_len = 0;
913 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000914 int renegotiation_info_seen = 0;
915 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200916 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +0100917 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +0200918 pk_type_t pk_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +0000919
920 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
921
Paul Bakker48916f92012-09-16 19:57:18 +0000922 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
923 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000924 {
925 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
926 return( ret );
927 }
928
929 buf = ssl->in_hdr;
930
Paul Bakker78a8c712013-03-06 17:01:52 +0100931#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
932 if( ( buf[0] & 0x80 ) != 0 )
933 return ssl_parse_client_hello_v2( ssl );
934#endif
935
Paul Bakkerec636f32012-09-09 19:17:02 +0000936 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
937
938 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
939 buf[0] ) );
940 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
941 ( buf[3] << 8 ) | buf[4] ) );
942 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
943 buf[1], buf[2] ) );
944
945 /*
946 * SSLv3 Client Hello
947 *
948 * Record layer:
949 * 0 . 0 message type
950 * 1 . 2 protocol version
951 * 3 . 4 message length
952 */
953 if( buf[0] != SSL_MSG_HANDSHAKE ||
954 buf[1] != SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000955 {
Paul Bakkerec636f32012-09-09 19:17:02 +0000956 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
957 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
958 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000959
Paul Bakkerec636f32012-09-09 19:17:02 +0000960 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000961
Manuel Pégourié-Gonnard72882b22013-08-02 13:36:00 +0200962 if( n < 45 || n > 2048 )
Paul Bakkerec636f32012-09-09 19:17:02 +0000963 {
964 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
965 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
966 }
967
Paul Bakker48916f92012-09-16 19:57:18 +0000968 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
969 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +0000970 {
971 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
972 return( ret );
973 }
974
975 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +0000976 if( !ssl->renegotiation )
977 n = ssl->in_left - 5;
978 else
979 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +0000980
Paul Bakker48916f92012-09-16 19:57:18 +0000981 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +0000982
983 /*
984 * SSL layer:
985 * 0 . 0 handshake type
986 * 1 . 3 handshake length
987 * 4 . 5 protocol version
988 * 6 . 9 UNIX time()
989 * 10 . 37 random bytes
990 * 38 . 38 session id length
991 * 39 . 38+x session id
992 * 39+x . 40+x ciphersuitelist length
993 * 41+x . .. ciphersuitelist
994 * .. . .. compression alg.
995 * .. . .. extensions
996 */
997 SSL_DEBUG_BUF( 4, "record contents", buf, n );
998
999 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1000 buf[0] ) );
1001 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1002 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1003 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1004 buf[4], buf[5] ) );
1005
1006 /*
1007 * Check the handshake type and protocol version
1008 */
1009 if( buf[0] != SSL_HS_CLIENT_HELLO ||
1010 buf[4] != SSL_MAJOR_VERSION_3 )
1011 {
1012 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1013 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1014 }
1015
1016 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +02001017 ssl->minor_ver = ( buf[5] <= ssl->max_minor_ver )
1018 ? buf[5] : ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001019
Paul Bakker1d29fb52012-09-28 13:28:45 +00001020 if( ssl->minor_ver < ssl->min_minor_ver )
1021 {
1022 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
1023 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001024 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001025
1026 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1027 SSL_ALERT_MSG_PROTOCOL_VERSION );
1028
1029 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1030 }
1031
Paul Bakker2fbefde2013-06-29 16:01:15 +02001032 ssl->handshake->max_major_ver = buf[4];
1033 ssl->handshake->max_minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001034
Paul Bakker48916f92012-09-16 19:57:18 +00001035 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001036
1037 /*
1038 * Check the handshake message length
1039 */
1040 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1041 {
1042 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1043 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1044 }
1045
1046 /*
1047 * Check the session length
1048 */
1049 sess_len = buf[38];
1050
1051 if( sess_len > 32 )
1052 {
1053 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1054 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1055 }
1056
Paul Bakker48916f92012-09-16 19:57:18 +00001057 ssl->session_negotiate->length = sess_len;
1058 memset( ssl->session_negotiate->id, 0,
1059 sizeof( ssl->session_negotiate->id ) );
1060 memcpy( ssl->session_negotiate->id, buf + 39,
1061 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001062
1063 /*
1064 * Check the ciphersuitelist length
1065 */
1066 ciph_len = ( buf[39 + sess_len] << 8 )
1067 | ( buf[40 + sess_len] );
1068
1069 if( ciph_len < 2 || ciph_len > 256 || ( ciph_len % 2 ) != 0 )
1070 {
1071 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1072 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1073 }
1074
1075 /*
1076 * Check the compression algorithms length
1077 */
1078 comp_len = buf[41 + sess_len + ciph_len];
1079
1080 if( comp_len < 1 || comp_len > 16 )
1081 {
1082 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1083 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1084 }
1085
Paul Bakker48916f92012-09-16 19:57:18 +00001086 /*
1087 * Check the extension length
1088 */
1089 if( n > 42 + sess_len + ciph_len + comp_len )
1090 {
1091 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1092 | ( buf[43 + sess_len + ciph_len + comp_len] );
1093
1094 if( ( ext_len > 0 && ext_len < 4 ) ||
1095 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1096 {
1097 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1098 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1099 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1100 }
1101 }
1102
1103 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001104#if defined(POLARSSL_ZLIB_SUPPORT)
1105 for( i = 0; i < comp_len; ++i )
1106 {
Paul Bakker48916f92012-09-16 19:57:18 +00001107 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001108 {
Paul Bakker48916f92012-09-16 19:57:18 +00001109 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001110 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001111 }
1112 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001113#endif
1114
Paul Bakkerec636f32012-09-09 19:17:02 +00001115 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1116 buf + 6, 32 );
1117 SSL_DEBUG_BUF( 3, "client hello, session id",
1118 buf + 38, sess_len );
1119 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1120 buf + 41 + sess_len, ciph_len );
1121 SSL_DEBUG_BUF( 3, "client hello, compression",
1122 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001123
Paul Bakkerec636f32012-09-09 19:17:02 +00001124 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001125 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1126 */
1127 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1128 {
1129 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1130 {
1131 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1132 if( ssl->renegotiation == SSL_RENEGOTIATION )
1133 {
1134 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001135
1136 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1137 return( ret );
1138
Paul Bakker48916f92012-09-16 19:57:18 +00001139 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1140 }
1141 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1142 break;
1143 }
1144 }
1145
Paul Bakker48916f92012-09-16 19:57:18 +00001146 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001147
1148 while( ext_len )
1149 {
1150 unsigned int ext_id = ( ( ext[0] << 8 )
1151 | ( ext[1] ) );
1152 unsigned int ext_size = ( ( ext[2] << 8 )
1153 | ( ext[3] ) );
1154
1155 if( ext_size + 4 > ext_len )
1156 {
1157 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1158 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1159 }
1160 switch( ext_id )
1161 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001162#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001163 case TLS_EXT_SERVERNAME:
1164 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1165 if( ssl->f_sni == NULL )
1166 break;
1167
1168 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1169 if( ret != 0 )
1170 return( ret );
1171 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001172#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001173
Paul Bakker48916f92012-09-16 19:57:18 +00001174 case TLS_EXT_RENEGOTIATION_INFO:
1175 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1176 renegotiation_info_seen = 1;
1177
Paul Bakker23f36802012-09-28 14:15:14 +00001178 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1179 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001180 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001181 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001182
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001183#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001184 case TLS_EXT_SIG_ALG:
1185 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1186 if( ssl->renegotiation == SSL_RENEGOTIATION )
1187 break;
1188
1189 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1190 if( ret != 0 )
1191 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001192 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001193#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001194
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001195#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001196 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1197 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1198
1199 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1200 if( ret != 0 )
1201 return( ret );
1202 break;
1203
1204 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1205 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1206
1207 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1208 if( ret != 0 )
1209 return( ret );
1210 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001211#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001212
Paul Bakker05decb22013-08-15 13:33:48 +02001213#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001214 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1215 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1216
1217 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1218 if( ret != 0 )
1219 return( ret );
1220 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001221#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001222
Paul Bakker1f2bc622013-08-15 13:45:55 +02001223#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001224 case TLS_EXT_TRUNCATED_HMAC:
1225 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1226
1227 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1228 if( ret != 0 )
1229 return( ret );
1230 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001231#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001232
Paul Bakkera503a632013-08-14 13:48:06 +02001233#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001234 case TLS_EXT_SESSION_TICKET:
1235 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1236
1237 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1238 if( ret != 0 )
1239 return( ret );
1240 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001241#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001242
Paul Bakker48916f92012-09-16 19:57:18 +00001243 default:
1244 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1245 ext_id ) );
1246 }
1247
1248 ext_len -= 4 + ext_size;
1249 ext += 4 + ext_size;
1250
1251 if( ext_len > 0 && ext_len < 4 )
1252 {
1253 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1254 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1255 }
1256 }
1257
1258 /*
1259 * Renegotiation security checks
1260 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001261 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1262 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1263 {
1264 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1265 handshake_failure = 1;
1266 }
1267 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1268 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1269 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001270 {
1271 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001272 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001273 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001274 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1275 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1276 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001277 {
1278 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001279 handshake_failure = 1;
1280 }
1281 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1282 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1283 renegotiation_info_seen == 1 )
1284 {
1285 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1286 handshake_failure = 1;
1287 }
1288
1289 if( handshake_failure == 1 )
1290 {
1291 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1292 return( ret );
1293
Paul Bakker48916f92012-09-16 19:57:18 +00001294 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1295 }
Paul Bakker380da532012-04-18 16:10:25 +00001296
Paul Bakker41c83d32013-03-20 14:39:14 +01001297 /*
1298 * Search for a matching ciphersuite
1299 * (At the end because we need information from the EC-based extensions)
1300 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001301 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
1302 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001303 {
1304 for( j = 0, p = buf + 41 + sess_len; j < ciph_len;
1305 j += 2, p += 2 )
1306 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001307 if( p[0] == ( ( ciphersuites[i] >> 8 ) & 0xFF ) &&
1308 p[1] == ( ( ciphersuites[i] ) & 0xFF ) )
Paul Bakker41c83d32013-03-20 14:39:14 +01001309 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001310 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
Paul Bakker41c83d32013-03-20 14:39:14 +01001311
1312 if( ciphersuite_info == NULL )
1313 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001314 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found",
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001315 ciphersuites[i] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001316 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1317 }
1318
Paul Bakker2fbefde2013-06-29 16:01:15 +02001319 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
1320 ciphersuite_info->max_minor_ver < ssl->minor_ver )
1321 continue;
1322
Paul Bakker5fd49172013-08-19 13:29:26 +02001323#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001324 if( ( ciphersuite_info->flags & POLARSSL_CIPHERSUITE_EC ) &&
1325 ssl->handshake->ec_curve == 0 )
1326 continue;
Paul Bakker5fd49172013-08-19 13:29:26 +02001327#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001328
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001329 /* If ciphersuite requires us to have a private key of a
1330 * certain type, make sure we do */
1331 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
1332 if( pk_alg != POLARSSL_PK_NONE &&
1333 ( ssl->pk_key == NULL ||
1334 ! pk_can_do( ssl->pk_key, pk_alg ) ) )
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001335 continue;
1336
Paul Bakker41c83d32013-03-20 14:39:14 +01001337 goto have_ciphersuite;
1338 }
1339 }
1340 }
1341
1342 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1343
1344 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1345 return( ret );
1346
1347 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1348
1349have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001350 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001351 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1352 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1353
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 ssl->in_left = 0;
1355 ssl->state++;
1356
1357 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1358
1359 return( 0 );
1360}
1361
Paul Bakker1f2bc622013-08-15 13:45:55 +02001362#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001363static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1364 unsigned char *buf,
1365 size_t *olen )
1366{
1367 unsigned char *p = buf;
1368
1369 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1370 {
1371 *olen = 0;
1372 return;
1373 }
1374
1375 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1376
1377 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1378 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1379
1380 *p++ = 0x00;
1381 *p++ = 0x00;
1382
1383 *olen = 4;
1384}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001385#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001386
Paul Bakkera503a632013-08-14 13:48:06 +02001387#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001388static void ssl_write_session_ticket_ext( ssl_context *ssl,
1389 unsigned char *buf,
1390 size_t *olen )
1391{
1392 unsigned char *p = buf;
1393
1394 if( ssl->handshake->new_session_ticket == 0 )
1395 {
1396 *olen = 0;
1397 return;
1398 }
1399
1400 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1401
1402 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1403 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1404
1405 *p++ = 0x00;
1406 *p++ = 0x00;
1407
1408 *olen = 4;
1409}
Paul Bakkera503a632013-08-14 13:48:06 +02001410#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001411
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001412static void ssl_write_renegotiation_ext( ssl_context *ssl,
1413 unsigned char *buf,
1414 size_t *olen )
1415{
1416 unsigned char *p = buf;
1417
1418 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1419 {
1420 *olen = 0;
1421 return;
1422 }
1423
1424 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1425
1426 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1427 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1428
1429 *p++ = 0x00;
1430 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1431 *p++ = ssl->verify_data_len * 2 & 0xFF;
1432
1433 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1434 p += ssl->verify_data_len;
1435 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1436 p += ssl->verify_data_len;
1437
1438 *olen = 5 + ssl->verify_data_len * 2;
1439}
1440
Paul Bakker05decb22013-08-15 13:33:48 +02001441#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001442static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1443 unsigned char *buf,
1444 size_t *olen )
1445{
1446 unsigned char *p = buf;
1447
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001448 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1449 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001450 *olen = 0;
1451 return;
1452 }
1453
1454 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1455
1456 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1457 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1458
1459 *p++ = 0x00;
1460 *p++ = 1;
1461
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001462 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001463
1464 *olen = 5;
1465}
Paul Bakker05decb22013-08-15 13:33:48 +02001466#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001467
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001468#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001469static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1470 unsigned char *buf,
1471 size_t *olen )
1472{
1473 unsigned char *p = buf;
1474 ((void) ssl);
1475
1476 *olen = 0;
1477
1478 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1479
1480 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1481 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1482
1483 *p++ = 0x00;
1484 *p++ = 2;
1485
1486 *p++ = 1;
1487 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1488
1489 *olen = 6;
1490}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001491#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001492
Paul Bakker5121ce52009-01-03 21:22:43 +00001493static int ssl_write_server_hello( ssl_context *ssl )
1494{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001495#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001496 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001497#endif
Paul Bakkera3d195c2011-11-27 21:07:34 +00001498 int ret, n;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001499 size_t olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001500 unsigned char *buf, *p;
1501
1502 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1503
1504 /*
1505 * 0 . 0 handshake type
1506 * 1 . 3 handshake length
1507 * 4 . 5 protocol version
1508 * 6 . 9 UNIX time()
1509 * 10 . 37 random bytes
1510 */
1511 buf = ssl->out_msg;
1512 p = buf + 4;
1513
1514 *p++ = (unsigned char) ssl->major_ver;
1515 *p++ = (unsigned char) ssl->minor_ver;
1516
1517 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1518 buf[4], buf[5] ) );
1519
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001520#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001521 t = time( NULL );
1522 *p++ = (unsigned char)( t >> 24 );
1523 *p++ = (unsigned char)( t >> 16 );
1524 *p++ = (unsigned char)( t >> 8 );
1525 *p++ = (unsigned char)( t );
1526
1527 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001528#else
1529 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1530 return( ret );
1531
1532 p += 4;
1533#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001534
Paul Bakkera3d195c2011-11-27 21:07:34 +00001535 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1536 return( ret );
1537
1538 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001539
Paul Bakker48916f92012-09-16 19:57:18 +00001540 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001541
1542 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1543
1544 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001545 * Resume is 0 by default, see ssl_handshake_init().
1546 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1547 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001548 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001549 if( ssl->handshake->resume == 0 &&
1550 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001551 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001552 ssl->f_get_cache != NULL &&
1553 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1554 {
1555 ssl->handshake->resume = 1;
1556 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001557
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001558 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001559 {
1560 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001561 * New session, create a new session id,
1562 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001563 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001564 ssl->state++;
1565
Paul Bakkera503a632013-08-14 13:48:06 +02001566#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001567 if( ssl->handshake->new_session_ticket == 0 )
1568 {
1569 ssl->session_negotiate->length = n = 32;
1570 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001571 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001572 return( ret );
1573 }
1574 else
1575 {
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001576 ssl->session_negotiate->length = n = 0;
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001577 memset( ssl->session_negotiate->id, 0, 32 );
1578 }
Paul Bakkera503a632013-08-14 13:48:06 +02001579#else
1580 ssl->session_negotiate->length = n = 32;
1581 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
1582 n ) ) != 0 )
1583 return( ret );
1584#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001585 }
1586 else
1587 {
1588 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001589 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001590 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001591 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001592 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001593
1594 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1595 {
1596 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1597 return( ret );
1598 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001599 }
1600
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001601 /*
1602 * 38 . 38 session id length
1603 * 39 . 38+n session id
1604 * 39+n . 40+n chosen ciphersuite
1605 * 41+n . 41+n chosen compression alg.
1606 * 42+n . 43+n extensions length
1607 * 44+n . 43+n+m extensions
1608 */
1609 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001610 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1611 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001612
1613 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1614 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1615 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001616 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001617
Paul Bakker48916f92012-09-16 19:57:18 +00001618 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1619 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1620 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001621
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001622 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: 0x%04X",
Paul Bakker48916f92012-09-16 19:57:18 +00001623 ssl->session_negotiate->ciphersuite ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001624 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001625 ssl->session_negotiate->compression ) );
1626
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001627 /*
1628 * First write extensions, then the total length
1629 */
1630 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1631 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001632
Paul Bakker05decb22013-08-15 13:33:48 +02001633#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001634 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1635 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001636#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001637
Paul Bakker1f2bc622013-08-15 13:45:55 +02001638#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001639 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1640 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001641#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001642
Paul Bakkera503a632013-08-14 13:48:06 +02001643#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001644 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1645 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001646#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001647
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001648#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001649 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1650 ext_len += olen;
1651#endif
1652
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001653 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001654
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001655 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1656 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1657 p += ext_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001658
1659 ssl->out_msglen = p - buf;
1660 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1661 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1662
1663 ret = ssl_write_record( ssl );
1664
1665 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1666
1667 return( ret );
1668}
1669
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001670#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1671 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
1672 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001673static int ssl_write_certificate_request( ssl_context *ssl )
1674{
Paul Bakkered27a042013-04-18 22:46:23 +02001675 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1676 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001677
1678 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1679
1680 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1681 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1682 {
1683 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1684 ssl->state++;
1685 return( 0 );
1686 }
1687
1688 return( ret );
1689}
1690#else
1691static int ssl_write_certificate_request( ssl_context *ssl )
1692{
1693 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1694 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001695 size_t dn_size, total_dn_size; /* excluding length bytes */
1696 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00001697 unsigned char *buf, *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001698 const x509_cert *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001699
1700 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1701
1702 ssl->state++;
1703
Paul Bakkerfbb17802013-04-17 19:10:21 +02001704 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001705 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001706 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001707 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001708 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001709 return( 0 );
1710 }
1711
1712 /*
1713 * 0 . 0 handshake type
1714 * 1 . 3 handshake length
1715 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01001716 * 5 .. m-1 cert types
1717 * m .. m+1 sig alg length (TLS 1.2 only)
1718 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001719 * n .. n+1 length of all DNs
1720 * n+2 .. n+3 length of DN 1
1721 * n+4 .. ... Distinguished Name #1
1722 * ... .. ... length of DN 2, etc.
1723 */
1724 buf = ssl->out_msg;
1725 p = buf + 4;
1726
1727 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001728 * Supported certificate types
1729 *
1730 * ClientCertificateType certificate_types<1..2^8-1>;
1731 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00001732 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001733 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01001734
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001735#if defined(POLARSSL_RSA_C)
1736 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
1737#endif
1738#if defined(POLARSSL_ECDSA_C)
1739 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
1740#endif
1741
1742 p[0] = ct_len++;
1743 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01001744
Paul Bakker577e0062013-08-28 11:57:20 +02001745 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001746#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01001747 /*
1748 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01001749 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001750 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
1751 *
1752 * struct {
1753 * HashAlgorithm hash;
1754 * SignatureAlgorithm signature;
1755 * } SignatureAndHashAlgorithm;
1756 *
1757 * enum { (255) } HashAlgorithm;
1758 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01001759 */
Paul Bakker21dca692013-01-03 11:41:08 +01001760 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01001761 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001762 /*
1763 * Only use current running hash algorithm that is already required
1764 * for requested ciphersuite.
1765 */
Paul Bakker926af752012-11-23 13:38:07 +01001766 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
1767
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001768 if( ssl->transform_negotiate->ciphersuite_info->mac ==
1769 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01001770 {
1771 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
1772 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02001773
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001774 /*
1775 * Supported signature algorithms
1776 */
1777#if defined(POLARSSL_RSA_C)
1778 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1779 p[2 + sa_len++] = SSL_SIG_RSA;
1780#endif
1781#if defined(POLARSSL_ECDSA_C)
1782 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1783 p[2 + sa_len++] = SSL_SIG_ECDSA;
1784#endif
Paul Bakker926af752012-11-23 13:38:07 +01001785
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001786 p[0] = (unsigned char)( sa_len >> 8 );
1787 p[1] = (unsigned char)( sa_len );
1788 sa_len += 2;
1789 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01001790 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001791#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001792
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001793 /*
1794 * DistinguishedName certificate_authorities<0..2^16-1>;
1795 * opaque DistinguishedName<1..2^16-1>;
1796 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001797 p += 2;
1798 crt = ssl->ca_chain;
1799
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001800 total_dn_size = 0;
Paul Bakker29087132010-03-21 21:03:34 +00001801 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001802 {
1803 if( p - buf > 4096 )
1804 break;
1805
Paul Bakker926af752012-11-23 13:38:07 +01001806 dn_size = crt->subject_raw.len;
1807 *p++ = (unsigned char)( dn_size >> 8 );
1808 *p++ = (unsigned char)( dn_size );
1809 memcpy( p, crt->subject_raw.p, dn_size );
1810 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001811
Paul Bakker926af752012-11-23 13:38:07 +01001812 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
1813
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001814 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01001815 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00001816 }
1817
Paul Bakker926af752012-11-23 13:38:07 +01001818 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001819 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1820 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001821 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
1822 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001823
1824 ret = ssl_write_record( ssl );
1825
1826 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
1827
1828 return( ret );
1829}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001830#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
1831 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
1832 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001833
Paul Bakker41c83d32013-03-20 14:39:14 +01001834static int ssl_write_server_key_exchange( ssl_context *ssl )
1835{
Paul Bakker23986e52011-04-24 08:57:21 +00001836 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001837 size_t n = 0, len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001838 unsigned char *p = ssl->out_msg + 4;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001839 const ssl_ciphersuite_t *ciphersuite_info;
1840
1841#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1842 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
1843 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1844 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001845 unsigned char *dig_signed = p;
1846 size_t dig_signed_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001847 ((void) dig_signed);
1848 ((void) dig_signed_len);
1849#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001850
Paul Bakker41c83d32013-03-20 14:39:14 +01001851 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001852
1853 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
1854
Paul Bakker41c83d32013-03-20 14:39:14 +01001855 if( ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_RSA &&
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001856 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_ECDHE_RSA &&
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001857 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA &&
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001858 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001859 {
1860 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
1861 ssl->state++;
1862 return( 0 );
1863 }
1864
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001865#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1866 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1867 {
1868 /* TODO: Support identity hints */
1869 *(p++) = 0x00;
1870 *(p++) = 0x00;
1871
1872 n += 2;
1873 }
1874#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1875
1876#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1877 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1878 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1879 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00001880 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001881 /*
1882 * Ephemeral DH parameters:
1883 *
1884 * struct {
1885 * opaque dh_p<1..2^16-1>;
1886 * opaque dh_g<1..2^16-1>;
1887 * opaque dh_Ys<1..2^16-1>;
1888 * } ServerDHParams;
1889 */
1890 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
1891 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
1892 {
1893 SSL_DEBUG_RET( 1, "mpi_copy", ret );
1894 return( ret );
1895 }
Paul Bakker48916f92012-09-16 19:57:18 +00001896
Paul Bakker41c83d32013-03-20 14:39:14 +01001897 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
1898 mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001899 p,
1900 &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001901 {
1902 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
1903 return( ret );
1904 }
1905
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001906 dig_signed = p;
1907 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001908
1909 p += len;
1910 n += len;
1911
Paul Bakker41c83d32013-03-20 14:39:14 +01001912 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
1913 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1914 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1915 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
1916 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001917#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1918 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001919
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001920#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1921 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1922 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1923 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001924 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001925 /*
1926 * Ephemeral ECDH parameters:
1927 *
1928 * struct {
1929 * ECParameters curve_params;
1930 * ECPoint public;
1931 * } ServerECDHParams;
1932 */
Paul Bakker41c83d32013-03-20 14:39:14 +01001933 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
1934 ssl->handshake->ec_curve ) ) != 0 )
1935 {
1936 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
1937 return( ret );
1938 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001939
Paul Bakker41c83d32013-03-20 14:39:14 +01001940 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001941 &len,
1942 p,
Paul Bakker41c83d32013-03-20 14:39:14 +01001943 1000, ssl->f_rng, ssl->p_rng ) ) != 0 )
1944 {
1945 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
1946 return( ret );
1947 }
1948
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001949 dig_signed = p;
1950 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001951
1952 p += len;
1953 n += len;
1954
Paul Bakker41c83d32013-03-20 14:39:14 +01001955 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
1956 }
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001957#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1958 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001959
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001960#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001961 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1962 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001963 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001964 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1965 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001966 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001967 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001968 unsigned int hashlen = 0;
1969 unsigned char hash[64];
1970 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00001971
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001972 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001973 * Choose hash algorithm. NONE means MD5 + SHA1 here.
1974 */
Paul Bakker577e0062013-08-28 11:57:20 +02001975#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001976 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
1977 {
1978 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
1979
1980 if( md_alg == POLARSSL_MD_NONE )
1981 {
1982 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1983 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1984 }
1985 }
Paul Bakker577e0062013-08-28 11:57:20 +02001986 else
1987#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001988#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1989 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker577e0062013-08-28 11:57:20 +02001990 if ( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001991 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
1992 {
1993 md_alg = POLARSSL_MD_SHA1;
1994 }
1995 else
Paul Bakker577e0062013-08-28 11:57:20 +02001996#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001997 {
1998 md_alg = POLARSSL_MD_NONE;
1999 }
2000
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002001 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002002 * Compute the hash to be signed
2003 */
Paul Bakker577e0062013-08-28 11:57:20 +02002004#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2005 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002006 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002007 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002008 md5_context md5;
2009 sha1_context sha1;
2010
2011 /*
2012 * digitally-signed struct {
2013 * opaque md5_hash[16];
2014 * opaque sha_hash[20];
2015 * };
2016 *
2017 * md5_hash
2018 * MD5(ClientHello.random + ServerHello.random
2019 * + ServerParams);
2020 * sha_hash
2021 * SHA(ClientHello.random + ServerHello.random
2022 * + ServerParams);
2023 */
2024 md5_starts( &md5 );
2025 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002026 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002027 md5_finish( &md5, hash );
2028
2029 sha1_starts( &sha1 );
2030 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002031 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002032 sha1_finish( &sha1, hash + 16 );
2033
2034 hashlen = 36;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002035 }
2036 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002037#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2038 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002039#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2040 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002041 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002042 {
2043 md_context_t ctx;
2044
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002045 /* Info from md_alg will be used instead */
2046 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002047
2048 /*
2049 * digitally-signed struct {
2050 * opaque client_random[32];
2051 * opaque server_random[32];
2052 * ServerDHParams params;
2053 * };
2054 */
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002055 if( ( ret = md_init_ctx( &ctx, md_info_from_type(md_alg) ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002056 {
2057 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2058 return( ret );
2059 }
2060
2061 md_starts( &ctx );
2062 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002063 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002064 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02002065
2066 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
2067 {
2068 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
2069 return( ret );
2070 }
2071
Paul Bakker23f36802012-09-28 14:15:14 +00002072 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002073 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002074#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2075 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002076 {
2077 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002078 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02002079 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002080
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002081 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2082 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002083
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002084 /*
2085 * Make the signature
2086 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02002087 if( ssl->pk_key == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002088 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002089 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2090 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002091 }
Paul Bakker23f36802012-09-28 14:15:14 +00002092
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002093#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002094 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2095 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002096 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002097 *(p++) = ssl_sig_from_pk( ssl->pk_key );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002098
2099 n += 2;
2100 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002101#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002102
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002103 if( ( ret = pk_sign( ssl->pk_key, md_alg, hash, hashlen,
2104 p + 2 , &signature_len,
2105 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002106 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002107 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002108 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002109 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002110
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002111 *(p++) = (unsigned char)( signature_len >> 8 );
2112 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002113 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002114
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002115 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002116
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002117 p += signature_len;
2118 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002119 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002120#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002121 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2122 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002123
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002124 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2126 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2127
2128 ssl->state++;
2129
2130 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2131 {
2132 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2133 return( ret );
2134 }
2135
2136 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2137
2138 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002139}
2140
2141static int ssl_write_server_hello_done( ssl_context *ssl )
2142{
2143 int ret;
2144
2145 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2146
2147 ssl->out_msglen = 4;
2148 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2149 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2150
2151 ssl->state++;
2152
2153 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2154 {
2155 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2156 return( ret );
2157 }
2158
2159 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2160
2161 return( 0 );
2162}
2163
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002164#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2165 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2166static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2167 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002168{
2169 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002170 size_t n;
2171
2172 /*
2173 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2174 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002175 if( *p + 2 > end )
2176 {
2177 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2178 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2179 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002180
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002181 n = ( (*p)[0] << 8 ) | (*p)[1];
2182 *p += 2;
2183
2184 if( n < 1 || n > ssl->handshake->dhm_ctx.len || *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002185 {
2186 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2187 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2188 }
2189
2190 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002191 *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002192 {
2193 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2194 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2195 }
2196
2197 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2198
Paul Bakker70df2fb2013-04-17 17:19:09 +02002199 return( ret );
2200}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002201#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2202 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002203
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02002204#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2205 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002206static int ssl_parse_client_ecdh_public( ssl_context *ssl )
2207{
2208 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002209 size_t n;
2210
2211 /*
2212 * Receive client public key and calculate premaster
2213 */
2214 n = ssl->in_msg[3];
2215
2216 if( n < 1 || n > mpi_size( &ssl->handshake->ecdh_ctx.grp.P ) * 2 + 2 ||
2217 n + 4 != ssl->in_hslen )
2218 {
2219 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2220 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2221 }
2222
2223 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2224 ssl->in_msg + 4, n ) ) != 0 )
2225 {
2226 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2227 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2228 }
2229
2230 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2231
Paul Bakker70df2fb2013-04-17 17:19:09 +02002232 return( ret );
2233}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02002234#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2235 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002236
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002237#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002238static int ssl_parse_encrypted_pms_secret( ssl_context *ssl )
2239{
2240 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2241 size_t i, n = 0;
2242
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002243 if( ! pk_can_do( ssl->pk_key, POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002244 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002245 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002246 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2247 }
2248
2249 /*
2250 * Decrypt the premaster using own private RSA key
2251 */
2252 i = 4;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002253 n = pk_get_len( ssl->pk_key );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002254 ssl->handshake->pmslen = 48;
2255
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002256#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2257 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002258 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2259 {
2260 i += 2;
2261 if( ssl->in_msg[4] != ( ( n >> 8 ) & 0xFF ) ||
2262 ssl->in_msg[5] != ( ( n ) & 0xFF ) )
2263 {
2264 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2265 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2266 }
2267 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002268#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002269
2270 if( ssl->in_hslen != i + n )
2271 {
2272 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2273 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2274 }
2275
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002276 ret = pk_decrypt( ssl->pk_key,
2277 ssl->in_msg + i, n,
2278 ssl->handshake->premaster, &ssl->handshake->pmslen,
2279 sizeof(ssl->handshake->premaster),
2280 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002281
2282 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Paul Bakker2fbefde2013-06-29 16:01:15 +02002283 ssl->handshake->premaster[0] != ssl->handshake->max_major_ver ||
2284 ssl->handshake->premaster[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002285 {
2286 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2287
2288 /*
2289 * Protection against Bleichenbacher's attack:
2290 * invalid PKCS#1 v1.5 padding must not cause
2291 * the connection to end immediately; instead,
2292 * send a bad_record_mac later in the handshake.
2293 */
2294 ssl->handshake->pmslen = 48;
2295
2296 ret = ssl->f_rng( ssl->p_rng, ssl->handshake->premaster,
2297 ssl->handshake->pmslen );
2298 if( ret != 0 )
2299 return( ret );
2300 }
2301
2302 return( ret );
2303}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002304#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002305
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002306#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2307 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2308static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2309 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002310{
2311 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002312 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002313
2314 if( ssl->psk == NULL || ssl->psk_identity == NULL ||
2315 ssl->psk_identity_len == 0 || ssl->psk_len == 0 )
2316 {
2317 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2318 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2319 }
2320
2321 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002322 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002323 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002324 if( *p + 2 > end )
2325 {
2326 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2327 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2328 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002329
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002330 n = ( (*p)[0] << 8 ) | (*p)[1];
2331 *p += 2;
2332
2333 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002334 {
2335 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2336 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2337 }
2338
2339 if( n != ssl->psk_identity_len ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002340 memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002341 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002342 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002343 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2344 }
2345
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002346 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002347 ret = 0;
2348
Paul Bakkerfbb17802013-04-17 19:10:21 +02002349 return( ret );
2350}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002351#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
2352 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002353
Paul Bakker5121ce52009-01-03 21:22:43 +00002354static int ssl_parse_client_key_exchange( ssl_context *ssl )
2355{
Paul Bakker23986e52011-04-24 08:57:21 +00002356 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002357 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002358 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002359
Paul Bakker41c83d32013-03-20 14:39:14 +01002360 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002361
2362 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2363
2364 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2365 {
2366 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2367 return( ret );
2368 }
2369
2370 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2371 {
2372 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002373 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002374 }
2375
2376 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2377 {
2378 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002379 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002380 }
2381
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002382 p = ssl->in_msg + 4;
2383 end = ssl->in_msg + ssl->in_msglen;
2384
2385#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002386 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002387 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002388 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002389 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002390 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2391 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002392 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002393
2394 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
2395
Manuel Pégourié-Gonnard032c34e2013-09-07 13:06:27 +02002396 /* No blinding needed for DHE, but will be needed for fixed DH! */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002397 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2398 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002399 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard032c34e2013-09-07 13:06:27 +02002400 NULL, NULL ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002401 {
2402 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2403 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2404 }
2405
2406 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002407 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002408 else
2409#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002410#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2411 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2412 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2413 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002414 {
2415 if( ( ret = ssl_parse_client_ecdh_public( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002416 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002417 SSL_DEBUG_RET( 1, ( "ssl_parse_client_ecdh_public" ), ret );
2418 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002419 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002420
2421 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2422 &ssl->handshake->pmslen,
2423 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002424 POLARSSL_MPI_MAX_SIZE,
2425 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002426 {
2427 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2428 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2429 }
2430
2431 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002432 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002433 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002434#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2435 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002436#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2437 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002438 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002439 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002440 {
2441 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2442 return( ret );
2443 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002444
2445 // Set up the premaster secret
2446 //
2447 p = ssl->handshake->premaster;
2448 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2449 *(p++) = (unsigned char)( ssl->psk_len );
2450 p += ssl->psk_len;
2451
2452 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2453 *(p++) = (unsigned char)( ssl->psk_len );
2454 memcpy( p, ssl->psk, ssl->psk_len );
2455 p += ssl->psk_len;
2456
2457 ssl->handshake->pmslen = 4 + 2 * ssl->psk_len;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002458 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002459 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002460#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
2461#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2462 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2463 {
2464 size_t n;
2465
2466 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2467 {
2468 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2469 return( ret );
2470 }
2471 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2472 {
2473 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2474 return( ret );
2475 }
2476
2477 // Set up the premaster secret
2478 //
2479 p = ssl->handshake->premaster;
2480 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len >> 8 );
2481 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len );
2482
Paul Bakker577e0062013-08-28 11:57:20 +02002483 n = ssl->handshake->dhm_ctx.len;
2484
Manuel Pégourié-Gonnard032c34e2013-09-07 13:06:27 +02002485 /* No blinding needed since this is ephemeral DHM */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002486 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard032c34e2013-09-07 13:06:27 +02002487 p, &n, NULL, NULL ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002488 {
2489 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2490 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2491 }
2492
2493 if( n != ssl->handshake->dhm_ctx.len )
2494 {
2495 SSL_DEBUG_MSG( 1, ( "dhm_calc_secret result smaller than DHM" ) );
2496 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2497 }
2498
2499 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2500
2501 p += ssl->handshake->dhm_ctx.len;
2502
2503 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2504 *(p++) = (unsigned char)( ssl->psk_len );
2505 memcpy( p, ssl->psk, ssl->psk_len );
2506 p += ssl->psk_len;
2507
2508 ssl->handshake->pmslen = 4 + ssl->handshake->dhm_ctx.len + ssl->psk_len;
2509 }
2510 else
2511#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
2512#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2513 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002514 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002515 if( ( ret = ssl_parse_encrypted_pms_secret( ssl ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002516 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002517 SSL_DEBUG_RET( 1, ( "ssl_parse_client_ecdh_public" ), ret );
2518 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002519 }
2520 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002521 else
2522#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2523 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002524 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002525 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2526 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002527
Paul Bakkerff60ee62010-03-16 21:09:09 +00002528 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2529 {
2530 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2531 return( ret );
2532 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002533
Paul Bakker5121ce52009-01-03 21:22:43 +00002534 ssl->state++;
2535
2536 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2537
2538 return( 0 );
2539}
2540
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002541#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2542 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2543 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002544static int ssl_parse_certificate_verify( ssl_context *ssl )
2545{
Paul Bakkered27a042013-04-18 22:46:23 +02002546 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002547 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002548
2549 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2550
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002551 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2552 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002553 {
2554 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2555 ssl->state++;
2556 return( 0 );
2557 }
2558
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002559 return( ret );
2560}
2561#else
2562static int ssl_parse_certificate_verify( ssl_context *ssl )
2563{
2564 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002565 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002566 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002567 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002568 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02002569#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002570 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02002571#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002572 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002573 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2574
2575 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2576
2577 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2578 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2579 {
2580 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2581 ssl->state++;
2582 return( 0 );
2583 }
2584
Paul Bakkered27a042013-04-18 22:46:23 +02002585 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002586 {
2587 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2588 ssl->state++;
2589 return( 0 );
2590 }
2591
Paul Bakker48916f92012-09-16 19:57:18 +00002592 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002593
2594 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2595 {
2596 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2597 return( ret );
2598 }
2599
2600 ssl->state++;
2601
2602 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2603 {
2604 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002605 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002606 }
2607
2608 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
2609 {
2610 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002611 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002612 }
2613
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002614 /*
2615 * 0 . 0 handshake type
2616 * 1 . 3 handshake length
2617 * 4 . 5 sig alg (TLS 1.2 only)
2618 * 4+n . 5+n signature length (n = sa_len)
2619 * 6+n . 6+n+m signature (m = sig_len)
2620 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002621
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002622#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2623 defined(POLARSSL_SSL_PROTO_TLS1_1)
2624 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002625 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002626 sa_len = 0;
2627
Paul Bakkerc70b9822013-04-07 22:00:46 +02002628 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002629 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002630
2631 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
2632 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
2633 POLARSSL_PK_ECDSA ) )
2634 {
2635 hash_start += 16;
2636 hashlen -= 16;
2637 md_alg = POLARSSL_MD_SHA1;
2638 }
Paul Bakker926af752012-11-23 13:38:07 +01002639 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002640 else
2641#endif
Paul Bakker577e0062013-08-28 11:57:20 +02002642#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2643 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002644 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002645 sa_len = 2;
2646
Paul Bakker5121ce52009-01-03 21:22:43 +00002647 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002648 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00002649 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002650 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002651 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002652 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2653 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01002654 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2655 }
2656
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002657 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01002658
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002659 /* Info from md_alg will be used instead */
2660 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002661
2662 /*
2663 * Signature
2664 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002665 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
2666 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002667 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002668 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2669 " for verify message" ) );
2670 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002671 }
2672
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002673 /*
2674 * Check the certificate's key type matches the signature alg
2675 */
2676 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
2677 {
2678 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
2679 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2680 }
Paul Bakker577e0062013-08-28 11:57:20 +02002681 }
2682 else
2683#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2684 {
2685 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002686 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002687 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002688
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002689 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01002690
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002691 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002692 {
2693 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002694 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002695 }
2696
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002697 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002698 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002699 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002700 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002701 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002702 return( ret );
2703 }
2704
2705 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
2706
Paul Bakkered27a042013-04-18 22:46:23 +02002707 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002708}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002709#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2710 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2711 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002712
Paul Bakkera503a632013-08-14 13:48:06 +02002713#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002714static int ssl_write_new_session_ticket( ssl_context *ssl )
2715{
2716 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002717 size_t tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002718
2719 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
2720
2721 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2722 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
2723
2724 /*
2725 * struct {
2726 * uint32 ticket_lifetime_hint;
2727 * opaque ticket<0..2^16-1>;
2728 * } NewSessionTicket;
2729 *
2730 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
2731 * 8 . 9 ticket_len (n)
2732 * 10 . 9+n ticket content
2733 */
2734 ssl->out_msg[4] = 0x00;
2735 ssl->out_msg[5] = 0x00;
2736 ssl->out_msg[6] = 0x00;
2737 ssl->out_msg[7] = 0x00;
2738
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02002739 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
2740 {
2741 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
2742 tlen = 0;
2743 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002744
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002745 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
2746 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002747
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002748 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002749
2750 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2751 {
2752 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2753 return( ret );
2754 }
2755
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002756 /* No need to remember writing a NewSessionTicket any more */
2757 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002758
2759 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
2760
2761 return( 0 );
2762}
Paul Bakkera503a632013-08-14 13:48:06 +02002763#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002764
Paul Bakker5121ce52009-01-03 21:22:43 +00002765/*
Paul Bakker1961b702013-01-25 14:49:24 +01002766 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002767 */
Paul Bakker1961b702013-01-25 14:49:24 +01002768int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002769{
2770 int ret = 0;
2771
Paul Bakker1961b702013-01-25 14:49:24 +01002772 if( ssl->state == SSL_HANDSHAKE_OVER )
2773 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002774
Paul Bakker1961b702013-01-25 14:49:24 +01002775 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
2776
2777 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2778 return( ret );
2779
2780 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002781 {
Paul Bakker1961b702013-01-25 14:49:24 +01002782 case SSL_HELLO_REQUEST:
2783 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002784 break;
2785
Paul Bakker1961b702013-01-25 14:49:24 +01002786 /*
2787 * <== ClientHello
2788 */
2789 case SSL_CLIENT_HELLO:
2790 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002791 break;
Paul Bakker1961b702013-01-25 14:49:24 +01002792
2793 /*
2794 * ==> ServerHello
2795 * Certificate
2796 * ( ServerKeyExchange )
2797 * ( CertificateRequest )
2798 * ServerHelloDone
2799 */
2800 case SSL_SERVER_HELLO:
2801 ret = ssl_write_server_hello( ssl );
2802 break;
2803
2804 case SSL_SERVER_CERTIFICATE:
2805 ret = ssl_write_certificate( ssl );
2806 break;
2807
2808 case SSL_SERVER_KEY_EXCHANGE:
2809 ret = ssl_write_server_key_exchange( ssl );
2810 break;
2811
2812 case SSL_CERTIFICATE_REQUEST:
2813 ret = ssl_write_certificate_request( ssl );
2814 break;
2815
2816 case SSL_SERVER_HELLO_DONE:
2817 ret = ssl_write_server_hello_done( ssl );
2818 break;
2819
2820 /*
2821 * <== ( Certificate/Alert )
2822 * ClientKeyExchange
2823 * ( CertificateVerify )
2824 * ChangeCipherSpec
2825 * Finished
2826 */
2827 case SSL_CLIENT_CERTIFICATE:
2828 ret = ssl_parse_certificate( ssl );
2829 break;
2830
2831 case SSL_CLIENT_KEY_EXCHANGE:
2832 ret = ssl_parse_client_key_exchange( ssl );
2833 break;
2834
2835 case SSL_CERTIFICATE_VERIFY:
2836 ret = ssl_parse_certificate_verify( ssl );
2837 break;
2838
2839 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2840 ret = ssl_parse_change_cipher_spec( ssl );
2841 break;
2842
2843 case SSL_CLIENT_FINISHED:
2844 ret = ssl_parse_finished( ssl );
2845 break;
2846
2847 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002848 * ==> ( NewSessionTicket )
2849 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002850 * Finished
2851 */
2852 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002853#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002854 if( ssl->handshake->new_session_ticket != 0 )
2855 ret = ssl_write_new_session_ticket( ssl );
2856 else
Paul Bakkera503a632013-08-14 13:48:06 +02002857#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002858 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002859 break;
2860
2861 case SSL_SERVER_FINISHED:
2862 ret = ssl_write_finished( ssl );
2863 break;
2864
2865 case SSL_FLUSH_BUFFERS:
2866 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2867 ssl->state = SSL_HANDSHAKE_WRAPUP;
2868 break;
2869
2870 case SSL_HANDSHAKE_WRAPUP:
2871 ssl_handshake_wrapup( ssl );
2872 break;
2873
2874 default:
2875 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2876 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002877 }
2878
Paul Bakker5121ce52009-01-03 21:22:43 +00002879 return( ret );
2880}
Paul Bakker5121ce52009-01-03 21:22:43 +00002881#endif