blob: 117830980d9a0019933f52072ed128761ab60f05 [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
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020050/*
51 * Serialize a session in the following format:
52 * 0 . n-1 session structure, n = sizeof(ssl_session)
53 * n . n+2 peer_cert length = m (0 if no certificate)
54 * n+3 . n+2+m peer cert ASN.1
55 *
56 * Assumes ticket is NULL (always true on server side).
57 */
58static void ssl_save_session( const ssl_session *session,
59 unsigned char *buf, size_t *olen )
60{
61 unsigned char *p = buf;
62#if defined(POLARSSL_X509_PARSE_C)
63 size_t cert_len;
64#endif /* POLARSSL_X509_PARSE_C */
65
66 memcpy( p, session, sizeof( ssl_session ) );
67 p += sizeof( ssl_session );
68
69#if defined(POLARSSL_X509_PARSE_C)
70 ((ssl_session *) buf)->peer_cert = NULL;
71
72 if( session->peer_cert == NULL )
73 cert_len = 0;
74 else
75 cert_len = session->peer_cert->raw.len;
76
77 *p++ = (unsigned char)( cert_len >> 16 & 0xFF );
78 *p++ = (unsigned char)( cert_len >> 8 & 0xFF );
79 *p++ = (unsigned char)( cert_len & 0xFF );
80
81 if( session->peer_cert != NULL )
82 memcpy( p, session->peer_cert->raw.p, cert_len );
83
84 p += cert_len;
85#endif /* POLARSSL_X509_PARSE_C */
86
87 *olen = p - buf;
88}
89
90/*
91 * Unserialise session, see ssl_save_session()
92 */
93static int ssl_load_session( ssl_session *session,
94 const unsigned char *buf, size_t len )
95{
96 int ret;
97 const unsigned char *p = buf;
98 const unsigned char * const end = buf + len;
99#if defined(POLARSSL_X509_PARSE_C)
100 size_t cert_len;
101#endif /* POLARSSL_X509_PARSE_C */
102
103 if( p + sizeof( ssl_session ) > end )
104 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
105
106 memcpy( session, p, sizeof( ssl_session ) );
107 p += sizeof( ssl_session );
108
109#if defined(POLARSSL_X509_PARSE_C)
110 if( p + 3 > end )
111 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
112
113 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
114 p += 3;
115
116 if( cert_len == 0 )
117 {
118 session->peer_cert = NULL;
119 }
120 else
121 {
122 if( p + cert_len > end )
123 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
124
125 session->peer_cert = polarssl_malloc( cert_len );
126
127 if( session->peer_cert == NULL )
128 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
129
130 memset( session->peer_cert, 0, sizeof( x509_cert ) );
131
132 if( ( ret = x509parse_crt( session->peer_cert, p, cert_len ) ) != 0 )
133 {
134 polarssl_free( session->peer_cert );
135 free( session->peer_cert );
136 session->peer_cert = NULL;
137 return( ret );
138 }
139
140 p += cert_len;
141 }
142#endif /* POLARSSL_X509_PARSE_C */
143
144 if( p != end )
145 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
146
147 return( 0 );
148}
149
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200150/*
151 * Create session ticket, secured as recommended in RFC 5077 section 4:
152 *
153 * struct {
154 * opaque key_name[16];
155 * opaque iv[16];
156 * opaque encrypted_state<0..2^16-1>;
157 * opaque mac[32];
158 * } ticket;
159 *
160 * (the internal state structure differs, however).
161 */
162static int ssl_write_ticket( ssl_context *ssl, size_t *tlen )
163{
164 unsigned char * const start = ssl->out_msg + 10;
165 unsigned char *p = start;
166 size_t clear_len, enc_len;
167
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200168 if( ssl->ticket_keys == NULL )
169 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
170
171 memcpy( p, ssl->ticket_keys->key_name, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200172 p += 16;
173
174 memset( p, 0, 16 ); // TODO: iv
175 p += 16;
176
177 ssl_save_session( ssl->session_negotiate, p + 2, &clear_len );
178 SSL_DEBUG_BUF( 4, "session ticket cleartext", p, clear_len );
179
180 // TODO: encrypt ticket
181 enc_len = clear_len;
182 (void) enc_len;
183
184 *p++ = (unsigned char)( ( clear_len >> 8 ) & 0xFF );
185 *p++ = (unsigned char)( ( clear_len ) & 0xFF );
186 p += clear_len;
187
188 memset( p, 0, 32 ); // TODO: mac
189 p += 32;
190
191 *tlen = p - start;
192
193 SSL_DEBUG_BUF( 4, "final session ticket", start, *tlen );
194
195 return( 0 );
196}
197
198/*
199 * Load session ticket (see ssl_write_ticket for structure)
200 */
201static int ssl_parse_ticket( ssl_context *ssl,
202 const unsigned char *buf,
203 size_t len )
204{
205 int ret;
206 ssl_session session;
207 const unsigned char *key_name = buf;
208 const unsigned char *iv = buf + 16;
209 const unsigned char *enc_len_p = iv + 16;
210 const unsigned char *ticket = enc_len_p + 2;
211 const unsigned char *mac;
212 size_t enc_len, clear_len;
213
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200214 if( len < 34 || ssl->ticket_keys == NULL )
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200215 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
216
217 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
218 mac = ticket + enc_len;
219
220 if( len != enc_len + 66 )
221 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
222
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200223 if( memcmp( key_name, ssl->ticket_keys->key_name, 16 ) != 0 )
224 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200225
226 // TODO: check hmac
227 (void) mac;
228
229 // TODO: decrypt ticket
230 clear_len = enc_len;
231
232 if( ( ret = ssl_load_session( &session, ticket, clear_len ) ) != 0 )
233 {
234 SSL_DEBUG_MSG( 1, ( "failed to parse ticket content" ) );
235 memset( &session, 0, sizeof( ssl_session ) );
236 return( ret );
237 }
238
239 /*
240 * Keep the session ID sent by the client, since we MUST send it back to
241 * inform him we're accepting the ticket (RFC 5077 section 3.4)
242 */
243 session.length = ssl->session_negotiate->length;
244 memcpy( &session.id, ssl->session_negotiate->id, session.length );
245
246 ssl_session_free( ssl->session_negotiate );
247 memcpy( ssl->session_negotiate, &session, sizeof( ssl_session ) );
248 memset( &session, 0, sizeof( ssl_session ) );
249
250 return( 0 );
251}
252
Paul Bakker5701cdc2012-09-27 21:49:42 +0000253static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000254 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000255 size_t len )
256{
257 int ret;
258 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000259 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000260
261 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
262 if( servername_list_size + 2 != len )
263 {
264 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
265 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
266 }
267
268 p = buf + 2;
269 while( servername_list_size > 0 )
270 {
271 hostname_len = ( ( p[1] << 8 ) | p[2] );
272 if( hostname_len + 3 > servername_list_size )
273 {
274 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
275 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
276 }
277
278 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
279 {
280 ret = ssl->f_sni( ssl->p_sni, ssl, p + 3, hostname_len );
281 if( ret != 0 )
282 {
283 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
284 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
285 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
286 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000287 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000288 }
289
290 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000291 p += hostname_len + 3;
292 }
293
294 if( servername_list_size != 0 )
295 {
296 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
297 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000298 }
299
300 return( 0 );
301}
302
Paul Bakker48916f92012-09-16 19:57:18 +0000303static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000304 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000305 size_t len )
306{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000307 int ret;
308
Paul Bakker48916f92012-09-16 19:57:18 +0000309 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
310 {
311 if( len != 1 || buf[0] != 0x0 )
312 {
313 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000314
315 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
316 return( ret );
317
Paul Bakker48916f92012-09-16 19:57:18 +0000318 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
319 }
320
321 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
322 }
323 else
324 {
325 if( len != 1 + ssl->verify_data_len ||
326 buf[0] != ssl->verify_data_len ||
327 memcmp( buf + 1, ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
328 {
329 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000330
331 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
332 return( ret );
333
Paul Bakker48916f92012-09-16 19:57:18 +0000334 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
335 }
336 }
337
338 return( 0 );
339}
340
Paul Bakker23f36802012-09-28 14:15:14 +0000341static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
342 const unsigned char *buf,
343 size_t len )
344{
345 size_t sig_alg_list_size;
346 const unsigned char *p;
347
348 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
349 if( sig_alg_list_size + 2 != len ||
350 sig_alg_list_size %2 != 0 )
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( sig_alg_list_size > 0 )
358 {
359 if( p[1] != SSL_SIG_RSA )
Paul Bakker8611e732012-10-30 07:52:29 +0000360 {
361 sig_alg_list_size -= 2;
362 p += 2;
Paul Bakker23f36802012-09-28 14:15:14 +0000363 continue;
Paul Bakker8611e732012-10-30 07:52:29 +0000364 }
Paul Bakker9e36f042013-06-30 14:34:05 +0200365#if defined(POLARSSL_SHA512_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000366 if( p[0] == SSL_HASH_SHA512 )
367 {
368 ssl->handshake->sig_alg = SSL_HASH_SHA512;
369 break;
370 }
371 if( p[0] == SSL_HASH_SHA384 )
372 {
373 ssl->handshake->sig_alg = SSL_HASH_SHA384;
374 break;
375 }
376#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200377#if defined(POLARSSL_SHA256_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000378 if( p[0] == SSL_HASH_SHA256 )
379 {
380 ssl->handshake->sig_alg = SSL_HASH_SHA256;
381 break;
382 }
383 if( p[0] == SSL_HASH_SHA224 )
384 {
385 ssl->handshake->sig_alg = SSL_HASH_SHA224;
386 break;
387 }
388#endif
389 if( p[0] == SSL_HASH_SHA1 )
390 {
391 ssl->handshake->sig_alg = SSL_HASH_SHA1;
392 break;
393 }
394 if( p[0] == SSL_HASH_MD5 )
395 {
396 ssl->handshake->sig_alg = SSL_HASH_MD5;
397 break;
398 }
399
400 sig_alg_list_size -= 2;
401 p += 2;
402 }
403
404 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
405 ssl->handshake->sig_alg ) );
406
407 return( 0 );
408}
409
Paul Bakker41c83d32013-03-20 14:39:14 +0100410#if defined(POLARSSL_ECP_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200411static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
412 const unsigned char *buf,
413 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100414{
415 size_t list_size;
416 const unsigned char *p;
417
418 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
419 if( list_size + 2 != len ||
420 list_size % 2 != 0 )
421 {
422 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
423 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
424 }
425
426 p = buf + 2;
427 while( list_size > 0 )
428 {
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200429#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
430 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP192R1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100431 {
432 ssl->handshake->ec_curve = p[1];
433 return( 0 );
434 }
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200435#endif
436#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
437 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP224R1 )
438 {
439 ssl->handshake->ec_curve = p[1];
440 return( 0 );
441 }
442#endif
443#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
444 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP256R1 )
445 {
446 ssl->handshake->ec_curve = p[1];
447 return( 0 );
448 }
449#endif
450#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
451 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP384R1 )
452 {
453 ssl->handshake->ec_curve = p[1];
454 return( 0 );
455 }
456#endif
457#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
458 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP521R1 )
459 {
460 ssl->handshake->ec_curve = p[1];
461 return( 0 );
462 }
463#endif
Paul Bakker41c83d32013-03-20 14:39:14 +0100464
465 list_size -= 2;
466 p += 2;
467 }
468
469 return( 0 );
470}
471
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200472static int ssl_parse_supported_point_formats( ssl_context *ssl,
473 const unsigned char *buf,
474 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100475{
476 size_t list_size;
477 const unsigned char *p;
478
479 list_size = buf[0];
480 if( list_size + 1 != len )
481 {
482 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
483 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
484 }
485
486 p = buf + 2;
487 while( list_size > 0 )
488 {
489 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
490 p[0] == POLARSSL_ECP_PF_COMPRESSED )
491 {
492 ssl->handshake->ec_point_format = p[0];
493 return( 0 );
494 }
495
496 list_size--;
497 p++;
498 }
499
500 return( 0 );
501}
502#endif /* POLARSSL_ECP_C */
503
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200504static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
505 const unsigned char *buf,
506 size_t len )
507{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200508 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200509 {
510 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
511 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
512 }
513
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200514 ssl->session_negotiate->mfl_code = buf[0];
515
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200516 return( 0 );
517}
518
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200519static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
520 const unsigned char *buf,
521 size_t len )
522{
523 if( len != 0 )
524 {
525 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
526 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
527 }
528
529 ((void) buf);
530
531 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
532
533 return( 0 );
534}
535
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200536static int ssl_parse_session_ticket_ext( ssl_context *ssl,
537 const unsigned char *buf,
538 size_t len )
539{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200540 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
541 return( 0 );
542
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200543 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200544 ssl->handshake->new_session_ticket = 1;
545
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200546 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
547
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200548 if( len == 0 )
549 return( 0 );
550
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200551 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
552 {
553 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
554 return( 0 );
555 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200556
557 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200558 * Failures are ok: just ignore the ticket and proceed.
559 */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200560 if( ssl_parse_ticket( ssl, buf, len ) != 0 )
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200561 return( 0 );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200562
563 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
564
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200565 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200566
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200567 /* Don't send a new ticket after all, this one is OK */
568 ssl->handshake->new_session_ticket = 0;
569
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200570 return( 0 );
571}
572
Paul Bakker78a8c712013-03-06 17:01:52 +0100573#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
574static int ssl_parse_client_hello_v2( ssl_context *ssl )
575{
576 int ret;
577 unsigned int i, j;
578 size_t n;
579 unsigned int ciph_len, sess_len, chal_len;
580 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200581 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200582 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100583
584 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
585
586 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
587 {
588 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
589
590 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
591 return( ret );
592
593 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
594 }
595
596 buf = ssl->in_hdr;
597
598 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
599
600 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
601 buf[2] ) );
602 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
603 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
604 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
605 buf[3], buf[4] ) );
606
607 /*
608 * SSLv2 Client Hello
609 *
610 * Record layer:
611 * 0 . 1 message length
612 *
613 * SSL layer:
614 * 2 . 2 message type
615 * 3 . 4 protocol version
616 */
617 if( buf[2] != SSL_HS_CLIENT_HELLO ||
618 buf[3] != SSL_MAJOR_VERSION_3 )
619 {
620 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
621 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
622 }
623
624 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
625
626 if( n < 17 || n > 512 )
627 {
628 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
629 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
630 }
631
632 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200633 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
634 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100635
636 if( ssl->minor_ver < ssl->min_minor_ver )
637 {
638 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
639 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
640 ssl->min_major_ver, ssl->min_minor_ver ) );
641
642 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
643 SSL_ALERT_MSG_PROTOCOL_VERSION );
644 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
645 }
646
Paul Bakker2fbefde2013-06-29 16:01:15 +0200647 ssl->handshake->max_major_ver = buf[3];
648 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100649
650 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
651 {
652 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
653 return( ret );
654 }
655
656 ssl->handshake->update_checksum( ssl, buf + 2, n );
657
658 buf = ssl->in_msg;
659 n = ssl->in_left - 5;
660
661 /*
662 * 0 . 1 ciphersuitelist length
663 * 2 . 3 session id length
664 * 4 . 5 challenge length
665 * 6 . .. ciphersuitelist
666 * .. . .. session id
667 * .. . .. challenge
668 */
669 SSL_DEBUG_BUF( 4, "record contents", buf, n );
670
671 ciph_len = ( buf[0] << 8 ) | buf[1];
672 sess_len = ( buf[2] << 8 ) | buf[3];
673 chal_len = ( buf[4] << 8 ) | buf[5];
674
675 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
676 ciph_len, sess_len, chal_len ) );
677
678 /*
679 * Make sure each parameter length is valid
680 */
681 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
682 {
683 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
684 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
685 }
686
687 if( sess_len > 32 )
688 {
689 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
690 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
691 }
692
693 if( chal_len < 8 || chal_len > 32 )
694 {
695 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
696 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
697 }
698
699 if( n != 6 + ciph_len + sess_len + chal_len )
700 {
701 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
702 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
703 }
704
705 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
706 buf + 6, ciph_len );
707 SSL_DEBUG_BUF( 3, "client hello, session id",
708 buf + 6 + ciph_len, sess_len );
709 SSL_DEBUG_BUF( 3, "client hello, challenge",
710 buf + 6 + ciph_len + sess_len, chal_len );
711
712 p = buf + 6 + ciph_len;
713 ssl->session_negotiate->length = sess_len;
714 memset( ssl->session_negotiate->id, 0, sizeof( ssl->session_negotiate->id ) );
715 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
716
717 p += sess_len;
718 memset( ssl->handshake->randbytes, 0, 64 );
719 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
720
721 /*
722 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
723 */
724 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
725 {
726 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
727 {
728 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
729 if( ssl->renegotiation == SSL_RENEGOTIATION )
730 {
731 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
732
733 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
734 return( ret );
735
736 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
737 }
738 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
739 break;
740 }
741 }
742
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200743 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
744 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +0100745 {
746 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
747 {
Paul Bakker41c83d32013-03-20 14:39:14 +0100748 // Only allow non-ECC ciphersuites as we do not have extensions
749 //
Paul Bakker59c28a22013-06-29 15:33:42 +0200750 if( p[0] == 0 && p[1] == 0 &&
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200751 ( ( ciphersuites[i] >> 8 ) & 0xFF ) == 0 &&
752 p[2] == ( ciphersuites[i] & 0xFF ) )
Paul Bakker59c28a22013-06-29 15:33:42 +0200753 {
754 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
755
756 if( ciphersuite_info == NULL )
757 {
758 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %02x not found",
759 ciphersuites[i] ) );
760 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
761 }
762
Paul Bakker2fbefde2013-06-29 16:01:15 +0200763 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
764 ciphersuite_info->max_minor_ver < ssl->minor_ver )
765 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +0200766
Paul Bakker78a8c712013-03-06 17:01:52 +0100767 goto have_ciphersuite_v2;
Paul Bakker59c28a22013-06-29 15:33:42 +0200768 }
Paul Bakker78a8c712013-03-06 17:01:52 +0100769 }
770 }
771
772 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
773
774 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
775
776have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200777 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +0200778 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +0100779 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +0100780
781 /*
782 * SSLv2 Client Hello relevant renegotiation security checks
783 */
784 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
785 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
786 {
787 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
788
789 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
790 return( ret );
791
792 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
793 }
794
795 ssl->in_left = 0;
796 ssl->state++;
797
798 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
799
800 return( 0 );
801}
802#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
803
Paul Bakker5121ce52009-01-03 21:22:43 +0000804static int ssl_parse_client_hello( ssl_context *ssl )
805{
Paul Bakker23986e52011-04-24 08:57:21 +0000806 int ret;
807 unsigned int i, j;
808 size_t n;
809 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +0000810 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +0000811 unsigned int ext_len = 0;
812 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000813 int renegotiation_info_seen = 0;
814 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200815 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +0100816 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000817
818 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
819
Paul Bakker48916f92012-09-16 19:57:18 +0000820 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
821 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000822 {
823 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
824 return( ret );
825 }
826
827 buf = ssl->in_hdr;
828
Paul Bakker78a8c712013-03-06 17:01:52 +0100829#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
830 if( ( buf[0] & 0x80 ) != 0 )
831 return ssl_parse_client_hello_v2( ssl );
832#endif
833
Paul Bakkerec636f32012-09-09 19:17:02 +0000834 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
835
836 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
837 buf[0] ) );
838 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
839 ( buf[3] << 8 ) | buf[4] ) );
840 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
841 buf[1], buf[2] ) );
842
843 /*
844 * SSLv3 Client Hello
845 *
846 * Record layer:
847 * 0 . 0 message type
848 * 1 . 2 protocol version
849 * 3 . 4 message length
850 */
851 if( buf[0] != SSL_MSG_HANDSHAKE ||
852 buf[1] != SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000853 {
Paul Bakkerec636f32012-09-09 19:17:02 +0000854 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
855 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
856 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000857
Paul Bakkerec636f32012-09-09 19:17:02 +0000858 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000859
Manuel Pégourié-Gonnard72882b22013-08-02 13:36:00 +0200860 if( n < 45 || n > 2048 )
Paul Bakkerec636f32012-09-09 19:17:02 +0000861 {
862 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
863 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
864 }
865
Paul Bakker48916f92012-09-16 19:57:18 +0000866 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
867 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +0000868 {
869 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
870 return( ret );
871 }
872
873 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +0000874 if( !ssl->renegotiation )
875 n = ssl->in_left - 5;
876 else
877 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +0000878
Paul Bakker48916f92012-09-16 19:57:18 +0000879 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +0000880
881 /*
882 * SSL layer:
883 * 0 . 0 handshake type
884 * 1 . 3 handshake length
885 * 4 . 5 protocol version
886 * 6 . 9 UNIX time()
887 * 10 . 37 random bytes
888 * 38 . 38 session id length
889 * 39 . 38+x session id
890 * 39+x . 40+x ciphersuitelist length
891 * 41+x . .. ciphersuitelist
892 * .. . .. compression alg.
893 * .. . .. extensions
894 */
895 SSL_DEBUG_BUF( 4, "record contents", buf, n );
896
897 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
898 buf[0] ) );
899 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
900 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
901 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
902 buf[4], buf[5] ) );
903
904 /*
905 * Check the handshake type and protocol version
906 */
907 if( buf[0] != SSL_HS_CLIENT_HELLO ||
908 buf[4] != SSL_MAJOR_VERSION_3 )
909 {
910 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
911 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
912 }
913
914 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200915 ssl->minor_ver = ( buf[5] <= ssl->max_minor_ver )
916 ? buf[5] : ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +0000917
Paul Bakker1d29fb52012-09-28 13:28:45 +0000918 if( ssl->minor_ver < ssl->min_minor_ver )
919 {
920 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
921 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +0000922 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +0000923
924 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
925 SSL_ALERT_MSG_PROTOCOL_VERSION );
926
927 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
928 }
929
Paul Bakker2fbefde2013-06-29 16:01:15 +0200930 ssl->handshake->max_major_ver = buf[4];
931 ssl->handshake->max_minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +0000932
Paul Bakker48916f92012-09-16 19:57:18 +0000933 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +0000934
935 /*
936 * Check the handshake message length
937 */
938 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
939 {
940 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
941 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
942 }
943
944 /*
945 * Check the session length
946 */
947 sess_len = buf[38];
948
949 if( sess_len > 32 )
950 {
951 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
952 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
953 }
954
Paul Bakker48916f92012-09-16 19:57:18 +0000955 ssl->session_negotiate->length = sess_len;
956 memset( ssl->session_negotiate->id, 0,
957 sizeof( ssl->session_negotiate->id ) );
958 memcpy( ssl->session_negotiate->id, buf + 39,
959 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +0000960
961 /*
962 * Check the ciphersuitelist length
963 */
964 ciph_len = ( buf[39 + sess_len] << 8 )
965 | ( buf[40 + sess_len] );
966
967 if( ciph_len < 2 || ciph_len > 256 || ( ciph_len % 2 ) != 0 )
968 {
969 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
970 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
971 }
972
973 /*
974 * Check the compression algorithms length
975 */
976 comp_len = buf[41 + sess_len + ciph_len];
977
978 if( comp_len < 1 || comp_len > 16 )
979 {
980 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
981 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
982 }
983
Paul Bakker48916f92012-09-16 19:57:18 +0000984 /*
985 * Check the extension length
986 */
987 if( n > 42 + sess_len + ciph_len + comp_len )
988 {
989 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
990 | ( buf[43 + sess_len + ciph_len + comp_len] );
991
992 if( ( ext_len > 0 && ext_len < 4 ) ||
993 n != 44 + sess_len + ciph_len + comp_len + ext_len )
994 {
995 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
996 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
997 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
998 }
999 }
1000
1001 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001002#if defined(POLARSSL_ZLIB_SUPPORT)
1003 for( i = 0; i < comp_len; ++i )
1004 {
Paul Bakker48916f92012-09-16 19:57:18 +00001005 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001006 {
Paul Bakker48916f92012-09-16 19:57:18 +00001007 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001008 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001009 }
1010 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001011#endif
1012
Paul Bakkerec636f32012-09-09 19:17:02 +00001013 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1014 buf + 6, 32 );
1015 SSL_DEBUG_BUF( 3, "client hello, session id",
1016 buf + 38, sess_len );
1017 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1018 buf + 41 + sess_len, ciph_len );
1019 SSL_DEBUG_BUF( 3, "client hello, compression",
1020 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
Paul Bakkerec636f32012-09-09 19:17:02 +00001022 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001023 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1024 */
1025 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1026 {
1027 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1028 {
1029 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1030 if( ssl->renegotiation == SSL_RENEGOTIATION )
1031 {
1032 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001033
1034 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1035 return( ret );
1036
Paul Bakker48916f92012-09-16 19:57:18 +00001037 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1038 }
1039 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1040 break;
1041 }
1042 }
1043
Paul Bakker48916f92012-09-16 19:57:18 +00001044 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001045
1046 while( ext_len )
1047 {
1048 unsigned int ext_id = ( ( ext[0] << 8 )
1049 | ( ext[1] ) );
1050 unsigned int ext_size = ( ( ext[2] << 8 )
1051 | ( ext[3] ) );
1052
1053 if( ext_size + 4 > ext_len )
1054 {
1055 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1056 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1057 }
1058 switch( ext_id )
1059 {
Paul Bakker5701cdc2012-09-27 21:49:42 +00001060 case TLS_EXT_SERVERNAME:
1061 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1062 if( ssl->f_sni == NULL )
1063 break;
1064
1065 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1066 if( ret != 0 )
1067 return( ret );
1068 break;
1069
Paul Bakker48916f92012-09-16 19:57:18 +00001070 case TLS_EXT_RENEGOTIATION_INFO:
1071 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1072 renegotiation_info_seen = 1;
1073
Paul Bakker23f36802012-09-28 14:15:14 +00001074 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1075 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001076 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001077 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001078
Paul Bakker23f36802012-09-28 14:15:14 +00001079 case TLS_EXT_SIG_ALG:
1080 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1081 if( ssl->renegotiation == SSL_RENEGOTIATION )
1082 break;
1083
1084 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1085 if( ret != 0 )
1086 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001087 break;
1088
Paul Bakker41c83d32013-03-20 14:39:14 +01001089#if defined(POLARSSL_ECP_C)
1090 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1091 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1092
1093 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1094 if( ret != 0 )
1095 return( ret );
1096 break;
1097
1098 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1099 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1100
1101 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1102 if( ret != 0 )
1103 return( ret );
1104 break;
1105#endif /* POLARSSL_ECP_C */
1106
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001107 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1108 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1109
1110 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1111 if( ret != 0 )
1112 return( ret );
1113 break;
1114
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001115 case TLS_EXT_TRUNCATED_HMAC:
1116 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1117
1118 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1119 if( ret != 0 )
1120 return( ret );
1121 break;
1122
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001123 case TLS_EXT_SESSION_TICKET:
1124 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1125
1126 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1127 if( ret != 0 )
1128 return( ret );
1129 break;
1130
Paul Bakker48916f92012-09-16 19:57:18 +00001131 default:
1132 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1133 ext_id ) );
1134 }
1135
1136 ext_len -= 4 + ext_size;
1137 ext += 4 + ext_size;
1138
1139 if( ext_len > 0 && ext_len < 4 )
1140 {
1141 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1142 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1143 }
1144 }
1145
1146 /*
1147 * Renegotiation security checks
1148 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001149 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1150 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1151 {
1152 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1153 handshake_failure = 1;
1154 }
1155 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1156 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1157 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001158 {
1159 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001160 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001161 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001162 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1163 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1164 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001165 {
1166 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001167 handshake_failure = 1;
1168 }
1169 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1170 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1171 renegotiation_info_seen == 1 )
1172 {
1173 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1174 handshake_failure = 1;
1175 }
1176
1177 if( handshake_failure == 1 )
1178 {
1179 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1180 return( ret );
1181
Paul Bakker48916f92012-09-16 19:57:18 +00001182 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1183 }
Paul Bakker380da532012-04-18 16:10:25 +00001184
Paul Bakker41c83d32013-03-20 14:39:14 +01001185 /*
1186 * Search for a matching ciphersuite
1187 * (At the end because we need information from the EC-based extensions)
1188 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001189 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
1190 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001191 {
1192 for( j = 0, p = buf + 41 + sess_len; j < ciph_len;
1193 j += 2, p += 2 )
1194 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001195 if( p[0] == ( ( ciphersuites[i] >> 8 ) & 0xFF ) &&
1196 p[1] == ( ( ciphersuites[i] ) & 0xFF ) )
Paul Bakker41c83d32013-03-20 14:39:14 +01001197 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001198 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
Paul Bakker41c83d32013-03-20 14:39:14 +01001199
1200 if( ciphersuite_info == NULL )
1201 {
1202 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %02x not found",
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001203 ciphersuites[i] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001204 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1205 }
1206
Paul Bakker2fbefde2013-06-29 16:01:15 +02001207 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
1208 ciphersuite_info->max_minor_ver < ssl->minor_ver )
1209 continue;
1210
Paul Bakker41c83d32013-03-20 14:39:14 +01001211 if( ( ciphersuite_info->flags & POLARSSL_CIPHERSUITE_EC ) &&
1212 ssl->handshake->ec_curve == 0 )
1213 continue;
1214
1215 goto have_ciphersuite;
1216 }
1217 }
1218 }
1219
1220 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1221
1222 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1223 return( ret );
1224
1225 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1226
1227have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001228 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001229 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1230 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1231
Paul Bakker5121ce52009-01-03 21:22:43 +00001232 ssl->in_left = 0;
1233 ssl->state++;
1234
1235 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1236
1237 return( 0 );
1238}
1239
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001240static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1241 unsigned char *buf,
1242 size_t *olen )
1243{
1244 unsigned char *p = buf;
1245
1246 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1247 {
1248 *olen = 0;
1249 return;
1250 }
1251
1252 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1253
1254 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1255 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1256
1257 *p++ = 0x00;
1258 *p++ = 0x00;
1259
1260 *olen = 4;
1261}
1262
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001263static void ssl_write_session_ticket_ext( ssl_context *ssl,
1264 unsigned char *buf,
1265 size_t *olen )
1266{
1267 unsigned char *p = buf;
1268
1269 if( ssl->handshake->new_session_ticket == 0 )
1270 {
1271 *olen = 0;
1272 return;
1273 }
1274
1275 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1276
1277 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1278 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1279
1280 *p++ = 0x00;
1281 *p++ = 0x00;
1282
1283 *olen = 4;
1284}
1285
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001286static void ssl_write_renegotiation_ext( ssl_context *ssl,
1287 unsigned char *buf,
1288 size_t *olen )
1289{
1290 unsigned char *p = buf;
1291
1292 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1293 {
1294 *olen = 0;
1295 return;
1296 }
1297
1298 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1299
1300 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1301 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1302
1303 *p++ = 0x00;
1304 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1305 *p++ = ssl->verify_data_len * 2 & 0xFF;
1306
1307 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1308 p += ssl->verify_data_len;
1309 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1310 p += ssl->verify_data_len;
1311
1312 *olen = 5 + ssl->verify_data_len * 2;
1313}
1314
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001315static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1316 unsigned char *buf,
1317 size_t *olen )
1318{
1319 unsigned char *p = buf;
1320
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001321 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1322 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001323 *olen = 0;
1324 return;
1325 }
1326
1327 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1328
1329 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1330 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1331
1332 *p++ = 0x00;
1333 *p++ = 1;
1334
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001335 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001336
1337 *olen = 5;
1338}
1339
Paul Bakker5121ce52009-01-03 21:22:43 +00001340static int ssl_write_server_hello( ssl_context *ssl )
1341{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001342#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001343 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001344#endif
Paul Bakkera3d195c2011-11-27 21:07:34 +00001345 int ret, n;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001346 size_t olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001347 unsigned char *buf, *p;
1348
1349 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1350
1351 /*
1352 * 0 . 0 handshake type
1353 * 1 . 3 handshake length
1354 * 4 . 5 protocol version
1355 * 6 . 9 UNIX time()
1356 * 10 . 37 random bytes
1357 */
1358 buf = ssl->out_msg;
1359 p = buf + 4;
1360
1361 *p++ = (unsigned char) ssl->major_ver;
1362 *p++ = (unsigned char) ssl->minor_ver;
1363
1364 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1365 buf[4], buf[5] ) );
1366
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001367#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001368 t = time( NULL );
1369 *p++ = (unsigned char)( t >> 24 );
1370 *p++ = (unsigned char)( t >> 16 );
1371 *p++ = (unsigned char)( t >> 8 );
1372 *p++ = (unsigned char)( t );
1373
1374 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001375#else
1376 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1377 return( ret );
1378
1379 p += 4;
1380#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001381
Paul Bakkera3d195c2011-11-27 21:07:34 +00001382 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1383 return( ret );
1384
1385 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001386
Paul Bakker48916f92012-09-16 19:57:18 +00001387 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001388
1389 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1390
1391 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001392 * Resume is 0 by default, see ssl_handshake_init().
1393 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1394 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001395 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001396 if( ssl->handshake->resume == 0 &&
1397 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001398 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001399 ssl->f_get_cache != NULL &&
1400 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1401 {
1402 ssl->handshake->resume = 1;
1403 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001404
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001405 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001406 {
1407 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001408 * New session, create a new session id,
1409 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001410 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001411 ssl->state++;
1412
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001413 if( ssl->handshake->new_session_ticket == 0 )
1414 {
1415 ssl->session_negotiate->length = n = 32;
1416 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
1417 n ) ) != 0 )
1418 return( ret );
1419 }
1420 else
1421 {
1422 ssl->session_negotiate->length = 0;
1423 memset( ssl->session_negotiate->id, 0, 32 );
1424 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001425 }
1426 else
1427 {
1428 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001429 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001430 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001431 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001432
1433 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1434 {
1435 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1436 return( ret );
1437 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001438 }
1439
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001440 /*
1441 * 38 . 38 session id length
1442 * 39 . 38+n session id
1443 * 39+n . 40+n chosen ciphersuite
1444 * 41+n . 41+n chosen compression alg.
1445 * 42+n . 43+n extensions length
1446 * 44+n . 43+n+m extensions
1447 */
1448 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001449 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1450 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001451
1452 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1453 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1454 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001455 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001456
Paul Bakker48916f92012-09-16 19:57:18 +00001457 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1458 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1459 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001460
Paul Bakkere3166ce2011-01-27 17:40:50 +00001461 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001462 ssl->session_negotiate->ciphersuite ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001463 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001464 ssl->session_negotiate->compression ) );
1465
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001466 /*
1467 * First write extensions, then the total length
1468 */
1469 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1470 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001471
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001472 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1473 ext_len += olen;
1474
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001475 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1476 ext_len += olen;
1477
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001478 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1479 ext_len += olen;
1480
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001481 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001482
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001483 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1484 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1485 p += ext_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001486
1487 ssl->out_msglen = p - buf;
1488 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1489 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1490
1491 ret = ssl_write_record( ssl );
1492
1493 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1494
1495 return( ret );
1496}
1497
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001498#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1499 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
1500 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001501static int ssl_write_certificate_request( ssl_context *ssl )
1502{
Paul Bakkered27a042013-04-18 22:46:23 +02001503 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1504 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001505
1506 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1507
1508 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1509 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1510 {
1511 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1512 ssl->state++;
1513 return( 0 );
1514 }
1515
1516 return( ret );
1517}
1518#else
1519static int ssl_write_certificate_request( ssl_context *ssl )
1520{
1521 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1522 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker926af752012-11-23 13:38:07 +01001523 size_t n = 0, dn_size, total_dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001524 unsigned char *buf, *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001525 const x509_cert *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001526
1527 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1528
1529 ssl->state++;
1530
Paul Bakkerfbb17802013-04-17 19:10:21 +02001531 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001532 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001533 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001534 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001535 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001536 return( 0 );
1537 }
1538
1539 /*
1540 * 0 . 0 handshake type
1541 * 1 . 3 handshake length
1542 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01001543 * 5 .. m-1 cert types
1544 * m .. m+1 sig alg length (TLS 1.2 only)
1545 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001546 * n .. n+1 length of all DNs
1547 * n+2 .. n+3 length of DN 1
1548 * n+4 .. ... Distinguished Name #1
1549 * ... .. ... length of DN 2, etc.
1550 */
1551 buf = ssl->out_msg;
1552 p = buf + 4;
1553
1554 /*
1555 * At the moment, only RSA certificates are supported
1556 */
1557 *p++ = 1;
Paul Bakker926af752012-11-23 13:38:07 +01001558 *p++ = SSL_CERT_TYPE_RSA_SIGN;
1559
1560 /*
1561 * Add signature_algorithms for verify (TLS 1.2)
1562 * Only add current running algorithm that is already required for
1563 * requested ciphersuite.
1564 *
1565 * Length is always 2
1566 */
Paul Bakker21dca692013-01-03 11:41:08 +01001567 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01001568 {
1569 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
1570
1571 *p++ = 0;
1572 *p++ = 2;
1573
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001574 if( ssl->transform_negotiate->ciphersuite_info->mac ==
1575 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01001576 {
1577 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
1578 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02001579
Paul Bakker926af752012-11-23 13:38:07 +01001580 *p++ = ssl->handshake->verify_sig_alg;
1581 *p++ = SSL_SIG_RSA;
1582
1583 n += 4;
1584 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001585
1586 p += 2;
1587 crt = ssl->ca_chain;
1588
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001589 total_dn_size = 0;
Paul Bakker29087132010-03-21 21:03:34 +00001590 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001591 {
1592 if( p - buf > 4096 )
1593 break;
1594
Paul Bakker926af752012-11-23 13:38:07 +01001595 dn_size = crt->subject_raw.len;
1596 *p++ = (unsigned char)( dn_size >> 8 );
1597 *p++ = (unsigned char)( dn_size );
1598 memcpy( p, crt->subject_raw.p, dn_size );
1599 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001600
Paul Bakker926af752012-11-23 13:38:07 +01001601 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
1602
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001603 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01001604 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00001605 }
1606
Paul Bakker926af752012-11-23 13:38:07 +01001607 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001608 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1609 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Paul Bakker926af752012-11-23 13:38:07 +01001610 ssl->out_msg[6 + n] = (unsigned char)( total_dn_size >> 8 );
1611 ssl->out_msg[7 + n] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001612
1613 ret = ssl_write_record( ssl );
1614
1615 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
1616
1617 return( ret );
1618}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001619#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
1620 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
1621 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001622
Paul Bakker41c83d32013-03-20 14:39:14 +01001623static int ssl_write_server_key_exchange( ssl_context *ssl )
1624{
Paul Bakker23986e52011-04-24 08:57:21 +00001625 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001626 size_t n = 0, len;
Paul Bakker23f36802012-09-28 14:15:14 +00001627 unsigned char hash[64];
Paul Bakkerc70b9822013-04-07 22:00:46 +02001628 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker35a7fe52012-10-31 09:07:14 +00001629 unsigned int hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001630 unsigned char *p = ssl->out_msg + 4;
1631 unsigned char *dig_sig = p;
1632 size_t dig_sig_len = 0;
Paul Bakker41c83d32013-03-20 14:39:14 +01001633
1634 const ssl_ciphersuite_t *ciphersuite_info;
1635 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001636
1637 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
1638
Paul Bakker41c83d32013-03-20 14:39:14 +01001639 if( ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_RSA &&
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001640 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_ECDHE_RSA &&
1641 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001642 {
1643 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
1644 ssl->state++;
1645 return( 0 );
1646 }
1647
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001648#if defined(POLARSSL_RSA_C)
Paul Bakker43b7e352011-01-18 15:27:19 +00001649 if( ssl->rsa_key == NULL )
1650 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00001651 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
1652 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker43b7e352011-01-18 15:27:19 +00001653 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001654#endif /* POLARSSL_RSA_C */
Paul Bakker43b7e352011-01-18 15:27:19 +00001655
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001656#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1657 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1658 {
1659 /* TODO: Support identity hints */
1660 *(p++) = 0x00;
1661 *(p++) = 0x00;
1662
1663 n += 2;
1664 }
1665#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1666
1667#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1668 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1669 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1670 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00001671 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001672 /*
1673 * Ephemeral DH parameters:
1674 *
1675 * struct {
1676 * opaque dh_p<1..2^16-1>;
1677 * opaque dh_g<1..2^16-1>;
1678 * opaque dh_Ys<1..2^16-1>;
1679 * } ServerDHParams;
1680 */
1681 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
1682 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
1683 {
1684 SSL_DEBUG_RET( 1, "mpi_copy", ret );
1685 return( ret );
1686 }
Paul Bakker48916f92012-09-16 19:57:18 +00001687
Paul Bakker41c83d32013-03-20 14:39:14 +01001688 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
1689 mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001690 p,
1691 &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001692 {
1693 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
1694 return( ret );
1695 }
1696
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001697 dig_sig = p;
1698 dig_sig_len = len;
1699
1700 p += len;
1701 n += len;
1702
Paul Bakker41c83d32013-03-20 14:39:14 +01001703 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
1704 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1705 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1706 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
1707 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001708#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1709 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001710
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001711#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01001712 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001713 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001714 /*
1715 * Ephemeral ECDH parameters:
1716 *
1717 * struct {
1718 * ECParameters curve_params;
1719 * ECPoint public;
1720 * } ServerECDHParams;
1721 */
Paul Bakker41c83d32013-03-20 14:39:14 +01001722 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
1723 ssl->handshake->ec_curve ) ) != 0 )
1724 {
1725 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
1726 return( ret );
1727 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001728
Paul Bakker41c83d32013-03-20 14:39:14 +01001729 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001730 &len,
1731 p,
Paul Bakker41c83d32013-03-20 14:39:14 +01001732 1000, ssl->f_rng, ssl->p_rng ) ) != 0 )
1733 {
1734 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
1735 return( ret );
1736 }
1737
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001738 dig_sig = p;
1739 dig_sig_len = len;
1740
1741 p += len;
1742 n += len;
1743
Paul Bakker41c83d32013-03-20 14:39:14 +01001744 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
1745 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001746#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001747
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001748#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1749 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
1750 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1751 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001752 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001753 size_t rsa_key_len = 0;
Paul Bakker23f36802012-09-28 14:15:14 +00001754
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001755 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker23f36802012-09-28 14:15:14 +00001756 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001757 md5_context md5;
1758 sha1_context sha1;
1759
1760 /*
1761 * digitally-signed struct {
1762 * opaque md5_hash[16];
1763 * opaque sha_hash[20];
1764 * };
1765 *
1766 * md5_hash
1767 * MD5(ClientHello.random + ServerHello.random
1768 * + ServerParams);
1769 * sha_hash
1770 * SHA(ClientHello.random + ServerHello.random
1771 * + ServerParams);
1772 */
1773 md5_starts( &md5 );
1774 md5_update( &md5, ssl->handshake->randbytes, 64 );
1775 md5_update( &md5, dig_sig, dig_sig_len );
1776 md5_finish( &md5, hash );
1777
1778 sha1_starts( &sha1 );
1779 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
1780 sha1_update( &sha1, dig_sig, dig_sig_len );
1781 sha1_finish( &sha1, hash + 16 );
1782
1783 hashlen = 36;
1784 md_alg = POLARSSL_MD_NONE;
1785 }
1786 else
1787 {
1788 md_context_t ctx;
1789
1790 /*
1791 * digitally-signed struct {
1792 * opaque client_random[32];
1793 * opaque server_random[32];
1794 * ServerDHParams params;
1795 * };
1796 */
1797 switch( ssl->handshake->sig_alg )
1798 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001799#if defined(POLARSSL_MD5_C)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001800 case SSL_HASH_MD5:
1801 md_alg = POLARSSL_MD_MD5;
1802 break;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001803#endif
1804#if defined(POLARSSL_SHA1_C)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001805 case SSL_HASH_SHA1:
1806 md_alg = POLARSSL_MD_SHA1;
1807 break;
Paul Bakker23f36802012-09-28 14:15:14 +00001808#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02001809#if defined(POLARSSL_SHA256_C)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001810 case SSL_HASH_SHA224:
1811 md_alg = POLARSSL_MD_SHA224;
1812 break;
1813 case SSL_HASH_SHA256:
1814 md_alg = POLARSSL_MD_SHA256;
1815 break;
Paul Bakker23f36802012-09-28 14:15:14 +00001816#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02001817#if defined(POLARSSL_SHA512_C)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001818 case SSL_HASH_SHA384:
1819 md_alg = POLARSSL_MD_SHA384;
1820 break;
1821 case SSL_HASH_SHA512:
1822 md_alg = POLARSSL_MD_SHA512;
1823 break;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001824#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001825 default:
1826 /* Should never happen */
1827 return( -1 );
1828 }
1829
1830 if( ( ret = md_init_ctx( &ctx, md_info_from_type( md_alg ) ) ) != 0 )
1831 {
1832 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
1833 return( ret );
1834 }
1835
1836 md_starts( &ctx );
1837 md_update( &ctx, ssl->handshake->randbytes, 64 );
1838 md_update( &ctx, dig_sig, dig_sig_len );
1839 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02001840
1841 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
1842 {
1843 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
1844 return( ret );
1845 }
1846
Paul Bakker23f36802012-09-28 14:15:14 +00001847 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001848
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001849 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
1850
1851 if ( ssl->rsa_key )
1852 rsa_key_len = ssl->rsa_key_len( ssl->rsa_key );
1853
1854 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker23f36802012-09-28 14:15:14 +00001855 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001856 *(p++) = ssl->handshake->sig_alg;
1857 *(p++) = SSL_SIG_RSA;
1858
1859 n += 2;
1860 }
1861
1862 *(p++) = (unsigned char)( rsa_key_len >> 8 );
1863 *(p++) = (unsigned char)( rsa_key_len );
1864 n += 2;
1865
1866 if ( ssl->rsa_key )
1867 {
1868 ret = ssl->rsa_sign( ssl->rsa_key, ssl->f_rng, ssl->p_rng,
1869 RSA_PRIVATE, md_alg, hashlen, hash, p );
1870 }
1871
1872 if( ret != 0 )
1873 {
1874 SSL_DEBUG_RET( 1, "pkcs1_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001875 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001876 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001877
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001878 SSL_DEBUG_BUF( 3, "my RSA sig", p, rsa_key_len );
1879
1880 p += rsa_key_len;
1881 n += rsa_key_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001882 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001883#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
1884 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001885
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001886 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001887 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1888 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
1889
1890 ssl->state++;
1891
1892 if( ( ret = ssl_write_record( ssl ) ) != 0 )
1893 {
1894 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
1895 return( ret );
1896 }
1897
1898 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
1899
1900 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001901}
1902
1903static int ssl_write_server_hello_done( ssl_context *ssl )
1904{
1905 int ret;
1906
1907 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
1908
1909 ssl->out_msglen = 4;
1910 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1911 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
1912
1913 ssl->state++;
1914
1915 if( ( ret = ssl_write_record( ssl ) ) != 0 )
1916 {
1917 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
1918 return( ret );
1919 }
1920
1921 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
1922
1923 return( 0 );
1924}
1925
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001926#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1927 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1928static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
1929 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02001930{
1931 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02001932 size_t n;
1933
1934 /*
1935 * Receive G^Y mod P, premaster = (G^Y)^X mod P
1936 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001937 if( *p + 2 > end )
1938 {
1939 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
1940 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
1941 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02001942
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001943 n = ( (*p)[0] << 8 ) | (*p)[1];
1944 *p += 2;
1945
1946 if( n < 1 || n > ssl->handshake->dhm_ctx.len || *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02001947 {
1948 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
1949 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
1950 }
1951
1952 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001953 *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02001954 {
1955 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
1956 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
1957 }
1958
1959 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
1960
Paul Bakker70df2fb2013-04-17 17:19:09 +02001961 return( ret );
1962}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001963#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1964 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02001965
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001966#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
Paul Bakker70df2fb2013-04-17 17:19:09 +02001967static int ssl_parse_client_ecdh_public( ssl_context *ssl )
1968{
1969 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02001970 size_t n;
1971
1972 /*
1973 * Receive client public key and calculate premaster
1974 */
1975 n = ssl->in_msg[3];
1976
1977 if( n < 1 || n > mpi_size( &ssl->handshake->ecdh_ctx.grp.P ) * 2 + 2 ||
1978 n + 4 != ssl->in_hslen )
1979 {
1980 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
1981 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
1982 }
1983
1984 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
1985 ssl->in_msg + 4, n ) ) != 0 )
1986 {
1987 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
1988 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
1989 }
1990
1991 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
1992
Paul Bakker70df2fb2013-04-17 17:19:09 +02001993 return( ret );
1994}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001995#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02001996
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001997#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakker70df2fb2013-04-17 17:19:09 +02001998static int ssl_parse_encrypted_pms_secret( ssl_context *ssl )
1999{
2000 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2001 size_t i, n = 0;
2002
2003 if( ssl->rsa_key == NULL )
2004 {
2005 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2006 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2007 }
2008
2009 /*
2010 * Decrypt the premaster using own private RSA key
2011 */
2012 i = 4;
2013 if( ssl->rsa_key )
2014 n = ssl->rsa_key_len( ssl->rsa_key );
2015 ssl->handshake->pmslen = 48;
2016
2017 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2018 {
2019 i += 2;
2020 if( ssl->in_msg[4] != ( ( n >> 8 ) & 0xFF ) ||
2021 ssl->in_msg[5] != ( ( n ) & 0xFF ) )
2022 {
2023 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2024 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2025 }
2026 }
2027
2028 if( ssl->in_hslen != i + n )
2029 {
2030 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2031 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2032 }
2033
2034 if( ssl->rsa_key ) {
2035 ret = ssl->rsa_decrypt( ssl->rsa_key, RSA_PRIVATE,
2036 &ssl->handshake->pmslen,
2037 ssl->in_msg + i,
2038 ssl->handshake->premaster,
2039 sizeof(ssl->handshake->premaster) );
2040 }
2041
2042 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Paul Bakker2fbefde2013-06-29 16:01:15 +02002043 ssl->handshake->premaster[0] != ssl->handshake->max_major_ver ||
2044 ssl->handshake->premaster[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002045 {
2046 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2047
2048 /*
2049 * Protection against Bleichenbacher's attack:
2050 * invalid PKCS#1 v1.5 padding must not cause
2051 * the connection to end immediately; instead,
2052 * send a bad_record_mac later in the handshake.
2053 */
2054 ssl->handshake->pmslen = 48;
2055
2056 ret = ssl->f_rng( ssl->p_rng, ssl->handshake->premaster,
2057 ssl->handshake->pmslen );
2058 if( ret != 0 )
2059 return( ret );
2060 }
2061
2062 return( ret );
2063}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002064#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002065
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002066#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2067 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2068static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2069 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002070{
2071 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002072 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002073
2074 if( ssl->psk == NULL || ssl->psk_identity == NULL ||
2075 ssl->psk_identity_len == 0 || ssl->psk_len == 0 )
2076 {
2077 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2078 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2079 }
2080
2081 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002082 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002083 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002084 if( *p + 2 > end )
2085 {
2086 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2087 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2088 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002089
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002090 n = ( (*p)[0] << 8 ) | (*p)[1];
2091 *p += 2;
2092
2093 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002094 {
2095 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2096 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2097 }
2098
2099 if( n != ssl->psk_identity_len ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002100 memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002101 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002102 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002103 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2104 }
2105
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002106 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002107 ret = 0;
2108
Paul Bakkerfbb17802013-04-17 19:10:21 +02002109 return( ret );
2110}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002111#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
2112 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002113
Paul Bakker5121ce52009-01-03 21:22:43 +00002114static int ssl_parse_client_key_exchange( ssl_context *ssl )
2115{
Paul Bakker23986e52011-04-24 08:57:21 +00002116 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002117 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002118 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002119
Paul Bakker41c83d32013-03-20 14:39:14 +01002120 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002121
2122 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2123
2124 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2125 {
2126 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2127 return( ret );
2128 }
2129
2130 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2131 {
2132 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002133 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002134 }
2135
2136 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2137 {
2138 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002139 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002140 }
2141
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002142 p = ssl->in_msg + 4;
2143 end = ssl->in_msg + ssl->in_msglen;
2144
2145#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002146 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002147 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002148 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002149 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002150 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2151 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002152 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002153
2154 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
2155
2156 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2157 ssl->handshake->premaster,
2158 &ssl->handshake->pmslen ) ) != 0 )
2159 {
2160 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2161 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2162 }
2163
2164 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002165 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002166 else
2167#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
2168#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
2169 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002170 {
2171 if( ( ret = ssl_parse_client_ecdh_public( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002172 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002173 SSL_DEBUG_RET( 1, ( "ssl_parse_client_ecdh_public" ), ret );
2174 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002175 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002176
2177 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2178 &ssl->handshake->pmslen,
2179 ssl->handshake->premaster,
2180 POLARSSL_MPI_MAX_SIZE ) ) != 0 )
2181 {
2182 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2183 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2184 }
2185
2186 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002187 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002188 else
2189#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
2190#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2191 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002192 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002193 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002194 {
2195 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2196 return( ret );
2197 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002198
2199 // Set up the premaster secret
2200 //
2201 p = ssl->handshake->premaster;
2202 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2203 *(p++) = (unsigned char)( ssl->psk_len );
2204 p += ssl->psk_len;
2205
2206 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2207 *(p++) = (unsigned char)( ssl->psk_len );
2208 memcpy( p, ssl->psk, ssl->psk_len );
2209 p += ssl->psk_len;
2210
2211 ssl->handshake->pmslen = 4 + 2 * ssl->psk_len;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002212 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002213 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002214#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
2215#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2216 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2217 {
2218 size_t n;
2219
2220 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2221 {
2222 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2223 return( ret );
2224 }
2225 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2226 {
2227 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2228 return( ret );
2229 }
2230
2231 // Set up the premaster secret
2232 //
2233 p = ssl->handshake->premaster;
2234 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len >> 8 );
2235 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len );
2236
2237 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2238 p, &n ) ) != 0 )
2239 {
2240 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2241 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2242 }
2243
2244 if( n != ssl->handshake->dhm_ctx.len )
2245 {
2246 SSL_DEBUG_MSG( 1, ( "dhm_calc_secret result smaller than DHM" ) );
2247 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2248 }
2249
2250 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2251
2252 p += ssl->handshake->dhm_ctx.len;
2253
2254 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2255 *(p++) = (unsigned char)( ssl->psk_len );
2256 memcpy( p, ssl->psk, ssl->psk_len );
2257 p += ssl->psk_len;
2258
2259 ssl->handshake->pmslen = 4 + ssl->handshake->dhm_ctx.len + ssl->psk_len;
2260 }
2261 else
2262#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
2263#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2264 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002265 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002266 if( ( ret = ssl_parse_encrypted_pms_secret( ssl ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002267 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002268 SSL_DEBUG_RET( 1, ( "ssl_parse_client_ecdh_public" ), ret );
2269 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002270 }
2271 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002272 else
2273#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2274 {
2275 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2276 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002277
Paul Bakkerff60ee62010-03-16 21:09:09 +00002278 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2279 {
2280 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2281 return( ret );
2282 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002283
Paul Bakker5121ce52009-01-03 21:22:43 +00002284 ssl->state++;
2285
2286 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2287
2288 return( 0 );
2289}
2290
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002291#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2292 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2293 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002294static int ssl_parse_certificate_verify( ssl_context *ssl )
2295{
Paul Bakkered27a042013-04-18 22:46:23 +02002296 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002297 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002298
2299 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2300
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002301 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2302 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002303 {
2304 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2305 ssl->state++;
2306 return( 0 );
2307 }
2308
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002309 return( ret );
2310}
2311#else
2312static int ssl_parse_certificate_verify( ssl_context *ssl )
2313{
2314 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2315 size_t n = 0, n1, n2;
2316 unsigned char hash[48];
2317 md_type_t md_alg = POLARSSL_MD_NONE;
2318 unsigned int hashlen = 0;
2319 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2320
2321 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2322
2323 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2324 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2325 {
2326 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2327 ssl->state++;
2328 return( 0 );
2329 }
2330
Paul Bakkered27a042013-04-18 22:46:23 +02002331 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002332 {
2333 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2334 ssl->state++;
2335 return( 0 );
2336 }
2337
Paul Bakker48916f92012-09-16 19:57:18 +00002338 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002339
2340 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2341 {
2342 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2343 return( ret );
2344 }
2345
2346 ssl->state++;
2347
2348 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2349 {
2350 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002351 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002352 }
2353
2354 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
2355 {
2356 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002357 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002358 }
2359
Paul Bakker926af752012-11-23 13:38:07 +01002360 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2361 {
2362 /*
2363 * As server we know we either have SSL_HASH_SHA384 or
2364 * SSL_HASH_SHA256
2365 */
2366 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg ||
2367 ssl->in_msg[5] != SSL_SIG_RSA )
2368 {
2369 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg for verify message" ) );
2370 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2371 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002372
Paul Bakker926af752012-11-23 13:38:07 +01002373 if( ssl->handshake->verify_sig_alg == SSL_HASH_SHA384 )
Paul Bakkerc70b9822013-04-07 22:00:46 +02002374 md_alg = POLARSSL_MD_SHA384;
Paul Bakker926af752012-11-23 13:38:07 +01002375 else
Paul Bakkerc70b9822013-04-07 22:00:46 +02002376 md_alg = POLARSSL_MD_SHA256;
Paul Bakker926af752012-11-23 13:38:07 +01002377
2378 n += 2;
2379 }
2380 else
2381 {
2382 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002383 md_alg = POLARSSL_MD_NONE;
Paul Bakker926af752012-11-23 13:38:07 +01002384 }
2385
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002386 /* EC NOT IMPLEMENTED YET */
2387 if( ssl->session_negotiate->peer_cert->pk.type != POLARSSL_PK_RSA )
2388 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2389
2390 n1 = pk_rsa( ssl->session_negotiate->peer_cert->pk )->len;
Paul Bakker78ce5072012-11-23 14:23:53 +01002391 n2 = ( ssl->in_msg[4 + n] << 8 ) | ssl->in_msg[5 + n];
Paul Bakker926af752012-11-23 13:38:07 +01002392
2393 if( n + n1 + 6 != ssl->in_hslen || n1 != n2 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002394 {
2395 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002396 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002397 }
2398
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002399 ret = rsa_pkcs1_verify( pk_rsa( ssl->session_negotiate->peer_cert->pk ),
2400 RSA_PUBLIC, md_alg, hashlen, hash,
2401 ssl->in_msg + 6 + n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002402 if( ret != 0 )
2403 {
2404 SSL_DEBUG_RET( 1, "rsa_pkcs1_verify", ret );
2405 return( ret );
2406 }
2407
2408 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
2409
Paul Bakkered27a042013-04-18 22:46:23 +02002410 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002411}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002412#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2413 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2414 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002415
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002416static int ssl_write_new_session_ticket( ssl_context *ssl )
2417{
2418 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002419 size_t tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002420
2421 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
2422
2423 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2424 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
2425
2426 /*
2427 * struct {
2428 * uint32 ticket_lifetime_hint;
2429 * opaque ticket<0..2^16-1>;
2430 * } NewSessionTicket;
2431 *
2432 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
2433 * 8 . 9 ticket_len (n)
2434 * 10 . 9+n ticket content
2435 */
2436 ssl->out_msg[4] = 0x00;
2437 ssl->out_msg[5] = 0x00;
2438 ssl->out_msg[6] = 0x00;
2439 ssl->out_msg[7] = 0x00;
2440
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +02002441 ssl_write_ticket( ssl, &tlen );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002442
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002443 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
2444 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002445
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002446 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002447
2448 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2449 {
2450 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2451 return( ret );
2452 }
2453
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002454 /* No need to remember writing a NewSessionTicket any more */
2455 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002456
2457 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
2458
2459 return( 0 );
2460}
2461
Paul Bakker5121ce52009-01-03 21:22:43 +00002462/*
Paul Bakker1961b702013-01-25 14:49:24 +01002463 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002464 */
Paul Bakker1961b702013-01-25 14:49:24 +01002465int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002466{
2467 int ret = 0;
2468
Paul Bakker1961b702013-01-25 14:49:24 +01002469 if( ssl->state == SSL_HANDSHAKE_OVER )
2470 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002471
Paul Bakker1961b702013-01-25 14:49:24 +01002472 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
2473
2474 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2475 return( ret );
2476
2477 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002478 {
Paul Bakker1961b702013-01-25 14:49:24 +01002479 case SSL_HELLO_REQUEST:
2480 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002481 break;
2482
Paul Bakker1961b702013-01-25 14:49:24 +01002483 /*
2484 * <== ClientHello
2485 */
2486 case SSL_CLIENT_HELLO:
2487 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002488 break;
Paul Bakker1961b702013-01-25 14:49:24 +01002489
2490 /*
2491 * ==> ServerHello
2492 * Certificate
2493 * ( ServerKeyExchange )
2494 * ( CertificateRequest )
2495 * ServerHelloDone
2496 */
2497 case SSL_SERVER_HELLO:
2498 ret = ssl_write_server_hello( ssl );
2499 break;
2500
2501 case SSL_SERVER_CERTIFICATE:
2502 ret = ssl_write_certificate( ssl );
2503 break;
2504
2505 case SSL_SERVER_KEY_EXCHANGE:
2506 ret = ssl_write_server_key_exchange( ssl );
2507 break;
2508
2509 case SSL_CERTIFICATE_REQUEST:
2510 ret = ssl_write_certificate_request( ssl );
2511 break;
2512
2513 case SSL_SERVER_HELLO_DONE:
2514 ret = ssl_write_server_hello_done( ssl );
2515 break;
2516
2517 /*
2518 * <== ( Certificate/Alert )
2519 * ClientKeyExchange
2520 * ( CertificateVerify )
2521 * ChangeCipherSpec
2522 * Finished
2523 */
2524 case SSL_CLIENT_CERTIFICATE:
2525 ret = ssl_parse_certificate( ssl );
2526 break;
2527
2528 case SSL_CLIENT_KEY_EXCHANGE:
2529 ret = ssl_parse_client_key_exchange( ssl );
2530 break;
2531
2532 case SSL_CERTIFICATE_VERIFY:
2533 ret = ssl_parse_certificate_verify( ssl );
2534 break;
2535
2536 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2537 ret = ssl_parse_change_cipher_spec( ssl );
2538 break;
2539
2540 case SSL_CLIENT_FINISHED:
2541 ret = ssl_parse_finished( ssl );
2542 break;
2543
2544 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002545 * ==> ( NewSessionTicket )
2546 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002547 * Finished
2548 */
2549 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002550 if( ssl->handshake->new_session_ticket != 0 )
2551 ret = ssl_write_new_session_ticket( ssl );
2552 else
2553 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002554 break;
2555
2556 case SSL_SERVER_FINISHED:
2557 ret = ssl_write_finished( ssl );
2558 break;
2559
2560 case SSL_FLUSH_BUFFERS:
2561 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2562 ssl->state = SSL_HANDSHAKE_WRAPUP;
2563 break;
2564
2565 case SSL_HANDSHAKE_WRAPUP:
2566 ssl_handshake_wrapup( ssl );
2567 break;
2568
2569 default:
2570 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2571 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002572 }
2573
Paul Bakker5121ce52009-01-03 21:22:43 +00002574 return( ret );
2575}
Paul Bakker5121ce52009-01-03 21:22:43 +00002576#endif