blob: 5b7db1718e47e6e33f303129084ab10c288264d1 [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
168 memset( p, 0, 16 ); // TODO: key_name
169 p += 16;
170
171 memset( p, 0, 16 ); // TODO: iv
172 p += 16;
173
174 ssl_save_session( ssl->session_negotiate, p + 2, &clear_len );
175 SSL_DEBUG_BUF( 4, "session ticket cleartext", p, clear_len );
176
177 // TODO: encrypt ticket
178 enc_len = clear_len;
179 (void) enc_len;
180
181 *p++ = (unsigned char)( ( clear_len >> 8 ) & 0xFF );
182 *p++ = (unsigned char)( ( clear_len ) & 0xFF );
183 p += clear_len;
184
185 memset( p, 0, 32 ); // TODO: mac
186 p += 32;
187
188 *tlen = p - start;
189
190 SSL_DEBUG_BUF( 4, "final session ticket", start, *tlen );
191
192 return( 0 );
193}
194
195/*
196 * Load session ticket (see ssl_write_ticket for structure)
197 */
198static int ssl_parse_ticket( ssl_context *ssl,
199 const unsigned char *buf,
200 size_t len )
201{
202 int ret;
203 ssl_session session;
204 const unsigned char *key_name = buf;
205 const unsigned char *iv = buf + 16;
206 const unsigned char *enc_len_p = iv + 16;
207 const unsigned char *ticket = enc_len_p + 2;
208 const unsigned char *mac;
209 size_t enc_len, clear_len;
210
211 if( len < 34 )
212 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
213
214 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
215 mac = ticket + enc_len;
216
217 if( len != enc_len + 66 )
218 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
219
220 // TODO: check key_name
221 (void) key_name;
222
223 // TODO: check hmac
224 (void) mac;
225
226 // TODO: decrypt ticket
227 clear_len = enc_len;
228
229 if( ( ret = ssl_load_session( &session, ticket, clear_len ) ) != 0 )
230 {
231 SSL_DEBUG_MSG( 1, ( "failed to parse ticket content" ) );
232 memset( &session, 0, sizeof( ssl_session ) );
233 return( ret );
234 }
235
236 /*
237 * Keep the session ID sent by the client, since we MUST send it back to
238 * inform him we're accepting the ticket (RFC 5077 section 3.4)
239 */
240 session.length = ssl->session_negotiate->length;
241 memcpy( &session.id, ssl->session_negotiate->id, session.length );
242
243 ssl_session_free( ssl->session_negotiate );
244 memcpy( ssl->session_negotiate, &session, sizeof( ssl_session ) );
245 memset( &session, 0, sizeof( ssl_session ) );
246
247 return( 0 );
248}
249
Paul Bakker5701cdc2012-09-27 21:49:42 +0000250static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000251 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000252 size_t len )
253{
254 int ret;
255 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000256 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000257
258 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
259 if( servername_list_size + 2 != len )
260 {
261 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
262 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
263 }
264
265 p = buf + 2;
266 while( servername_list_size > 0 )
267 {
268 hostname_len = ( ( p[1] << 8 ) | p[2] );
269 if( hostname_len + 3 > servername_list_size )
270 {
271 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
272 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
273 }
274
275 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
276 {
277 ret = ssl->f_sni( ssl->p_sni, ssl, p + 3, hostname_len );
278 if( ret != 0 )
279 {
280 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
281 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
282 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
283 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000284 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000285 }
286
287 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000288 p += hostname_len + 3;
289 }
290
291 if( servername_list_size != 0 )
292 {
293 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
294 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000295 }
296
297 return( 0 );
298}
299
Paul Bakker48916f92012-09-16 19:57:18 +0000300static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000301 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000302 size_t len )
303{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000304 int ret;
305
Paul Bakker48916f92012-09-16 19:57:18 +0000306 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
307 {
308 if( len != 1 || buf[0] != 0x0 )
309 {
310 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000311
312 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
313 return( ret );
314
Paul Bakker48916f92012-09-16 19:57:18 +0000315 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
316 }
317
318 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
319 }
320 else
321 {
322 if( len != 1 + ssl->verify_data_len ||
323 buf[0] != ssl->verify_data_len ||
324 memcmp( buf + 1, ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
325 {
326 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000327
328 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
329 return( ret );
330
Paul Bakker48916f92012-09-16 19:57:18 +0000331 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
332 }
333 }
334
335 return( 0 );
336}
337
Paul Bakker23f36802012-09-28 14:15:14 +0000338static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
339 const unsigned char *buf,
340 size_t len )
341{
342 size_t sig_alg_list_size;
343 const unsigned char *p;
344
345 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
346 if( sig_alg_list_size + 2 != len ||
347 sig_alg_list_size %2 != 0 )
348 {
349 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
350 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
351 }
352
353 p = buf + 2;
354 while( sig_alg_list_size > 0 )
355 {
356 if( p[1] != SSL_SIG_RSA )
Paul Bakker8611e732012-10-30 07:52:29 +0000357 {
358 sig_alg_list_size -= 2;
359 p += 2;
Paul Bakker23f36802012-09-28 14:15:14 +0000360 continue;
Paul Bakker8611e732012-10-30 07:52:29 +0000361 }
Paul Bakker9e36f042013-06-30 14:34:05 +0200362#if defined(POLARSSL_SHA512_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000363 if( p[0] == SSL_HASH_SHA512 )
364 {
365 ssl->handshake->sig_alg = SSL_HASH_SHA512;
366 break;
367 }
368 if( p[0] == SSL_HASH_SHA384 )
369 {
370 ssl->handshake->sig_alg = SSL_HASH_SHA384;
371 break;
372 }
373#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200374#if defined(POLARSSL_SHA256_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000375 if( p[0] == SSL_HASH_SHA256 )
376 {
377 ssl->handshake->sig_alg = SSL_HASH_SHA256;
378 break;
379 }
380 if( p[0] == SSL_HASH_SHA224 )
381 {
382 ssl->handshake->sig_alg = SSL_HASH_SHA224;
383 break;
384 }
385#endif
386 if( p[0] == SSL_HASH_SHA1 )
387 {
388 ssl->handshake->sig_alg = SSL_HASH_SHA1;
389 break;
390 }
391 if( p[0] == SSL_HASH_MD5 )
392 {
393 ssl->handshake->sig_alg = SSL_HASH_MD5;
394 break;
395 }
396
397 sig_alg_list_size -= 2;
398 p += 2;
399 }
400
401 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
402 ssl->handshake->sig_alg ) );
403
404 return( 0 );
405}
406
Paul Bakker41c83d32013-03-20 14:39:14 +0100407#if defined(POLARSSL_ECP_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200408static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
409 const unsigned char *buf,
410 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100411{
412 size_t list_size;
413 const unsigned char *p;
414
415 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
416 if( list_size + 2 != len ||
417 list_size % 2 != 0 )
418 {
419 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
420 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
421 }
422
423 p = buf + 2;
424 while( list_size > 0 )
425 {
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200426#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
427 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP192R1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100428 {
429 ssl->handshake->ec_curve = p[1];
430 return( 0 );
431 }
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200432#endif
433#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
434 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP224R1 )
435 {
436 ssl->handshake->ec_curve = p[1];
437 return( 0 );
438 }
439#endif
440#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
441 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP256R1 )
442 {
443 ssl->handshake->ec_curve = p[1];
444 return( 0 );
445 }
446#endif
447#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
448 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP384R1 )
449 {
450 ssl->handshake->ec_curve = p[1];
451 return( 0 );
452 }
453#endif
454#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
455 if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP521R1 )
456 {
457 ssl->handshake->ec_curve = p[1];
458 return( 0 );
459 }
460#endif
Paul Bakker41c83d32013-03-20 14:39:14 +0100461
462 list_size -= 2;
463 p += 2;
464 }
465
466 return( 0 );
467}
468
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200469static int ssl_parse_supported_point_formats( ssl_context *ssl,
470 const unsigned char *buf,
471 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100472{
473 size_t list_size;
474 const unsigned char *p;
475
476 list_size = buf[0];
477 if( list_size + 1 != len )
478 {
479 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
480 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
481 }
482
483 p = buf + 2;
484 while( list_size > 0 )
485 {
486 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
487 p[0] == POLARSSL_ECP_PF_COMPRESSED )
488 {
489 ssl->handshake->ec_point_format = p[0];
490 return( 0 );
491 }
492
493 list_size--;
494 p++;
495 }
496
497 return( 0 );
498}
499#endif /* POLARSSL_ECP_C */
500
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200501static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
502 const unsigned char *buf,
503 size_t len )
504{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200505 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200506 {
507 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
508 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
509 }
510
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200511 ssl->session_negotiate->mfl_code = buf[0];
512
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200513 return( 0 );
514}
515
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200516static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
517 const unsigned char *buf,
518 size_t len )
519{
520 if( len != 0 )
521 {
522 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
523 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
524 }
525
526 ((void) buf);
527
528 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
529
530 return( 0 );
531}
532
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200533static int ssl_parse_session_ticket_ext( ssl_context *ssl,
534 const unsigned char *buf,
535 size_t len )
536{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200537 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
538 return( 0 );
539
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200540 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200541 ssl->handshake->new_session_ticket = 1;
542
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200543 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
544
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200545 if( len == 0 )
546 return( 0 );
547
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200548 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
549 {
550 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
551 return( 0 );
552 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200553
554 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200555 * Failures are ok: just ignore the ticket and proceed.
556 */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200557 if( ssl_parse_ticket( ssl, buf, len ) != 0 )
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200558 return( 0 );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200559
560 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
561
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200562 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200563
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200564 /* Don't send a new ticket after all, this one is OK */
565 ssl->handshake->new_session_ticket = 0;
566
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200567 return( 0 );
568}
569
Paul Bakker78a8c712013-03-06 17:01:52 +0100570#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
571static int ssl_parse_client_hello_v2( ssl_context *ssl )
572{
573 int ret;
574 unsigned int i, j;
575 size_t n;
576 unsigned int ciph_len, sess_len, chal_len;
577 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200578 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200579 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100580
581 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
582
583 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
584 {
585 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
586
587 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
588 return( ret );
589
590 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
591 }
592
593 buf = ssl->in_hdr;
594
595 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
596
597 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
598 buf[2] ) );
599 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
600 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
601 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
602 buf[3], buf[4] ) );
603
604 /*
605 * SSLv2 Client Hello
606 *
607 * Record layer:
608 * 0 . 1 message length
609 *
610 * SSL layer:
611 * 2 . 2 message type
612 * 3 . 4 protocol version
613 */
614 if( buf[2] != SSL_HS_CLIENT_HELLO ||
615 buf[3] != SSL_MAJOR_VERSION_3 )
616 {
617 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
618 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
619 }
620
621 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
622
623 if( n < 17 || n > 512 )
624 {
625 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
626 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
627 }
628
629 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200630 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
631 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100632
633 if( ssl->minor_ver < ssl->min_minor_ver )
634 {
635 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
636 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
637 ssl->min_major_ver, ssl->min_minor_ver ) );
638
639 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
640 SSL_ALERT_MSG_PROTOCOL_VERSION );
641 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
642 }
643
Paul Bakker2fbefde2013-06-29 16:01:15 +0200644 ssl->handshake->max_major_ver = buf[3];
645 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100646
647 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
648 {
649 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
650 return( ret );
651 }
652
653 ssl->handshake->update_checksum( ssl, buf + 2, n );
654
655 buf = ssl->in_msg;
656 n = ssl->in_left - 5;
657
658 /*
659 * 0 . 1 ciphersuitelist length
660 * 2 . 3 session id length
661 * 4 . 5 challenge length
662 * 6 . .. ciphersuitelist
663 * .. . .. session id
664 * .. . .. challenge
665 */
666 SSL_DEBUG_BUF( 4, "record contents", buf, n );
667
668 ciph_len = ( buf[0] << 8 ) | buf[1];
669 sess_len = ( buf[2] << 8 ) | buf[3];
670 chal_len = ( buf[4] << 8 ) | buf[5];
671
672 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
673 ciph_len, sess_len, chal_len ) );
674
675 /*
676 * Make sure each parameter length is valid
677 */
678 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
679 {
680 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
681 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
682 }
683
684 if( sess_len > 32 )
685 {
686 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
687 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
688 }
689
690 if( chal_len < 8 || chal_len > 32 )
691 {
692 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
693 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
694 }
695
696 if( n != 6 + ciph_len + sess_len + chal_len )
697 {
698 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
699 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
700 }
701
702 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
703 buf + 6, ciph_len );
704 SSL_DEBUG_BUF( 3, "client hello, session id",
705 buf + 6 + ciph_len, sess_len );
706 SSL_DEBUG_BUF( 3, "client hello, challenge",
707 buf + 6 + ciph_len + sess_len, chal_len );
708
709 p = buf + 6 + ciph_len;
710 ssl->session_negotiate->length = sess_len;
711 memset( ssl->session_negotiate->id, 0, sizeof( ssl->session_negotiate->id ) );
712 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
713
714 p += sess_len;
715 memset( ssl->handshake->randbytes, 0, 64 );
716 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
717
718 /*
719 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
720 */
721 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
722 {
723 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
724 {
725 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
726 if( ssl->renegotiation == SSL_RENEGOTIATION )
727 {
728 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
729
730 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
731 return( ret );
732
733 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
734 }
735 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
736 break;
737 }
738 }
739
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200740 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
741 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +0100742 {
743 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
744 {
Paul Bakker41c83d32013-03-20 14:39:14 +0100745 // Only allow non-ECC ciphersuites as we do not have extensions
746 //
Paul Bakker59c28a22013-06-29 15:33:42 +0200747 if( p[0] == 0 && p[1] == 0 &&
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200748 ( ( ciphersuites[i] >> 8 ) & 0xFF ) == 0 &&
749 p[2] == ( ciphersuites[i] & 0xFF ) )
Paul Bakker59c28a22013-06-29 15:33:42 +0200750 {
751 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
752
753 if( ciphersuite_info == NULL )
754 {
755 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %02x not found",
756 ciphersuites[i] ) );
757 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
758 }
759
Paul Bakker2fbefde2013-06-29 16:01:15 +0200760 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
761 ciphersuite_info->max_minor_ver < ssl->minor_ver )
762 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +0200763
Paul Bakker78a8c712013-03-06 17:01:52 +0100764 goto have_ciphersuite_v2;
Paul Bakker59c28a22013-06-29 15:33:42 +0200765 }
Paul Bakker78a8c712013-03-06 17:01:52 +0100766 }
767 }
768
769 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
770
771 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
772
773have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200774 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +0200775 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +0100776 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +0100777
778 /*
779 * SSLv2 Client Hello relevant renegotiation security checks
780 */
781 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
782 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
783 {
784 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
785
786 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
787 return( ret );
788
789 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
790 }
791
792 ssl->in_left = 0;
793 ssl->state++;
794
795 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
796
797 return( 0 );
798}
799#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
800
Paul Bakker5121ce52009-01-03 21:22:43 +0000801static int ssl_parse_client_hello( ssl_context *ssl )
802{
Paul Bakker23986e52011-04-24 08:57:21 +0000803 int ret;
804 unsigned int i, j;
805 size_t n;
806 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +0000807 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +0000808 unsigned int ext_len = 0;
809 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000810 int renegotiation_info_seen = 0;
811 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200812 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +0100813 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000814
815 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
816
Paul Bakker48916f92012-09-16 19:57:18 +0000817 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
818 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000819 {
820 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
821 return( ret );
822 }
823
824 buf = ssl->in_hdr;
825
Paul Bakker78a8c712013-03-06 17:01:52 +0100826#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
827 if( ( buf[0] & 0x80 ) != 0 )
828 return ssl_parse_client_hello_v2( ssl );
829#endif
830
Paul Bakkerec636f32012-09-09 19:17:02 +0000831 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
832
833 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
834 buf[0] ) );
835 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
836 ( buf[3] << 8 ) | buf[4] ) );
837 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
838 buf[1], buf[2] ) );
839
840 /*
841 * SSLv3 Client Hello
842 *
843 * Record layer:
844 * 0 . 0 message type
845 * 1 . 2 protocol version
846 * 3 . 4 message length
847 */
848 if( buf[0] != SSL_MSG_HANDSHAKE ||
849 buf[1] != SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000850 {
Paul Bakkerec636f32012-09-09 19:17:02 +0000851 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
852 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
853 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000854
Paul Bakkerec636f32012-09-09 19:17:02 +0000855 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000856
Manuel Pégourié-Gonnard72882b22013-08-02 13:36:00 +0200857 if( n < 45 || n > 2048 )
Paul Bakkerec636f32012-09-09 19:17:02 +0000858 {
859 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
860 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
861 }
862
Paul Bakker48916f92012-09-16 19:57:18 +0000863 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
864 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +0000865 {
866 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
867 return( ret );
868 }
869
870 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +0000871 if( !ssl->renegotiation )
872 n = ssl->in_left - 5;
873 else
874 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +0000875
Paul Bakker48916f92012-09-16 19:57:18 +0000876 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +0000877
878 /*
879 * SSL layer:
880 * 0 . 0 handshake type
881 * 1 . 3 handshake length
882 * 4 . 5 protocol version
883 * 6 . 9 UNIX time()
884 * 10 . 37 random bytes
885 * 38 . 38 session id length
886 * 39 . 38+x session id
887 * 39+x . 40+x ciphersuitelist length
888 * 41+x . .. ciphersuitelist
889 * .. . .. compression alg.
890 * .. . .. extensions
891 */
892 SSL_DEBUG_BUF( 4, "record contents", buf, n );
893
894 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
895 buf[0] ) );
896 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
897 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
898 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
899 buf[4], buf[5] ) );
900
901 /*
902 * Check the handshake type and protocol version
903 */
904 if( buf[0] != SSL_HS_CLIENT_HELLO ||
905 buf[4] != SSL_MAJOR_VERSION_3 )
906 {
907 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
908 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
909 }
910
911 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200912 ssl->minor_ver = ( buf[5] <= ssl->max_minor_ver )
913 ? buf[5] : ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +0000914
Paul Bakker1d29fb52012-09-28 13:28:45 +0000915 if( ssl->minor_ver < ssl->min_minor_ver )
916 {
917 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
918 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +0000919 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +0000920
921 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
922 SSL_ALERT_MSG_PROTOCOL_VERSION );
923
924 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
925 }
926
Paul Bakker2fbefde2013-06-29 16:01:15 +0200927 ssl->handshake->max_major_ver = buf[4];
928 ssl->handshake->max_minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +0000929
Paul Bakker48916f92012-09-16 19:57:18 +0000930 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +0000931
932 /*
933 * Check the handshake message length
934 */
935 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
936 {
937 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
938 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
939 }
940
941 /*
942 * Check the session length
943 */
944 sess_len = buf[38];
945
946 if( sess_len > 32 )
947 {
948 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
949 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
950 }
951
Paul Bakker48916f92012-09-16 19:57:18 +0000952 ssl->session_negotiate->length = sess_len;
953 memset( ssl->session_negotiate->id, 0,
954 sizeof( ssl->session_negotiate->id ) );
955 memcpy( ssl->session_negotiate->id, buf + 39,
956 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +0000957
958 /*
959 * Check the ciphersuitelist length
960 */
961 ciph_len = ( buf[39 + sess_len] << 8 )
962 | ( buf[40 + sess_len] );
963
964 if( ciph_len < 2 || ciph_len > 256 || ( ciph_len % 2 ) != 0 )
965 {
966 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
967 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
968 }
969
970 /*
971 * Check the compression algorithms length
972 */
973 comp_len = buf[41 + sess_len + ciph_len];
974
975 if( comp_len < 1 || comp_len > 16 )
976 {
977 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
978 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
979 }
980
Paul Bakker48916f92012-09-16 19:57:18 +0000981 /*
982 * Check the extension length
983 */
984 if( n > 42 + sess_len + ciph_len + comp_len )
985 {
986 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
987 | ( buf[43 + sess_len + ciph_len + comp_len] );
988
989 if( ( ext_len > 0 && ext_len < 4 ) ||
990 n != 44 + sess_len + ciph_len + comp_len + ext_len )
991 {
992 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
993 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
994 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
995 }
996 }
997
998 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +0000999#if defined(POLARSSL_ZLIB_SUPPORT)
1000 for( i = 0; i < comp_len; ++i )
1001 {
Paul Bakker48916f92012-09-16 19:57:18 +00001002 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001003 {
Paul Bakker48916f92012-09-16 19:57:18 +00001004 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001005 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001006 }
1007 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001008#endif
1009
Paul Bakkerec636f32012-09-09 19:17:02 +00001010 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1011 buf + 6, 32 );
1012 SSL_DEBUG_BUF( 3, "client hello, session id",
1013 buf + 38, sess_len );
1014 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1015 buf + 41 + sess_len, ciph_len );
1016 SSL_DEBUG_BUF( 3, "client hello, compression",
1017 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001018
Paul Bakkerec636f32012-09-09 19:17:02 +00001019 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001020 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1021 */
1022 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1023 {
1024 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1025 {
1026 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1027 if( ssl->renegotiation == SSL_RENEGOTIATION )
1028 {
1029 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001030
1031 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1032 return( ret );
1033
Paul Bakker48916f92012-09-16 19:57:18 +00001034 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1035 }
1036 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1037 break;
1038 }
1039 }
1040
Paul Bakker48916f92012-09-16 19:57:18 +00001041 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001042
1043 while( ext_len )
1044 {
1045 unsigned int ext_id = ( ( ext[0] << 8 )
1046 | ( ext[1] ) );
1047 unsigned int ext_size = ( ( ext[2] << 8 )
1048 | ( ext[3] ) );
1049
1050 if( ext_size + 4 > ext_len )
1051 {
1052 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1053 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1054 }
1055 switch( ext_id )
1056 {
Paul Bakker5701cdc2012-09-27 21:49:42 +00001057 case TLS_EXT_SERVERNAME:
1058 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1059 if( ssl->f_sni == NULL )
1060 break;
1061
1062 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1063 if( ret != 0 )
1064 return( ret );
1065 break;
1066
Paul Bakker48916f92012-09-16 19:57:18 +00001067 case TLS_EXT_RENEGOTIATION_INFO:
1068 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1069 renegotiation_info_seen = 1;
1070
Paul Bakker23f36802012-09-28 14:15:14 +00001071 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1072 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001073 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001074 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001075
Paul Bakker23f36802012-09-28 14:15:14 +00001076 case TLS_EXT_SIG_ALG:
1077 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1078 if( ssl->renegotiation == SSL_RENEGOTIATION )
1079 break;
1080
1081 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1082 if( ret != 0 )
1083 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001084 break;
1085
Paul Bakker41c83d32013-03-20 14:39:14 +01001086#if defined(POLARSSL_ECP_C)
1087 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1088 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1089
1090 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1091 if( ret != 0 )
1092 return( ret );
1093 break;
1094
1095 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1096 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1097
1098 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1099 if( ret != 0 )
1100 return( ret );
1101 break;
1102#endif /* POLARSSL_ECP_C */
1103
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001104 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1105 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1106
1107 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1108 if( ret != 0 )
1109 return( ret );
1110 break;
1111
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001112 case TLS_EXT_TRUNCATED_HMAC:
1113 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1114
1115 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1116 if( ret != 0 )
1117 return( ret );
1118 break;
1119
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001120 case TLS_EXT_SESSION_TICKET:
1121 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1122
1123 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1124 if( ret != 0 )
1125 return( ret );
1126 break;
1127
Paul Bakker48916f92012-09-16 19:57:18 +00001128 default:
1129 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1130 ext_id ) );
1131 }
1132
1133 ext_len -= 4 + ext_size;
1134 ext += 4 + ext_size;
1135
1136 if( ext_len > 0 && ext_len < 4 )
1137 {
1138 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1139 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1140 }
1141 }
1142
1143 /*
1144 * Renegotiation security checks
1145 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001146 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1147 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1148 {
1149 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1150 handshake_failure = 1;
1151 }
1152 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1153 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1154 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001155 {
1156 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001157 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001158 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001159 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1160 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1161 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001162 {
1163 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001164 handshake_failure = 1;
1165 }
1166 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1167 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1168 renegotiation_info_seen == 1 )
1169 {
1170 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1171 handshake_failure = 1;
1172 }
1173
1174 if( handshake_failure == 1 )
1175 {
1176 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1177 return( ret );
1178
Paul Bakker48916f92012-09-16 19:57:18 +00001179 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1180 }
Paul Bakker380da532012-04-18 16:10:25 +00001181
Paul Bakker41c83d32013-03-20 14:39:14 +01001182 /*
1183 * Search for a matching ciphersuite
1184 * (At the end because we need information from the EC-based extensions)
1185 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001186 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
1187 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001188 {
1189 for( j = 0, p = buf + 41 + sess_len; j < ciph_len;
1190 j += 2, p += 2 )
1191 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001192 if( p[0] == ( ( ciphersuites[i] >> 8 ) & 0xFF ) &&
1193 p[1] == ( ( ciphersuites[i] ) & 0xFF ) )
Paul Bakker41c83d32013-03-20 14:39:14 +01001194 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001195 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
Paul Bakker41c83d32013-03-20 14:39:14 +01001196
1197 if( ciphersuite_info == NULL )
1198 {
1199 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %02x not found",
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001200 ciphersuites[i] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001201 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1202 }
1203
Paul Bakker2fbefde2013-06-29 16:01:15 +02001204 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
1205 ciphersuite_info->max_minor_ver < ssl->minor_ver )
1206 continue;
1207
Paul Bakker41c83d32013-03-20 14:39:14 +01001208 if( ( ciphersuite_info->flags & POLARSSL_CIPHERSUITE_EC ) &&
1209 ssl->handshake->ec_curve == 0 )
1210 continue;
1211
1212 goto have_ciphersuite;
1213 }
1214 }
1215 }
1216
1217 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1218
1219 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1220 return( ret );
1221
1222 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1223
1224have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001225 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001226 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1227 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1228
Paul Bakker5121ce52009-01-03 21:22:43 +00001229 ssl->in_left = 0;
1230 ssl->state++;
1231
1232 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1233
1234 return( 0 );
1235}
1236
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001237static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1238 unsigned char *buf,
1239 size_t *olen )
1240{
1241 unsigned char *p = buf;
1242
1243 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1244 {
1245 *olen = 0;
1246 return;
1247 }
1248
1249 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1250
1251 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1252 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1253
1254 *p++ = 0x00;
1255 *p++ = 0x00;
1256
1257 *olen = 4;
1258}
1259
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001260static void ssl_write_session_ticket_ext( ssl_context *ssl,
1261 unsigned char *buf,
1262 size_t *olen )
1263{
1264 unsigned char *p = buf;
1265
1266 if( ssl->handshake->new_session_ticket == 0 )
1267 {
1268 *olen = 0;
1269 return;
1270 }
1271
1272 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1273
1274 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1275 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1276
1277 *p++ = 0x00;
1278 *p++ = 0x00;
1279
1280 *olen = 4;
1281}
1282
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001283static void ssl_write_renegotiation_ext( ssl_context *ssl,
1284 unsigned char *buf,
1285 size_t *olen )
1286{
1287 unsigned char *p = buf;
1288
1289 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1290 {
1291 *olen = 0;
1292 return;
1293 }
1294
1295 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1296
1297 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1298 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1299
1300 *p++ = 0x00;
1301 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1302 *p++ = ssl->verify_data_len * 2 & 0xFF;
1303
1304 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1305 p += ssl->verify_data_len;
1306 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1307 p += ssl->verify_data_len;
1308
1309 *olen = 5 + ssl->verify_data_len * 2;
1310}
1311
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001312static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1313 unsigned char *buf,
1314 size_t *olen )
1315{
1316 unsigned char *p = buf;
1317
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001318 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1319 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001320 *olen = 0;
1321 return;
1322 }
1323
1324 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1325
1326 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1327 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1328
1329 *p++ = 0x00;
1330 *p++ = 1;
1331
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001332 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001333
1334 *olen = 5;
1335}
1336
Paul Bakker5121ce52009-01-03 21:22:43 +00001337static int ssl_write_server_hello( ssl_context *ssl )
1338{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001339#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001340 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001341#endif
Paul Bakkera3d195c2011-11-27 21:07:34 +00001342 int ret, n;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001343 size_t olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001344 unsigned char *buf, *p;
1345
1346 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1347
1348 /*
1349 * 0 . 0 handshake type
1350 * 1 . 3 handshake length
1351 * 4 . 5 protocol version
1352 * 6 . 9 UNIX time()
1353 * 10 . 37 random bytes
1354 */
1355 buf = ssl->out_msg;
1356 p = buf + 4;
1357
1358 *p++ = (unsigned char) ssl->major_ver;
1359 *p++ = (unsigned char) ssl->minor_ver;
1360
1361 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1362 buf[4], buf[5] ) );
1363
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001364#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001365 t = time( NULL );
1366 *p++ = (unsigned char)( t >> 24 );
1367 *p++ = (unsigned char)( t >> 16 );
1368 *p++ = (unsigned char)( t >> 8 );
1369 *p++ = (unsigned char)( t );
1370
1371 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001372#else
1373 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1374 return( ret );
1375
1376 p += 4;
1377#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001378
Paul Bakkera3d195c2011-11-27 21:07:34 +00001379 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1380 return( ret );
1381
1382 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001383
Paul Bakker48916f92012-09-16 19:57:18 +00001384 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001385
1386 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1387
1388 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001389 * Resume is 0 by default, see ssl_handshake_init().
1390 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1391 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001392 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001393 if( ssl->handshake->resume == 0 &&
1394 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001395 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001396 ssl->f_get_cache != NULL &&
1397 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1398 {
1399 ssl->handshake->resume = 1;
1400 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001401
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001402 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001403 {
1404 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001405 * New session, create a new session id,
1406 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001407 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001408 ssl->state++;
1409
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001410 if( ssl->handshake->new_session_ticket == 0 )
1411 {
1412 ssl->session_negotiate->length = n = 32;
1413 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
1414 n ) ) != 0 )
1415 return( ret );
1416 }
1417 else
1418 {
1419 ssl->session_negotiate->length = 0;
1420 memset( ssl->session_negotiate->id, 0, 32 );
1421 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001422 }
1423 else
1424 {
1425 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001426 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001427 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001428 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001429
1430 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1431 {
1432 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1433 return( ret );
1434 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001435 }
1436
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001437 /*
1438 * 38 . 38 session id length
1439 * 39 . 38+n session id
1440 * 39+n . 40+n chosen ciphersuite
1441 * 41+n . 41+n chosen compression alg.
1442 * 42+n . 43+n extensions length
1443 * 44+n . 43+n+m extensions
1444 */
1445 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001446 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1447 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001448
1449 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1450 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1451 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001452 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001453
Paul Bakker48916f92012-09-16 19:57:18 +00001454 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1455 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1456 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001457
Paul Bakkere3166ce2011-01-27 17:40:50 +00001458 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001459 ssl->session_negotiate->ciphersuite ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001460 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001461 ssl->session_negotiate->compression ) );
1462
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001463 /*
1464 * First write extensions, then the total length
1465 */
1466 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1467 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001468
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001469 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1470 ext_len += olen;
1471
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001472 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1473 ext_len += olen;
1474
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001475 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1476 ext_len += olen;
1477
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001478 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001479
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001480 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1481 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1482 p += ext_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001483
1484 ssl->out_msglen = p - buf;
1485 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1486 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1487
1488 ret = ssl_write_record( ssl );
1489
1490 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1491
1492 return( ret );
1493}
1494
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001495#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1496 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
1497 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001498static int ssl_write_certificate_request( ssl_context *ssl )
1499{
Paul Bakkered27a042013-04-18 22:46:23 +02001500 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1501 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001502
1503 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1504
1505 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1506 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1507 {
1508 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1509 ssl->state++;
1510 return( 0 );
1511 }
1512
1513 return( ret );
1514}
1515#else
1516static int ssl_write_certificate_request( ssl_context *ssl )
1517{
1518 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1519 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker926af752012-11-23 13:38:07 +01001520 size_t n = 0, dn_size, total_dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001521 unsigned char *buf, *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001522 const x509_cert *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001523
1524 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1525
1526 ssl->state++;
1527
Paul Bakkerfbb17802013-04-17 19:10:21 +02001528 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001529 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001530 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001531 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001532 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001533 return( 0 );
1534 }
1535
1536 /*
1537 * 0 . 0 handshake type
1538 * 1 . 3 handshake length
1539 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01001540 * 5 .. m-1 cert types
1541 * m .. m+1 sig alg length (TLS 1.2 only)
1542 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001543 * n .. n+1 length of all DNs
1544 * n+2 .. n+3 length of DN 1
1545 * n+4 .. ... Distinguished Name #1
1546 * ... .. ... length of DN 2, etc.
1547 */
1548 buf = ssl->out_msg;
1549 p = buf + 4;
1550
1551 /*
1552 * At the moment, only RSA certificates are supported
1553 */
1554 *p++ = 1;
Paul Bakker926af752012-11-23 13:38:07 +01001555 *p++ = SSL_CERT_TYPE_RSA_SIGN;
1556
1557 /*
1558 * Add signature_algorithms for verify (TLS 1.2)
1559 * Only add current running algorithm that is already required for
1560 * requested ciphersuite.
1561 *
1562 * Length is always 2
1563 */
Paul Bakker21dca692013-01-03 11:41:08 +01001564 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01001565 {
1566 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
1567
1568 *p++ = 0;
1569 *p++ = 2;
1570
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001571 if( ssl->transform_negotiate->ciphersuite_info->mac ==
1572 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01001573 {
1574 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
1575 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02001576
Paul Bakker926af752012-11-23 13:38:07 +01001577 *p++ = ssl->handshake->verify_sig_alg;
1578 *p++ = SSL_SIG_RSA;
1579
1580 n += 4;
1581 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001582
1583 p += 2;
1584 crt = ssl->ca_chain;
1585
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001586 total_dn_size = 0;
Paul Bakker29087132010-03-21 21:03:34 +00001587 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001588 {
1589 if( p - buf > 4096 )
1590 break;
1591
Paul Bakker926af752012-11-23 13:38:07 +01001592 dn_size = crt->subject_raw.len;
1593 *p++ = (unsigned char)( dn_size >> 8 );
1594 *p++ = (unsigned char)( dn_size );
1595 memcpy( p, crt->subject_raw.p, dn_size );
1596 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001597
Paul Bakker926af752012-11-23 13:38:07 +01001598 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
1599
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001600 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01001601 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00001602 }
1603
Paul Bakker926af752012-11-23 13:38:07 +01001604 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001605 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1606 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Paul Bakker926af752012-11-23 13:38:07 +01001607 ssl->out_msg[6 + n] = (unsigned char)( total_dn_size >> 8 );
1608 ssl->out_msg[7 + n] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001609
1610 ret = ssl_write_record( ssl );
1611
1612 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
1613
1614 return( ret );
1615}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001616#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
1617 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
1618 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001619
Paul Bakker41c83d32013-03-20 14:39:14 +01001620static int ssl_write_server_key_exchange( ssl_context *ssl )
1621{
Paul Bakker23986e52011-04-24 08:57:21 +00001622 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001623 size_t n = 0, len;
Paul Bakker23f36802012-09-28 14:15:14 +00001624 unsigned char hash[64];
Paul Bakkerc70b9822013-04-07 22:00:46 +02001625 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker35a7fe52012-10-31 09:07:14 +00001626 unsigned int hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001627 unsigned char *p = ssl->out_msg + 4;
1628 unsigned char *dig_sig = p;
1629 size_t dig_sig_len = 0;
Paul Bakker41c83d32013-03-20 14:39:14 +01001630
1631 const ssl_ciphersuite_t *ciphersuite_info;
1632 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001633
1634 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
1635
Paul Bakker41c83d32013-03-20 14:39:14 +01001636 if( ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_RSA &&
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001637 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_ECDHE_RSA &&
1638 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001639 {
1640 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
1641 ssl->state++;
1642 return( 0 );
1643 }
1644
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001645#if defined(POLARSSL_RSA_C)
Paul Bakker43b7e352011-01-18 15:27:19 +00001646 if( ssl->rsa_key == NULL )
1647 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00001648 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
1649 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker43b7e352011-01-18 15:27:19 +00001650 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001651#endif /* POLARSSL_RSA_C */
Paul Bakker43b7e352011-01-18 15:27:19 +00001652
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001653#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1654 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1655 {
1656 /* TODO: Support identity hints */
1657 *(p++) = 0x00;
1658 *(p++) = 0x00;
1659
1660 n += 2;
1661 }
1662#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1663
1664#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1665 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1666 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1667 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00001668 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001669 /*
1670 * Ephemeral DH parameters:
1671 *
1672 * struct {
1673 * opaque dh_p<1..2^16-1>;
1674 * opaque dh_g<1..2^16-1>;
1675 * opaque dh_Ys<1..2^16-1>;
1676 * } ServerDHParams;
1677 */
1678 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
1679 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
1680 {
1681 SSL_DEBUG_RET( 1, "mpi_copy", ret );
1682 return( ret );
1683 }
Paul Bakker48916f92012-09-16 19:57:18 +00001684
Paul Bakker41c83d32013-03-20 14:39:14 +01001685 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
1686 mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001687 p,
1688 &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001689 {
1690 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
1691 return( ret );
1692 }
1693
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001694 dig_sig = p;
1695 dig_sig_len = len;
1696
1697 p += len;
1698 n += len;
1699
Paul Bakker41c83d32013-03-20 14:39:14 +01001700 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
1701 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1702 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1703 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
1704 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001705#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1706 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001707
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001708#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01001709 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001710 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001711 /*
1712 * Ephemeral ECDH parameters:
1713 *
1714 * struct {
1715 * ECParameters curve_params;
1716 * ECPoint public;
1717 * } ServerECDHParams;
1718 */
Paul Bakker41c83d32013-03-20 14:39:14 +01001719 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
1720 ssl->handshake->ec_curve ) ) != 0 )
1721 {
1722 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
1723 return( ret );
1724 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001725
Paul Bakker41c83d32013-03-20 14:39:14 +01001726 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001727 &len,
1728 p,
Paul Bakker41c83d32013-03-20 14:39:14 +01001729 1000, ssl->f_rng, ssl->p_rng ) ) != 0 )
1730 {
1731 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
1732 return( ret );
1733 }
1734
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001735 dig_sig = p;
1736 dig_sig_len = len;
1737
1738 p += len;
1739 n += len;
1740
Paul Bakker41c83d32013-03-20 14:39:14 +01001741 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
1742 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001743#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001744
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001745#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1746 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
1747 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1748 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001749 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001750 size_t rsa_key_len = 0;
Paul Bakker23f36802012-09-28 14:15:14 +00001751
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001752 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker23f36802012-09-28 14:15:14 +00001753 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001754 md5_context md5;
1755 sha1_context sha1;
1756
1757 /*
1758 * digitally-signed struct {
1759 * opaque md5_hash[16];
1760 * opaque sha_hash[20];
1761 * };
1762 *
1763 * md5_hash
1764 * MD5(ClientHello.random + ServerHello.random
1765 * + ServerParams);
1766 * sha_hash
1767 * SHA(ClientHello.random + ServerHello.random
1768 * + ServerParams);
1769 */
1770 md5_starts( &md5 );
1771 md5_update( &md5, ssl->handshake->randbytes, 64 );
1772 md5_update( &md5, dig_sig, dig_sig_len );
1773 md5_finish( &md5, hash );
1774
1775 sha1_starts( &sha1 );
1776 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
1777 sha1_update( &sha1, dig_sig, dig_sig_len );
1778 sha1_finish( &sha1, hash + 16 );
1779
1780 hashlen = 36;
1781 md_alg = POLARSSL_MD_NONE;
1782 }
1783 else
1784 {
1785 md_context_t ctx;
1786
1787 /*
1788 * digitally-signed struct {
1789 * opaque client_random[32];
1790 * opaque server_random[32];
1791 * ServerDHParams params;
1792 * };
1793 */
1794 switch( ssl->handshake->sig_alg )
1795 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001796#if defined(POLARSSL_MD5_C)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001797 case SSL_HASH_MD5:
1798 md_alg = POLARSSL_MD_MD5;
1799 break;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001800#endif
1801#if defined(POLARSSL_SHA1_C)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001802 case SSL_HASH_SHA1:
1803 md_alg = POLARSSL_MD_SHA1;
1804 break;
Paul Bakker23f36802012-09-28 14:15:14 +00001805#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02001806#if defined(POLARSSL_SHA256_C)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001807 case SSL_HASH_SHA224:
1808 md_alg = POLARSSL_MD_SHA224;
1809 break;
1810 case SSL_HASH_SHA256:
1811 md_alg = POLARSSL_MD_SHA256;
1812 break;
Paul Bakker23f36802012-09-28 14:15:14 +00001813#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02001814#if defined(POLARSSL_SHA512_C)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001815 case SSL_HASH_SHA384:
1816 md_alg = POLARSSL_MD_SHA384;
1817 break;
1818 case SSL_HASH_SHA512:
1819 md_alg = POLARSSL_MD_SHA512;
1820 break;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001821#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001822 default:
1823 /* Should never happen */
1824 return( -1 );
1825 }
1826
1827 if( ( ret = md_init_ctx( &ctx, md_info_from_type( md_alg ) ) ) != 0 )
1828 {
1829 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
1830 return( ret );
1831 }
1832
1833 md_starts( &ctx );
1834 md_update( &ctx, ssl->handshake->randbytes, 64 );
1835 md_update( &ctx, dig_sig, dig_sig_len );
1836 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02001837
1838 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
1839 {
1840 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
1841 return( ret );
1842 }
1843
Paul Bakker23f36802012-09-28 14:15:14 +00001844 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001845
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001846 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
1847
1848 if ( ssl->rsa_key )
1849 rsa_key_len = ssl->rsa_key_len( ssl->rsa_key );
1850
1851 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker23f36802012-09-28 14:15:14 +00001852 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001853 *(p++) = ssl->handshake->sig_alg;
1854 *(p++) = SSL_SIG_RSA;
1855
1856 n += 2;
1857 }
1858
1859 *(p++) = (unsigned char)( rsa_key_len >> 8 );
1860 *(p++) = (unsigned char)( rsa_key_len );
1861 n += 2;
1862
1863 if ( ssl->rsa_key )
1864 {
1865 ret = ssl->rsa_sign( ssl->rsa_key, ssl->f_rng, ssl->p_rng,
1866 RSA_PRIVATE, md_alg, hashlen, hash, p );
1867 }
1868
1869 if( ret != 0 )
1870 {
1871 SSL_DEBUG_RET( 1, "pkcs1_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001872 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001873 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001874
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001875 SSL_DEBUG_BUF( 3, "my RSA sig", p, rsa_key_len );
1876
1877 p += rsa_key_len;
1878 n += rsa_key_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001879 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001880#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
1881 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001882
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001883 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001884 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1885 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
1886
1887 ssl->state++;
1888
1889 if( ( ret = ssl_write_record( ssl ) ) != 0 )
1890 {
1891 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
1892 return( ret );
1893 }
1894
1895 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
1896
1897 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001898}
1899
1900static int ssl_write_server_hello_done( ssl_context *ssl )
1901{
1902 int ret;
1903
1904 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
1905
1906 ssl->out_msglen = 4;
1907 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1908 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
1909
1910 ssl->state++;
1911
1912 if( ( ret = ssl_write_record( ssl ) ) != 0 )
1913 {
1914 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
1915 return( ret );
1916 }
1917
1918 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
1919
1920 return( 0 );
1921}
1922
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001923#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1924 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1925static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
1926 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02001927{
1928 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02001929 size_t n;
1930
1931 /*
1932 * Receive G^Y mod P, premaster = (G^Y)^X mod P
1933 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001934 if( *p + 2 > end )
1935 {
1936 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
1937 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
1938 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02001939
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001940 n = ( (*p)[0] << 8 ) | (*p)[1];
1941 *p += 2;
1942
1943 if( n < 1 || n > ssl->handshake->dhm_ctx.len || *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02001944 {
1945 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
1946 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
1947 }
1948
1949 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001950 *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02001951 {
1952 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
1953 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
1954 }
1955
1956 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
1957
Paul Bakker70df2fb2013-04-17 17:19:09 +02001958 return( ret );
1959}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001960#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1961 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02001962
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001963#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
Paul Bakker70df2fb2013-04-17 17:19:09 +02001964static int ssl_parse_client_ecdh_public( ssl_context *ssl )
1965{
1966 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02001967 size_t n;
1968
1969 /*
1970 * Receive client public key and calculate premaster
1971 */
1972 n = ssl->in_msg[3];
1973
1974 if( n < 1 || n > mpi_size( &ssl->handshake->ecdh_ctx.grp.P ) * 2 + 2 ||
1975 n + 4 != ssl->in_hslen )
1976 {
1977 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
1978 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
1979 }
1980
1981 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
1982 ssl->in_msg + 4, n ) ) != 0 )
1983 {
1984 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
1985 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
1986 }
1987
1988 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
1989
Paul Bakker70df2fb2013-04-17 17:19:09 +02001990 return( ret );
1991}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001992#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02001993
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001994#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakker70df2fb2013-04-17 17:19:09 +02001995static int ssl_parse_encrypted_pms_secret( ssl_context *ssl )
1996{
1997 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1998 size_t i, n = 0;
1999
2000 if( ssl->rsa_key == NULL )
2001 {
2002 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2003 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2004 }
2005
2006 /*
2007 * Decrypt the premaster using own private RSA key
2008 */
2009 i = 4;
2010 if( ssl->rsa_key )
2011 n = ssl->rsa_key_len( ssl->rsa_key );
2012 ssl->handshake->pmslen = 48;
2013
2014 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2015 {
2016 i += 2;
2017 if( ssl->in_msg[4] != ( ( n >> 8 ) & 0xFF ) ||
2018 ssl->in_msg[5] != ( ( n ) & 0xFF ) )
2019 {
2020 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2021 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2022 }
2023 }
2024
2025 if( ssl->in_hslen != i + n )
2026 {
2027 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2028 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2029 }
2030
2031 if( ssl->rsa_key ) {
2032 ret = ssl->rsa_decrypt( ssl->rsa_key, RSA_PRIVATE,
2033 &ssl->handshake->pmslen,
2034 ssl->in_msg + i,
2035 ssl->handshake->premaster,
2036 sizeof(ssl->handshake->premaster) );
2037 }
2038
2039 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Paul Bakker2fbefde2013-06-29 16:01:15 +02002040 ssl->handshake->premaster[0] != ssl->handshake->max_major_ver ||
2041 ssl->handshake->premaster[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002042 {
2043 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2044
2045 /*
2046 * Protection against Bleichenbacher's attack:
2047 * invalid PKCS#1 v1.5 padding must not cause
2048 * the connection to end immediately; instead,
2049 * send a bad_record_mac later in the handshake.
2050 */
2051 ssl->handshake->pmslen = 48;
2052
2053 ret = ssl->f_rng( ssl->p_rng, ssl->handshake->premaster,
2054 ssl->handshake->pmslen );
2055 if( ret != 0 )
2056 return( ret );
2057 }
2058
2059 return( ret );
2060}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002061#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002062
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002063#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2064 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2065static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2066 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002067{
2068 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002069 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002070
2071 if( ssl->psk == NULL || ssl->psk_identity == NULL ||
2072 ssl->psk_identity_len == 0 || ssl->psk_len == 0 )
2073 {
2074 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2075 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2076 }
2077
2078 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002079 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002080 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002081 if( *p + 2 > end )
2082 {
2083 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2084 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2085 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002086
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002087 n = ( (*p)[0] << 8 ) | (*p)[1];
2088 *p += 2;
2089
2090 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002091 {
2092 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2093 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2094 }
2095
2096 if( n != ssl->psk_identity_len ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002097 memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002098 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002099 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002100 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2101 }
2102
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002103 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002104 ret = 0;
2105
Paul Bakkerfbb17802013-04-17 19:10:21 +02002106 return( ret );
2107}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002108#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
2109 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002110
Paul Bakker5121ce52009-01-03 21:22:43 +00002111static int ssl_parse_client_key_exchange( ssl_context *ssl )
2112{
Paul Bakker23986e52011-04-24 08:57:21 +00002113 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002114 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002115 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002116
Paul Bakker41c83d32013-03-20 14:39:14 +01002117 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002118
2119 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2120
2121 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2122 {
2123 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2124 return( ret );
2125 }
2126
2127 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2128 {
2129 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002130 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002131 }
2132
2133 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2134 {
2135 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002136 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002137 }
2138
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002139 p = ssl->in_msg + 4;
2140 end = ssl->in_msg + ssl->in_msglen;
2141
2142#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002143 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002144 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002145 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002146 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002147 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2148 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002149 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002150
2151 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
2152
2153 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2154 ssl->handshake->premaster,
2155 &ssl->handshake->pmslen ) ) != 0 )
2156 {
2157 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2158 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2159 }
2160
2161 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002162 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002163 else
2164#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
2165#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
2166 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002167 {
2168 if( ( ret = ssl_parse_client_ecdh_public( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002169 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002170 SSL_DEBUG_RET( 1, ( "ssl_parse_client_ecdh_public" ), ret );
2171 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002172 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002173
2174 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2175 &ssl->handshake->pmslen,
2176 ssl->handshake->premaster,
2177 POLARSSL_MPI_MAX_SIZE ) ) != 0 )
2178 {
2179 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2180 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2181 }
2182
2183 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002184 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002185 else
2186#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
2187#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2188 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002189 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002190 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002191 {
2192 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2193 return( ret );
2194 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002195
2196 // Set up the premaster secret
2197 //
2198 p = ssl->handshake->premaster;
2199 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2200 *(p++) = (unsigned char)( ssl->psk_len );
2201 p += ssl->psk_len;
2202
2203 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2204 *(p++) = (unsigned char)( ssl->psk_len );
2205 memcpy( p, ssl->psk, ssl->psk_len );
2206 p += ssl->psk_len;
2207
2208 ssl->handshake->pmslen = 4 + 2 * ssl->psk_len;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002209 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002210 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002211#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
2212#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2213 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2214 {
2215 size_t n;
2216
2217 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2218 {
2219 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2220 return( ret );
2221 }
2222 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2223 {
2224 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2225 return( ret );
2226 }
2227
2228 // Set up the premaster secret
2229 //
2230 p = ssl->handshake->premaster;
2231 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len >> 8 );
2232 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len );
2233
2234 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2235 p, &n ) ) != 0 )
2236 {
2237 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2238 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2239 }
2240
2241 if( n != ssl->handshake->dhm_ctx.len )
2242 {
2243 SSL_DEBUG_MSG( 1, ( "dhm_calc_secret result smaller than DHM" ) );
2244 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2245 }
2246
2247 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2248
2249 p += ssl->handshake->dhm_ctx.len;
2250
2251 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2252 *(p++) = (unsigned char)( ssl->psk_len );
2253 memcpy( p, ssl->psk, ssl->psk_len );
2254 p += ssl->psk_len;
2255
2256 ssl->handshake->pmslen = 4 + ssl->handshake->dhm_ctx.len + ssl->psk_len;
2257 }
2258 else
2259#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
2260#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2261 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002262 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002263 if( ( ret = ssl_parse_encrypted_pms_secret( ssl ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002264 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002265 SSL_DEBUG_RET( 1, ( "ssl_parse_client_ecdh_public" ), ret );
2266 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002267 }
2268 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002269 else
2270#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2271 {
2272 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2273 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002274
Paul Bakkerff60ee62010-03-16 21:09:09 +00002275 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2276 {
2277 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2278 return( ret );
2279 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002280
Paul Bakker5121ce52009-01-03 21:22:43 +00002281 ssl->state++;
2282
2283 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2284
2285 return( 0 );
2286}
2287
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002288#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2289 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2290 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002291static int ssl_parse_certificate_verify( ssl_context *ssl )
2292{
Paul Bakkered27a042013-04-18 22:46:23 +02002293 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002294 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002295
2296 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2297
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002298 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2299 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002300 {
2301 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2302 ssl->state++;
2303 return( 0 );
2304 }
2305
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002306 return( ret );
2307}
2308#else
2309static int ssl_parse_certificate_verify( ssl_context *ssl )
2310{
2311 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2312 size_t n = 0, n1, n2;
2313 unsigned char hash[48];
2314 md_type_t md_alg = POLARSSL_MD_NONE;
2315 unsigned int hashlen = 0;
2316 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2317
2318 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2319
2320 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2321 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2322 {
2323 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2324 ssl->state++;
2325 return( 0 );
2326 }
2327
Paul Bakkered27a042013-04-18 22:46:23 +02002328 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002329 {
2330 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2331 ssl->state++;
2332 return( 0 );
2333 }
2334
Paul Bakker48916f92012-09-16 19:57:18 +00002335 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002336
2337 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2338 {
2339 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2340 return( ret );
2341 }
2342
2343 ssl->state++;
2344
2345 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2346 {
2347 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002348 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002349 }
2350
2351 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
2352 {
2353 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002354 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002355 }
2356
Paul Bakker926af752012-11-23 13:38:07 +01002357 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2358 {
2359 /*
2360 * As server we know we either have SSL_HASH_SHA384 or
2361 * SSL_HASH_SHA256
2362 */
2363 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg ||
2364 ssl->in_msg[5] != SSL_SIG_RSA )
2365 {
2366 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg for verify message" ) );
2367 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2368 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002369
Paul Bakker926af752012-11-23 13:38:07 +01002370 if( ssl->handshake->verify_sig_alg == SSL_HASH_SHA384 )
Paul Bakkerc70b9822013-04-07 22:00:46 +02002371 md_alg = POLARSSL_MD_SHA384;
Paul Bakker926af752012-11-23 13:38:07 +01002372 else
Paul Bakkerc70b9822013-04-07 22:00:46 +02002373 md_alg = POLARSSL_MD_SHA256;
Paul Bakker926af752012-11-23 13:38:07 +01002374
2375 n += 2;
2376 }
2377 else
2378 {
2379 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002380 md_alg = POLARSSL_MD_NONE;
Paul Bakker926af752012-11-23 13:38:07 +01002381 }
2382
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002383 /* EC NOT IMPLEMENTED YET */
2384 if( ssl->session_negotiate->peer_cert->pk.type != POLARSSL_PK_RSA )
2385 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2386
2387 n1 = pk_rsa( ssl->session_negotiate->peer_cert->pk )->len;
Paul Bakker78ce5072012-11-23 14:23:53 +01002388 n2 = ( ssl->in_msg[4 + n] << 8 ) | ssl->in_msg[5 + n];
Paul Bakker926af752012-11-23 13:38:07 +01002389
2390 if( n + n1 + 6 != ssl->in_hslen || n1 != n2 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002391 {
2392 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002393 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002394 }
2395
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002396 ret = rsa_pkcs1_verify( pk_rsa( ssl->session_negotiate->peer_cert->pk ),
2397 RSA_PUBLIC, md_alg, hashlen, hash,
2398 ssl->in_msg + 6 + n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002399 if( ret != 0 )
2400 {
2401 SSL_DEBUG_RET( 1, "rsa_pkcs1_verify", ret );
2402 return( ret );
2403 }
2404
2405 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
2406
Paul Bakkered27a042013-04-18 22:46:23 +02002407 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002408}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002409#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2410 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2411 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002412
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002413static int ssl_write_new_session_ticket( ssl_context *ssl )
2414{
2415 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002416 size_t tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002417
2418 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
2419
2420 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2421 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
2422
2423 /*
2424 * struct {
2425 * uint32 ticket_lifetime_hint;
2426 * opaque ticket<0..2^16-1>;
2427 * } NewSessionTicket;
2428 *
2429 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
2430 * 8 . 9 ticket_len (n)
2431 * 10 . 9+n ticket content
2432 */
2433 ssl->out_msg[4] = 0x00;
2434 ssl->out_msg[5] = 0x00;
2435 ssl->out_msg[6] = 0x00;
2436 ssl->out_msg[7] = 0x00;
2437
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +02002438 ssl_write_ticket( ssl, &tlen );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002439
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002440 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
2441 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002442
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002443 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002444
2445 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2446 {
2447 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2448 return( ret );
2449 }
2450
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002451 /* No need to remember writing a NewSessionTicket any more */
2452 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002453
2454 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
2455
2456 return( 0 );
2457}
2458
Paul Bakker5121ce52009-01-03 21:22:43 +00002459/*
Paul Bakker1961b702013-01-25 14:49:24 +01002460 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 */
Paul Bakker1961b702013-01-25 14:49:24 +01002462int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002463{
2464 int ret = 0;
2465
Paul Bakker1961b702013-01-25 14:49:24 +01002466 if( ssl->state == SSL_HANDSHAKE_OVER )
2467 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002468
Paul Bakker1961b702013-01-25 14:49:24 +01002469 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
2470
2471 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2472 return( ret );
2473
2474 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002475 {
Paul Bakker1961b702013-01-25 14:49:24 +01002476 case SSL_HELLO_REQUEST:
2477 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002478 break;
2479
Paul Bakker1961b702013-01-25 14:49:24 +01002480 /*
2481 * <== ClientHello
2482 */
2483 case SSL_CLIENT_HELLO:
2484 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002485 break;
Paul Bakker1961b702013-01-25 14:49:24 +01002486
2487 /*
2488 * ==> ServerHello
2489 * Certificate
2490 * ( ServerKeyExchange )
2491 * ( CertificateRequest )
2492 * ServerHelloDone
2493 */
2494 case SSL_SERVER_HELLO:
2495 ret = ssl_write_server_hello( ssl );
2496 break;
2497
2498 case SSL_SERVER_CERTIFICATE:
2499 ret = ssl_write_certificate( ssl );
2500 break;
2501
2502 case SSL_SERVER_KEY_EXCHANGE:
2503 ret = ssl_write_server_key_exchange( ssl );
2504 break;
2505
2506 case SSL_CERTIFICATE_REQUEST:
2507 ret = ssl_write_certificate_request( ssl );
2508 break;
2509
2510 case SSL_SERVER_HELLO_DONE:
2511 ret = ssl_write_server_hello_done( ssl );
2512 break;
2513
2514 /*
2515 * <== ( Certificate/Alert )
2516 * ClientKeyExchange
2517 * ( CertificateVerify )
2518 * ChangeCipherSpec
2519 * Finished
2520 */
2521 case SSL_CLIENT_CERTIFICATE:
2522 ret = ssl_parse_certificate( ssl );
2523 break;
2524
2525 case SSL_CLIENT_KEY_EXCHANGE:
2526 ret = ssl_parse_client_key_exchange( ssl );
2527 break;
2528
2529 case SSL_CERTIFICATE_VERIFY:
2530 ret = ssl_parse_certificate_verify( ssl );
2531 break;
2532
2533 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2534 ret = ssl_parse_change_cipher_spec( ssl );
2535 break;
2536
2537 case SSL_CLIENT_FINISHED:
2538 ret = ssl_parse_finished( ssl );
2539 break;
2540
2541 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002542 * ==> ( NewSessionTicket )
2543 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002544 * Finished
2545 */
2546 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002547 if( ssl->handshake->new_session_ticket != 0 )
2548 ret = ssl_write_new_session_ticket( ssl );
2549 else
2550 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002551 break;
2552
2553 case SSL_SERVER_FINISHED:
2554 ret = ssl_write_finished( ssl );
2555 break;
2556
2557 case SSL_FLUSH_BUFFERS:
2558 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2559 ssl->state = SSL_HANDSHAKE_WRAPUP;
2560 break;
2561
2562 case SSL_HANDSHAKE_WRAPUP:
2563 ssl_handshake_wrapup( ssl );
2564 break;
2565
2566 default:
2567 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2568 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002569 }
2570
Paul Bakker5121ce52009-01-03 21:22:43 +00002571 return( ret );
2572}
Paul Bakker5121ce52009-01-03 21:22:43 +00002573#endif