PolarSSL v1.2.7
ssl_srv.c
Go to the documentation of this file.
1 /*
2  * SSLv3/TLSv1 server-side functions
3  *
4  * Copyright (C) 2006-2012, Brainspark B.V.
5  *
6  * This file is part of PolarSSL (http://www.polarssl.org)
7  * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8  *
9  * All rights reserved.
10  *
11  * 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 
26 #include "polarssl/config.h"
27 
28 #if defined(POLARSSL_SSL_SRV_C)
29 
30 #include "polarssl/debug.h"
31 #include "polarssl/ssl.h"
32 
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <time.h>
36 
37 static int ssl_parse_servername_ext( ssl_context *ssl,
38  const unsigned char *buf,
39  size_t len )
40 {
41  int ret;
42  size_t servername_list_size, hostname_len;
43  const unsigned char *p;
44 
45  servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
46  if( servername_list_size + 2 != len )
47  {
48  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
50  }
51 
52  p = buf + 2;
53  while( servername_list_size > 0 )
54  {
55  hostname_len = ( ( p[1] << 8 ) | p[2] );
56  if( hostname_len + 3 > servername_list_size )
57  {
58  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
60  }
61 
62  if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
63  {
64  ret = ssl->f_sni( ssl->p_sni, ssl, p + 3, hostname_len );
65  if( ret != 0 )
66  {
70  }
71  return( 0 );
72  }
73 
74  servername_list_size -= hostname_len + 3;
75  p += hostname_len + 3;
76  }
77 
78  if( servername_list_size != 0 )
79  {
80  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
82  }
83 
84  return( 0 );
85 }
86 
87 static int ssl_parse_renegotiation_info( ssl_context *ssl,
88  const unsigned char *buf,
89  size_t len )
90 {
91  int ret;
92 
94  {
95  if( len != 1 || buf[0] != 0x0 )
96  {
97  SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
98 
99  if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
100  return( ret );
101 
103  }
104 
106  }
107  else
108  {
109  if( len != 1 + ssl->verify_data_len ||
110  buf[0] != ssl->verify_data_len ||
111  memcmp( buf + 1, ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
112  {
113  SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
114 
115  if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
116  return( ret );
117 
119  }
120  }
121 
122  return( 0 );
123 }
124 
125 static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
126  const unsigned char *buf,
127  size_t len )
128 {
129  size_t sig_alg_list_size;
130  const unsigned char *p;
131 
132  sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
133  if( sig_alg_list_size + 2 != len ||
134  sig_alg_list_size %2 != 0 )
135  {
136  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
138  }
139 
140  p = buf + 2;
141  while( sig_alg_list_size > 0 )
142  {
143  if( p[1] != SSL_SIG_RSA )
144  {
145  sig_alg_list_size -= 2;
146  p += 2;
147  continue;
148  }
149 #if defined(POLARSSL_SHA4_C)
150  if( p[0] == SSL_HASH_SHA512 )
151  {
153  break;
154  }
155  if( p[0] == SSL_HASH_SHA384 )
156  {
158  break;
159  }
160 #endif
161 #if defined(POLARSSL_SHA2_C)
162  if( p[0] == SSL_HASH_SHA256 )
163  {
165  break;
166  }
167  if( p[0] == SSL_HASH_SHA224 )
168  {
170  break;
171  }
172 #endif
173  if( p[0] == SSL_HASH_SHA1 )
174  {
176  break;
177  }
178  if( p[0] == SSL_HASH_MD5 )
179  {
181  break;
182  }
183 
184  sig_alg_list_size -= 2;
185  p += 2;
186  }
187 
188  SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
189  ssl->handshake->sig_alg ) );
190 
191  return( 0 );
192 }
193 
194 #if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
195 static int ssl_parse_client_hello_v2( ssl_context *ssl )
196 {
197  int ret;
198  unsigned int i, j;
199  size_t n;
200  unsigned int ciph_len, sess_len, chal_len;
201  unsigned char *buf, *p;
202 
203  SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
204 
206  {
207  SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
208 
209  if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
210  return( ret );
211 
213  }
214 
215  buf = ssl->in_hdr;
216 
217  SSL_DEBUG_BUF( 4, "record header", buf, 5 );
218 
219  SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
220  buf[2] ) );
221  SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
222  ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
223  SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
224  buf[3], buf[4] ) );
225 
226  /*
227  * SSLv2 Client Hello
228  *
229  * Record layer:
230  * 0 . 1 message length
231  *
232  * SSL layer:
233  * 2 . 2 message type
234  * 3 . 4 protocol version
235  */
236  if( buf[2] != SSL_HS_CLIENT_HELLO ||
237  buf[3] != SSL_MAJOR_VERSION_3 )
238  {
239  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
241  }
242 
243  n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
244 
245  if( n < 17 || n > 512 )
246  {
247  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
249  }
250 
252  ssl->minor_ver = ( buf[4] <= SSL_MINOR_VERSION_3 )
253  ? buf[4] : SSL_MINOR_VERSION_3;
254 
255  if( ssl->minor_ver < ssl->min_minor_ver )
256  {
257  SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
258  " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
259  ssl->min_major_ver, ssl->min_minor_ver ) );
260 
264  }
265 
266  ssl->max_major_ver = buf[3];
267  ssl->max_minor_ver = buf[4];
268 
269  if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
270  {
271  SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
272  return( ret );
273  }
274 
275  ssl->handshake->update_checksum( ssl, buf + 2, n );
276 
277  buf = ssl->in_msg;
278  n = ssl->in_left - 5;
279 
280  /*
281  * 0 . 1 ciphersuitelist length
282  * 2 . 3 session id length
283  * 4 . 5 challenge length
284  * 6 . .. ciphersuitelist
285  * .. . .. session id
286  * .. . .. challenge
287  */
288  SSL_DEBUG_BUF( 4, "record contents", buf, n );
289 
290  ciph_len = ( buf[0] << 8 ) | buf[1];
291  sess_len = ( buf[2] << 8 ) | buf[3];
292  chal_len = ( buf[4] << 8 ) | buf[5];
293 
294  SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
295  ciph_len, sess_len, chal_len ) );
296 
297  /*
298  * Make sure each parameter length is valid
299  */
300  if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
301  {
302  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
304  }
305 
306  if( sess_len > 32 )
307  {
308  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
310  }
311 
312  if( chal_len < 8 || chal_len > 32 )
313  {
314  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
316  }
317 
318  if( n != 6 + ciph_len + sess_len + chal_len )
319  {
320  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
322  }
323 
324  SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
325  buf + 6, ciph_len );
326  SSL_DEBUG_BUF( 3, "client hello, session id",
327  buf + 6 + ciph_len, sess_len );
328  SSL_DEBUG_BUF( 3, "client hello, challenge",
329  buf + 6 + ciph_len + sess_len, chal_len );
330 
331  p = buf + 6 + ciph_len;
332  ssl->session_negotiate->length = sess_len;
333  memset( ssl->session_negotiate->id, 0, sizeof( ssl->session_negotiate->id ) );
334  memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
335 
336  p += sess_len;
337  memset( ssl->handshake->randbytes, 0, 64 );
338  memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
339 
340  /*
341  * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
342  */
343  for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
344  {
345  if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
346  {
347  SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
348  if( ssl->renegotiation == SSL_RENEGOTIATION )
349  {
350  SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
351 
352  if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
353  return( ret );
354 
356  }
358  break;
359  }
360  }
361 
362  for( i = 0; ssl->ciphersuites[ssl->minor_ver][i] != 0; i++ )
363  {
364  for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
365  {
366  if( p[0] == 0 &&
367  p[1] == 0 &&
368  p[2] == ssl->ciphersuites[ssl->minor_ver][i] )
369  goto have_ciphersuite_v2;
370  }
371  }
372 
373  SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
374 
376 
377 have_ciphersuite_v2:
378  ssl->session_negotiate->ciphersuite = ssl->ciphersuites[ssl->minor_ver][i];
380 
381  /*
382  * SSLv2 Client Hello relevant renegotiation security checks
383  */
386  {
387  SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
388 
389  if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
390  return( ret );
391 
393  }
394 
395  ssl->in_left = 0;
396  ssl->state++;
397 
398  SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
399 
400  return( 0 );
401 }
402 #endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
403 
404 static int ssl_parse_client_hello( ssl_context *ssl )
405 {
406  int ret;
407  unsigned int i, j;
408  size_t n;
409  unsigned int ciph_len, sess_len;
410  unsigned int comp_len;
411  unsigned int ext_len = 0;
412  unsigned char *buf, *p, *ext;
413  int renegotiation_info_seen = 0;
414  int handshake_failure = 0;
415 
416  SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
417 
418  if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
419  ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
420  {
421  SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
422  return( ret );
423  }
424 
425  buf = ssl->in_hdr;
426 
427 #if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
428  if( ( buf[0] & 0x80 ) != 0 )
429  return ssl_parse_client_hello_v2( ssl );
430 #endif
431 
432  SSL_DEBUG_BUF( 4, "record header", buf, 5 );
433 
434  SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
435  buf[0] ) );
436  SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
437  ( buf[3] << 8 ) | buf[4] ) );
438  SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
439  buf[1], buf[2] ) );
440 
441  /*
442  * SSLv3 Client Hello
443  *
444  * Record layer:
445  * 0 . 0 message type
446  * 1 . 2 protocol version
447  * 3 . 4 message length
448  */
449  if( buf[0] != SSL_MSG_HANDSHAKE ||
450  buf[1] != SSL_MAJOR_VERSION_3 )
451  {
452  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
454  }
455 
456  n = ( buf[3] << 8 ) | buf[4];
457 
458  if( n < 45 || n > 512 )
459  {
460  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
462  }
463 
464  if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
465  ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
466  {
467  SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
468  return( ret );
469  }
470 
471  buf = ssl->in_msg;
472  if( !ssl->renegotiation )
473  n = ssl->in_left - 5;
474  else
475  n = ssl->in_msglen;
476 
477  ssl->handshake->update_checksum( ssl, buf, n );
478 
479  /*
480  * SSL layer:
481  * 0 . 0 handshake type
482  * 1 . 3 handshake length
483  * 4 . 5 protocol version
484  * 6 . 9 UNIX time()
485  * 10 . 37 random bytes
486  * 38 . 38 session id length
487  * 39 . 38+x session id
488  * 39+x . 40+x ciphersuitelist length
489  * 41+x . .. ciphersuitelist
490  * .. . .. compression alg.
491  * .. . .. extensions
492  */
493  SSL_DEBUG_BUF( 4, "record contents", buf, n );
494 
495  SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
496  buf[0] ) );
497  SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
498  ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
499  SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
500  buf[4], buf[5] ) );
501 
502  /*
503  * Check the handshake type and protocol version
504  */
505  if( buf[0] != SSL_HS_CLIENT_HELLO ||
506  buf[4] != SSL_MAJOR_VERSION_3 )
507  {
508  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
510  }
511 
513  ssl->minor_ver = ( buf[5] <= SSL_MINOR_VERSION_3 )
514  ? buf[5] : SSL_MINOR_VERSION_3;
515 
516  if( ssl->minor_ver < ssl->min_minor_ver )
517  {
518  SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
519  " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
520  ssl->min_major_ver, ssl->min_minor_ver ) );
521 
524 
526  }
527 
528  ssl->max_major_ver = buf[4];
529  ssl->max_minor_ver = buf[5];
530 
531  memcpy( ssl->handshake->randbytes, buf + 6, 32 );
532 
533  /*
534  * Check the handshake message length
535  */
536  if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
537  {
538  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
540  }
541 
542  /*
543  * Check the session length
544  */
545  sess_len = buf[38];
546 
547  if( sess_len > 32 )
548  {
549  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
551  }
552 
553  ssl->session_negotiate->length = sess_len;
554  memset( ssl->session_negotiate->id, 0,
555  sizeof( ssl->session_negotiate->id ) );
556  memcpy( ssl->session_negotiate->id, buf + 39,
557  ssl->session_negotiate->length );
558 
559  /*
560  * Check the ciphersuitelist length
561  */
562  ciph_len = ( buf[39 + sess_len] << 8 )
563  | ( buf[40 + sess_len] );
564 
565  if( ciph_len < 2 || ciph_len > 256 || ( ciph_len % 2 ) != 0 )
566  {
567  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
569  }
570 
571  /*
572  * Check the compression algorithms length
573  */
574  comp_len = buf[41 + sess_len + ciph_len];
575 
576  if( comp_len < 1 || comp_len > 16 )
577  {
578  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
580  }
581 
582  /*
583  * Check the extension length
584  */
585  if( n > 42 + sess_len + ciph_len + comp_len )
586  {
587  ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
588  | ( buf[43 + sess_len + ciph_len + comp_len] );
589 
590  if( ( ext_len > 0 && ext_len < 4 ) ||
591  n != 44 + sess_len + ciph_len + comp_len + ext_len )
592  {
593  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
594  SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
596  }
597  }
598 
600 #if defined(POLARSSL_ZLIB_SUPPORT)
601  for( i = 0; i < comp_len; ++i )
602  {
603  if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
604  {
606  break;
607  }
608  }
609 #endif
610 
611  SSL_DEBUG_BUF( 3, "client hello, random bytes",
612  buf + 6, 32 );
613  SSL_DEBUG_BUF( 3, "client hello, session id",
614  buf + 38, sess_len );
615  SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
616  buf + 41 + sess_len, ciph_len );
617  SSL_DEBUG_BUF( 3, "client hello, compression",
618  buf + 42 + sess_len + ciph_len, comp_len );
619 
620  /*
621  * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
622  */
623  for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
624  {
625  if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
626  {
627  SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
628  if( ssl->renegotiation == SSL_RENEGOTIATION )
629  {
630  SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
631 
632  if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
633  return( ret );
634 
636  }
638  break;
639  }
640  }
641 
642  /*
643  * Search for a matching ciphersuite
644  */
645  for( i = 0; ssl->ciphersuites[ssl->minor_ver][i] != 0; i++ )
646  {
647  for( j = 0, p = buf + 41 + sess_len; j < ciph_len;
648  j += 2, p += 2 )
649  {
650  if( p[0] == 0 && p[1] == ssl->ciphersuites[ssl->minor_ver][i] )
651  goto have_ciphersuite;
652  }
653  }
654 
655  SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
656 
658 
659 have_ciphersuite:
660  ssl->session_negotiate->ciphersuite = ssl->ciphersuites[ssl->minor_ver][i];
662 
663  ext = buf + 44 + sess_len + ciph_len + comp_len;
664 
665  while( ext_len )
666  {
667  unsigned int ext_id = ( ( ext[0] << 8 )
668  | ( ext[1] ) );
669  unsigned int ext_size = ( ( ext[2] << 8 )
670  | ( ext[3] ) );
671 
672  if( ext_size + 4 > ext_len )
673  {
674  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
676  }
677  switch( ext_id )
678  {
679  case TLS_EXT_SERVERNAME:
680  SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
681  if( ssl->f_sni == NULL )
682  break;
683 
684  ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
685  if( ret != 0 )
686  return( ret );
687  break;
688 
690  SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
691  renegotiation_info_seen = 1;
692 
693  ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
694  if( ret != 0 )
695  return( ret );
696  break;
697 
698  case TLS_EXT_SIG_ALG:
699  SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
700  if( ssl->renegotiation == SSL_RENEGOTIATION )
701  break;
702 
703  ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
704  if( ret != 0 )
705  return( ret );
706  break;
707 
708  default:
709  SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
710  ext_id ) );
711  }
712 
713  ext_len -= 4 + ext_size;
714  ext += 4 + ext_size;
715 
716  if( ext_len > 0 && ext_len < 4 )
717  {
718  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
720  }
721  }
722 
723  /*
724  * Renegotiation security checks
725  */
728  {
729  SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
730  handshake_failure = 1;
731  }
732  else if( ssl->renegotiation == SSL_RENEGOTIATION &&
734  renegotiation_info_seen == 0 )
735  {
736  SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
737  handshake_failure = 1;
738  }
739  else if( ssl->renegotiation == SSL_RENEGOTIATION &&
742  {
743  SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
744  handshake_failure = 1;
745  }
746  else if( ssl->renegotiation == SSL_RENEGOTIATION &&
748  renegotiation_info_seen == 1 )
749  {
750  SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
751  handshake_failure = 1;
752  }
753 
754  if( handshake_failure == 1 )
755  {
756  if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
757  return( ret );
758 
760  }
761 
762  ssl->in_left = 0;
763  ssl->state++;
764 
765  SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
766 
767  return( 0 );
768 }
769 
770 static int ssl_write_server_hello( ssl_context *ssl )
771 {
772  time_t t;
773  int ret, n;
774  size_t ext_len = 0;
775  unsigned char *buf, *p;
776 
777  SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
778 
779  /*
780  * 0 . 0 handshake type
781  * 1 . 3 handshake length
782  * 4 . 5 protocol version
783  * 6 . 9 UNIX time()
784  * 10 . 37 random bytes
785  */
786  buf = ssl->out_msg;
787  p = buf + 4;
788 
789  *p++ = (unsigned char) ssl->major_ver;
790  *p++ = (unsigned char) ssl->minor_ver;
791 
792  SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
793  buf[4], buf[5] ) );
794 
795  t = time( NULL );
796  *p++ = (unsigned char)( t >> 24 );
797  *p++ = (unsigned char)( t >> 16 );
798  *p++ = (unsigned char)( t >> 8 );
799  *p++ = (unsigned char)( t );
800 
801  SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
802 
803  if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
804  return( ret );
805 
806  p += 28;
807 
808  memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
809 
810  SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
811 
812  /*
813  * 38 . 38 session id length
814  * 39 . 38+n session id
815  * 39+n . 40+n chosen ciphersuite
816  * 41+n . 41+n chosen compression alg.
817  */
818  ssl->session_negotiate->length = n = 32;
819  *p++ = (unsigned char) ssl->session_negotiate->length;
820 
821  if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
822  ssl->f_get_cache == NULL ||
823  ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) != 0 )
824  {
825  /*
826  * Not found, create a new session id
827  */
828  ssl->handshake->resume = 0;
829  ssl->state++;
830 
831  if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
832  n ) ) != 0 )
833  return( ret );
834  }
835  else
836  {
837  /*
838  * Found a matching session, resuming it
839  */
840  ssl->handshake->resume = 1;
842 
843  if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
844  {
845  SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
846  return( ret );
847  }
848  }
849 
850  memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
851  p += ssl->session_negotiate->length;
852 
853  SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
854  SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
855  SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
856  ssl->handshake->resume ? "a" : "no" ) );
857 
858  *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
859  *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
860  *p++ = (unsigned char)( ssl->session_negotiate->compression );
861 
862  SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d",
863  ssl->session_negotiate->ciphersuite ) );
864  SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d",
865  ssl->session_negotiate->compression ) );
866 
867  SSL_DEBUG_MSG( 3, ( "server hello, prepping for secure renegotiation extension" ) );
868  ext_len += 5 + ssl->verify_data_len * 2;
869 
870  SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d",
871  ext_len ) );
872 
873  *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
874  *p++ = (unsigned char)( ( ext_len ) & 0xFF );
875 
876  /*
877  * Secure renegotiation
878  */
879  SSL_DEBUG_MSG( 3, ( "client hello, secure renegotiation extension" ) );
880 
881  *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
882  *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
883 
884  *p++ = 0x00;
885  *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
886  *p++ = ssl->verify_data_len * 2 & 0xFF;
887 
888  memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
889  p += ssl->verify_data_len;
890  memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
891  p += ssl->verify_data_len;
892 
893  ssl->out_msglen = p - buf;
895  ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
896 
897  ret = ssl_write_record( ssl );
898 
899  SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
900 
901  return( ret );
902 }
903 
904 static int ssl_write_certificate_request( ssl_context *ssl )
905 {
906  int ret;
907  size_t n = 0, dn_size, total_dn_size;
908  unsigned char *buf, *p;
909  const x509_cert *crt;
910 
911  SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
912 
913  ssl->state++;
914 
915  if( ssl->authmode == SSL_VERIFY_NONE )
916  {
917  SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
918  return( 0 );
919  }
920 
921  /*
922  * 0 . 0 handshake type
923  * 1 . 3 handshake length
924  * 4 . 4 cert type count
925  * 5 .. m-1 cert types
926  * m .. m+1 sig alg length (TLS 1.2 only)
927  * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
928  * n .. n+1 length of all DNs
929  * n+2 .. n+3 length of DN 1
930  * n+4 .. ... Distinguished Name #1
931  * ... .. ... length of DN 2, etc.
932  */
933  buf = ssl->out_msg;
934  p = buf + 4;
935 
936  /*
937  * At the moment, only RSA certificates are supported
938  */
939  *p++ = 1;
940  *p++ = SSL_CERT_TYPE_RSA_SIGN;
941 
942  /*
943  * Add signature_algorithms for verify (TLS 1.2)
944  * Only add current running algorithm that is already required for
945  * requested ciphersuite.
946  *
947  * Length is always 2
948  */
949  if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
950  {
952 
953  *p++ = 0;
954  *p++ = 2;
955 
958  {
960  }
961 
962  *p++ = ssl->handshake->verify_sig_alg;
963  *p++ = SSL_SIG_RSA;
964 
965  n += 4;
966  }
967 
968  p += 2;
969  crt = ssl->ca_chain;
970 
971  total_dn_size = 0;
972  while( crt != NULL )
973  {
974  if( p - buf > 4096 )
975  break;
976 
977  dn_size = crt->subject_raw.len;
978  *p++ = (unsigned char)( dn_size >> 8 );
979  *p++ = (unsigned char)( dn_size );
980  memcpy( p, crt->subject_raw.p, dn_size );
981  p += dn_size;
982 
983  SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
984 
985  total_dn_size += 2 + dn_size;
986  crt = crt->next;
987  }
988 
989  ssl->out_msglen = p - buf;
992  ssl->out_msg[6 + n] = (unsigned char)( total_dn_size >> 8 );
993  ssl->out_msg[7 + n] = (unsigned char)( total_dn_size );
994 
995  ret = ssl_write_record( ssl );
996 
997  SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
998 
999  return( ret );
1000 }
1001 
1002 static int ssl_write_server_key_exchange( ssl_context *ssl )
1003 {
1004 #if defined(POLARSSL_DHM_C)
1005  int ret;
1006  size_t n, rsa_key_len = 0;
1007  unsigned char hash[64];
1008  int hash_id = 0;
1009  unsigned int hashlen = 0;
1010 #endif
1011 
1012  SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
1013 
1026  {
1027  SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
1028  ssl->state++;
1029  return( 0 );
1030  }
1031 
1032 #if !defined(POLARSSL_DHM_C)
1033  SSL_DEBUG_MSG( 1, ( "support for dhm is not available" ) );
1035 #else
1036 
1037  if( ssl->rsa_key == NULL )
1038  {
1039  SSL_DEBUG_MSG( 1, ( "got no private key" ) );
1041  }
1042 
1043  /*
1044  * Ephemeral DH parameters:
1045  *
1046  * struct {
1047  * opaque dh_p<1..2^16-1>;
1048  * opaque dh_g<1..2^16-1>;
1049  * opaque dh_Ys<1..2^16-1>;
1050  * } ServerDHParams;
1051  */
1052  if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
1053  ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
1054  {
1055  SSL_DEBUG_RET( 1, "mpi_copy", ret );
1056  return( ret );
1057  }
1058 
1059  if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
1060  mpi_size( &ssl->handshake->dhm_ctx.P ),
1061  ssl->out_msg + 4,
1062  &n, ssl->f_rng, ssl->p_rng ) ) != 0 )
1063  {
1064  SSL_DEBUG_RET( 1, "dhm_make_params", ret );
1065  return( ret );
1066  }
1067 
1068  SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
1069  SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1070  SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1071  SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
1072 
1073  if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1074  {
1075  md5_context md5;
1077 
1078  /*
1079  * digitally-signed struct {
1080  * opaque md5_hash[16];
1081  * opaque sha_hash[20];
1082  * };
1083  *
1084  * md5_hash
1085  * MD5(ClientHello.random + ServerHello.random
1086  * + ServerParams);
1087  * sha_hash
1088  * SHA(ClientHello.random + ServerHello.random
1089  * + ServerParams);
1090  */
1091  md5_starts( &md5 );
1092  md5_update( &md5, ssl->handshake->randbytes, 64 );
1093  md5_update( &md5, ssl->out_msg + 4, n );
1094  md5_finish( &md5, hash );
1095 
1096  sha1_starts( &sha1 );
1097  sha1_update( &sha1, ssl->handshake->randbytes, 64 );
1098  sha1_update( &sha1, ssl->out_msg + 4, n );
1099  sha1_finish( &sha1, hash + 16 );
1100 
1101  hashlen = 36;
1102  hash_id = SIG_RSA_RAW;
1103  }
1104  else
1105  {
1106  /*
1107  * digitally-signed struct {
1108  * opaque client_random[32];
1109  * opaque server_random[32];
1110  * ServerDHParams params;
1111  * };
1112  */
1113 #if defined(POLARSSL_SHA4_C)
1114  if( ssl->handshake->sig_alg == SSL_HASH_SHA512 )
1115  {
1117 
1118  sha4_starts( &sha4, 0 );
1119  sha4_update( &sha4, ssl->handshake->randbytes, 64 );
1120  sha4_update( &sha4, ssl->out_msg + 4, n );
1121  sha4_finish( &sha4, hash );
1122 
1123  hashlen = 64;
1124  hash_id = SIG_RSA_SHA512;
1125  }
1126  else if( ssl->handshake->sig_alg == SSL_HASH_SHA384 )
1127  {
1129 
1130  sha4_starts( &sha4, 1 );
1131  sha4_update( &sha4, ssl->handshake->randbytes, 64 );
1132  sha4_update( &sha4, ssl->out_msg + 4, n );
1133  sha4_finish( &sha4, hash );
1134 
1135  hashlen = 48;
1136  hash_id = SIG_RSA_SHA384;
1137  }
1138  else
1139 #endif
1140 #if defined(POLARSSL_SHA2_C)
1141  if( ssl->handshake->sig_alg == SSL_HASH_SHA256 )
1142  {
1144 
1145  sha2_starts( &sha2, 0 );
1146  sha2_update( &sha2, ssl->handshake->randbytes, 64 );
1147  sha2_update( &sha2, ssl->out_msg + 4, n );
1148  sha2_finish( &sha2, hash );
1149 
1150  hashlen = 32;
1151  hash_id = SIG_RSA_SHA256;
1152  }
1153  else if( ssl->handshake->sig_alg == SSL_HASH_SHA224 )
1154  {
1156 
1157  sha2_starts( &sha2, 1 );
1158  sha2_update( &sha2, ssl->handshake->randbytes, 64 );
1159  sha2_update( &sha2, ssl->out_msg + 4, n );
1160  sha2_finish( &sha2, hash );
1161 
1162  hashlen = 24;
1163  hash_id = SIG_RSA_SHA224;
1164  }
1165  else
1166 #endif
1167  if( ssl->handshake->sig_alg == SSL_HASH_SHA1 )
1168  {
1170 
1171  sha1_starts( &sha1 );
1172  sha1_update( &sha1, ssl->handshake->randbytes, 64 );
1173  sha1_update( &sha1, ssl->out_msg + 4, n );
1174  sha1_finish( &sha1, hash );
1175 
1176  hashlen = 20;
1177  hash_id = SIG_RSA_SHA1;
1178  }
1179  else if( ssl->handshake->sig_alg == SSL_HASH_MD5 )
1180  {
1181  md5_context md5;
1182 
1183  md5_starts( &md5 );
1184  md5_update( &md5, ssl->handshake->randbytes, 64 );
1185  md5_update( &md5, ssl->out_msg + 4, n );
1186  md5_finish( &md5, hash );
1187 
1188  hashlen = 16;
1189  hash_id = SIG_RSA_MD5;
1190  }
1191  }
1192 
1193  SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
1194 
1195  if ( ssl->rsa_key )
1196  rsa_key_len = ssl->rsa_key_len( ssl->rsa_key );
1197 
1198  if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
1199  {
1200  ssl->out_msg[4 + n] = ssl->handshake->sig_alg;
1201  ssl->out_msg[5 + n] = SSL_SIG_RSA;
1202 
1203  n += 2;
1204  }
1205 
1206  ssl->out_msg[4 + n] = (unsigned char)( rsa_key_len >> 8 );
1207  ssl->out_msg[5 + n] = (unsigned char)( rsa_key_len );
1208 
1209  if ( ssl->rsa_key )
1210  {
1211  ret = ssl->rsa_sign( ssl->rsa_key, ssl->f_rng, ssl->p_rng,
1212  RSA_PRIVATE,
1213  hash_id, hashlen, hash,
1214  ssl->out_msg + 6 + n );
1215  }
1216 
1217  if( ret != 0 )
1218  {
1219  SSL_DEBUG_RET( 1, "pkcs1_sign", ret );
1220  return( ret );
1221  }
1222 
1223  SSL_DEBUG_BUF( 3, "my RSA sig", ssl->out_msg + 6 + n, rsa_key_len );
1224 
1225  ssl->out_msglen = 6 + n + rsa_key_len;
1228 
1229  ssl->state++;
1230 
1231  if( ( ret = ssl_write_record( ssl ) ) != 0 )
1232  {
1233  SSL_DEBUG_RET( 1, "ssl_write_record", ret );
1234  return( ret );
1235  }
1236 
1237  SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
1238 
1239  return( 0 );
1240 #endif
1241 }
1242 
1243 static int ssl_write_server_hello_done( ssl_context *ssl )
1244 {
1245  int ret;
1246 
1247  SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
1248 
1249  ssl->out_msglen = 4;
1252 
1253  ssl->state++;
1254 
1255  if( ( ret = ssl_write_record( ssl ) ) != 0 )
1256  {
1257  SSL_DEBUG_RET( 1, "ssl_write_record", ret );
1258  return( ret );
1259  }
1260 
1261  SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
1262 
1263  return( 0 );
1264 }
1265 
1266 static int ssl_parse_client_key_exchange( ssl_context *ssl )
1267 {
1268  int ret;
1269  size_t i, n = 0;
1270 
1271  SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
1272 
1273  if( ( ret = ssl_read_record( ssl ) ) != 0 )
1274  {
1275  SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1276  return( ret );
1277  }
1278 
1279  if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1280  {
1281  SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
1283  }
1284 
1285  if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
1286  {
1287  SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
1289  }
1290 
1303  {
1304 #if !defined(POLARSSL_DHM_C)
1305  SSL_DEBUG_MSG( 1, ( "support for dhm is not available" ) );
1307 #else
1308  /*
1309  * Receive G^Y mod P, premaster = (G^Y)^X mod P
1310  */
1311  n = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
1312 
1313  if( n < 1 || n > ssl->handshake->dhm_ctx.len ||
1314  n + 6 != ssl->in_hslen )
1315  {
1316  SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
1318  }
1319 
1320  if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx,
1321  ssl->in_msg + 6, n ) ) != 0 )
1322  {
1323  SSL_DEBUG_RET( 1, "dhm_read_public", ret );
1325  }
1326 
1327  SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
1328 
1329  ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
1330 
1331  if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
1332  ssl->handshake->premaster,
1333  &ssl->handshake->pmslen ) ) != 0 )
1334  {
1335  SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
1337  }
1338 
1339  SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1340 #endif
1341  }
1342  else
1343  {
1344  if( ssl->rsa_key == NULL )
1345  {
1346  SSL_DEBUG_MSG( 1, ( "got no private key" ) );
1348  }
1349 
1350  /*
1351  * Decrypt the premaster using own private RSA key
1352  */
1353  i = 4;
1354  if( ssl->rsa_key )
1355  n = ssl->rsa_key_len( ssl->rsa_key );
1356  ssl->handshake->pmslen = 48;
1357 
1358  if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
1359  {
1360  i += 2;
1361  if( ssl->in_msg[4] != ( ( n >> 8 ) & 0xFF ) ||
1362  ssl->in_msg[5] != ( ( n ) & 0xFF ) )
1363  {
1364  SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
1366  }
1367  }
1368 
1369  if( ssl->in_hslen != i + n )
1370  {
1371  SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
1373  }
1374 
1375  if( ssl->rsa_key ) {
1376  ret = ssl->rsa_decrypt( ssl->rsa_key, RSA_PRIVATE,
1377  &ssl->handshake->pmslen,
1378  ssl->in_msg + i,
1379  ssl->handshake->premaster,
1380  sizeof(ssl->handshake->premaster) );
1381  }
1382 
1383  if( ret != 0 || ssl->handshake->pmslen != 48 ||
1384  ssl->handshake->premaster[0] != ssl->max_major_ver ||
1385  ssl->handshake->premaster[1] != ssl->max_minor_ver )
1386  {
1387  SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
1388 
1389  /*
1390  * Protection against Bleichenbacher's attack:
1391  * invalid PKCS#1 v1.5 padding must not cause
1392  * the connection to end immediately; instead,
1393  * send a bad_record_mac later in the handshake.
1394  */
1395  ssl->handshake->pmslen = 48;
1396 
1397  ret = ssl->f_rng( ssl->p_rng, ssl->handshake->premaster,
1398  ssl->handshake->pmslen );
1399  if( ret != 0 )
1400  return( ret );
1401  }
1402  }
1403 
1404  if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1405  {
1406  SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1407  return( ret );
1408  }
1409 
1410  ssl->state++;
1411 
1412  SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
1413 
1414  return( 0 );
1415 }
1416 
1417 static int ssl_parse_certificate_verify( ssl_context *ssl )
1418 {
1419  int ret;
1420  size_t n = 0, n1, n2;
1421  unsigned char hash[48];
1422  int hash_id;
1423  unsigned int hashlen;
1424 
1425  SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
1426 
1427  if( ssl->session_negotiate->peer_cert == NULL )
1428  {
1429  SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
1430  ssl->state++;
1431  return( 0 );
1432  }
1433 
1434  ssl->handshake->calc_verify( ssl, hash );
1435 
1436  if( ( ret = ssl_read_record( ssl ) ) != 0 )
1437  {
1438  SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1439  return( ret );
1440  }
1441 
1442  ssl->state++;
1443 
1444  if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1445  {
1446  SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
1448  }
1449 
1450  if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
1451  {
1452  SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
1454  }
1455 
1456  if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
1457  {
1458  /*
1459  * As server we know we either have SSL_HASH_SHA384 or
1460  * SSL_HASH_SHA256
1461  */
1462  if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg ||
1463  ssl->in_msg[5] != SSL_SIG_RSA )
1464  {
1465  SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg for verify message" ) );
1467  }
1468 
1470  {
1471  hashlen = 48;
1472  hash_id = SIG_RSA_SHA384;
1473  }
1474  else
1475  {
1476  hashlen = 32;
1477  hash_id = SIG_RSA_SHA256;
1478  }
1479 
1480  n += 2;
1481  }
1482  else
1483  {
1484  hashlen = 36;
1485  hash_id = SIG_RSA_RAW;
1486  }
1487 
1488  n1 = ssl->session_negotiate->peer_cert->rsa.len;
1489  n2 = ( ssl->in_msg[4 + n] << 8 ) | ssl->in_msg[5 + n];
1490 
1491  if( n + n1 + 6 != ssl->in_hslen || n1 != n2 )
1492  {
1493  SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
1495  }
1496 
1498  hash_id, hashlen, hash, ssl->in_msg + 6 + n );
1499  if( ret != 0 )
1500  {
1501  SSL_DEBUG_RET( 1, "rsa_pkcs1_verify", ret );
1502  return( ret );
1503  }
1504 
1505  SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
1506 
1507  return( 0 );
1508 }
1509 
1510 /*
1511  * SSL handshake -- server side -- single step
1512  */
1514 {
1515  int ret = 0;
1516 
1517  if( ssl->state == SSL_HANDSHAKE_OVER )
1519 
1520  SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
1521 
1522  if( ( ret = ssl_flush_output( ssl ) ) != 0 )
1523  return( ret );
1524 
1525  switch( ssl->state )
1526  {
1527  case SSL_HELLO_REQUEST:
1528  ssl->state = SSL_CLIENT_HELLO;
1529  break;
1530 
1531  /*
1532  * <== ClientHello
1533  */
1534  case SSL_CLIENT_HELLO:
1535  ret = ssl_parse_client_hello( ssl );
1536  break;
1537 
1538  /*
1539  * ==> ServerHello
1540  * Certificate
1541  * ( ServerKeyExchange )
1542  * ( CertificateRequest )
1543  * ServerHelloDone
1544  */
1545  case SSL_SERVER_HELLO:
1546  ret = ssl_write_server_hello( ssl );
1547  break;
1548 
1550  ret = ssl_write_certificate( ssl );
1551  break;
1552 
1554  ret = ssl_write_server_key_exchange( ssl );
1555  break;
1556 
1558  ret = ssl_write_certificate_request( ssl );
1559  break;
1560 
1561  case SSL_SERVER_HELLO_DONE:
1562  ret = ssl_write_server_hello_done( ssl );
1563  break;
1564 
1565  /*
1566  * <== ( Certificate/Alert )
1567  * ClientKeyExchange
1568  * ( CertificateVerify )
1569  * ChangeCipherSpec
1570  * Finished
1571  */
1573  ret = ssl_parse_certificate( ssl );
1574  break;
1575 
1577  ret = ssl_parse_client_key_exchange( ssl );
1578  break;
1579 
1581  ret = ssl_parse_certificate_verify( ssl );
1582  break;
1583 
1585  ret = ssl_parse_change_cipher_spec( ssl );
1586  break;
1587 
1588  case SSL_CLIENT_FINISHED:
1589  ret = ssl_parse_finished( ssl );
1590  break;
1591 
1592  /*
1593  * ==> ChangeCipherSpec
1594  * Finished
1595  */
1597  ret = ssl_write_change_cipher_spec( ssl );
1598  break;
1599 
1600  case SSL_SERVER_FINISHED:
1601  ret = ssl_write_finished( ssl );
1602  break;
1603 
1604  case SSL_FLUSH_BUFFERS:
1605  SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
1606  ssl->state = SSL_HANDSHAKE_WRAPUP;
1607  break;
1608 
1609  case SSL_HANDSHAKE_WRAPUP:
1610  ssl_handshake_wrapup( ssl );
1611  break;
1612 
1613  default:
1614  SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
1616  }
1617 
1618  return( ret );
1619 }
1620 #endif