PolarSSL v1.1.4
ssl_srv.c
Go to the documentation of this file.
1 /*
2  * SSLv3/TLSv1 server-side functions
3  *
4  * Copyright (C) 2006-2010, 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 #if defined(POLARSSL_PKCS11_C)
34 #include "polarssl/pkcs11.h"
35 #endif /* defined(POLARSSL_PKCS11_C) */
36 
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <time.h>
40 
41 static int ssl_parse_client_hello( ssl_context *ssl )
42 {
43  int ret;
44  unsigned int i, j;
45  size_t n;
46  unsigned int ciph_len, sess_len;
47  unsigned int chal_len, comp_len;
48  unsigned char *buf, *p;
49 
50  SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
51 
52  if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
53  {
54  SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
55  return( ret );
56  }
57 
58  buf = ssl->in_hdr;
59 
60  if( ( buf[0] & 0x80 ) != 0 )
61  {
62  SSL_DEBUG_BUF( 4, "record header", buf, 5 );
63 
64  SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
65  buf[2] ) );
66  SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
67  ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
68  SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
69  buf[3], buf[4] ) );
70 
71  /*
72  * SSLv2 Client Hello
73  *
74  * Record layer:
75  * 0 . 1 message length
76  *
77  * SSL layer:
78  * 2 . 2 message type
79  * 3 . 4 protocol version
80  */
81  if( buf[2] != SSL_HS_CLIENT_HELLO ||
82  buf[3] != SSL_MAJOR_VERSION_3 )
83  {
84  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
86  }
87 
88  n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
89 
90  if( n < 17 || n > 512 )
91  {
92  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
94  }
95 
96  ssl->max_major_ver = buf[3];
97  ssl->max_minor_ver = buf[4];
98 
100  ssl->minor_ver = ( buf[4] <= SSL_MINOR_VERSION_2 )
101  ? buf[4] : SSL_MINOR_VERSION_2;
102 
103  if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
104  {
105  SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
106  return( ret );
107  }
108 
109  md5_update( &ssl->fin_md5 , buf + 2, n );
110  sha1_update( &ssl->fin_sha1, buf + 2, n );
111 
112  buf = ssl->in_msg;
113  n = ssl->in_left - 5;
114 
115  /*
116  * 0 . 1 ciphersuitelist length
117  * 2 . 3 session id length
118  * 4 . 5 challenge length
119  * 6 . .. ciphersuitelist
120  * .. . .. session id
121  * .. . .. challenge
122  */
123  SSL_DEBUG_BUF( 4, "record contents", buf, n );
124 
125  ciph_len = ( buf[0] << 8 ) | buf[1];
126  sess_len = ( buf[2] << 8 ) | buf[3];
127  chal_len = ( buf[4] << 8 ) | buf[5];
128 
129  SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
130  ciph_len, sess_len, chal_len ) );
131 
132  /*
133  * Make sure each parameter length is valid
134  */
135  if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
136  {
137  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
139  }
140 
141  if( sess_len > 32 )
142  {
143  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
145  }
146 
147  if( chal_len < 8 || chal_len > 32 )
148  {
149  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
151  }
152 
153  if( n != 6 + ciph_len + sess_len + chal_len )
154  {
155  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
157  }
158 
159  SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
160  buf + 6, ciph_len );
161  SSL_DEBUG_BUF( 3, "client hello, session id",
162  buf + 6 + ciph_len, sess_len );
163  SSL_DEBUG_BUF( 3, "client hello, challenge",
164  buf + 6 + ciph_len + sess_len, chal_len );
165 
166  p = buf + 6 + ciph_len;
167  ssl->session->length = sess_len;
168  memset( ssl->session->id, 0, sizeof( ssl->session->id ) );
169  memcpy( ssl->session->id, p, ssl->session->length );
170 
171  p += sess_len;
172  memset( ssl->randbytes, 0, 64 );
173  memcpy( ssl->randbytes + 32 - chal_len, p, chal_len );
174 
175  for( i = 0; ssl->ciphersuites[i] != 0; i++ )
176  {
177  for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
178  {
179  if( p[0] == 0 &&
180  p[1] == 0 &&
181  p[2] == ssl->ciphersuites[i] )
182  goto have_ciphersuite;
183  }
184  }
185  }
186  else
187  {
188  SSL_DEBUG_BUF( 4, "record header", buf, 5 );
189 
190  SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
191  buf[0] ) );
192  SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
193  ( buf[3] << 8 ) | buf[4] ) );
194  SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
195  buf[1], buf[2] ) );
196 
197  /*
198  * SSLv3 Client Hello
199  *
200  * Record layer:
201  * 0 . 0 message type
202  * 1 . 2 protocol version
203  * 3 . 4 message length
204  */
205  if( buf[0] != SSL_MSG_HANDSHAKE ||
206  buf[1] != SSL_MAJOR_VERSION_3 )
207  {
208  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
210  }
211 
212  n = ( buf[3] << 8 ) | buf[4];
213 
214  if( n < 45 || n > 512 )
215  {
216  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
218  }
219 
220  if( ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
221  {
222  SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
223  return( ret );
224  }
225 
226  buf = ssl->in_msg;
227  n = ssl->in_left - 5;
228 
229  md5_update( &ssl->fin_md5 , buf, n );
230  sha1_update( &ssl->fin_sha1, buf, n );
231 
232  /*
233  * SSL layer:
234  * 0 . 0 handshake type
235  * 1 . 3 handshake length
236  * 4 . 5 protocol version
237  * 6 . 9 UNIX time()
238  * 10 . 37 random bytes
239  * 38 . 38 session id length
240  * 39 . 38+x session id
241  * 39+x . 40+x ciphersuitelist length
242  * 41+x . .. ciphersuitelist
243  * .. . .. compression alg.
244  * .. . .. extensions
245  */
246  SSL_DEBUG_BUF( 4, "record contents", buf, n );
247 
248  SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
249  buf[0] ) );
250  SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
251  ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
252  SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
253  buf[4], buf[5] ) );
254 
255  /*
256  * Check the handshake type and protocol version
257  */
258  if( buf[0] != SSL_HS_CLIENT_HELLO ||
259  buf[4] != SSL_MAJOR_VERSION_3 )
260  {
261  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
263  }
264 
266  ssl->minor_ver = ( buf[5] <= SSL_MINOR_VERSION_2 )
267  ? buf[5] : SSL_MINOR_VERSION_2;
268 
269  ssl->max_major_ver = buf[4];
270  ssl->max_minor_ver = buf[5];
271 
272  memcpy( ssl->randbytes, buf + 6, 32 );
273 
274  /*
275  * Check the handshake message length
276  */
277  if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
278  {
279  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
281  }
282 
283  /*
284  * Check the session length
285  */
286  sess_len = buf[38];
287 
288  if( sess_len > 32 )
289  {
290  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
292  }
293 
294  ssl->session->length = sess_len;
295  memset( ssl->session->id, 0, sizeof( ssl->session->id ) );
296  memcpy( ssl->session->id, buf + 39 , ssl->session->length );
297 
298  /*
299  * Check the ciphersuitelist length
300  */
301  ciph_len = ( buf[39 + sess_len] << 8 )
302  | ( buf[40 + sess_len] );
303 
304  if( ciph_len < 2 || ciph_len > 256 || ( ciph_len % 2 ) != 0 )
305  {
306  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
308  }
309 
310  /*
311  * Check the compression algorithms length
312  */
313  comp_len = buf[41 + sess_len + ciph_len];
314 
315  if( comp_len < 1 || comp_len > 16 )
316  {
317  SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
319  }
320 
321  SSL_DEBUG_BUF( 3, "client hello, random bytes",
322  buf + 6, 32 );
323  SSL_DEBUG_BUF( 3, "client hello, session id",
324  buf + 38, sess_len );
325  SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
326  buf + 41 + sess_len, ciph_len );
327  SSL_DEBUG_BUF( 3, "client hello, compression",
328  buf + 42 + sess_len + ciph_len, comp_len );
329 
330  /*
331  * Search for a matching ciphersuite
332  */
333  for( i = 0; ssl->ciphersuites[i] != 0; i++ )
334  {
335  for( j = 0, p = buf + 41 + sess_len; j < ciph_len;
336  j += 2, p += 2 )
337  {
338  if( p[0] == 0 && p[1] == ssl->ciphersuites[i] )
339  goto have_ciphersuite;
340  }
341  }
342  }
343 
344  SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
345 
347 
348 have_ciphersuite:
349 
350  ssl->session->ciphersuite = ssl->ciphersuites[i];
351  ssl->in_left = 0;
352  ssl->state++;
353 
354  SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
355 
356  return( 0 );
357 }
358 
359 static int ssl_write_server_hello( ssl_context *ssl )
360 {
361  time_t t;
362  int ret, n;
363  unsigned char *buf, *p;
364 
365  SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
366 
367  /*
368  * 0 . 0 handshake type
369  * 1 . 3 handshake length
370  * 4 . 5 protocol version
371  * 6 . 9 UNIX time()
372  * 10 . 37 random bytes
373  */
374  buf = ssl->out_msg;
375  p = buf + 4;
376 
377  *p++ = (unsigned char) ssl->major_ver;
378  *p++ = (unsigned char) ssl->minor_ver;
379 
380  SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
381  buf[4], buf[5] ) );
382 
383  t = time( NULL );
384  *p++ = (unsigned char)( t >> 24 );
385  *p++ = (unsigned char)( t >> 16 );
386  *p++ = (unsigned char)( t >> 8 );
387  *p++ = (unsigned char)( t );
388 
389  SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
390 
391  if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
392  return( ret );
393 
394  p += 28;
395 
396  memcpy( ssl->randbytes + 32, buf + 6, 32 );
397 
398  SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
399 
400  /*
401  * 38 . 38 session id length
402  * 39 . 38+n session id
403  * 39+n . 40+n chosen ciphersuite
404  * 41+n . 41+n chosen compression alg.
405  */
406  ssl->session->length = n = 32;
407  *p++ = (unsigned char) ssl->session->length;
408 
409  if( ssl->s_get == NULL ||
410  ssl->s_get( ssl ) != 0 )
411  {
412  /*
413  * Not found, create a new session id
414  */
415  ssl->resume = 0;
416  ssl->state++;
417 
418  if( ssl->session == NULL )
419  {
420  SSL_DEBUG_MSG( 1, ( "No session struct set" ) );
422  }
423 
424  if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session->id, n ) ) != 0 )
425  return( ret );
426  }
427  else
428  {
429  /*
430  * Found a matching session, resume it
431  */
432  ssl->resume = 1;
434 
435  if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
436  {
437  SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
438  return( ret );
439  }
440  }
441 
442  memcpy( p, ssl->session->id, ssl->session->length );
443  p += ssl->session->length;
444 
445  SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
446  SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
447  SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
448  ssl->resume ? "a" : "no" ) );
449 
450  *p++ = (unsigned char)( ssl->session->ciphersuite >> 8 );
451  *p++ = (unsigned char)( ssl->session->ciphersuite );
452  *p++ = SSL_COMPRESS_NULL;
453 
454  SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d",
455  ssl->session->ciphersuite ) );
456  SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", 0 ) );
457 
458  ssl->out_msglen = p - buf;
460  ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
461 
462  ret = ssl_write_record( ssl );
463 
464  SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
465 
466  return( ret );
467 }
468 
469 static int ssl_write_certificate_request( ssl_context *ssl )
470 {
471  int ret;
472  size_t n;
473  unsigned char *buf, *p;
474  const x509_cert *crt;
475 
476  SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
477 
478  ssl->state++;
479 
480  if( ssl->authmode == SSL_VERIFY_NONE )
481  {
482  SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
483  return( 0 );
484  }
485 
486  /*
487  * 0 . 0 handshake type
488  * 1 . 3 handshake length
489  * 4 . 4 cert type count
490  * 5 .. n-1 cert types
491  * n .. n+1 length of all DNs
492  * n+2 .. n+3 length of DN 1
493  * n+4 .. ... Distinguished Name #1
494  * ... .. ... length of DN 2, etc.
495  */
496  buf = ssl->out_msg;
497  p = buf + 4;
498 
499  /*
500  * At the moment, only RSA certificates are supported
501  */
502  *p++ = 1;
503  *p++ = 1;
504 
505  p += 2;
506  crt = ssl->ca_chain;
507 
508  while( crt != NULL )
509  {
510  if( p - buf > 4096 )
511  break;
512 
513  n = crt->subject_raw.len;
514  *p++ = (unsigned char)( n >> 8 );
515  *p++ = (unsigned char)( n );
516  memcpy( p, crt->subject_raw.p, n );
517 
518  SSL_DEBUG_BUF( 3, "requested DN", p, n );
519  p += n; crt = crt->next;
520  }
521 
522  ssl->out_msglen = n = p - buf;
525  ssl->out_msg[6] = (unsigned char)( ( n - 8 ) >> 8 );
526  ssl->out_msg[7] = (unsigned char)( ( n - 8 ) );
527 
528  ret = ssl_write_record( ssl );
529 
530  SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
531 
532  return( ret );
533 }
534 
535 static int ssl_write_server_key_exchange( ssl_context *ssl )
536 {
537 #if defined(POLARSSL_DHM_C)
538  int ret;
539  size_t n, rsa_key_len = 0;
540  unsigned char hash[36];
543 #endif
544 
545  SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
546 
552  {
553  SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
554  ssl->state++;
555  return( 0 );
556  }
557 
558 #if !defined(POLARSSL_DHM_C)
559  SSL_DEBUG_MSG( 1, ( "support for dhm is not available" ) );
561 #else
562 
563  if( ssl->rsa_key == NULL )
564  {
565 #if defined(POLARSSL_PKCS11_C)
566  if( ssl->pkcs11_key == NULL )
567  {
568 #endif /* defined(POLARSSL_PKCS11_C) */
569  SSL_DEBUG_MSG( 1, ( "got no private key" ) );
571 #if defined(POLARSSL_PKCS11_C)
572  }
573 #endif /* defined(POLARSSL_PKCS11_C) */
574  }
575 
576  /*
577  * Ephemeral DH parameters:
578  *
579  * struct {
580  * opaque dh_p<1..2^16-1>;
581  * opaque dh_g<1..2^16-1>;
582  * opaque dh_Ys<1..2^16-1>;
583  * } ServerDHParams;
584  */
585  if( ( ret = dhm_make_params( &ssl->dhm_ctx, 256, ssl->out_msg + 4,
586  &n, ssl->f_rng, ssl->p_rng ) ) != 0 )
587  {
588  SSL_DEBUG_RET( 1, "dhm_make_params", ret );
589  return( ret );
590  }
591 
592  SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->dhm_ctx.X );
593  SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->dhm_ctx.P );
594  SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->dhm_ctx.G );
595  SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->dhm_ctx.GX );
596 
597  /*
598  * digitally-signed struct {
599  * opaque md5_hash[16];
600  * opaque sha_hash[20];
601  * };
602  *
603  * md5_hash
604  * MD5(ClientHello.random + ServerHello.random
605  * + ServerParams);
606  * sha_hash
607  * SHA(ClientHello.random + ServerHello.random
608  * + ServerParams);
609  */
610  md5_starts( &md5 );
611  md5_update( &md5, ssl->randbytes, 64 );
612  md5_update( &md5, ssl->out_msg + 4, n );
613  md5_finish( &md5, hash );
614 
615  sha1_starts( &sha1 );
616  sha1_update( &sha1, ssl->randbytes, 64 );
617  sha1_update( &sha1, ssl->out_msg + 4, n );
618  sha1_finish( &sha1, hash + 16 );
619 
620  SSL_DEBUG_BUF( 3, "parameters hash", hash, 36 );
621 
622  if ( ssl->rsa_key )
623  rsa_key_len = ssl->rsa_key->len;
624 #if defined(POLARSSL_PKCS11_C)
625  else
626  rsa_key_len = ssl->pkcs11_key->len;
627 #endif /* defined(POLARSSL_PKCS11_C) */
628 
629  ssl->out_msg[4 + n] = (unsigned char)( rsa_key_len >> 8 );
630  ssl->out_msg[5 + n] = (unsigned char)( rsa_key_len );
631 
632  if ( ssl->rsa_key )
633  {
634  ret = rsa_pkcs1_sign( ssl->rsa_key, ssl->f_rng, ssl->p_rng,
635  RSA_PRIVATE,
636  SIG_RSA_RAW, 36, hash, ssl->out_msg + 6 + n );
637  }
638 #if defined(POLARSSL_PKCS11_C)
639  else {
640  ret = pkcs11_sign( ssl->pkcs11_key, RSA_PRIVATE,
641  SIG_RSA_RAW, 36, hash, ssl->out_msg + 6 + n );
642  }
643 #endif /* defined(POLARSSL_PKCS11_C) */
644 
645  if( ret != 0 )
646  {
647  SSL_DEBUG_RET( 1, "pkcs1_sign", ret );
648  return( ret );
649  }
650 
651  SSL_DEBUG_BUF( 3, "my RSA sig", ssl->out_msg + 6 + n, rsa_key_len );
652 
653  ssl->out_msglen = 6 + n + rsa_key_len;
656 
657  ssl->state++;
658 
659  if( ( ret = ssl_write_record( ssl ) ) != 0 )
660  {
661  SSL_DEBUG_RET( 1, "ssl_write_record", ret );
662  return( ret );
663  }
664 
665  SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
666 
667  return( 0 );
668 #endif
669 }
670 
671 static int ssl_write_server_hello_done( ssl_context *ssl )
672 {
673  int ret;
674 
675  SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
676 
677  ssl->out_msglen = 4;
680 
681  ssl->state++;
682 
683  if( ( ret = ssl_write_record( ssl ) ) != 0 )
684  {
685  SSL_DEBUG_RET( 1, "ssl_write_record", ret );
686  return( ret );
687  }
688 
689  SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
690 
691  return( 0 );
692 }
693 
694 static int ssl_parse_client_key_exchange( ssl_context *ssl )
695 {
696  int ret;
697  size_t i, n = 0;
698 
699  SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
700 
701  if( ( ret = ssl_read_record( ssl ) ) != 0 )
702  {
703  SSL_DEBUG_RET( 1, "ssl_read_record", ret );
704  return( ret );
705  }
706 
707  if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
708  {
709  SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
711  }
712 
713  if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
714  {
715  SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
717  }
718 
724  {
725 #if !defined(POLARSSL_DHM_C)
726  SSL_DEBUG_MSG( 1, ( "support for dhm is not available" ) );
728 #else
729  /*
730  * Receive G^Y mod P, premaster = (G^Y)^X mod P
731  */
732  n = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
733 
734  if( n < 1 || n > ssl->dhm_ctx.len ||
735  n + 6 != ssl->in_hslen )
736  {
737  SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
739  }
740 
741  if( ( ret = dhm_read_public( &ssl->dhm_ctx,
742  ssl->in_msg + 6, n ) ) != 0 )
743  {
744  SSL_DEBUG_RET( 1, "dhm_read_public", ret );
746  }
747 
748  SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->dhm_ctx.GY );
749 
750  ssl->pmslen = ssl->dhm_ctx.len;
751 
752  if( ( ret = dhm_calc_secret( &ssl->dhm_ctx,
753  ssl->premaster, &ssl->pmslen ) ) != 0 )
754  {
755  SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
757  }
758 
759  SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->dhm_ctx.K );
760 #endif
761  }
762  else
763  {
764  if( ssl->rsa_key == NULL )
765  {
766 #if defined(POLARSSL_PKCS11_C)
767  if( ssl->pkcs11_key == NULL )
768  {
769 #endif
770  SSL_DEBUG_MSG( 1, ( "got no private key" ) );
772 #if defined(POLARSSL_PKCS11_C)
773  }
774 #endif
775  }
776 
777  /*
778  * Decrypt the premaster using own private RSA key
779  */
780  i = 4;
781  if( ssl->rsa_key )
782  n = ssl->rsa_key->len;
783 #if defined(POLARSSL_PKCS11_C)
784  else
785  n = ssl->pkcs11_key->len;
786 #endif
787  ssl->pmslen = 48;
788 
789  if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
790  {
791  i += 2;
792  if( ssl->in_msg[4] != ( ( n >> 8 ) & 0xFF ) ||
793  ssl->in_msg[5] != ( ( n ) & 0xFF ) )
794  {
795  SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
797  }
798  }
799 
800  if( ssl->in_hslen != i + n )
801  {
802  SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
804  }
805 
806  if( ssl->rsa_key ) {
807  ret = rsa_pkcs1_decrypt( ssl->rsa_key, RSA_PRIVATE, &ssl->pmslen,
808  ssl->in_msg + i, ssl->premaster,
809  sizeof(ssl->premaster) );
810  }
811 #if defined(POLARSSL_PKCS11_C)
812  else {
813  ret = pkcs11_decrypt( ssl->pkcs11_key, RSA_PRIVATE, &ssl->pmslen,
814  ssl->in_msg + i, ssl->premaster,
815  sizeof(ssl->premaster) );
816  }
817 #endif /* defined(POLARSSL_PKCS11_C) */
818 
819  if( ret != 0 || ssl->pmslen != 48 ||
820  ssl->premaster[0] != ssl->max_major_ver ||
821  ssl->premaster[1] != ssl->max_minor_ver )
822  {
823  SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
824 
825  /*
826  * Protection against Bleichenbacher's attack:
827  * invalid PKCS#1 v1.5 padding must not cause
828  * the connection to end immediately; instead,
829  * send a bad_record_mac later in the handshake.
830  */
831  ssl->pmslen = 48;
832 
833  ret = ssl->f_rng( ssl->p_rng, ssl->premaster, ssl->pmslen );
834  if( ret != 0 )
835  return( ret );
836  }
837  }
838 
839  if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
840  {
841  SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
842  return( ret );
843  }
844 
845  if( ssl->s_set != NULL )
846  ssl->s_set( ssl );
847 
848  ssl->state++;
849 
850  SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
851 
852  return( 0 );
853 }
854 
855 static int ssl_parse_certificate_verify( ssl_context *ssl )
856 {
857  int ret;
858  size_t n1, n2;
859  unsigned char hash[36];
860 
861  SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
862 
863  if( ssl->peer_cert == NULL )
864  {
865  SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
866  ssl->state++;
867  return( 0 );
868  }
869 
870  ssl_calc_verify( ssl, hash );
871 
872  if( ( ret = ssl_read_record( ssl ) ) != 0 )
873  {
874  SSL_DEBUG_RET( 1, "ssl_read_record", ret );
875  return( ret );
876  }
877 
878  ssl->state++;
879 
880  if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
881  {
882  SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
884  }
885 
886  if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
887  {
888  SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
890  }
891 
892  n1 = ssl->peer_cert->rsa.len;
893  n2 = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
894 
895  if( n1 + 6 != ssl->in_hslen || n1 != n2 )
896  {
897  SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
899  }
900 
901  ret = rsa_pkcs1_verify( &ssl->peer_cert->rsa, RSA_PUBLIC,
902  SIG_RSA_RAW, 36, hash, ssl->in_msg + 6 );
903  if( ret != 0 )
904  {
905  SSL_DEBUG_RET( 1, "rsa_pkcs1_verify", ret );
906  return( ret );
907  }
908 
909  SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
910 
911  return( 0 );
912 }
913 
914 /*
915  * SSL handshake -- server side
916  */
918 {
919  int ret = 0;
920 
921  SSL_DEBUG_MSG( 2, ( "=> handshake server" ) );
922 
923  while( ssl->state != SSL_HANDSHAKE_OVER )
924  {
925  SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
926 
927  if( ( ret = ssl_flush_output( ssl ) ) != 0 )
928  break;
929 
930  switch( ssl->state )
931  {
932  case SSL_HELLO_REQUEST:
933  ssl->state = SSL_CLIENT_HELLO;
934  break;
935 
936  /*
937  * <== ClientHello
938  */
939  case SSL_CLIENT_HELLO:
940  ret = ssl_parse_client_hello( ssl );
941  break;
942 
943  /*
944  * ==> ServerHello
945  * Certificate
946  * ( ServerKeyExchange )
947  * ( CertificateRequest )
948  * ServerHelloDone
949  */
950  case SSL_SERVER_HELLO:
951  ret = ssl_write_server_hello( ssl );
952  break;
953 
955  ret = ssl_write_certificate( ssl );
956  break;
957 
959  ret = ssl_write_server_key_exchange( ssl );
960  break;
961 
963  ret = ssl_write_certificate_request( ssl );
964  break;
965 
967  ret = ssl_write_server_hello_done( ssl );
968  break;
969 
970  /*
971  * <== ( Certificate/Alert )
972  * ClientKeyExchange
973  * ( CertificateVerify )
974  * ChangeCipherSpec
975  * Finished
976  */
978  ret = ssl_parse_certificate( ssl );
979  break;
980 
982  ret = ssl_parse_client_key_exchange( ssl );
983  break;
984 
986  ret = ssl_parse_certificate_verify( ssl );
987  break;
988 
990  ret = ssl_parse_change_cipher_spec( ssl );
991  break;
992 
993  case SSL_CLIENT_FINISHED:
994  ret = ssl_parse_finished( ssl );
995  break;
996 
997  /*
998  * ==> ChangeCipherSpec
999  * Finished
1000  */
1002  ret = ssl_write_change_cipher_spec( ssl );
1003  break;
1004 
1005  case SSL_SERVER_FINISHED:
1006  ret = ssl_write_finished( ssl );
1007  break;
1008 
1009  case SSL_FLUSH_BUFFERS:
1010  SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
1011  ssl->state = SSL_HANDSHAKE_OVER;
1012  break;
1013 
1014  default:
1015  SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
1017  }
1018 
1019  if( ret != 0 )
1020  break;
1021  }
1022 
1023  SSL_DEBUG_MSG( 2, ( "<= handshake server" ) );
1024 
1025  return( ret );
1026 }
1027 
1028 #endif