PolarSSL v1.2.5
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 static int ssl_parse_client_hello( ssl_context *ssl )
195 {
196  int ret;
197  unsigned int i, j;
198  size_t n;
199  unsigned int ciph_len, sess_len;
200  unsigned int comp_len;
201  unsigned int ext_len = 0;
202  unsigned char *buf, *p, *ext;
203  int renegotiation_info_seen = 0;
204  int handshake_failure = 0;
205 
206  SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
207 
208  if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
209  ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
210  {
211  SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
212  return( ret );
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 v3, message type: %d",
220  buf[0] ) );
221  SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
222  ( buf[3] << 8 ) | buf[4] ) );
223  SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
224  buf[1], buf[2] ) );
225 
226  /*
227  * SSLv3 Client Hello
228  *
229  * Record layer:
230  * 0 . 0 message type
231  * 1 . 2 protocol version
232  * 3 . 4 message length
233  */
234  if( buf[0] != SSL_MSG_HANDSHAKE ||
235  buf[1] != SSL_MAJOR_VERSION_3 )
236  {
237  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
239  }
240 
241  n = ( buf[3] << 8 ) | buf[4];
242 
243  if( n < 45 || n > 512 )
244  {
245  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
247  }
248 
249  if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
250  ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
251  {
252  SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
253  return( ret );
254  }
255 
256  buf = ssl->in_msg;
257  if( !ssl->renegotiation )
258  n = ssl->in_left - 5;
259  else
260  n = ssl->in_msglen;
261 
262  ssl->handshake->update_checksum( ssl, buf, n );
263 
264  /*
265  * SSL layer:
266  * 0 . 0 handshake type
267  * 1 . 3 handshake length
268  * 4 . 5 protocol version
269  * 6 . 9 UNIX time()
270  * 10 . 37 random bytes
271  * 38 . 38 session id length
272  * 39 . 38+x session id
273  * 39+x . 40+x ciphersuitelist length
274  * 41+x . .. ciphersuitelist
275  * .. . .. compression alg.
276  * .. . .. extensions
277  */
278  SSL_DEBUG_BUF( 4, "record contents", buf, n );
279 
280  SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
281  buf[0] ) );
282  SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
283  ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
284  SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
285  buf[4], buf[5] ) );
286 
287  /*
288  * Check the handshake type and protocol version
289  */
290  if( buf[0] != SSL_HS_CLIENT_HELLO ||
291  buf[4] != SSL_MAJOR_VERSION_3 )
292  {
293  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
295  }
296 
298  ssl->minor_ver = ( buf[5] <= SSL_MINOR_VERSION_3 )
299  ? buf[5] : SSL_MINOR_VERSION_3;
300 
301  if( ssl->minor_ver < ssl->min_minor_ver )
302  {
303  SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
304  " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
305  ssl->min_major_ver, ssl->min_minor_ver ) );
306 
309 
311  }
312 
313  ssl->max_major_ver = buf[4];
314  ssl->max_minor_ver = buf[5];
315 
316  memcpy( ssl->handshake->randbytes, buf + 6, 32 );
317 
318  /*
319  * Check the handshake message length
320  */
321  if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
322  {
323  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
325  }
326 
327  /*
328  * Check the session length
329  */
330  sess_len = buf[38];
331 
332  if( sess_len > 32 )
333  {
334  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
336  }
337 
338  ssl->session_negotiate->length = sess_len;
339  memset( ssl->session_negotiate->id, 0,
340  sizeof( ssl->session_negotiate->id ) );
341  memcpy( ssl->session_negotiate->id, buf + 39,
342  ssl->session_negotiate->length );
343 
344  /*
345  * Check the ciphersuitelist length
346  */
347  ciph_len = ( buf[39 + sess_len] << 8 )
348  | ( buf[40 + sess_len] );
349 
350  if( ciph_len < 2 || ciph_len > 256 || ( ciph_len % 2 ) != 0 )
351  {
352  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
354  }
355 
356  /*
357  * Check the compression algorithms length
358  */
359  comp_len = buf[41 + sess_len + ciph_len];
360 
361  if( comp_len < 1 || comp_len > 16 )
362  {
363  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
365  }
366 
367  /*
368  * Check the extension length
369  */
370  if( n > 42 + sess_len + ciph_len + comp_len )
371  {
372  ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
373  | ( buf[43 + sess_len + ciph_len + comp_len] );
374 
375  if( ( ext_len > 0 && ext_len < 4 ) ||
376  n != 44 + sess_len + ciph_len + comp_len + ext_len )
377  {
378  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
379  SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
381  }
382  }
383 
385 #if defined(POLARSSL_ZLIB_SUPPORT)
386  for( i = 0; i < comp_len; ++i )
387  {
388  if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
389  {
391  break;
392  }
393  }
394 #endif
395 
396  SSL_DEBUG_BUF( 3, "client hello, random bytes",
397  buf + 6, 32 );
398  SSL_DEBUG_BUF( 3, "client hello, session id",
399  buf + 38, sess_len );
400  SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
401  buf + 41 + sess_len, ciph_len );
402  SSL_DEBUG_BUF( 3, "client hello, compression",
403  buf + 42 + sess_len + ciph_len, comp_len );
404 
405  /*
406  * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
407  */
408  for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
409  {
410  if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
411  {
412  SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
413  if( ssl->renegotiation == SSL_RENEGOTIATION )
414  {
415  SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
416 
417  if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
418  return( ret );
419 
421  }
423  break;
424  }
425  }
426 
427  /*
428  * Search for a matching ciphersuite
429  */
430  for( i = 0; ssl->ciphersuites[i] != 0; i++ )
431  {
432  for( j = 0, p = buf + 41 + sess_len; j < ciph_len;
433  j += 2, p += 2 )
434  {
435  if( p[0] == 0 && p[1] == ssl->ciphersuites[i] )
436  goto have_ciphersuite;
437  }
438  }
439 
440  SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
441 
443 
444 have_ciphersuite:
447 
448  ext = buf + 44 + sess_len + ciph_len + comp_len;
449 
450  while( ext_len )
451  {
452  unsigned int ext_id = ( ( ext[0] << 8 )
453  | ( ext[1] ) );
454  unsigned int ext_size = ( ( ext[2] << 8 )
455  | ( ext[3] ) );
456 
457  if( ext_size + 4 > ext_len )
458  {
459  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
461  }
462  switch( ext_id )
463  {
464  case TLS_EXT_SERVERNAME:
465  SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
466  if( ssl->f_sni == NULL )
467  break;
468 
469  ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
470  if( ret != 0 )
471  return( ret );
472  break;
473 
475  SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
476  renegotiation_info_seen = 1;
477 
478  ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
479  if( ret != 0 )
480  return( ret );
481  break;
482 
483  case TLS_EXT_SIG_ALG:
484  SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
485  if( ssl->renegotiation == SSL_RENEGOTIATION )
486  break;
487 
488  ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
489  if( ret != 0 )
490  return( ret );
491  break;
492 
493  default:
494  SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
495  ext_id ) );
496  }
497 
498  ext_len -= 4 + ext_size;
499  ext += 4 + ext_size;
500 
501  if( ext_len > 0 && ext_len < 4 )
502  {
503  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
505  }
506  }
507 
508  /*
509  * Renegotiation security checks
510  */
513  {
514  SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
515  handshake_failure = 1;
516  }
517  else if( ssl->renegotiation == SSL_RENEGOTIATION &&
519  renegotiation_info_seen == 0 )
520  {
521  SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
522  handshake_failure = 1;
523  }
524  else if( ssl->renegotiation == SSL_RENEGOTIATION &&
527  {
528  SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
529  handshake_failure = 1;
530  }
531  else if( ssl->renegotiation == SSL_RENEGOTIATION &&
533  renegotiation_info_seen == 1 )
534  {
535  SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
536  handshake_failure = 1;
537  }
538 
539  if( handshake_failure == 1 )
540  {
541  if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
542  return( ret );
543 
545  }
546 
547  ssl->in_left = 0;
548  ssl->state++;
549 
550  SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
551 
552  return( 0 );
553 }
554 
555 static int ssl_write_server_hello( ssl_context *ssl )
556 {
557  time_t t;
558  int ret, n;
559  size_t ext_len = 0;
560  unsigned char *buf, *p;
561 
562  SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
563 
564  /*
565  * 0 . 0 handshake type
566  * 1 . 3 handshake length
567  * 4 . 5 protocol version
568  * 6 . 9 UNIX time()
569  * 10 . 37 random bytes
570  */
571  buf = ssl->out_msg;
572  p = buf + 4;
573 
574  *p++ = (unsigned char) ssl->major_ver;
575  *p++ = (unsigned char) ssl->minor_ver;
576 
577  SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
578  buf[4], buf[5] ) );
579 
580  t = time( NULL );
581  *p++ = (unsigned char)( t >> 24 );
582  *p++ = (unsigned char)( t >> 16 );
583  *p++ = (unsigned char)( t >> 8 );
584  *p++ = (unsigned char)( t );
585 
586  SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
587 
588  if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
589  return( ret );
590 
591  p += 28;
592 
593  memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
594 
595  SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
596 
597  /*
598  * 38 . 38 session id length
599  * 39 . 38+n session id
600  * 39+n . 40+n chosen ciphersuite
601  * 41+n . 41+n chosen compression alg.
602  */
603  ssl->session_negotiate->length = n = 32;
604  *p++ = (unsigned char) ssl->session_negotiate->length;
605 
606  if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
607  ssl->f_get_cache == NULL ||
608  ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) != 0 )
609  {
610  /*
611  * Not found, create a new session id
612  */
613  ssl->handshake->resume = 0;
614  ssl->state++;
615 
616  if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
617  n ) ) != 0 )
618  return( ret );
619  }
620  else
621  {
622  /*
623  * Found a matching session, resuming it
624  */
625  ssl->handshake->resume = 1;
627 
628  if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
629  {
630  SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
631  return( ret );
632  }
633  }
634 
635  memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
636  p += ssl->session_negotiate->length;
637 
638  SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
639  SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
640  SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
641  ssl->handshake->resume ? "a" : "no" ) );
642 
643  *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
644  *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
645  *p++ = (unsigned char)( ssl->session_negotiate->compression );
646 
647  SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d",
648  ssl->session_negotiate->ciphersuite ) );
649  SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d",
650  ssl->session_negotiate->compression ) );
651 
652  SSL_DEBUG_MSG( 3, ( "server hello, prepping for secure renegotiation extension" ) );
653  ext_len += 5 + ssl->verify_data_len * 2;
654 
655  SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d",
656  ext_len ) );
657 
658  *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
659  *p++ = (unsigned char)( ( ext_len ) & 0xFF );
660 
661  /*
662  * Secure renegotiation
663  */
664  SSL_DEBUG_MSG( 3, ( "client hello, secure renegotiation extension" ) );
665 
666  *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
667  *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
668 
669  *p++ = 0x00;
670  *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
671  *p++ = ssl->verify_data_len * 2 & 0xFF;
672 
673  memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
674  p += ssl->verify_data_len;
675  memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
676  p += ssl->verify_data_len;
677 
678  ssl->out_msglen = p - buf;
680  ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
681 
682  ret = ssl_write_record( ssl );
683 
684  SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
685 
686  return( ret );
687 }
688 
689 static int ssl_write_certificate_request( ssl_context *ssl )
690 {
691  int ret;
692  size_t n = 0, dn_size, total_dn_size;
693  unsigned char *buf, *p;
694  const x509_cert *crt;
695 
696  SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
697 
698  ssl->state++;
699 
700  if( ssl->authmode == SSL_VERIFY_NONE )
701  {
702  SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
703  return( 0 );
704  }
705 
706  /*
707  * 0 . 0 handshake type
708  * 1 . 3 handshake length
709  * 4 . 4 cert type count
710  * 5 .. m-1 cert types
711  * m .. m+1 sig alg length (TLS 1.2 only)
712  * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
713  * n .. n+1 length of all DNs
714  * n+2 .. n+3 length of DN 1
715  * n+4 .. ... Distinguished Name #1
716  * ... .. ... length of DN 2, etc.
717  */
718  buf = ssl->out_msg;
719  p = buf + 4;
720 
721  /*
722  * At the moment, only RSA certificates are supported
723  */
724  *p++ = 1;
725  *p++ = SSL_CERT_TYPE_RSA_SIGN;
726 
727  /*
728  * Add signature_algorithms for verify (TLS 1.2)
729  * Only add current running algorithm that is already required for
730  * requested ciphersuite.
731  *
732  * Length is always 2
733  */
734  if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
735  {
737 
738  *p++ = 0;
739  *p++ = 2;
740 
743  {
745  }
746 
747  *p++ = ssl->handshake->verify_sig_alg;
748  *p++ = SSL_SIG_RSA;
749 
750  n += 4;
751  }
752 
753  p += 2;
754  crt = ssl->ca_chain;
755 
756  total_dn_size = 0;
757  while( crt != NULL )
758  {
759  if( p - buf > 4096 )
760  break;
761 
762  dn_size = crt->subject_raw.len;
763  *p++ = (unsigned char)( dn_size >> 8 );
764  *p++ = (unsigned char)( dn_size );
765  memcpy( p, crt->subject_raw.p, dn_size );
766  p += dn_size;
767 
768  SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
769 
770  total_dn_size += 2 + dn_size;
771  crt = crt->next;
772  }
773 
774  ssl->out_msglen = p - buf;
777  ssl->out_msg[6 + n] = (unsigned char)( total_dn_size >> 8 );
778  ssl->out_msg[7 + n] = (unsigned char)( total_dn_size );
779 
780  ret = ssl_write_record( ssl );
781 
782  SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
783 
784  return( ret );
785 }
786 
787 static int ssl_write_server_key_exchange( ssl_context *ssl )
788 {
789 #if defined(POLARSSL_DHM_C)
790  int ret;
791  size_t n, rsa_key_len = 0;
792  unsigned char hash[64];
793  int hash_id = 0;
794  unsigned int hashlen = 0;
795 #endif
796 
797  SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
798 
811  {
812  SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
813  ssl->state++;
814  return( 0 );
815  }
816 
817 #if !defined(POLARSSL_DHM_C)
818  SSL_DEBUG_MSG( 1, ( "support for dhm is not available" ) );
820 #else
821 
822  if( ssl->rsa_key == NULL )
823  {
824  SSL_DEBUG_MSG( 1, ( "got no private key" ) );
826  }
827 
828  /*
829  * Ephemeral DH parameters:
830  *
831  * struct {
832  * opaque dh_p<1..2^16-1>;
833  * opaque dh_g<1..2^16-1>;
834  * opaque dh_Ys<1..2^16-1>;
835  * } ServerDHParams;
836  */
837  if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
838  ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
839  {
840  SSL_DEBUG_RET( 1, "mpi_copy", ret );
841  return( ret );
842  }
843 
844  if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
845  mpi_size( &ssl->handshake->dhm_ctx.P ),
846  ssl->out_msg + 4,
847  &n, ssl->f_rng, ssl->p_rng ) ) != 0 )
848  {
849  SSL_DEBUG_RET( 1, "dhm_make_params", ret );
850  return( ret );
851  }
852 
853  SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
854  SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
855  SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
856  SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
857 
858  if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
859  {
862 
863  /*
864  * digitally-signed struct {
865  * opaque md5_hash[16];
866  * opaque sha_hash[20];
867  * };
868  *
869  * md5_hash
870  * MD5(ClientHello.random + ServerHello.random
871  * + ServerParams);
872  * sha_hash
873  * SHA(ClientHello.random + ServerHello.random
874  * + ServerParams);
875  */
876  md5_starts( &md5 );
877  md5_update( &md5, ssl->handshake->randbytes, 64 );
878  md5_update( &md5, ssl->out_msg + 4, n );
879  md5_finish( &md5, hash );
880 
881  sha1_starts( &sha1 );
882  sha1_update( &sha1, ssl->handshake->randbytes, 64 );
883  sha1_update( &sha1, ssl->out_msg + 4, n );
884  sha1_finish( &sha1, hash + 16 );
885 
886  hashlen = 36;
887  hash_id = SIG_RSA_RAW;
888  }
889  else
890  {
891  /*
892  * digitally-signed struct {
893  * opaque client_random[32];
894  * opaque server_random[32];
895  * ServerDHParams params;
896  * };
897  */
898 #if defined(POLARSSL_SHA4_C)
899  if( ssl->handshake->sig_alg == SSL_HASH_SHA512 )
900  {
902 
903  sha4_starts( &sha4, 0 );
904  sha4_update( &sha4, ssl->handshake->randbytes, 64 );
905  sha4_update( &sha4, ssl->out_msg + 4, n );
906  sha4_finish( &sha4, hash );
907 
908  hashlen = 64;
909  hash_id = SIG_RSA_SHA512;
910  }
911  else if( ssl->handshake->sig_alg == SSL_HASH_SHA384 )
912  {
914 
915  sha4_starts( &sha4, 1 );
916  sha4_update( &sha4, ssl->handshake->randbytes, 64 );
917  sha4_update( &sha4, ssl->out_msg + 4, n );
918  sha4_finish( &sha4, hash );
919 
920  hashlen = 48;
921  hash_id = SIG_RSA_SHA384;
922  }
923  else
924 #endif
925 #if defined(POLARSSL_SHA2_C)
926  if( ssl->handshake->sig_alg == SSL_HASH_SHA256 )
927  {
929 
930  sha2_starts( &sha2, 0 );
931  sha2_update( &sha2, ssl->handshake->randbytes, 64 );
932  sha2_update( &sha2, ssl->out_msg + 4, n );
933  sha2_finish( &sha2, hash );
934 
935  hashlen = 32;
936  hash_id = SIG_RSA_SHA256;
937  }
938  else if( ssl->handshake->sig_alg == SSL_HASH_SHA224 )
939  {
941 
942  sha2_starts( &sha2, 1 );
943  sha2_update( &sha2, ssl->handshake->randbytes, 64 );
944  sha2_update( &sha2, ssl->out_msg + 4, n );
945  sha2_finish( &sha2, hash );
946 
947  hashlen = 24;
948  hash_id = SIG_RSA_SHA224;
949  }
950  else
951 #endif
952  if( ssl->handshake->sig_alg == SSL_HASH_SHA1 )
953  {
955 
956  sha1_starts( &sha1 );
957  sha1_update( &sha1, ssl->handshake->randbytes, 64 );
958  sha1_update( &sha1, ssl->out_msg + 4, n );
959  sha1_finish( &sha1, hash );
960 
961  hashlen = 20;
962  hash_id = SIG_RSA_SHA1;
963  }
964  else if( ssl->handshake->sig_alg == SSL_HASH_MD5 )
965  {
967 
968  md5_starts( &md5 );
969  md5_update( &md5, ssl->handshake->randbytes, 64 );
970  md5_update( &md5, ssl->out_msg + 4, n );
971  md5_finish( &md5, hash );
972 
973  hashlen = 16;
974  hash_id = SIG_RSA_MD5;
975  }
976  }
977 
978  SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
979 
980  if ( ssl->rsa_key )
981  rsa_key_len = ssl->rsa_key_len( ssl->rsa_key );
982 
983  if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
984  {
985  ssl->out_msg[4 + n] = ssl->handshake->sig_alg;
986  ssl->out_msg[5 + n] = SSL_SIG_RSA;
987 
988  n += 2;
989  }
990 
991  ssl->out_msg[4 + n] = (unsigned char)( rsa_key_len >> 8 );
992  ssl->out_msg[5 + n] = (unsigned char)( rsa_key_len );
993 
994  if ( ssl->rsa_key )
995  {
996  ret = ssl->rsa_sign( ssl->rsa_key, ssl->f_rng, ssl->p_rng,
997  RSA_PRIVATE,
998  hash_id, hashlen, hash,
999  ssl->out_msg + 6 + n );
1000  }
1001 
1002  if( ret != 0 )
1003  {
1004  SSL_DEBUG_RET( 1, "pkcs1_sign", ret );
1005  return( ret );
1006  }
1007 
1008  SSL_DEBUG_BUF( 3, "my RSA sig", ssl->out_msg + 6 + n, rsa_key_len );
1009 
1010  ssl->out_msglen = 6 + n + rsa_key_len;
1013 
1014  ssl->state++;
1015 
1016  if( ( ret = ssl_write_record( ssl ) ) != 0 )
1017  {
1018  SSL_DEBUG_RET( 1, "ssl_write_record", ret );
1019  return( ret );
1020  }
1021 
1022  SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
1023 
1024  return( 0 );
1025 #endif
1026 }
1027 
1028 static int ssl_write_server_hello_done( ssl_context *ssl )
1029 {
1030  int ret;
1031 
1032  SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
1033 
1034  ssl->out_msglen = 4;
1037 
1038  ssl->state++;
1039 
1040  if( ( ret = ssl_write_record( ssl ) ) != 0 )
1041  {
1042  SSL_DEBUG_RET( 1, "ssl_write_record", ret );
1043  return( ret );
1044  }
1045 
1046  SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
1047 
1048  return( 0 );
1049 }
1050 
1051 static int ssl_parse_client_key_exchange( ssl_context *ssl )
1052 {
1053  int ret;
1054  size_t i, n = 0;
1055 
1056  SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
1057 
1058  if( ( ret = ssl_read_record( ssl ) ) != 0 )
1059  {
1060  SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1061  return( ret );
1062  }
1063 
1064  if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1065  {
1066  SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
1068  }
1069 
1070  if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
1071  {
1072  SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
1074  }
1075 
1088  {
1089 #if !defined(POLARSSL_DHM_C)
1090  SSL_DEBUG_MSG( 1, ( "support for dhm is not available" ) );
1092 #else
1093  /*
1094  * Receive G^Y mod P, premaster = (G^Y)^X mod P
1095  */
1096  n = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
1097 
1098  if( n < 1 || n > ssl->handshake->dhm_ctx.len ||
1099  n + 6 != ssl->in_hslen )
1100  {
1101  SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
1103  }
1104 
1105  if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx,
1106  ssl->in_msg + 6, n ) ) != 0 )
1107  {
1108  SSL_DEBUG_RET( 1, "dhm_read_public", ret );
1110  }
1111 
1112  SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
1113 
1114  ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
1115 
1116  if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
1117  ssl->handshake->premaster,
1118  &ssl->handshake->pmslen ) ) != 0 )
1119  {
1120  SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
1122  }
1123 
1124  SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1125 #endif
1126  }
1127  else
1128  {
1129  if( ssl->rsa_key == NULL )
1130  {
1131  SSL_DEBUG_MSG( 1, ( "got no private key" ) );
1133  }
1134 
1135  /*
1136  * Decrypt the premaster using own private RSA key
1137  */
1138  i = 4;
1139  if( ssl->rsa_key )
1140  n = ssl->rsa_key_len( ssl->rsa_key );
1141  ssl->handshake->pmslen = 48;
1142 
1143  if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
1144  {
1145  i += 2;
1146  if( ssl->in_msg[4] != ( ( n >> 8 ) & 0xFF ) ||
1147  ssl->in_msg[5] != ( ( n ) & 0xFF ) )
1148  {
1149  SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
1151  }
1152  }
1153 
1154  if( ssl->in_hslen != i + n )
1155  {
1156  SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
1158  }
1159 
1160  if( ssl->rsa_key ) {
1161  ret = ssl->rsa_decrypt( ssl->rsa_key, RSA_PRIVATE,
1162  &ssl->handshake->pmslen,
1163  ssl->in_msg + i,
1164  ssl->handshake->premaster,
1165  sizeof(ssl->handshake->premaster) );
1166  }
1167 
1168  if( ret != 0 || ssl->handshake->pmslen != 48 ||
1169  ssl->handshake->premaster[0] != ssl->max_major_ver ||
1170  ssl->handshake->premaster[1] != ssl->max_minor_ver )
1171  {
1172  SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
1173 
1174  /*
1175  * Protection against Bleichenbacher's attack:
1176  * invalid PKCS#1 v1.5 padding must not cause
1177  * the connection to end immediately; instead,
1178  * send a bad_record_mac later in the handshake.
1179  */
1180  ssl->handshake->pmslen = 48;
1181 
1182  ret = ssl->f_rng( ssl->p_rng, ssl->handshake->premaster,
1183  ssl->handshake->pmslen );
1184  if( ret != 0 )
1185  return( ret );
1186  }
1187  }
1188 
1189  if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1190  {
1191  SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1192  return( ret );
1193  }
1194 
1195  ssl->state++;
1196 
1197  SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
1198 
1199  return( 0 );
1200 }
1201 
1202 static int ssl_parse_certificate_verify( ssl_context *ssl )
1203 {
1204  int ret;
1205  size_t n = 0, n1, n2;
1206  unsigned char hash[48];
1207  int hash_id;
1208  unsigned int hashlen;
1209 
1210  SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
1211 
1212  if( ssl->session_negotiate->peer_cert == NULL )
1213  {
1214  SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
1215  ssl->state++;
1216  return( 0 );
1217  }
1218 
1219  ssl->handshake->calc_verify( ssl, hash );
1220 
1221  if( ( ret = ssl_read_record( ssl ) ) != 0 )
1222  {
1223  SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1224  return( ret );
1225  }
1226 
1227  ssl->state++;
1228 
1229  if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1230  {
1231  SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
1233  }
1234 
1235  if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
1236  {
1237  SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
1239  }
1240 
1241  if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
1242  {
1243  /*
1244  * As server we know we either have SSL_HASH_SHA384 or
1245  * SSL_HASH_SHA256
1246  */
1247  if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg ||
1248  ssl->in_msg[5] != SSL_SIG_RSA )
1249  {
1250  SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg for verify message" ) );
1252  }
1253 
1255  {
1256  hashlen = 48;
1257  hash_id = SIG_RSA_SHA384;
1258  }
1259  else
1260  {
1261  hashlen = 32;
1262  hash_id = SIG_RSA_SHA256;
1263  }
1264 
1265  n += 2;
1266  }
1267  else
1268  {
1269  hashlen = 36;
1270  hash_id = SIG_RSA_RAW;
1271  }
1272 
1273  n1 = ssl->session_negotiate->peer_cert->rsa.len;
1274  n2 = ( ssl->in_msg[4 + n] << 8 ) | ssl->in_msg[5 + n];
1275 
1276  if( n + n1 + 6 != ssl->in_hslen || n1 != n2 )
1277  {
1278  SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
1280  }
1281 
1283  hash_id, hashlen, hash, ssl->in_msg + 6 + n );
1284  if( ret != 0 )
1285  {
1286  SSL_DEBUG_RET( 1, "rsa_pkcs1_verify", ret );
1287  return( ret );
1288  }
1289 
1290  SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
1291 
1292  return( 0 );
1293 }
1294 
1295 /*
1296  * SSL handshake -- server side -- single step
1297  */
1299 {
1300  int ret = 0;
1301 
1302  if( ssl->state == SSL_HANDSHAKE_OVER )
1304 
1305  SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
1306 
1307  if( ( ret = ssl_flush_output( ssl ) ) != 0 )
1308  return( ret );
1309 
1310  switch( ssl->state )
1311  {
1312  case SSL_HELLO_REQUEST:
1313  ssl->state = SSL_CLIENT_HELLO;
1314  break;
1315 
1316  /*
1317  * <== ClientHello
1318  */
1319  case SSL_CLIENT_HELLO:
1320  ret = ssl_parse_client_hello( ssl );
1321  break;
1322 
1323  /*
1324  * ==> ServerHello
1325  * Certificate
1326  * ( ServerKeyExchange )
1327  * ( CertificateRequest )
1328  * ServerHelloDone
1329  */
1330  case SSL_SERVER_HELLO:
1331  ret = ssl_write_server_hello( ssl );
1332  break;
1333 
1335  ret = ssl_write_certificate( ssl );
1336  break;
1337 
1339  ret = ssl_write_server_key_exchange( ssl );
1340  break;
1341 
1343  ret = ssl_write_certificate_request( ssl );
1344  break;
1345 
1346  case SSL_SERVER_HELLO_DONE:
1347  ret = ssl_write_server_hello_done( ssl );
1348  break;
1349 
1350  /*
1351  * <== ( Certificate/Alert )
1352  * ClientKeyExchange
1353  * ( CertificateVerify )
1354  * ChangeCipherSpec
1355  * Finished
1356  */
1358  ret = ssl_parse_certificate( ssl );
1359  break;
1360 
1362  ret = ssl_parse_client_key_exchange( ssl );
1363  break;
1364 
1366  ret = ssl_parse_certificate_verify( ssl );
1367  break;
1368 
1370  ret = ssl_parse_change_cipher_spec( ssl );
1371  break;
1372 
1373  case SSL_CLIENT_FINISHED:
1374  ret = ssl_parse_finished( ssl );
1375  break;
1376 
1377  /*
1378  * ==> ChangeCipherSpec
1379  * Finished
1380  */
1382  ret = ssl_write_change_cipher_spec( ssl );
1383  break;
1384 
1385  case SSL_SERVER_FINISHED:
1386  ret = ssl_write_finished( ssl );
1387  break;
1388 
1389  case SSL_FLUSH_BUFFERS:
1390  SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
1391  ssl->state = SSL_HANDSHAKE_WRAPUP;
1392  break;
1393 
1394  case SSL_HANDSHAKE_WRAPUP:
1395  ssl_handshake_wrapup( ssl );
1396  break;
1397 
1398  default:
1399  SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
1401  }
1402 
1403  return( ret );
1404 }
1405 #endif