blob: ad9142c9559401ccb3bd337c929090a15a632d0f [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é-Gonnard306827e2013-08-02 18:05:14 +0200537 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200538 ssl->handshake->new_session_ticket = 1;
539
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200540 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
541
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200542 if( len == 0 )
543 return( 0 );
544
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200545 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
546 {
547 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
548 return( 0 );
549 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200550
551 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200552 * Failures are ok: just ignore the ticket and proceed.
553 */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200554 if( ssl_parse_ticket( ssl, buf, len ) != 0 )
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200555 return( 0 );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200556
557 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
558
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200559 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200560
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200561 /* Don't send a new ticket after all, this one is OK */
562 ssl->handshake->new_session_ticket = 0;
563
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200564 return( 0 );
565}
566
Paul Bakker78a8c712013-03-06 17:01:52 +0100567#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
568static int ssl_parse_client_hello_v2( ssl_context *ssl )
569{
570 int ret;
571 unsigned int i, j;
572 size_t n;
573 unsigned int ciph_len, sess_len, chal_len;
574 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200575 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200576 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100577
578 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
579
580 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
581 {
582 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
583
584 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
585 return( ret );
586
587 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
588 }
589
590 buf = ssl->in_hdr;
591
592 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
593
594 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
595 buf[2] ) );
596 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
597 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
598 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
599 buf[3], buf[4] ) );
600
601 /*
602 * SSLv2 Client Hello
603 *
604 * Record layer:
605 * 0 . 1 message length
606 *
607 * SSL layer:
608 * 2 . 2 message type
609 * 3 . 4 protocol version
610 */
611 if( buf[2] != SSL_HS_CLIENT_HELLO ||
612 buf[3] != SSL_MAJOR_VERSION_3 )
613 {
614 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
615 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
616 }
617
618 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
619
620 if( n < 17 || n > 512 )
621 {
622 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
623 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
624 }
625
626 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200627 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
628 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100629
630 if( ssl->minor_ver < ssl->min_minor_ver )
631 {
632 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
633 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
634 ssl->min_major_ver, ssl->min_minor_ver ) );
635
636 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
637 SSL_ALERT_MSG_PROTOCOL_VERSION );
638 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
639 }
640
Paul Bakker2fbefde2013-06-29 16:01:15 +0200641 ssl->handshake->max_major_ver = buf[3];
642 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100643
644 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
645 {
646 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
647 return( ret );
648 }
649
650 ssl->handshake->update_checksum( ssl, buf + 2, n );
651
652 buf = ssl->in_msg;
653 n = ssl->in_left - 5;
654
655 /*
656 * 0 . 1 ciphersuitelist length
657 * 2 . 3 session id length
658 * 4 . 5 challenge length
659 * 6 . .. ciphersuitelist
660 * .. . .. session id
661 * .. . .. challenge
662 */
663 SSL_DEBUG_BUF( 4, "record contents", buf, n );
664
665 ciph_len = ( buf[0] << 8 ) | buf[1];
666 sess_len = ( buf[2] << 8 ) | buf[3];
667 chal_len = ( buf[4] << 8 ) | buf[5];
668
669 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
670 ciph_len, sess_len, chal_len ) );
671
672 /*
673 * Make sure each parameter length is valid
674 */
675 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
676 {
677 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
678 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
679 }
680
681 if( sess_len > 32 )
682 {
683 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
684 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
685 }
686
687 if( chal_len < 8 || chal_len > 32 )
688 {
689 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
690 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
691 }
692
693 if( n != 6 + ciph_len + sess_len + chal_len )
694 {
695 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
696 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
697 }
698
699 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
700 buf + 6, ciph_len );
701 SSL_DEBUG_BUF( 3, "client hello, session id",
702 buf + 6 + ciph_len, sess_len );
703 SSL_DEBUG_BUF( 3, "client hello, challenge",
704 buf + 6 + ciph_len + sess_len, chal_len );
705
706 p = buf + 6 + ciph_len;
707 ssl->session_negotiate->length = sess_len;
708 memset( ssl->session_negotiate->id, 0, sizeof( ssl->session_negotiate->id ) );
709 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
710
711 p += sess_len;
712 memset( ssl->handshake->randbytes, 0, 64 );
713 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
714
715 /*
716 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
717 */
718 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
719 {
720 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
721 {
722 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
723 if( ssl->renegotiation == SSL_RENEGOTIATION )
724 {
725 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
726
727 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
728 return( ret );
729
730 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
731 }
732 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
733 break;
734 }
735 }
736
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200737 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
738 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +0100739 {
740 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
741 {
Paul Bakker41c83d32013-03-20 14:39:14 +0100742 // Only allow non-ECC ciphersuites as we do not have extensions
743 //
Paul Bakker59c28a22013-06-29 15:33:42 +0200744 if( p[0] == 0 && p[1] == 0 &&
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200745 ( ( ciphersuites[i] >> 8 ) & 0xFF ) == 0 &&
746 p[2] == ( ciphersuites[i] & 0xFF ) )
Paul Bakker59c28a22013-06-29 15:33:42 +0200747 {
748 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
749
750 if( ciphersuite_info == NULL )
751 {
752 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %02x not found",
753 ciphersuites[i] ) );
754 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
755 }
756
Paul Bakker2fbefde2013-06-29 16:01:15 +0200757 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
758 ciphersuite_info->max_minor_ver < ssl->minor_ver )
759 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +0200760
Paul Bakker78a8c712013-03-06 17:01:52 +0100761 goto have_ciphersuite_v2;
Paul Bakker59c28a22013-06-29 15:33:42 +0200762 }
Paul Bakker78a8c712013-03-06 17:01:52 +0100763 }
764 }
765
766 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
767
768 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
769
770have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200771 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +0200772 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +0100773 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +0100774
775 /*
776 * SSLv2 Client Hello relevant renegotiation security checks
777 */
778 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
779 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
780 {
781 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
782
783 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
784 return( ret );
785
786 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
787 }
788
789 ssl->in_left = 0;
790 ssl->state++;
791
792 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
793
794 return( 0 );
795}
796#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
797
Paul Bakker5121ce52009-01-03 21:22:43 +0000798static int ssl_parse_client_hello( ssl_context *ssl )
799{
Paul Bakker23986e52011-04-24 08:57:21 +0000800 int ret;
801 unsigned int i, j;
802 size_t n;
803 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +0000804 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +0000805 unsigned int ext_len = 0;
806 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000807 int renegotiation_info_seen = 0;
808 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200809 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +0100810 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000811
812 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
813
Paul Bakker48916f92012-09-16 19:57:18 +0000814 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
815 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000816 {
817 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
818 return( ret );
819 }
820
821 buf = ssl->in_hdr;
822
Paul Bakker78a8c712013-03-06 17:01:52 +0100823#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
824 if( ( buf[0] & 0x80 ) != 0 )
825 return ssl_parse_client_hello_v2( ssl );
826#endif
827
Paul Bakkerec636f32012-09-09 19:17:02 +0000828 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
829
830 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
831 buf[0] ) );
832 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
833 ( buf[3] << 8 ) | buf[4] ) );
834 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
835 buf[1], buf[2] ) );
836
837 /*
838 * SSLv3 Client Hello
839 *
840 * Record layer:
841 * 0 . 0 message type
842 * 1 . 2 protocol version
843 * 3 . 4 message length
844 */
845 if( buf[0] != SSL_MSG_HANDSHAKE ||
846 buf[1] != SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000847 {
Paul Bakkerec636f32012-09-09 19:17:02 +0000848 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
849 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
850 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000851
Paul Bakkerec636f32012-09-09 19:17:02 +0000852 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000853
Manuel Pégourié-Gonnard72882b22013-08-02 13:36:00 +0200854 if( n < 45 || n > 2048 )
Paul Bakkerec636f32012-09-09 19:17:02 +0000855 {
856 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
857 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
858 }
859
Paul Bakker48916f92012-09-16 19:57:18 +0000860 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
861 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +0000862 {
863 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
864 return( ret );
865 }
866
867 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +0000868 if( !ssl->renegotiation )
869 n = ssl->in_left - 5;
870 else
871 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +0000872
Paul Bakker48916f92012-09-16 19:57:18 +0000873 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +0000874
875 /*
876 * SSL layer:
877 * 0 . 0 handshake type
878 * 1 . 3 handshake length
879 * 4 . 5 protocol version
880 * 6 . 9 UNIX time()
881 * 10 . 37 random bytes
882 * 38 . 38 session id length
883 * 39 . 38+x session id
884 * 39+x . 40+x ciphersuitelist length
885 * 41+x . .. ciphersuitelist
886 * .. . .. compression alg.
887 * .. . .. extensions
888 */
889 SSL_DEBUG_BUF( 4, "record contents", buf, n );
890
891 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
892 buf[0] ) );
893 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
894 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
895 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
896 buf[4], buf[5] ) );
897
898 /*
899 * Check the handshake type and protocol version
900 */
901 if( buf[0] != SSL_HS_CLIENT_HELLO ||
902 buf[4] != SSL_MAJOR_VERSION_3 )
903 {
904 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
905 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
906 }
907
908 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200909 ssl->minor_ver = ( buf[5] <= ssl->max_minor_ver )
910 ? buf[5] : ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +0000911
Paul Bakker1d29fb52012-09-28 13:28:45 +0000912 if( ssl->minor_ver < ssl->min_minor_ver )
913 {
914 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
915 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +0000916 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +0000917
918 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
919 SSL_ALERT_MSG_PROTOCOL_VERSION );
920
921 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
922 }
923
Paul Bakker2fbefde2013-06-29 16:01:15 +0200924 ssl->handshake->max_major_ver = buf[4];
925 ssl->handshake->max_minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +0000926
Paul Bakker48916f92012-09-16 19:57:18 +0000927 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +0000928
929 /*
930 * Check the handshake message length
931 */
932 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
933 {
934 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
935 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
936 }
937
938 /*
939 * Check the session length
940 */
941 sess_len = buf[38];
942
943 if( sess_len > 32 )
944 {
945 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
946 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
947 }
948
Paul Bakker48916f92012-09-16 19:57:18 +0000949 ssl->session_negotiate->length = sess_len;
950 memset( ssl->session_negotiate->id, 0,
951 sizeof( ssl->session_negotiate->id ) );
952 memcpy( ssl->session_negotiate->id, buf + 39,
953 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +0000954
955 /*
956 * Check the ciphersuitelist length
957 */
958 ciph_len = ( buf[39 + sess_len] << 8 )
959 | ( buf[40 + sess_len] );
960
961 if( ciph_len < 2 || ciph_len > 256 || ( ciph_len % 2 ) != 0 )
962 {
963 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
964 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
965 }
966
967 /*
968 * Check the compression algorithms length
969 */
970 comp_len = buf[41 + sess_len + ciph_len];
971
972 if( comp_len < 1 || comp_len > 16 )
973 {
974 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
975 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
976 }
977
Paul Bakker48916f92012-09-16 19:57:18 +0000978 /*
979 * Check the extension length
980 */
981 if( n > 42 + sess_len + ciph_len + comp_len )
982 {
983 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
984 | ( buf[43 + sess_len + ciph_len + comp_len] );
985
986 if( ( ext_len > 0 && ext_len < 4 ) ||
987 n != 44 + sess_len + ciph_len + comp_len + ext_len )
988 {
989 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
990 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
991 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
992 }
993 }
994
995 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +0000996#if defined(POLARSSL_ZLIB_SUPPORT)
997 for( i = 0; i < comp_len; ++i )
998 {
Paul Bakker48916f92012-09-16 19:57:18 +0000999 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001000 {
Paul Bakker48916f92012-09-16 19:57:18 +00001001 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001002 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001003 }
1004 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001005#endif
1006
Paul Bakkerec636f32012-09-09 19:17:02 +00001007 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1008 buf + 6, 32 );
1009 SSL_DEBUG_BUF( 3, "client hello, session id",
1010 buf + 38, sess_len );
1011 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1012 buf + 41 + sess_len, ciph_len );
1013 SSL_DEBUG_BUF( 3, "client hello, compression",
1014 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001015
Paul Bakkerec636f32012-09-09 19:17:02 +00001016 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001017 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1018 */
1019 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1020 {
1021 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1022 {
1023 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1024 if( ssl->renegotiation == SSL_RENEGOTIATION )
1025 {
1026 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001027
1028 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1029 return( ret );
1030
Paul Bakker48916f92012-09-16 19:57:18 +00001031 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1032 }
1033 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1034 break;
1035 }
1036 }
1037
Paul Bakker48916f92012-09-16 19:57:18 +00001038 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001039
1040 while( ext_len )
1041 {
1042 unsigned int ext_id = ( ( ext[0] << 8 )
1043 | ( ext[1] ) );
1044 unsigned int ext_size = ( ( ext[2] << 8 )
1045 | ( ext[3] ) );
1046
1047 if( ext_size + 4 > ext_len )
1048 {
1049 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1050 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1051 }
1052 switch( ext_id )
1053 {
Paul Bakker5701cdc2012-09-27 21:49:42 +00001054 case TLS_EXT_SERVERNAME:
1055 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1056 if( ssl->f_sni == NULL )
1057 break;
1058
1059 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1060 if( ret != 0 )
1061 return( ret );
1062 break;
1063
Paul Bakker48916f92012-09-16 19:57:18 +00001064 case TLS_EXT_RENEGOTIATION_INFO:
1065 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1066 renegotiation_info_seen = 1;
1067
Paul Bakker23f36802012-09-28 14:15:14 +00001068 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1069 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001070 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001071 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001072
Paul Bakker23f36802012-09-28 14:15:14 +00001073 case TLS_EXT_SIG_ALG:
1074 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1075 if( ssl->renegotiation == SSL_RENEGOTIATION )
1076 break;
1077
1078 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1079 if( ret != 0 )
1080 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001081 break;
1082
Paul Bakker41c83d32013-03-20 14:39:14 +01001083#if defined(POLARSSL_ECP_C)
1084 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1085 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1086
1087 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1088 if( ret != 0 )
1089 return( ret );
1090 break;
1091
1092 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1093 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1094
1095 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1096 if( ret != 0 )
1097 return( ret );
1098 break;
1099#endif /* POLARSSL_ECP_C */
1100
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001101 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1102 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1103
1104 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1105 if( ret != 0 )
1106 return( ret );
1107 break;
1108
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001109 case TLS_EXT_TRUNCATED_HMAC:
1110 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1111
1112 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1113 if( ret != 0 )
1114 return( ret );
1115 break;
1116
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001117 case TLS_EXT_SESSION_TICKET:
1118 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1119
1120 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1121 if( ret != 0 )
1122 return( ret );
1123 break;
1124
Paul Bakker48916f92012-09-16 19:57:18 +00001125 default:
1126 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1127 ext_id ) );
1128 }
1129
1130 ext_len -= 4 + ext_size;
1131 ext += 4 + ext_size;
1132
1133 if( ext_len > 0 && ext_len < 4 )
1134 {
1135 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1136 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1137 }
1138 }
1139
1140 /*
1141 * Renegotiation security checks
1142 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001143 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1144 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1145 {
1146 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1147 handshake_failure = 1;
1148 }
1149 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1150 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1151 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001152 {
1153 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001154 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001155 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001156 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1157 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1158 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001159 {
1160 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001161 handshake_failure = 1;
1162 }
1163 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1164 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1165 renegotiation_info_seen == 1 )
1166 {
1167 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1168 handshake_failure = 1;
1169 }
1170
1171 if( handshake_failure == 1 )
1172 {
1173 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1174 return( ret );
1175
Paul Bakker48916f92012-09-16 19:57:18 +00001176 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1177 }
Paul Bakker380da532012-04-18 16:10:25 +00001178
Paul Bakker41c83d32013-03-20 14:39:14 +01001179 /*
1180 * Search for a matching ciphersuite
1181 * (At the end because we need information from the EC-based extensions)
1182 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001183 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
1184 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001185 {
1186 for( j = 0, p = buf + 41 + sess_len; j < ciph_len;
1187 j += 2, p += 2 )
1188 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001189 if( p[0] == ( ( ciphersuites[i] >> 8 ) & 0xFF ) &&
1190 p[1] == ( ( ciphersuites[i] ) & 0xFF ) )
Paul Bakker41c83d32013-03-20 14:39:14 +01001191 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001192 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
Paul Bakker41c83d32013-03-20 14:39:14 +01001193
1194 if( ciphersuite_info == NULL )
1195 {
1196 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %02x not found",
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001197 ciphersuites[i] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001198 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1199 }
1200
Paul Bakker2fbefde2013-06-29 16:01:15 +02001201 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
1202 ciphersuite_info->max_minor_ver < ssl->minor_ver )
1203 continue;
1204
Paul Bakker41c83d32013-03-20 14:39:14 +01001205 if( ( ciphersuite_info->flags & POLARSSL_CIPHERSUITE_EC ) &&
1206 ssl->handshake->ec_curve == 0 )
1207 continue;
1208
1209 goto have_ciphersuite;
1210 }
1211 }
1212 }
1213
1214 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1215
1216 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1217 return( ret );
1218
1219 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1220
1221have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001222 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001223 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1224 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1225
Paul Bakker5121ce52009-01-03 21:22:43 +00001226 ssl->in_left = 0;
1227 ssl->state++;
1228
1229 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1230
1231 return( 0 );
1232}
1233
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001234static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1235 unsigned char *buf,
1236 size_t *olen )
1237{
1238 unsigned char *p = buf;
1239
1240 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1241 {
1242 *olen = 0;
1243 return;
1244 }
1245
1246 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1247
1248 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1249 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1250
1251 *p++ = 0x00;
1252 *p++ = 0x00;
1253
1254 *olen = 4;
1255}
1256
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001257static void ssl_write_session_ticket_ext( ssl_context *ssl,
1258 unsigned char *buf,
1259 size_t *olen )
1260{
1261 unsigned char *p = buf;
1262
1263 if( ssl->handshake->new_session_ticket == 0 )
1264 {
1265 *olen = 0;
1266 return;
1267 }
1268
1269 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1270
1271 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1272 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1273
1274 *p++ = 0x00;
1275 *p++ = 0x00;
1276
1277 *olen = 4;
1278}
1279
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001280static void ssl_write_renegotiation_ext( ssl_context *ssl,
1281 unsigned char *buf,
1282 size_t *olen )
1283{
1284 unsigned char *p = buf;
1285
1286 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1287 {
1288 *olen = 0;
1289 return;
1290 }
1291
1292 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1293
1294 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1295 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1296
1297 *p++ = 0x00;
1298 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1299 *p++ = ssl->verify_data_len * 2 & 0xFF;
1300
1301 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1302 p += ssl->verify_data_len;
1303 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1304 p += ssl->verify_data_len;
1305
1306 *olen = 5 + ssl->verify_data_len * 2;
1307}
1308
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001309static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1310 unsigned char *buf,
1311 size_t *olen )
1312{
1313 unsigned char *p = buf;
1314
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001315 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1316 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001317 *olen = 0;
1318 return;
1319 }
1320
1321 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1322
1323 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1324 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1325
1326 *p++ = 0x00;
1327 *p++ = 1;
1328
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001329 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001330
1331 *olen = 5;
1332}
1333
Paul Bakker5121ce52009-01-03 21:22:43 +00001334static int ssl_write_server_hello( ssl_context *ssl )
1335{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001336#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001337 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001338#endif
Paul Bakkera3d195c2011-11-27 21:07:34 +00001339 int ret, n;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001340 size_t olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001341 unsigned char *buf, *p;
1342
1343 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1344
1345 /*
1346 * 0 . 0 handshake type
1347 * 1 . 3 handshake length
1348 * 4 . 5 protocol version
1349 * 6 . 9 UNIX time()
1350 * 10 . 37 random bytes
1351 */
1352 buf = ssl->out_msg;
1353 p = buf + 4;
1354
1355 *p++ = (unsigned char) ssl->major_ver;
1356 *p++ = (unsigned char) ssl->minor_ver;
1357
1358 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1359 buf[4], buf[5] ) );
1360
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001361#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001362 t = time( NULL );
1363 *p++ = (unsigned char)( t >> 24 );
1364 *p++ = (unsigned char)( t >> 16 );
1365 *p++ = (unsigned char)( t >> 8 );
1366 *p++ = (unsigned char)( t );
1367
1368 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001369#else
1370 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1371 return( ret );
1372
1373 p += 4;
1374#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001375
Paul Bakkera3d195c2011-11-27 21:07:34 +00001376 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1377 return( ret );
1378
1379 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001380
Paul Bakker48916f92012-09-16 19:57:18 +00001381 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001382
1383 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1384
1385 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001386 * Resume is 0 by default, see ssl_handshake_init().
1387 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1388 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001389 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001390 if( ssl->handshake->resume == 0 &&
1391 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001392 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001393 ssl->f_get_cache != NULL &&
1394 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1395 {
1396 ssl->handshake->resume = 1;
1397 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001398
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001399 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001400 {
1401 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001402 * New session, create a new session id,
1403 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001404 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001405 ssl->state++;
1406
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001407 if( ssl->handshake->new_session_ticket == 0 )
1408 {
1409 ssl->session_negotiate->length = n = 32;
1410 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
1411 n ) ) != 0 )
1412 return( ret );
1413 }
1414 else
1415 {
1416 ssl->session_negotiate->length = 0;
1417 memset( ssl->session_negotiate->id, 0, 32 );
1418 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001419 }
1420 else
1421 {
1422 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001423 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001424 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001425 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001426
1427 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1428 {
1429 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1430 return( ret );
1431 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001432 }
1433
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001434 /*
1435 * 38 . 38 session id length
1436 * 39 . 38+n session id
1437 * 39+n . 40+n chosen ciphersuite
1438 * 41+n . 41+n chosen compression alg.
1439 * 42+n . 43+n extensions length
1440 * 44+n . 43+n+m extensions
1441 */
1442 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001443 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1444 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001445
1446 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1447 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1448 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001449 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001450
Paul Bakker48916f92012-09-16 19:57:18 +00001451 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1452 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1453 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001454
Paul Bakkere3166ce2011-01-27 17:40:50 +00001455 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001456 ssl->session_negotiate->ciphersuite ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001457 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001458 ssl->session_negotiate->compression ) );
1459
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001460 /*
1461 * First write extensions, then the total length
1462 */
1463 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1464 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001465
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001466 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1467 ext_len += olen;
1468
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001469 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1470 ext_len += olen;
1471
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001472 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1473 ext_len += olen;
1474
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001475 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001476
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001477 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1478 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1479 p += ext_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001480
1481 ssl->out_msglen = p - buf;
1482 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1483 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1484
1485 ret = ssl_write_record( ssl );
1486
1487 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1488
1489 return( ret );
1490}
1491
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001492#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1493 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
1494 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001495static int ssl_write_certificate_request( ssl_context *ssl )
1496{
Paul Bakkered27a042013-04-18 22:46:23 +02001497 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1498 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001499
1500 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1501
1502 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1503 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1504 {
1505 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1506 ssl->state++;
1507 return( 0 );
1508 }
1509
1510 return( ret );
1511}
1512#else
1513static int ssl_write_certificate_request( ssl_context *ssl )
1514{
1515 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1516 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker926af752012-11-23 13:38:07 +01001517 size_t n = 0, dn_size, total_dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001518 unsigned char *buf, *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001519 const x509_cert *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001520
1521 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1522
1523 ssl->state++;
1524
Paul Bakkerfbb17802013-04-17 19:10:21 +02001525 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001526 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001527 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001528 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001529 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001530 return( 0 );
1531 }
1532
1533 /*
1534 * 0 . 0 handshake type
1535 * 1 . 3 handshake length
1536 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01001537 * 5 .. m-1 cert types
1538 * m .. m+1 sig alg length (TLS 1.2 only)
1539 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001540 * n .. n+1 length of all DNs
1541 * n+2 .. n+3 length of DN 1
1542 * n+4 .. ... Distinguished Name #1
1543 * ... .. ... length of DN 2, etc.
1544 */
1545 buf = ssl->out_msg;
1546 p = buf + 4;
1547
1548 /*
1549 * At the moment, only RSA certificates are supported
1550 */
1551 *p++ = 1;
Paul Bakker926af752012-11-23 13:38:07 +01001552 *p++ = SSL_CERT_TYPE_RSA_SIGN;
1553
1554 /*
1555 * Add signature_algorithms for verify (TLS 1.2)
1556 * Only add current running algorithm that is already required for
1557 * requested ciphersuite.
1558 *
1559 * Length is always 2
1560 */
Paul Bakker21dca692013-01-03 11:41:08 +01001561 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01001562 {
1563 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
1564
1565 *p++ = 0;
1566 *p++ = 2;
1567
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001568 if( ssl->transform_negotiate->ciphersuite_info->mac ==
1569 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01001570 {
1571 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
1572 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02001573
Paul Bakker926af752012-11-23 13:38:07 +01001574 *p++ = ssl->handshake->verify_sig_alg;
1575 *p++ = SSL_SIG_RSA;
1576
1577 n += 4;
1578 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001579
1580 p += 2;
1581 crt = ssl->ca_chain;
1582
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001583 total_dn_size = 0;
Paul Bakker29087132010-03-21 21:03:34 +00001584 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001585 {
1586 if( p - buf > 4096 )
1587 break;
1588
Paul Bakker926af752012-11-23 13:38:07 +01001589 dn_size = crt->subject_raw.len;
1590 *p++ = (unsigned char)( dn_size >> 8 );
1591 *p++ = (unsigned char)( dn_size );
1592 memcpy( p, crt->subject_raw.p, dn_size );
1593 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001594
Paul Bakker926af752012-11-23 13:38:07 +01001595 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
1596
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001597 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01001598 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00001599 }
1600
Paul Bakker926af752012-11-23 13:38:07 +01001601 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001602 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1603 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Paul Bakker926af752012-11-23 13:38:07 +01001604 ssl->out_msg[6 + n] = (unsigned char)( total_dn_size >> 8 );
1605 ssl->out_msg[7 + n] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001606
1607 ret = ssl_write_record( ssl );
1608
1609 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
1610
1611 return( ret );
1612}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001613#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
1614 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
1615 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001616
Paul Bakker41c83d32013-03-20 14:39:14 +01001617static int ssl_write_server_key_exchange( ssl_context *ssl )
1618{
Paul Bakker23986e52011-04-24 08:57:21 +00001619 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001620 size_t n = 0, len;
Paul Bakker23f36802012-09-28 14:15:14 +00001621 unsigned char hash[64];
Paul Bakkerc70b9822013-04-07 22:00:46 +02001622 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker35a7fe52012-10-31 09:07:14 +00001623 unsigned int hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001624 unsigned char *p = ssl->out_msg + 4;
1625 unsigned char *dig_sig = p;
1626 size_t dig_sig_len = 0;
Paul Bakker41c83d32013-03-20 14:39:14 +01001627
1628 const ssl_ciphersuite_t *ciphersuite_info;
1629 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001630
1631 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
1632
Paul Bakker41c83d32013-03-20 14:39:14 +01001633 if( ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_RSA &&
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001634 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_ECDHE_RSA &&
1635 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001636 {
1637 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
1638 ssl->state++;
1639 return( 0 );
1640 }
1641
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001642#if defined(POLARSSL_RSA_C)
Paul Bakker43b7e352011-01-18 15:27:19 +00001643 if( ssl->rsa_key == NULL )
1644 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00001645 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
1646 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker43b7e352011-01-18 15:27:19 +00001647 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001648#endif /* POLARSSL_RSA_C */
Paul Bakker43b7e352011-01-18 15:27:19 +00001649
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001650#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1651 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1652 {
1653 /* TODO: Support identity hints */
1654 *(p++) = 0x00;
1655 *(p++) = 0x00;
1656
1657 n += 2;
1658 }
1659#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1660
1661#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1662 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1663 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1664 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00001665 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001666 /*
1667 * Ephemeral DH parameters:
1668 *
1669 * struct {
1670 * opaque dh_p<1..2^16-1>;
1671 * opaque dh_g<1..2^16-1>;
1672 * opaque dh_Ys<1..2^16-1>;
1673 * } ServerDHParams;
1674 */
1675 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
1676 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
1677 {
1678 SSL_DEBUG_RET( 1, "mpi_copy", ret );
1679 return( ret );
1680 }
Paul Bakker48916f92012-09-16 19:57:18 +00001681
Paul Bakker41c83d32013-03-20 14:39:14 +01001682 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
1683 mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001684 p,
1685 &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001686 {
1687 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
1688 return( ret );
1689 }
1690
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001691 dig_sig = p;
1692 dig_sig_len = len;
1693
1694 p += len;
1695 n += len;
1696
Paul Bakker41c83d32013-03-20 14:39:14 +01001697 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
1698 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1699 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1700 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
1701 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001702#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1703 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001704
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001705#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01001706 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001707 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001708 /*
1709 * Ephemeral ECDH parameters:
1710 *
1711 * struct {
1712 * ECParameters curve_params;
1713 * ECPoint public;
1714 * } ServerECDHParams;
1715 */
Paul Bakker41c83d32013-03-20 14:39:14 +01001716 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
1717 ssl->handshake->ec_curve ) ) != 0 )
1718 {
1719 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
1720 return( ret );
1721 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001722
Paul Bakker41c83d32013-03-20 14:39:14 +01001723 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001724 &len,
1725 p,
Paul Bakker41c83d32013-03-20 14:39:14 +01001726 1000, ssl->f_rng, ssl->p_rng ) ) != 0 )
1727 {
1728 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
1729 return( ret );
1730 }
1731
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001732 dig_sig = p;
1733 dig_sig_len = len;
1734
1735 p += len;
1736 n += len;
1737
Paul Bakker41c83d32013-03-20 14:39:14 +01001738 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
1739 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001740#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001741
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001742#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1743 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
1744 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1745 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001746 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001747 size_t rsa_key_len = 0;
Paul Bakker23f36802012-09-28 14:15:14 +00001748
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001749 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker23f36802012-09-28 14:15:14 +00001750 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001751 md5_context md5;
1752 sha1_context sha1;
1753
1754 /*
1755 * digitally-signed struct {
1756 * opaque md5_hash[16];
1757 * opaque sha_hash[20];
1758 * };
1759 *
1760 * md5_hash
1761 * MD5(ClientHello.random + ServerHello.random
1762 * + ServerParams);
1763 * sha_hash
1764 * SHA(ClientHello.random + ServerHello.random
1765 * + ServerParams);
1766 */
1767 md5_starts( &md5 );
1768 md5_update( &md5, ssl->handshake->randbytes, 64 );
1769 md5_update( &md5, dig_sig, dig_sig_len );
1770 md5_finish( &md5, hash );
1771
1772 sha1_starts( &sha1 );
1773 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
1774 sha1_update( &sha1, dig_sig, dig_sig_len );
1775 sha1_finish( &sha1, hash + 16 );
1776
1777 hashlen = 36;
1778 md_alg = POLARSSL_MD_NONE;
1779 }
1780 else
1781 {
1782 md_context_t ctx;
1783
1784 /*
1785 * digitally-signed struct {
1786 * opaque client_random[32];
1787 * opaque server_random[32];
1788 * ServerDHParams params;
1789 * };
1790 */
1791 switch( ssl->handshake->sig_alg )
1792 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001793#if defined(POLARSSL_MD5_C)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001794 case SSL_HASH_MD5:
1795 md_alg = POLARSSL_MD_MD5;
1796 break;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001797#endif
1798#if defined(POLARSSL_SHA1_C)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001799 case SSL_HASH_SHA1:
1800 md_alg = POLARSSL_MD_SHA1;
1801 break;
Paul Bakker23f36802012-09-28 14:15:14 +00001802#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02001803#if defined(POLARSSL_SHA256_C)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001804 case SSL_HASH_SHA224:
1805 md_alg = POLARSSL_MD_SHA224;
1806 break;
1807 case SSL_HASH_SHA256:
1808 md_alg = POLARSSL_MD_SHA256;
1809 break;
Paul Bakker23f36802012-09-28 14:15:14 +00001810#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02001811#if defined(POLARSSL_SHA512_C)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001812 case SSL_HASH_SHA384:
1813 md_alg = POLARSSL_MD_SHA384;
1814 break;
1815 case SSL_HASH_SHA512:
1816 md_alg = POLARSSL_MD_SHA512;
1817 break;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001818#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001819 default:
1820 /* Should never happen */
1821 return( -1 );
1822 }
1823
1824 if( ( ret = md_init_ctx( &ctx, md_info_from_type( md_alg ) ) ) != 0 )
1825 {
1826 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
1827 return( ret );
1828 }
1829
1830 md_starts( &ctx );
1831 md_update( &ctx, ssl->handshake->randbytes, 64 );
1832 md_update( &ctx, dig_sig, dig_sig_len );
1833 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02001834
1835 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
1836 {
1837 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
1838 return( ret );
1839 }
1840
Paul Bakker23f36802012-09-28 14:15:14 +00001841 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001842
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001843 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
1844
1845 if ( ssl->rsa_key )
1846 rsa_key_len = ssl->rsa_key_len( ssl->rsa_key );
1847
1848 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker23f36802012-09-28 14:15:14 +00001849 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001850 *(p++) = ssl->handshake->sig_alg;
1851 *(p++) = SSL_SIG_RSA;
1852
1853 n += 2;
1854 }
1855
1856 *(p++) = (unsigned char)( rsa_key_len >> 8 );
1857 *(p++) = (unsigned char)( rsa_key_len );
1858 n += 2;
1859
1860 if ( ssl->rsa_key )
1861 {
1862 ret = ssl->rsa_sign( ssl->rsa_key, ssl->f_rng, ssl->p_rng,
1863 RSA_PRIVATE, md_alg, hashlen, hash, p );
1864 }
1865
1866 if( ret != 0 )
1867 {
1868 SSL_DEBUG_RET( 1, "pkcs1_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001869 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001870 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001871
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001872 SSL_DEBUG_BUF( 3, "my RSA sig", p, rsa_key_len );
1873
1874 p += rsa_key_len;
1875 n += rsa_key_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001876 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001877#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
1878 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001879
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001880 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001881 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1882 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
1883
1884 ssl->state++;
1885
1886 if( ( ret = ssl_write_record( ssl ) ) != 0 )
1887 {
1888 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
1889 return( ret );
1890 }
1891
1892 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
1893
1894 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001895}
1896
1897static int ssl_write_server_hello_done( ssl_context *ssl )
1898{
1899 int ret;
1900
1901 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
1902
1903 ssl->out_msglen = 4;
1904 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1905 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
1906
1907 ssl->state++;
1908
1909 if( ( ret = ssl_write_record( ssl ) ) != 0 )
1910 {
1911 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
1912 return( ret );
1913 }
1914
1915 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
1916
1917 return( 0 );
1918}
1919
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001920#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1921 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1922static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
1923 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02001924{
1925 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02001926 size_t n;
1927
1928 /*
1929 * Receive G^Y mod P, premaster = (G^Y)^X mod P
1930 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001931 if( *p + 2 > end )
1932 {
1933 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
1934 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
1935 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02001936
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001937 n = ( (*p)[0] << 8 ) | (*p)[1];
1938 *p += 2;
1939
1940 if( n < 1 || n > ssl->handshake->dhm_ctx.len || *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02001941 {
1942 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
1943 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
1944 }
1945
1946 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001947 *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02001948 {
1949 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
1950 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
1951 }
1952
1953 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
1954
Paul Bakker70df2fb2013-04-17 17:19:09 +02001955 return( ret );
1956}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001957#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1958 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02001959
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001960#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
Paul Bakker70df2fb2013-04-17 17:19:09 +02001961static int ssl_parse_client_ecdh_public( ssl_context *ssl )
1962{
1963 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02001964 size_t n;
1965
1966 /*
1967 * Receive client public key and calculate premaster
1968 */
1969 n = ssl->in_msg[3];
1970
1971 if( n < 1 || n > mpi_size( &ssl->handshake->ecdh_ctx.grp.P ) * 2 + 2 ||
1972 n + 4 != ssl->in_hslen )
1973 {
1974 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
1975 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
1976 }
1977
1978 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
1979 ssl->in_msg + 4, n ) ) != 0 )
1980 {
1981 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
1982 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
1983 }
1984
1985 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
1986
Paul Bakker70df2fb2013-04-17 17:19:09 +02001987 return( ret );
1988}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001989#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02001990
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001991#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakker70df2fb2013-04-17 17:19:09 +02001992static int ssl_parse_encrypted_pms_secret( ssl_context *ssl )
1993{
1994 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1995 size_t i, n = 0;
1996
1997 if( ssl->rsa_key == NULL )
1998 {
1999 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2000 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2001 }
2002
2003 /*
2004 * Decrypt the premaster using own private RSA key
2005 */
2006 i = 4;
2007 if( ssl->rsa_key )
2008 n = ssl->rsa_key_len( ssl->rsa_key );
2009 ssl->handshake->pmslen = 48;
2010
2011 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2012 {
2013 i += 2;
2014 if( ssl->in_msg[4] != ( ( n >> 8 ) & 0xFF ) ||
2015 ssl->in_msg[5] != ( ( n ) & 0xFF ) )
2016 {
2017 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2018 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2019 }
2020 }
2021
2022 if( ssl->in_hslen != i + n )
2023 {
2024 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2025 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2026 }
2027
2028 if( ssl->rsa_key ) {
2029 ret = ssl->rsa_decrypt( ssl->rsa_key, RSA_PRIVATE,
2030 &ssl->handshake->pmslen,
2031 ssl->in_msg + i,
2032 ssl->handshake->premaster,
2033 sizeof(ssl->handshake->premaster) );
2034 }
2035
2036 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Paul Bakker2fbefde2013-06-29 16:01:15 +02002037 ssl->handshake->premaster[0] != ssl->handshake->max_major_ver ||
2038 ssl->handshake->premaster[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002039 {
2040 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2041
2042 /*
2043 * Protection against Bleichenbacher's attack:
2044 * invalid PKCS#1 v1.5 padding must not cause
2045 * the connection to end immediately; instead,
2046 * send a bad_record_mac later in the handshake.
2047 */
2048 ssl->handshake->pmslen = 48;
2049
2050 ret = ssl->f_rng( ssl->p_rng, ssl->handshake->premaster,
2051 ssl->handshake->pmslen );
2052 if( ret != 0 )
2053 return( ret );
2054 }
2055
2056 return( ret );
2057}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002058#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002059
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002060#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2061 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2062static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2063 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002064{
2065 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002066 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002067
2068 if( ssl->psk == NULL || ssl->psk_identity == NULL ||
2069 ssl->psk_identity_len == 0 || ssl->psk_len == 0 )
2070 {
2071 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2072 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2073 }
2074
2075 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002076 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002077 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002078 if( *p + 2 > end )
2079 {
2080 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2081 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2082 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002083
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002084 n = ( (*p)[0] << 8 ) | (*p)[1];
2085 *p += 2;
2086
2087 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002088 {
2089 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2090 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2091 }
2092
2093 if( n != ssl->psk_identity_len ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002094 memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002095 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002096 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002097 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2098 }
2099
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002100 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002101 ret = 0;
2102
Paul Bakkerfbb17802013-04-17 19:10:21 +02002103 return( ret );
2104}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002105#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
2106 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002107
Paul Bakker5121ce52009-01-03 21:22:43 +00002108static int ssl_parse_client_key_exchange( ssl_context *ssl )
2109{
Paul Bakker23986e52011-04-24 08:57:21 +00002110 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002111 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002112 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002113
Paul Bakker41c83d32013-03-20 14:39:14 +01002114 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002115
2116 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2117
2118 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2119 {
2120 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2121 return( ret );
2122 }
2123
2124 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2125 {
2126 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002127 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002128 }
2129
2130 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2131 {
2132 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002133 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002134 }
2135
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002136 p = ssl->in_msg + 4;
2137 end = ssl->in_msg + ssl->in_msglen;
2138
2139#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002140 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002141 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002142 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002143 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002144 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2145 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002146 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002147
2148 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
2149
2150 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2151 ssl->handshake->premaster,
2152 &ssl->handshake->pmslen ) ) != 0 )
2153 {
2154 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2155 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2156 }
2157
2158 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002159 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002160 else
2161#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
2162#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
2163 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002164 {
2165 if( ( ret = ssl_parse_client_ecdh_public( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002166 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002167 SSL_DEBUG_RET( 1, ( "ssl_parse_client_ecdh_public" ), ret );
2168 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002169 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002170
2171 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2172 &ssl->handshake->pmslen,
2173 ssl->handshake->premaster,
2174 POLARSSL_MPI_MAX_SIZE ) ) != 0 )
2175 {
2176 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2177 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2178 }
2179
2180 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002181 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002182 else
2183#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
2184#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2185 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002186 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002187 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002188 {
2189 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2190 return( ret );
2191 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002192
2193 // Set up the premaster secret
2194 //
2195 p = ssl->handshake->premaster;
2196 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2197 *(p++) = (unsigned char)( ssl->psk_len );
2198 p += ssl->psk_len;
2199
2200 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2201 *(p++) = (unsigned char)( ssl->psk_len );
2202 memcpy( p, ssl->psk, ssl->psk_len );
2203 p += ssl->psk_len;
2204
2205 ssl->handshake->pmslen = 4 + 2 * ssl->psk_len;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002206 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002207 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002208#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
2209#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2210 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2211 {
2212 size_t n;
2213
2214 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2215 {
2216 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2217 return( ret );
2218 }
2219 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2220 {
2221 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2222 return( ret );
2223 }
2224
2225 // Set up the premaster secret
2226 //
2227 p = ssl->handshake->premaster;
2228 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len >> 8 );
2229 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len );
2230
2231 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2232 p, &n ) ) != 0 )
2233 {
2234 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2235 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2236 }
2237
2238 if( n != ssl->handshake->dhm_ctx.len )
2239 {
2240 SSL_DEBUG_MSG( 1, ( "dhm_calc_secret result smaller than DHM" ) );
2241 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2242 }
2243
2244 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2245
2246 p += ssl->handshake->dhm_ctx.len;
2247
2248 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
2249 *(p++) = (unsigned char)( ssl->psk_len );
2250 memcpy( p, ssl->psk, ssl->psk_len );
2251 p += ssl->psk_len;
2252
2253 ssl->handshake->pmslen = 4 + ssl->handshake->dhm_ctx.len + ssl->psk_len;
2254 }
2255 else
2256#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
2257#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2258 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002259 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002260 if( ( ret = ssl_parse_encrypted_pms_secret( ssl ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002261 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002262 SSL_DEBUG_RET( 1, ( "ssl_parse_client_ecdh_public" ), ret );
2263 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002264 }
2265 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002266 else
2267#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2268 {
2269 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2270 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002271
Paul Bakkerff60ee62010-03-16 21:09:09 +00002272 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2273 {
2274 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2275 return( ret );
2276 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002277
Paul Bakker5121ce52009-01-03 21:22:43 +00002278 ssl->state++;
2279
2280 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2281
2282 return( 0 );
2283}
2284
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002285#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2286 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2287 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002288static int ssl_parse_certificate_verify( ssl_context *ssl )
2289{
Paul Bakkered27a042013-04-18 22:46:23 +02002290 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002291 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002292
2293 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2294
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002295 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2296 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002297 {
2298 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2299 ssl->state++;
2300 return( 0 );
2301 }
2302
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002303 return( ret );
2304}
2305#else
2306static int ssl_parse_certificate_verify( ssl_context *ssl )
2307{
2308 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2309 size_t n = 0, n1, n2;
2310 unsigned char hash[48];
2311 md_type_t md_alg = POLARSSL_MD_NONE;
2312 unsigned int hashlen = 0;
2313 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2314
2315 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2316
2317 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2318 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2319 {
2320 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2321 ssl->state++;
2322 return( 0 );
2323 }
2324
Paul Bakkered27a042013-04-18 22:46:23 +02002325 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002326 {
2327 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2328 ssl->state++;
2329 return( 0 );
2330 }
2331
Paul Bakker48916f92012-09-16 19:57:18 +00002332 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002333
2334 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2335 {
2336 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2337 return( ret );
2338 }
2339
2340 ssl->state++;
2341
2342 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2343 {
2344 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002345 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002346 }
2347
2348 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
2349 {
2350 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002351 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002352 }
2353
Paul Bakker926af752012-11-23 13:38:07 +01002354 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2355 {
2356 /*
2357 * As server we know we either have SSL_HASH_SHA384 or
2358 * SSL_HASH_SHA256
2359 */
2360 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg ||
2361 ssl->in_msg[5] != SSL_SIG_RSA )
2362 {
2363 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg for verify message" ) );
2364 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2365 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002366
Paul Bakker926af752012-11-23 13:38:07 +01002367 if( ssl->handshake->verify_sig_alg == SSL_HASH_SHA384 )
Paul Bakkerc70b9822013-04-07 22:00:46 +02002368 md_alg = POLARSSL_MD_SHA384;
Paul Bakker926af752012-11-23 13:38:07 +01002369 else
Paul Bakkerc70b9822013-04-07 22:00:46 +02002370 md_alg = POLARSSL_MD_SHA256;
Paul Bakker926af752012-11-23 13:38:07 +01002371
2372 n += 2;
2373 }
2374 else
2375 {
2376 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002377 md_alg = POLARSSL_MD_NONE;
Paul Bakker926af752012-11-23 13:38:07 +01002378 }
2379
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002380 /* EC NOT IMPLEMENTED YET */
2381 if( ssl->session_negotiate->peer_cert->pk.type != POLARSSL_PK_RSA )
2382 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2383
2384 n1 = pk_rsa( ssl->session_negotiate->peer_cert->pk )->len;
Paul Bakker78ce5072012-11-23 14:23:53 +01002385 n2 = ( ssl->in_msg[4 + n] << 8 ) | ssl->in_msg[5 + n];
Paul Bakker926af752012-11-23 13:38:07 +01002386
2387 if( n + n1 + 6 != ssl->in_hslen || n1 != n2 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002388 {
2389 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002390 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002391 }
2392
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002393 ret = rsa_pkcs1_verify( pk_rsa( ssl->session_negotiate->peer_cert->pk ),
2394 RSA_PUBLIC, md_alg, hashlen, hash,
2395 ssl->in_msg + 6 + n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002396 if( ret != 0 )
2397 {
2398 SSL_DEBUG_RET( 1, "rsa_pkcs1_verify", ret );
2399 return( ret );
2400 }
2401
2402 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
2403
Paul Bakkered27a042013-04-18 22:46:23 +02002404 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002405}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002406#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2407 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2408 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002409
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002410static int ssl_write_new_session_ticket( ssl_context *ssl )
2411{
2412 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002413 size_t tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002414
2415 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
2416
2417 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2418 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
2419
2420 /*
2421 * struct {
2422 * uint32 ticket_lifetime_hint;
2423 * opaque ticket<0..2^16-1>;
2424 * } NewSessionTicket;
2425 *
2426 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
2427 * 8 . 9 ticket_len (n)
2428 * 10 . 9+n ticket content
2429 */
2430 ssl->out_msg[4] = 0x00;
2431 ssl->out_msg[5] = 0x00;
2432 ssl->out_msg[6] = 0x00;
2433 ssl->out_msg[7] = 0x00;
2434
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +02002435 ssl_write_ticket( ssl, &tlen );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002436
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002437 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
2438 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002439
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002440 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002441
2442 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2443 {
2444 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2445 return( ret );
2446 }
2447
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002448 /* No need to remember writing a NewSessionTicket any more */
2449 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002450
2451 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
2452
2453 return( 0 );
2454}
2455
Paul Bakker5121ce52009-01-03 21:22:43 +00002456/*
Paul Bakker1961b702013-01-25 14:49:24 +01002457 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002458 */
Paul Bakker1961b702013-01-25 14:49:24 +01002459int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002460{
2461 int ret = 0;
2462
Paul Bakker1961b702013-01-25 14:49:24 +01002463 if( ssl->state == SSL_HANDSHAKE_OVER )
2464 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002465
Paul Bakker1961b702013-01-25 14:49:24 +01002466 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
2467
2468 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2469 return( ret );
2470
2471 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002472 {
Paul Bakker1961b702013-01-25 14:49:24 +01002473 case SSL_HELLO_REQUEST:
2474 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002475 break;
2476
Paul Bakker1961b702013-01-25 14:49:24 +01002477 /*
2478 * <== ClientHello
2479 */
2480 case SSL_CLIENT_HELLO:
2481 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002482 break;
Paul Bakker1961b702013-01-25 14:49:24 +01002483
2484 /*
2485 * ==> ServerHello
2486 * Certificate
2487 * ( ServerKeyExchange )
2488 * ( CertificateRequest )
2489 * ServerHelloDone
2490 */
2491 case SSL_SERVER_HELLO:
2492 ret = ssl_write_server_hello( ssl );
2493 break;
2494
2495 case SSL_SERVER_CERTIFICATE:
2496 ret = ssl_write_certificate( ssl );
2497 break;
2498
2499 case SSL_SERVER_KEY_EXCHANGE:
2500 ret = ssl_write_server_key_exchange( ssl );
2501 break;
2502
2503 case SSL_CERTIFICATE_REQUEST:
2504 ret = ssl_write_certificate_request( ssl );
2505 break;
2506
2507 case SSL_SERVER_HELLO_DONE:
2508 ret = ssl_write_server_hello_done( ssl );
2509 break;
2510
2511 /*
2512 * <== ( Certificate/Alert )
2513 * ClientKeyExchange
2514 * ( CertificateVerify )
2515 * ChangeCipherSpec
2516 * Finished
2517 */
2518 case SSL_CLIENT_CERTIFICATE:
2519 ret = ssl_parse_certificate( ssl );
2520 break;
2521
2522 case SSL_CLIENT_KEY_EXCHANGE:
2523 ret = ssl_parse_client_key_exchange( ssl );
2524 break;
2525
2526 case SSL_CERTIFICATE_VERIFY:
2527 ret = ssl_parse_certificate_verify( ssl );
2528 break;
2529
2530 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2531 ret = ssl_parse_change_cipher_spec( ssl );
2532 break;
2533
2534 case SSL_CLIENT_FINISHED:
2535 ret = ssl_parse_finished( ssl );
2536 break;
2537
2538 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002539 * ==> ( NewSessionTicket )
2540 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002541 * Finished
2542 */
2543 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002544 if( ssl->handshake->new_session_ticket != 0 )
2545 ret = ssl_write_new_session_ticket( ssl );
2546 else
2547 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002548 break;
2549
2550 case SSL_SERVER_FINISHED:
2551 ret = ssl_write_finished( ssl );
2552 break;
2553
2554 case SSL_FLUSH_BUFFERS:
2555 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2556 ssl->state = SSL_HANDSHAKE_WRAPUP;
2557 break;
2558
2559 case SSL_HANDSHAKE_WRAPUP:
2560 ssl_handshake_wrapup( ssl );
2561 break;
2562
2563 default:
2564 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2565 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002566 }
2567
Paul Bakker5121ce52009-01-03 21:22:43 +00002568 return( ret );
2569}
Paul Bakker5121ce52009-01-03 21:22:43 +00002570#endif