PolarSSL v1.3.8
ssl_cli.c
Go to the documentation of this file.
1 /*
2  * SSLv3/TLSv1 client-side functions
3  *
4  * Copyright (C) 2006-2014, 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 #if !defined(POLARSSL_CONFIG_FILE)
27 #include "polarssl/config.h"
28 #else
29 #include POLARSSL_CONFIG_FILE
30 #endif
31 
32 #if defined(POLARSSL_SSL_CLI_C)
33 
34 #include "polarssl/debug.h"
35 #include "polarssl/ssl.h"
36 
37 #if defined(POLARSSL_PLATFORM_C)
38 #include "polarssl/platform.h"
39 #else
40 #define polarssl_malloc malloc
41 #define polarssl_free free
42 #endif
43 
44 #include <stdlib.h>
45 #include <stdio.h>
46 
47 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
48 #include <basetsd.h>
49 typedef UINT32 uint32_t;
50 #else
51 #include <inttypes.h>
52 #endif
53 
54 #if defined(POLARSSL_HAVE_TIME)
55 #include <time.h>
56 #endif
57 
58 #if defined(POLARSSL_SSL_SESSION_TICKETS)
59 /* Implementation that should never be optimized out by the compiler */
60 static void polarssl_zeroize( void *v, size_t n ) {
61  volatile unsigned char *p = v; while( n-- ) *p++ = 0;
62 }
63 #endif
64 
65 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
66 static void ssl_write_hostname_ext( ssl_context *ssl,
67  unsigned char *buf,
68  size_t *olen )
69 {
70  unsigned char *p = buf;
71 
72  *olen = 0;
73 
74  if( ssl->hostname == NULL )
75  return;
76 
77  SSL_DEBUG_MSG( 3, ( "client hello, adding server name extension: %s",
78  ssl->hostname ) );
79 
80  /*
81  * struct {
82  * NameType name_type;
83  * select (name_type) {
84  * case host_name: HostName;
85  * } name;
86  * } ServerName;
87  *
88  * enum {
89  * host_name(0), (255)
90  * } NameType;
91  *
92  * opaque HostName<1..2^16-1>;
93  *
94  * struct {
95  * ServerName server_name_list<1..2^16-1>
96  * } ServerNameList;
97  */
98  *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME >> 8 ) & 0xFF );
99  *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME ) & 0xFF );
100 
101  *p++ = (unsigned char)( ( (ssl->hostname_len + 5) >> 8 ) & 0xFF );
102  *p++ = (unsigned char)( ( (ssl->hostname_len + 5) ) & 0xFF );
103 
104  *p++ = (unsigned char)( ( (ssl->hostname_len + 3) >> 8 ) & 0xFF );
105  *p++ = (unsigned char)( ( (ssl->hostname_len + 3) ) & 0xFF );
106 
107  *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME_HOSTNAME ) & 0xFF );
108  *p++ = (unsigned char)( ( ssl->hostname_len >> 8 ) & 0xFF );
109  *p++ = (unsigned char)( ( ssl->hostname_len ) & 0xFF );
110 
111  memcpy( p, ssl->hostname, ssl->hostname_len );
112 
113  *olen = ssl->hostname_len + 9;
114 }
115 #endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
116 
117 static void ssl_write_renegotiation_ext( ssl_context *ssl,
118  unsigned char *buf,
119  size_t *olen )
120 {
121  unsigned char *p = buf;
122 
123  *olen = 0;
124 
125  if( ssl->renegotiation != SSL_RENEGOTIATION )
126  return;
127 
128  SSL_DEBUG_MSG( 3, ( "client hello, adding renegotiation extension" ) );
129 
130  /*
131  * Secure renegotiation
132  */
133  *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
134  *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
135 
136  *p++ = 0x00;
137  *p++ = ( ssl->verify_data_len + 1 ) & 0xFF;
138  *p++ = ssl->verify_data_len & 0xFF;
139 
140  memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
141 
142  *olen = 5 + ssl->verify_data_len;
143 }
144 
145 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
146 static void ssl_write_signature_algorithms_ext( ssl_context *ssl,
147  unsigned char *buf,
148  size_t *olen )
149 {
150  unsigned char *p = buf;
151  size_t sig_alg_len = 0;
152 #if defined(POLARSSL_RSA_C) || defined(POLARSSL_ECDSA_C)
153  unsigned char *sig_alg_list = buf + 6;
154 #endif
155 
156  *olen = 0;
157 
158  if( ssl->max_minor_ver != SSL_MINOR_VERSION_3 )
159  return;
160 
161  SSL_DEBUG_MSG( 3, ( "client hello, adding signature_algorithms extension" ) );
162 
163  /*
164  * Prepare signature_algorithms extension (TLS 1.2)
165  */
166 #if defined(POLARSSL_RSA_C)
167 #if defined(POLARSSL_SHA512_C)
168  sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
169  sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
170  sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
171  sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
172 #endif
173 #if defined(POLARSSL_SHA256_C)
174  sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
175  sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
176  sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
177  sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
178 #endif
179 #if defined(POLARSSL_SHA1_C)
180  sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
181  sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
182 #endif
183 #if defined(POLARSSL_MD5_C)
184  sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
185  sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
186 #endif
187 #endif /* POLARSSL_RSA_C */
188 #if defined(POLARSSL_ECDSA_C)
189 #if defined(POLARSSL_SHA512_C)
190  sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
191  sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
192  sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
193  sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
194 #endif
195 #if defined(POLARSSL_SHA256_C)
196  sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
197  sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
198  sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
199  sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
200 #endif
201 #if defined(POLARSSL_SHA1_C)
202  sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
203  sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
204 #endif
205 #if defined(POLARSSL_MD5_C)
206  sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
207  sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
208 #endif
209 #endif /* POLARSSL_ECDSA_C */
210 
211  /*
212  * enum {
213  * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
214  * sha512(6), (255)
215  * } HashAlgorithm;
216  *
217  * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
218  * SignatureAlgorithm;
219  *
220  * struct {
221  * HashAlgorithm hash;
222  * SignatureAlgorithm signature;
223  * } SignatureAndHashAlgorithm;
224  *
225  * SignatureAndHashAlgorithm
226  * supported_signature_algorithms<2..2^16-2>;
227  */
228  *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG >> 8 ) & 0xFF );
229  *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG ) & 0xFF );
230 
231  *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) >> 8 ) & 0xFF );
232  *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) ) & 0xFF );
233 
234  *p++ = (unsigned char)( ( sig_alg_len >> 8 ) & 0xFF );
235  *p++ = (unsigned char)( ( sig_alg_len ) & 0xFF );
236 
237  *olen = 6 + sig_alg_len;
238 }
239 #endif /* POLARSSL_SSL_PROTO_TLS1_2 */
240 
241 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
242 static void ssl_write_supported_elliptic_curves_ext( ssl_context *ssl,
243  unsigned char *buf,
244  size_t *olen )
245 {
246  unsigned char *p = buf;
247  unsigned char *elliptic_curve_list = p + 6;
248  size_t elliptic_curve_len = 0;
249  const ecp_curve_info *info;
250 #if defined(POLARSSL_SSL_SET_CURVES)
251  const ecp_group_id *grp_id;
252 #else
253  ((void) ssl);
254 #endif
255 
256  *olen = 0;
257 
258  SSL_DEBUG_MSG( 3, ( "client hello, adding supported_elliptic_curves extension" ) );
259 
260 #if defined(POLARSSL_SSL_SET_CURVES)
261  for( grp_id = ssl->curve_list; *grp_id != POLARSSL_ECP_DP_NONE; grp_id++ )
262  {
263  info = ecp_curve_info_from_grp_id( *grp_id );
264 #else
265  for( info = ecp_curve_list(); info->grp_id != POLARSSL_ECP_DP_NONE; info++ )
266  {
267 #endif
268 
269  elliptic_curve_list[elliptic_curve_len++] = info->tls_id >> 8;
270  elliptic_curve_list[elliptic_curve_len++] = info->tls_id & 0xFF;
271  }
272 
273  if( elliptic_curve_len == 0 )
274  return;
275 
276  *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES >> 8 ) & 0xFF );
277  *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ) & 0xFF );
278 
279  *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF );
280  *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) ) & 0xFF );
281 
282  *p++ = (unsigned char)( ( ( elliptic_curve_len ) >> 8 ) & 0xFF );
283  *p++ = (unsigned char)( ( ( elliptic_curve_len ) ) & 0xFF );
284 
285  *olen = 6 + elliptic_curve_len;
286 }
287 
288 static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
289  unsigned char *buf,
290  size_t *olen )
291 {
292  unsigned char *p = buf;
293  ((void) ssl);
294 
295  *olen = 0;
296 
297  SSL_DEBUG_MSG( 3, ( "client hello, adding supported_point_formats extension" ) );
298 
299  *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
300  *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
301 
302  *p++ = 0x00;
303  *p++ = 2;
304 
305  *p++ = 1;
307 
308  *olen = 6;
309 }
310 #endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
311 
312 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
313 static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
314  unsigned char *buf,
315  size_t *olen )
316 {
317  unsigned char *p = buf;
318 
319  if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ) {
320  *olen = 0;
321  return;
322  }
323 
324  SSL_DEBUG_MSG( 3, ( "client hello, adding max_fragment_length extension" ) );
325 
326  *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
327  *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
328 
329  *p++ = 0x00;
330  *p++ = 1;
331 
332  *p++ = ssl->mfl_code;
333 
334  *olen = 5;
335 }
336 #endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
337 
338 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
339 static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
340  unsigned char *buf, size_t *olen )
341 {
342  unsigned char *p = buf;
343 
344  if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
345  {
346  *olen = 0;
347  return;
348  }
349 
350  SSL_DEBUG_MSG( 3, ( "client hello, adding truncated_hmac extension" ) );
351 
352  *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
353  *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
354 
355  *p++ = 0x00;
356  *p++ = 0x00;
357 
358  *olen = 4;
359 }
360 #endif /* POLARSSL_SSL_TRUNCATED_HMAC */
361 
362 #if defined(POLARSSL_SSL_SESSION_TICKETS)
363 static void ssl_write_session_ticket_ext( ssl_context *ssl,
364  unsigned char *buf, size_t *olen )
365 {
366  unsigned char *p = buf;
367  size_t tlen = ssl->session_negotiate->ticket_len;
368 
370  {
371  *olen = 0;
372  return;
373  }
374 
375  SSL_DEBUG_MSG( 3, ( "client hello, adding session ticket extension" ) );
376 
377  *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
378  *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
379 
380  *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF );
381  *p++ = (unsigned char)( ( tlen ) & 0xFF );
382 
383  *olen = 4;
384 
385  if( ssl->session_negotiate->ticket == NULL ||
386  ssl->session_negotiate->ticket_len == 0 )
387  {
388  return;
389  }
390 
391  SSL_DEBUG_MSG( 3, ( "sending session ticket of length %d", tlen ) );
392 
393  memcpy( p, ssl->session_negotiate->ticket, tlen );
394 
395  *olen += tlen;
396 }
397 #endif /* POLARSSL_SSL_SESSION_TICKETS */
398 
399 #if defined(POLARSSL_SSL_ALPN)
400 static void ssl_write_alpn_ext( ssl_context *ssl,
401  unsigned char *buf, size_t *olen )
402 {
403  unsigned char *p = buf;
404  const char **cur;
405 
406  if( ssl->alpn_list == NULL )
407  {
408  *olen = 0;
409  return;
410  }
411 
412  SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
413 
414  *p++ = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
415  *p++ = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
416 
417  /*
418  * opaque ProtocolName<1..2^8-1>;
419  *
420  * struct {
421  * ProtocolName protocol_name_list<2..2^16-1>
422  * } ProtocolNameList;
423  */
424 
425  /* Skip writing extension and list length for now */
426  p += 4;
427 
428  for( cur = ssl->alpn_list; *cur != NULL; cur++ )
429  {
430  *p = (unsigned char)( strlen( *cur ) & 0xFF );
431  memcpy( p + 1, *cur, *p );
432  p += 1 + *p;
433  }
434 
435  *olen = p - buf;
436 
437  /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
438  buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
439  buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
440 
441  /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
442  buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
443  buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
444 }
445 #endif /* POLARSSL_SSL_ALPN */
446 
447 static int ssl_write_client_hello( ssl_context *ssl )
448 {
449  int ret;
450  size_t i, n, olen, ext_len = 0;
451  unsigned char *buf;
452  unsigned char *p, *q;
453 #if defined(POLARSSL_HAVE_TIME)
454  time_t t;
455 #endif
456  const int *ciphersuites;
457  const ssl_ciphersuite_t *ciphersuite_info;
458 
459  SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
460 
461  if( ssl->f_rng == NULL )
462  {
463  SSL_DEBUG_MSG( 1, ( "no RNG provided") );
464  return( POLARSSL_ERR_SSL_NO_RNG );
465  }
466 
468  {
469  ssl->major_ver = ssl->min_major_ver;
470  ssl->minor_ver = ssl->min_minor_ver;
471  }
472 
473  if( ssl->max_major_ver == 0 && ssl->max_minor_ver == 0 )
474  {
477  }
478 
479  /*
480  * 0 . 0 handshake type
481  * 1 . 3 handshake length
482  * 4 . 5 highest version supported
483  * 6 . 9 current UNIX time
484  * 10 . 37 random bytes
485  */
486  buf = ssl->out_msg;
487  p = buf + 4;
488 
489  *p++ = (unsigned char) ssl->max_major_ver;
490  *p++ = (unsigned char) ssl->max_minor_ver;
491 
492  SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
493  buf[4], buf[5] ) );
494 
495 #if defined(POLARSSL_HAVE_TIME)
496  t = time( NULL );
497  *p++ = (unsigned char)( t >> 24 );
498  *p++ = (unsigned char)( t >> 16 );
499  *p++ = (unsigned char)( t >> 8 );
500  *p++ = (unsigned char)( t );
501 
502  SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
503 #else
504  if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
505  return( ret );
506 
507  p += 4;
508 #endif /* POLARSSL_HAVE_TIME */
509 
510  if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
511  return( ret );
512 
513  p += 28;
514 
515  memcpy( ssl->handshake->randbytes, buf + 6, 32 );
516 
517  SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 6, 32 );
518 
519  /*
520  * 38 . 38 session id length
521  * 39 . 39+n session id
522  * 40+n . 41+n ciphersuitelist length
523  * 42+n . .. ciphersuitelist
524  * .. . .. compression methods length
525  * .. . .. compression methods
526  * .. . .. extensions length
527  * .. . .. extensions
528  */
529  n = ssl->session_negotiate->length;
530 
531  if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE || n < 16 || n > 32 ||
532  ssl->handshake->resume == 0 )
533  {
534  n = 0;
535  }
536 
537 #if defined(POLARSSL_SSL_SESSION_TICKETS)
538  /*
539  * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
540  * generate and include a Session ID in the TLS ClientHello."
541  */
542  if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
543  ssl->session_negotiate->ticket != NULL &&
544  ssl->session_negotiate->ticket_len != 0 )
545  {
546  ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
547 
548  if( ret != 0 )
549  return( ret );
550 
551  ssl->session_negotiate->length = n = 32;
552  }
553 #endif /* POLARSSL_SSL_SESSION_TICKETS */
554 
555  *p++ = (unsigned char) n;
556 
557  for( i = 0; i < n; i++ )
558  *p++ = ssl->session_negotiate->id[i];
559 
560  SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
561  SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
562 
563  ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
564  n = 0;
565  q = p;
566 
567  // Skip writing ciphersuite length for now
568  p += 2;
569 
570  /*
571  * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
572  */
574  {
575  *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
576  *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
577  n++;
578  }
579 
580  for( i = 0; ciphersuites[i] != 0; i++ )
581  {
582  ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
583 
584  if( ciphersuite_info == NULL )
585  continue;
586 
587  if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
588  ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
589  continue;
590 
591  SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
592  ciphersuites[i] ) );
593 
594  n++;
595  *p++ = (unsigned char)( ciphersuites[i] >> 8 );
596  *p++ = (unsigned char)( ciphersuites[i] );
597  }
598 
599  *q++ = (unsigned char)( n >> 7 );
600  *q++ = (unsigned char)( n << 1 );
601 
602  SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
603 
604 
605 #if defined(POLARSSL_ZLIB_SUPPORT)
606  SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
607  SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
609 
610  *p++ = 2;
611  *p++ = SSL_COMPRESS_DEFLATE;
612  *p++ = SSL_COMPRESS_NULL;
613 #else
614  SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
615  SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d", SSL_COMPRESS_NULL ) );
616 
617  *p++ = 1;
618  *p++ = SSL_COMPRESS_NULL;
619 #endif /* POLARSSL_ZLIB_SUPPORT */
620 
621  // First write extensions, then the total length
622  //
623 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
624  ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
625  ext_len += olen;
626 #endif
627 
628  ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
629  ext_len += olen;
630 
631 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
632  ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
633  ext_len += olen;
634 #endif
635 
636 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
637  ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
638  ext_len += olen;
639 
640  ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
641  ext_len += olen;
642 #endif
643 
644 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
645  ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
646  ext_len += olen;
647 #endif
648 
649 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
650  ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
651  ext_len += olen;
652 #endif
653 
654 #if defined(POLARSSL_SSL_SESSION_TICKETS)
655  ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
656  ext_len += olen;
657 #endif
658 
659 #if defined(POLARSSL_SSL_ALPN)
660  ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
661  ext_len += olen;
662 #endif
663 
664  SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
665  ext_len ) );
666 
667  if( ext_len > 0 )
668  {
669  *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
670  *p++ = (unsigned char)( ( ext_len ) & 0xFF );
671  p += ext_len;
672  }
673 
674  ssl->out_msglen = p - buf;
676  ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
677 
678  ssl->state++;
679 
680  if( ( ret = ssl_write_record( ssl ) ) != 0 )
681  {
682  SSL_DEBUG_RET( 1, "ssl_write_record", ret );
683  return( ret );
684  }
685 
686  SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
687 
688  return( 0 );
689 }
690 
691 static int ssl_parse_renegotiation_info( ssl_context *ssl,
692  const unsigned char *buf,
693  size_t len )
694 {
695  int ret;
696 
698  {
699  if( len != 1 || buf[0] != 0x0 )
700  {
701  SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
702 
703  if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
704  return( ret );
705 
707  }
708 
710  }
711  else
712  {
713  /* Check verify-data in constant-time. The length OTOH is no secret */
714  if( len != 1 + ssl->verify_data_len * 2 ||
715  buf[0] != ssl->verify_data_len * 2 ||
716  safer_memcmp( buf + 1,
717  ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
718  safer_memcmp( buf + 1 + ssl->verify_data_len,
719  ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
720  {
721  SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
722 
723  if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
724  return( ret );
725 
727  }
728  }
729 
730  return( 0 );
731 }
732 
733 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
734 static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
735  const unsigned char *buf,
736  size_t len )
737 {
738  /*
739  * server should use the extension only if we did,
740  * and if so the server's value should match ours (and len is always 1)
741  */
742  if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
743  len != 1 ||
744  buf[0] != ssl->mfl_code )
745  {
747  }
748 
749  return( 0 );
750 }
751 #endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
752 
753 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
754 static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
755  const unsigned char *buf,
756  size_t len )
757 {
758  if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
759  len != 0 )
760  {
762  }
763 
764  ((void) buf);
765 
767 
768  return( 0 );
769 }
770 #endif /* POLARSSL_SSL_TRUNCATED_HMAC */
771 
772 #if defined(POLARSSL_SSL_SESSION_TICKETS)
773 static int ssl_parse_session_ticket_ext( ssl_context *ssl,
774  const unsigned char *buf,
775  size_t len )
776 {
778  len != 0 )
779  {
781  }
782 
783  ((void) buf);
784 
785  ssl->handshake->new_session_ticket = 1;
786 
787  return( 0 );
788 }
789 #endif /* POLARSSL_SSL_SESSION_TICKETS */
790 
791 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
792 static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
793  const unsigned char *buf,
794  size_t len )
795 {
796  size_t list_size;
797  const unsigned char *p;
798 
799  list_size = buf[0];
800  if( list_size + 1 != len )
801  {
802  SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
804  }
805 
806  p = buf + 1;
807  while( list_size > 0 )
808  {
809  if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
811  {
812  ssl->handshake->ecdh_ctx.point_format = p[0];
813  SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
814  return( 0 );
815  }
816 
817  list_size--;
818  p++;
819  }
820 
821  SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
823 }
824 #endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
825 
826 #if defined(POLARSSL_SSL_ALPN)
827 static int ssl_parse_alpn_ext( ssl_context *ssl,
828  const unsigned char *buf, size_t len )
829 {
830  size_t list_len, name_len;
831  const char **p;
832 
833  /* If we didn't send it, the server shouldn't send it */
834  if( ssl->alpn_list == NULL )
836 
837  /*
838  * opaque ProtocolName<1..2^8-1>;
839  *
840  * struct {
841  * ProtocolName protocol_name_list<2..2^16-1>
842  * } ProtocolNameList;
843  *
844  * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
845  */
846 
847  /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
848  if( len < 4 )
850 
851  list_len = ( buf[0] << 8 ) | buf[1];
852  if( list_len != len - 2 )
854 
855  name_len = buf[2];
856  if( name_len != list_len - 1 )
858 
859  /* Check that the server chosen protocol was in our list and save it */
860  for( p = ssl->alpn_list; *p != NULL; p++ )
861  {
862  if( name_len == strlen( *p ) &&
863  memcmp( buf + 3, *p, name_len ) == 0 )
864  {
865  ssl->alpn_chosen = *p;
866  return( 0 );
867  }
868  }
869 
871 }
872 #endif /* POLARSSL_SSL_ALPN */
873 
874 static int ssl_parse_server_hello( ssl_context *ssl )
875 {
876  int ret, i, comp;
877  size_t n;
878  size_t ext_len = 0;
879  unsigned char *buf, *ext;
880  int renegotiation_info_seen = 0;
881  int handshake_failure = 0;
882 #if defined(POLARSSL_DEBUG_C)
883  uint32_t t;
884 #endif
885 
886  SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
887 
888  /*
889  * 0 . 0 handshake type
890  * 1 . 3 handshake length
891  * 4 . 5 protocol version
892  * 6 . 9 UNIX time()
893  * 10 . 37 random bytes
894  */
895  buf = ssl->in_msg;
896 
897  if( ( ret = ssl_read_record( ssl ) ) != 0 )
898  {
899  SSL_DEBUG_RET( 1, "ssl_read_record", ret );
900  return( ret );
901  }
902 
903  if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
904  {
905  SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
907  }
908 
909  SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
910  buf[4], buf[5] ) );
911 
912  if( ssl->in_hslen < 42 ||
913  buf[0] != SSL_HS_SERVER_HELLO ||
914  buf[4] != SSL_MAJOR_VERSION_3 )
915  {
916  SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
918  }
919 
920  if( buf[5] > ssl->max_minor_ver )
921  {
922  SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
924  }
925 
926  ssl->minor_ver = buf[5];
927 
928  if( ssl->minor_ver < ssl->min_minor_ver )
929  {
930  SSL_DEBUG_MSG( 1, ( "server only supports ssl smaller than minimum"
931  " [%d:%d] < [%d:%d]", ssl->major_ver,
932  ssl->minor_ver, buf[4], buf[5] ) );
933 
936 
938  }
939 
940 #if defined(POLARSSL_DEBUG_C)
941  t = ( (uint32_t) buf[6] << 24 )
942  | ( (uint32_t) buf[7] << 16 )
943  | ( (uint32_t) buf[8] << 8 )
944  | ( (uint32_t) buf[9] );
945  SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
946 #endif
947 
948  memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
949 
950  n = buf[38];
951 
952  SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
953 
954  if( n > 32 )
955  {
956  SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
958  }
959 
960  /*
961  * 38 . 38 session id length
962  * 39 . 38+n session id
963  * 39+n . 40+n chosen ciphersuite
964  * 41+n . 41+n chosen compression alg.
965  * 42+n . 43+n extensions length
966  * 44+n . 44+n+m extensions
967  */
968  if( ssl->in_hslen > 42 + n )
969  {
970  ext_len = ( ( buf[42 + n] << 8 )
971  | ( buf[43 + n] ) );
972 
973  if( ( ext_len > 0 && ext_len < 4 ) ||
974  ssl->in_hslen != 44 + n + ext_len )
975  {
976  SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
978  }
979  }
980 
981  i = ( buf[39 + n] << 8 ) | buf[40 + n];
982  comp = buf[41 + n];
983 
984  /*
985  * Initialize update checksum functions
986  */
988 
989  if( ssl->transform_negotiate->ciphersuite_info == NULL )
990  {
991  SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
993  }
994 
996 
997  SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
998  SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
999 
1000  /*
1001  * Check if the session can be resumed
1002  */
1003  if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
1004  ssl->handshake->resume == 0 || n == 0 ||
1005  ssl->session_negotiate->ciphersuite != i ||
1006  ssl->session_negotiate->compression != comp ||
1007  ssl->session_negotiate->length != n ||
1008  memcmp( ssl->session_negotiate->id, buf + 39, n ) != 0 )
1009  {
1010  ssl->state++;
1011  ssl->handshake->resume = 0;
1012 #if defined(POLARSSL_HAVE_TIME)
1013  ssl->session_negotiate->start = time( NULL );
1014 #endif
1015  ssl->session_negotiate->ciphersuite = i;
1016  ssl->session_negotiate->compression = comp;
1017  ssl->session_negotiate->length = n;
1018  memcpy( ssl->session_negotiate->id, buf + 39, n );
1019  }
1020  else
1021  {
1023 
1024  if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1025  {
1026  SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1027  return( ret );
1028  }
1029  }
1030 
1031  SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
1032  ssl->handshake->resume ? "a" : "no" ) );
1033 
1034  SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
1035  SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[41 + n] ) );
1036 
1037  i = 0;
1038  while( 1 )
1039  {
1040  if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
1041  {
1042  SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1044  }
1045 
1046  if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
1048  {
1049  break;
1050  }
1051  }
1052 
1053  if( comp != SSL_COMPRESS_NULL
1054 #if defined(POLARSSL_ZLIB_SUPPORT)
1055  && comp != SSL_COMPRESS_DEFLATE
1056 #endif
1057  )
1058  {
1059  SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1061  }
1062  ssl->session_negotiate->compression = comp;
1063 
1064  ext = buf + 44 + n;
1065 
1066  SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
1067 
1068  while( ext_len )
1069  {
1070  unsigned int ext_id = ( ( ext[0] << 8 )
1071  | ( ext[1] ) );
1072  unsigned int ext_size = ( ( ext[2] << 8 )
1073  | ( ext[3] ) );
1074 
1075  if( ext_size + 4 > ext_len )
1076  {
1077  SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1079  }
1080 
1081  switch( ext_id )
1082  {
1084  SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1085  renegotiation_info_seen = 1;
1086 
1087  if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1088  ext_size ) ) != 0 )
1089  return( ret );
1090 
1091  break;
1092 
1093 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
1095  SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
1096 
1097  if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1098  ext + 4, ext_size ) ) != 0 )
1099  {
1100  return( ret );
1101  }
1102 
1103  break;
1104 #endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
1105 
1106 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1108  SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
1109 
1110  if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1111  ext + 4, ext_size ) ) != 0 )
1112  {
1113  return( ret );
1114  }
1115 
1116  break;
1117 #endif /* POLARSSL_SSL_TRUNCATED_HMAC */
1118 
1119 #if defined(POLARSSL_SSL_SESSION_TICKETS)
1121  SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
1122 
1123  if( ( ret = ssl_parse_session_ticket_ext( ssl,
1124  ext + 4, ext_size ) ) != 0 )
1125  {
1126  return( ret );
1127  }
1128 
1129  break;
1130 #endif /* POLARSSL_SSL_SESSION_TICKETS */
1131 
1132 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
1134  SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
1135 
1136  if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1137  ext + 4, ext_size ) ) != 0 )
1138  {
1139  return( ret );
1140  }
1141 
1142  break;
1143 #endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
1144 
1145 #if defined(POLARSSL_SSL_ALPN)
1146  case TLS_EXT_ALPN:
1147  SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1148 
1149  if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1150  return( ret );
1151 
1152  break;
1153 #endif /* POLARSSL_SSL_ALPN */
1154 
1155  default:
1156  SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1157  ext_id ) );
1158  }
1159 
1160  ext_len -= 4 + ext_size;
1161  ext += 4 + ext_size;
1162 
1163  if( ext_len > 0 && ext_len < 4 )
1164  {
1165  SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1167  }
1168  }
1169 
1170  /*
1171  * Renegotiation security checks
1172  */
1175  {
1176  SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1177  handshake_failure = 1;
1178  }
1179  else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1181  renegotiation_info_seen == 0 )
1182  {
1183  SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1184  handshake_failure = 1;
1185  }
1186  else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1189  {
1190  SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
1191  handshake_failure = 1;
1192  }
1193  else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1195  renegotiation_info_seen == 1 )
1196  {
1197  SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1198  handshake_failure = 1;
1199  }
1200 
1201  if( handshake_failure == 1 )
1202  {
1203  if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1204  return( ret );
1205 
1207  }
1208 
1209  SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1210 
1211  return( 0 );
1212 }
1213 
1214 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1215  defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1216 static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
1217  unsigned char *end )
1218 {
1220 
1221  /*
1222  * Ephemeral DH parameters:
1223  *
1224  * struct {
1225  * opaque dh_p<1..2^16-1>;
1226  * opaque dh_g<1..2^16-1>;
1227  * opaque dh_Ys<1..2^16-1>;
1228  * } ServerDHParams;
1229  */
1230  if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1231  {
1232  SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
1233  return( ret );
1234  }
1235 
1236  if( ssl->handshake->dhm_ctx.len < 64 ||
1237  ssl->handshake->dhm_ctx.len > 512 )
1238  {
1239  SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
1241  }
1242 
1243  SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1244  SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1245  SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
1246 
1247  return( ret );
1248 }
1249 #endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1250  POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1251 
1252 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1253  defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1254  defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1255  defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1256  defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1257 static int ssl_check_server_ecdh_params( const ssl_context *ssl )
1258 {
1259  const ecp_curve_info *curve_info;
1260 
1261  curve_info = ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
1262  if( curve_info == NULL )
1263  {
1264  SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1266  }
1267 
1268  SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
1269 
1270 #if defined(POLARSSL_SSL_ECP_SET_CURVES)
1271  if( ! ssl_curve_is_acceptable( ssl, ssl->handshake->ecdh_ctx.grp.id ) )
1272 #else
1273  if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1274  ssl->handshake->ecdh_ctx.grp.nbits > 521 )
1275 #endif
1276  return( -1 );
1277 
1278  SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
1279 
1280  return( 0 );
1281 }
1282 #endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1283  POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1284  POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1285  POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1286  POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1287 
1288 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1289  defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1290  defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1291 static int ssl_parse_server_ecdh_params( ssl_context *ssl,
1292  unsigned char **p,
1293  unsigned char *end )
1294 {
1296 
1297  /*
1298  * Ephemeral ECDH parameters:
1299  *
1300  * struct {
1301  * ECParameters curve_params;
1302  * ECPoint public;
1303  * } ServerECDHParams;
1304  */
1305  if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
1306  (const unsigned char **) p, end ) ) != 0 )
1307  {
1308  SSL_DEBUG_RET( 1, ( "ecdh_read_params" ), ret );
1309  return( ret );
1310  }
1311 
1312  if( ssl_check_server_ecdh_params( ssl ) != 0 )
1313  {
1314  SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
1316  }
1317 
1318  return( ret );
1319 }
1320 #endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1321  POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1322  POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1323 
1324 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1325 static int ssl_parse_server_psk_hint( ssl_context *ssl,
1326  unsigned char **p,
1327  unsigned char *end )
1328 {
1330  size_t len;
1331  ((void) ssl);
1332 
1333  /*
1334  * PSK parameters:
1335  *
1336  * opaque psk_identity_hint<0..2^16-1>;
1337  */
1338  len = (*p)[0] << 8 | (*p)[1];
1339  *p += 2;
1340 
1341  if( (*p) + len > end )
1342  {
1343  SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
1345  }
1346 
1347  // TODO: Retrieve PSK identity hint and callback to app
1348  //
1349  *p += len;
1350  ret = 0;
1351 
1352  return( ret );
1353 }
1354 #endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
1355 
1356 #if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1357  defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1358 /*
1359  * Generate a pre-master secret and encrypt it with the server's RSA key
1360  */
1361 static int ssl_write_encrypted_pms( ssl_context *ssl,
1362  size_t offset, size_t *olen,
1363  size_t pms_offset )
1364 {
1365  int ret;
1366  size_t len_bytes = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 0 : 2;
1367  unsigned char *p = ssl->handshake->premaster + pms_offset;
1368 
1369  /*
1370  * Generate (part of) the pre-master as
1371  * struct {
1372  * ProtocolVersion client_version;
1373  * opaque random[46];
1374  * } PreMasterSecret;
1375  */
1376  p[0] = (unsigned char) ssl->max_major_ver;
1377  p[1] = (unsigned char) ssl->max_minor_ver;
1378 
1379  if( ( ret = ssl->f_rng( ssl->p_rng, p + 2, 46 ) ) != 0 )
1380  {
1381  SSL_DEBUG_RET( 1, "f_rng", ret );
1382  return( ret );
1383  }
1384 
1385  ssl->handshake->pmslen = 48;
1386 
1387  /*
1388  * Now write it out, encrypted
1389  */
1390  if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1391  POLARSSL_PK_RSA ) )
1392  {
1393  SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1395  }
1396 
1397  if( ( ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
1398  p, ssl->handshake->pmslen,
1399  ssl->out_msg + offset + len_bytes, olen,
1400  SSL_MAX_CONTENT_LEN - offset - len_bytes,
1401  ssl->f_rng, ssl->p_rng ) ) != 0 )
1402  {
1403  SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
1404  return( ret );
1405  }
1406 
1407 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1408  defined(POLARSSL_SSL_PROTO_TLS1_2)
1409  if( len_bytes == 2 )
1410  {
1411  ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
1412  ssl->out_msg[offset+1] = (unsigned char)( *olen );
1413  *olen += 2;
1414  }
1415 #endif
1416 
1417  return( 0 );
1418 }
1419 #endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
1420  POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
1421 
1422 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
1423 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1424  defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1425  defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1426 static int ssl_parse_signature_algorithm( ssl_context *ssl,
1427  unsigned char **p,
1428  unsigned char *end,
1429  md_type_t *md_alg,
1430  pk_type_t *pk_alg )
1431 {
1432  ((void) ssl);
1433  *md_alg = POLARSSL_MD_NONE;
1434  *pk_alg = POLARSSL_PK_NONE;
1435 
1436  /* Only in TLS 1.2 */
1437  if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1438  {
1439  return( 0 );
1440  }
1441 
1442  if( (*p) + 2 > end )
1444 
1445  /*
1446  * Get hash algorithm
1447  */
1448  if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
1449  {
1450  SSL_DEBUG_MSG( 2, ( "Server used unsupported "
1451  "HashAlgorithm %d", *(p)[0] ) );
1453  }
1454 
1455  /*
1456  * Get signature algorithm
1457  */
1458  if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
1459  {
1460  SSL_DEBUG_MSG( 2, ( "server used unsupported "
1461  "SignatureAlgorithm %d", (*p)[1] ) );
1463  }
1464 
1465  SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
1466  SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
1467  *p += 2;
1468 
1469  return( 0 );
1470 }
1471 #endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1472  POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1473  POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
1474 #endif /* POLARSSL_SSL_PROTO_TLS1_2 */
1475 
1476 
1477 #if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1478  defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1479 static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
1480 {
1481  int ret;
1482  const ecp_keypair *peer_key;
1483 
1484  if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1485  POLARSSL_PK_ECKEY ) )
1486  {
1487  SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
1489  }
1490 
1491  peer_key = pk_ec( ssl->session_negotiate->peer_cert->pk );
1492 
1493  if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
1494  POLARSSL_ECDH_THEIRS ) ) != 0 )
1495  {
1496  SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
1497  return( ret );
1498  }
1499 
1500  if( ssl_check_server_ecdh_params( ssl ) != 0 )
1501  {
1502  SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
1504  }
1505 
1506  return( ret );
1507 }
1508 #endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1509  POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1510 
1511 static int ssl_parse_server_key_exchange( ssl_context *ssl )
1512 {
1513  int ret;
1514  const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
1515  unsigned char *p, *end;
1516 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1517  defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1518  defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1519  size_t sig_len, params_len;
1520  unsigned char hash[64];
1521  md_type_t md_alg = POLARSSL_MD_NONE;
1522  size_t hashlen;
1523  pk_type_t pk_alg = POLARSSL_PK_NONE;
1524 #endif
1525 
1526  SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
1527 
1528 #if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
1529  if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
1530  {
1531  SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1532  ssl->state++;
1533  return( 0 );
1534  }
1535  ((void) p);
1536  ((void) end);
1537 #endif
1538 
1539 #if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1540  defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1541  if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
1542  ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
1543  {
1544  if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
1545  {
1546  SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
1547  return( ret );
1548  }
1549 
1550  SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1551  ssl->state++;
1552  return( 0 );
1553  }
1554  ((void) p);
1555  ((void) end);
1556 #endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1557  POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1558 
1559  if( ( ret = ssl_read_record( ssl ) ) != 0 )
1560  {
1561  SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1562  return( ret );
1563  }
1564 
1565  if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1566  {
1567  SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1569  }
1570 
1571  /*
1572  * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
1573  * doesn't use a psk_identity_hint
1574  */
1575  if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
1576  {
1577  if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1578  ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1579  {
1580  ssl->record_read = 1;
1581  goto exit;
1582  }
1583 
1584  SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1586  }
1587 
1588  p = ssl->in_msg + 4;
1589  end = ssl->in_msg + ssl->in_hslen;
1590  SSL_DEBUG_BUF( 3, "server key exchange", p, ssl->in_hslen - 4 );
1591 
1592 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1593  if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1594  ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1595  ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1596  ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1597  {
1598  if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1599  {
1600  SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1602  }
1603  } /* FALLTROUGH */
1604 #endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
1605 
1606 #if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
1607  defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1608  if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1609  ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1610  ; /* nothing more to do */
1611  else
1612 #endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
1613  POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
1614 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1615  defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1616  if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1617  ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1618  {
1619  if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
1620  {
1621  SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1623  }
1624  }
1625  else
1626 #endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1627  POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1628 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1629  defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1630  defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1631  if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1632  ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
1633  ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
1634  {
1635  if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
1636  {
1637  SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1639  }
1640  }
1641  else
1642 #endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1643  POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1644  POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
1645  {
1646  SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1648  }
1649 
1650 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1651  defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1652  defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1653  if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1654  ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1655  ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
1656  {
1657  params_len = p - ( ssl->in_msg + 4 );
1658 
1659  /*
1660  * Handle the digitally-signed structure
1661  */
1662 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
1663  if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
1664  {
1665  if( ssl_parse_signature_algorithm( ssl, &p, end,
1666  &md_alg, &pk_alg ) != 0 )
1667  {
1668  SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1670  }
1671 
1672  if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
1673  {
1674  SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1676  }
1677  }
1678  else
1679 #endif /* POLARSSL_SSL_PROTO_TLS1_2 */
1680 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1681  defined(POLARSSL_SSL_PROTO_TLS1_1)
1682  if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
1683  {
1684  pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
1685 
1686  /* Default hash for ECDSA is SHA-1 */
1687  if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
1688  md_alg = POLARSSL_MD_SHA1;
1689  }
1690  else
1691 #endif
1692  {
1693  SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1695  }
1696 
1697  /*
1698  * Read signature
1699  */
1700  sig_len = ( p[0] << 8 ) | p[1];
1701  p += 2;
1702 
1703  if( end != p + sig_len )
1704  {
1705  SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1707  }
1708 
1709  SSL_DEBUG_BUF( 3, "signature", p, sig_len );
1710 
1711  /*
1712  * Compute the hash that has been signed
1713  */
1714 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1715  defined(POLARSSL_SSL_PROTO_TLS1_1)
1716  if( md_alg == POLARSSL_MD_NONE )
1717  {
1718  md5_context md5;
1720 
1721  md5_init( &md5 );
1722  sha1_init( &sha1 );
1723 
1724  hashlen = 36;
1725 
1726  /*
1727  * digitally-signed struct {
1728  * opaque md5_hash[16];
1729  * opaque sha_hash[20];
1730  * };
1731  *
1732  * md5_hash
1733  * MD5(ClientHello.random + ServerHello.random
1734  * + ServerParams);
1735  * sha_hash
1736  * SHA(ClientHello.random + ServerHello.random
1737  * + ServerParams);
1738  */
1739  md5_starts( &md5 );
1740  md5_update( &md5, ssl->handshake->randbytes, 64 );
1741  md5_update( &md5, ssl->in_msg + 4, params_len );
1742  md5_finish( &md5, hash );
1743 
1744  sha1_starts( &sha1 );
1745  sha1_update( &sha1, ssl->handshake->randbytes, 64 );
1746  sha1_update( &sha1, ssl->in_msg + 4, params_len );
1747  sha1_finish( &sha1, hash + 16 );
1748 
1749  md5_free( &md5 );
1750  sha1_free( &sha1 );
1751  }
1752  else
1753 #endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
1754  POLARSSL_SSL_PROTO_TLS1_1 */
1755 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1756  defined(POLARSSL_SSL_PROTO_TLS1_2)
1757  if( md_alg != POLARSSL_MD_NONE )
1758  {
1759  md_context_t ctx;
1760 
1761  md_init( &ctx );
1762 
1763  /* Info from md_alg will be used instead */
1764  hashlen = 0;
1765 
1766  /*
1767  * digitally-signed struct {
1768  * opaque client_random[32];
1769  * opaque server_random[32];
1770  * ServerDHParams params;
1771  * };
1772  */
1773  if( ( ret = md_init_ctx( &ctx,
1774  md_info_from_type( md_alg ) ) ) != 0 )
1775  {
1776  SSL_DEBUG_RET( 1, "md_init_ctx", ret );
1777  return( ret );
1778  }
1779 
1780  md_starts( &ctx );
1781  md_update( &ctx, ssl->handshake->randbytes, 64 );
1782  md_update( &ctx, ssl->in_msg + 4, params_len );
1783  md_finish( &ctx, hash );
1784  md_free( &ctx );
1785  }
1786  else
1787 #endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1788  POLARSSL_SSL_PROTO_TLS1_2 */
1789  {
1790  SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1792  }
1793 
1794  SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
1795  (unsigned int) ( md_info_from_type( md_alg ) )->size );
1796 
1797  /*
1798  * Verify signature
1799  */
1800  if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
1801  {
1802  SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1804  }
1805 
1806  if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
1807  md_alg, hash, hashlen, p, sig_len ) ) != 0 )
1808  {
1809  SSL_DEBUG_RET( 1, "pk_verify", ret );
1810  return( ret );
1811  }
1812  }
1813 #endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1814  POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1815  POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
1816 
1817 exit:
1818  ssl->state++;
1819 
1820  SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
1821 
1822  return( 0 );
1823 }
1824 
1825 #if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1826  !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
1827  !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1828  !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1829 static int ssl_parse_certificate_request( ssl_context *ssl )
1830 {
1831  const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
1832 
1833  SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1834 
1835  if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1836  ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1837  ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1838  ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1839  {
1840  SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
1841  ssl->state++;
1842  return( 0 );
1843  }
1844 
1845  SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1847 }
1848 #else
1849 static int ssl_parse_certificate_request( ssl_context *ssl )
1850 {
1851  int ret;
1852  unsigned char *buf, *p;
1853  size_t n = 0, m = 0;
1854  size_t cert_type_len = 0, dn_len = 0;
1855  const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
1856 
1857  SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1858 
1859  if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1860  ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1861  ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1862  ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1863  {
1864  SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
1865  ssl->state++;
1866  return( 0 );
1867  }
1868 
1869  /*
1870  * 0 . 0 handshake type
1871  * 1 . 3 handshake length
1872  * 4 . 4 cert type count
1873  * 5 .. m-1 cert types
1874  * m .. m+1 sig alg length (TLS 1.2 only)
1875  * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
1876  * n .. n+1 length of all DNs
1877  * n+2 .. n+3 length of DN 1
1878  * n+4 .. ... Distinguished Name #1
1879  * ... .. ... length of DN 2, etc.
1880  */
1881  if( ssl->record_read == 0 )
1882  {
1883  if( ( ret = ssl_read_record( ssl ) ) != 0 )
1884  {
1885  SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1886  return( ret );
1887  }
1888 
1889  if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1890  {
1891  SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
1893  }
1894 
1895  ssl->record_read = 1;
1896  }
1897 
1898  ssl->client_auth = 0;
1899  ssl->state++;
1900 
1901  if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST )
1902  ssl->client_auth++;
1903 
1904  SSL_DEBUG_MSG( 3, ( "got %s certificate request",
1905  ssl->client_auth ? "a" : "no" ) );
1906 
1907  if( ssl->client_auth == 0 )
1908  goto exit;
1909 
1910  ssl->record_read = 0;
1911 
1912  // TODO: handshake_failure alert for an anonymous server to request
1913  // client authentication
1914 
1915  buf = ssl->in_msg;
1916 
1917  // Retrieve cert types
1918  //
1919  cert_type_len = buf[4];
1920  n = cert_type_len;
1921 
1922  if( ssl->in_hslen < 6 + n )
1923  {
1924  SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
1926  }
1927 
1928  p = buf + 5;
1929  while( cert_type_len > 0 )
1930  {
1931 #if defined(POLARSSL_RSA_C)
1932  if( *p == SSL_CERT_TYPE_RSA_SIGN &&
1934  {
1936  break;
1937  }
1938  else
1939 #endif
1940 #if defined(POLARSSL_ECDSA_C)
1941  if( *p == SSL_CERT_TYPE_ECDSA_SIGN &&
1943  {
1945  break;
1946  }
1947  else
1948 #endif
1949  {
1950  ; /* Unsupported cert type, ignore */
1951  }
1952 
1953  cert_type_len--;
1954  p++;
1955  }
1956 
1957 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
1958  if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
1959  {
1960  /* Ignored, see comments about hash in write_certificate_verify */
1961  // TODO: should check the signature part against our pk_key though
1962  size_t sig_alg_len = ( ( buf[5 + n] << 8 )
1963  | ( buf[6 + n] ) );
1964 
1965  p = buf + 7 + n;
1966  m += 2;
1967  n += sig_alg_len;
1968 
1969  if( ssl->in_hslen < 6 + n )
1970  {
1971  SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
1973  }
1974  }
1975 #endif /* POLARSSL_SSL_PROTO_TLS1_2 */
1976 
1977  /* Ignore certificate_authorities, we only have one cert anyway */
1978  // TODO: should not send cert if no CA matches
1979  dn_len = ( ( buf[5 + m + n] << 8 )
1980  | ( buf[6 + m + n] ) );
1981 
1982  n += dn_len;
1983  if( ssl->in_hslen != 7 + m + n )
1984  {
1985  SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
1987  }
1988 
1989 exit:
1990  SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
1991 
1992  return( 0 );
1993 }
1994 #endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
1995  !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
1996  !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
1997  !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
1998 
1999 static int ssl_parse_server_hello_done( ssl_context *ssl )
2000 {
2001  int ret;
2002 
2003  SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
2004 
2005  if( ssl->record_read == 0 )
2006  {
2007  if( ( ret = ssl_read_record( ssl ) ) != 0 )
2008  {
2009  SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2010  return( ret );
2011  }
2012 
2013  if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2014  {
2015  SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
2017  }
2018  }
2019  ssl->record_read = 0;
2020 
2021  if( ssl->in_hslen != 4 ||
2022  ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
2023  {
2024  SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
2026  }
2027 
2028  ssl->state++;
2029 
2030  SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
2031 
2032  return( 0 );
2033 }
2034 
2035 static int ssl_write_client_key_exchange( ssl_context *ssl )
2036 {
2037  int ret;
2038  size_t i, n;
2039  const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2040 
2041  SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
2042 
2043 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
2044  if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
2045  {
2046  /*
2047  * DHM key exchange -- send G^X mod P
2048  */
2049  n = ssl->handshake->dhm_ctx.len;
2050 
2051  ssl->out_msg[4] = (unsigned char)( n >> 8 );
2052  ssl->out_msg[5] = (unsigned char)( n );
2053  i = 6;
2054 
2055  ret = dhm_make_public( &ssl->handshake->dhm_ctx,
2056  (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2057  &ssl->out_msg[i], n,
2058  ssl->f_rng, ssl->p_rng );
2059  if( ret != 0 )
2060  {
2061  SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2062  return( ret );
2063  }
2064 
2065  SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2066  SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2067 
2069 
2070  if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2071  ssl->handshake->premaster,
2072  &ssl->handshake->pmslen,
2073  ssl->f_rng, ssl->p_rng ) ) != 0 )
2074  {
2075  SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2076  return( ret );
2077  }
2078 
2079  SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2080  }
2081  else
2082 #endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
2083 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2084  defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2085  defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2086  defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2087  if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2088  ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2089  ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2090  ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2091  {
2092  /*
2093  * ECDH key exchange -- send client public value
2094  */
2095  i = 4;
2096 
2097  ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
2098  &n,
2099  &ssl->out_msg[i], 1000,
2100  ssl->f_rng, ssl->p_rng );
2101  if( ret != 0 )
2102  {
2103  SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2104  return( ret );
2105  }
2106 
2107  SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2108 
2109  if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2110  &ssl->handshake->pmslen,
2111  ssl->handshake->premaster,
2113  ssl->f_rng, ssl->p_rng ) ) != 0 )
2114  {
2115  SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2116  return( ret );
2117  }
2118 
2119  SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2120  }
2121  else
2122 #endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2123  POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2124  POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2125  POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2126 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
2127  if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2128  ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2129  ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2130  ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2131  {
2132  /*
2133  * opaque psk_identity<0..2^16-1>;
2134  */
2135  if( ssl->psk == NULL || ssl->psk_identity == NULL )
2137 
2138  i = 4;
2139  n = ssl->psk_identity_len;
2140  ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2141  ssl->out_msg[i++] = (unsigned char)( n );
2142 
2143  memcpy( ssl->out_msg + i, ssl->psk_identity, ssl->psk_identity_len );
2144  i += ssl->psk_identity_len;
2145 
2146 #if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2147  if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
2148  {
2149  n = 0;
2150  }
2151  else
2152 #endif
2153 #if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2154  if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2155  {
2156  if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2157  return( ret );
2158  }
2159  else
2160 #endif
2161 #if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2162  if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2163  {
2164  /*
2165  * ClientDiffieHellmanPublic public (DHM send G^X mod P)
2166  */
2167  n = ssl->handshake->dhm_ctx.len;
2168  ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2169  ssl->out_msg[i++] = (unsigned char)( n );
2170 
2171  ret = dhm_make_public( &ssl->handshake->dhm_ctx,
2172  (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2173  &ssl->out_msg[i], n,
2174  ssl->f_rng, ssl->p_rng );
2175  if( ret != 0 )
2176  {
2177  SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2178  return( ret );
2179  }
2180  }
2181  else
2182 #endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
2183 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2184  if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2185  {
2186  /*
2187  * ClientECDiffieHellmanPublic public;
2188  */
2189  ret = ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
2190  &ssl->out_msg[i], SSL_MAX_CONTENT_LEN - i,
2191  ssl->f_rng, ssl->p_rng );
2192  if( ret != 0 )
2193  {
2194  SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2195  return( ret );
2196  }
2197 
2198  SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2199  }
2200  else
2201 #endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2202  {
2203  SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2205  }
2206 
2207  if( ( ret = ssl_psk_derive_premaster( ssl,
2208  ciphersuite_info->key_exchange ) ) != 0 )
2209  {
2210  SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2211  return( ret );
2212  }
2213  }
2214  else
2215 #endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
2216 #if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2217  if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
2218  {
2219  i = 4;
2220  if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
2221  return( ret );
2222  }
2223  else
2224 #endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2225  {
2226  ((void) ciphersuite_info);
2227  SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2229  }
2230 
2231  if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2232  {
2233  SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2234  return( ret );
2235  }
2236 
2237  ssl->out_msglen = i + n;
2240 
2241  ssl->state++;
2242 
2243  if( ( ret = ssl_write_record( ssl ) ) != 0 )
2244  {
2245  SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2246  return( ret );
2247  }
2248 
2249  SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
2250 
2251  return( 0 );
2252 }
2253 
2254 #if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2255  !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2256  !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2257  !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2258 static int ssl_write_certificate_verify( ssl_context *ssl )
2259 {
2260  const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2261 
2262  SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2263 
2264  if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2265  ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2266  ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
2267  ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2268  {
2269  SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2270  ssl->state++;
2271  return( 0 );
2272  }
2273 
2274  SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2276 }
2277 #else
2278 static int ssl_write_certificate_verify( ssl_context *ssl )
2279 {
2281  const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2282  size_t n = 0, offset = 0;
2283  unsigned char hash[48];
2284  unsigned char *hash_start = hash;
2285  md_type_t md_alg = POLARSSL_MD_NONE;
2286  unsigned int hashlen;
2287 
2288  SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2289 
2290  if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2291  ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2292  ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
2293  ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2294  {
2295  SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2296  ssl->state++;
2297  return( 0 );
2298  }
2299 
2300  if( ssl->client_auth == 0 || ssl_own_cert( ssl ) == NULL )
2301  {
2302  SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2303  ssl->state++;
2304  return( 0 );
2305  }
2306 
2307  if( ssl_own_key( ssl ) == NULL )
2308  {
2309  SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2311  }
2312 
2313  /*
2314  * Make an RSA signature of the handshake digests
2315  */
2316  ssl->handshake->calc_verify( ssl, hash );
2317 
2318 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2319  defined(POLARSSL_SSL_PROTO_TLS1_1)
2320  if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
2321  {
2322  /*
2323  * digitally-signed struct {
2324  * opaque md5_hash[16];
2325  * opaque sha_hash[20];
2326  * };
2327  *
2328  * md5_hash
2329  * MD5(handshake_messages);
2330  *
2331  * sha_hash
2332  * SHA(handshake_messages);
2333  */
2334  hashlen = 36;
2335  md_alg = POLARSSL_MD_NONE;
2336 
2337  /*
2338  * For ECDSA, default hash is SHA-1 only
2339  */
2340  if( pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
2341  {
2342  hash_start += 16;
2343  hashlen -= 16;
2344  md_alg = POLARSSL_MD_SHA1;
2345  }
2346  }
2347  else
2348 #endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2349  POLARSSL_SSL_PROTO_TLS1_1 */
2350 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
2351  if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2352  {
2353  /*
2354  * digitally-signed struct {
2355  * opaque handshake_messages[handshake_messages_length];
2356  * };
2357  *
2358  * Taking shortcut here. We assume that the server always allows the
2359  * PRF Hash function and has sent it in the allowed signature
2360  * algorithms list received in the Certificate Request message.
2361  *
2362  * Until we encounter a server that does not, we will take this
2363  * shortcut.
2364  *
2365  * Reason: Otherwise we should have running hashes for SHA512 and SHA224
2366  * in order to satisfy 'weird' needs from the server side.
2367  */
2370  {
2371  md_alg = POLARSSL_MD_SHA384;
2372  ssl->out_msg[4] = SSL_HASH_SHA384;
2373  }
2374  else
2375  {
2376  md_alg = POLARSSL_MD_SHA256;
2377  ssl->out_msg[4] = SSL_HASH_SHA256;
2378  }
2379  ssl->out_msg[5] = ssl_sig_from_pk( ssl_own_key( ssl ) );
2380 
2381  /* Info from md_alg will be used instead */
2382  hashlen = 0;
2383  offset = 2;
2384  }
2385  else
2386 #endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2387  {
2388  SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2390  }
2391 
2392  if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash_start, hashlen,
2393  ssl->out_msg + 6 + offset, &n,
2394  ssl->f_rng, ssl->p_rng ) ) != 0 )
2395  {
2396  SSL_DEBUG_RET( 1, "pk_sign", ret );
2397  return( ret );
2398  }
2399 
2400  ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
2401  ssl->out_msg[5 + offset] = (unsigned char)( n );
2402 
2403  ssl->out_msglen = 6 + n + offset;
2406 
2407  ssl->state++;
2408 
2409  if( ( ret = ssl_write_record( ssl ) ) != 0 )
2410  {
2411  SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2412  return( ret );
2413  }
2414 
2415  SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
2416 
2417  return( ret );
2418 }
2419 #endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2420  !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2421  !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
2422 
2423 #if defined(POLARSSL_SSL_SESSION_TICKETS)
2424 static int ssl_parse_new_session_ticket( ssl_context *ssl )
2425 {
2426  int ret;
2427  uint32_t lifetime;
2428  size_t ticket_len;
2429  unsigned char *ticket;
2430 
2431  SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2432 
2433  if( ( ret = ssl_read_record( ssl ) ) != 0 )
2434  {
2435  SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2436  return( ret );
2437  }
2438 
2439  if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2440  {
2441  SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2443  }
2444 
2445  /*
2446  * struct {
2447  * uint32 ticket_lifetime_hint;
2448  * opaque ticket<0..2^16-1>;
2449  * } NewSessionTicket;
2450  *
2451  * 0 . 0 handshake message type
2452  * 1 . 3 handshake message length
2453  * 4 . 7 ticket_lifetime_hint
2454  * 8 . 9 ticket_len (n)
2455  * 10 . 9+n ticket content
2456  */
2457  if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
2458  ssl->in_hslen < 10 )
2459  {
2460  SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2462  }
2463 
2464  lifetime = ( ssl->in_msg[4] << 24 ) | ( ssl->in_msg[5] << 16 ) |
2465  ( ssl->in_msg[6] << 8 ) | ( ssl->in_msg[7] );
2466 
2467  ticket_len = ( ssl->in_msg[8] << 8 ) | ( ssl->in_msg[9] );
2468 
2469  if( ticket_len + 10 != ssl->in_hslen )
2470  {
2471  SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2473  }
2474 
2475  SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
2476 
2477  /* We're not waiting for a NewSessionTicket message any more */
2478  ssl->handshake->new_session_ticket = 0;
2479 
2480  /*
2481  * Zero-length ticket means the server changed his mind and doesn't want
2482  * to send a ticket after all, so just forget it
2483  */
2484  if( ticket_len == 0 )
2485  return( 0 );
2486 
2487  polarssl_zeroize( ssl->session_negotiate->ticket,
2488  ssl->session_negotiate->ticket_len );
2490  ssl->session_negotiate->ticket = NULL;
2491  ssl->session_negotiate->ticket_len = 0;
2492 
2493  if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
2494  {
2495  SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
2497  }
2498 
2499  memcpy( ticket, ssl->in_msg + 10, ticket_len );
2500 
2501  ssl->session_negotiate->ticket = ticket;
2502  ssl->session_negotiate->ticket_len = ticket_len;
2503  ssl->session_negotiate->ticket_lifetime = lifetime;
2504 
2505  /*
2506  * RFC 5077 section 3.4:
2507  * "If the client receives a session ticket from the server, then it
2508  * discards any Session ID that was sent in the ServerHello."
2509  */
2510  SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
2511  ssl->session_negotiate->length = 0;
2512 
2513  SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2514 
2515  return( 0 );
2516 }
2517 #endif /* POLARSSL_SSL_SESSION_TICKETS */
2518 
2519 /*
2520  * SSL handshake -- client side -- single step
2521  */
2523 {
2524  int ret = 0;
2525 
2526  if( ssl->state == SSL_HANDSHAKE_OVER )
2528 
2529  SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
2530 
2531  if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2532  return( ret );
2533 
2534  switch( ssl->state )
2535  {
2536  case SSL_HELLO_REQUEST:
2537  ssl->state = SSL_CLIENT_HELLO;
2538  break;
2539 
2540  /*
2541  * ==> ClientHello
2542  */
2543  case SSL_CLIENT_HELLO:
2544  ret = ssl_write_client_hello( ssl );
2545  break;
2546 
2547  /*
2548  * <== ServerHello
2549  * Certificate
2550  * ( ServerKeyExchange )
2551  * ( CertificateRequest )
2552  * ServerHelloDone
2553  */
2554  case SSL_SERVER_HELLO:
2555  ret = ssl_parse_server_hello( ssl );
2556  break;
2557 
2559  ret = ssl_parse_certificate( ssl );
2560  break;
2561 
2563  ret = ssl_parse_server_key_exchange( ssl );
2564  break;
2565 
2567  ret = ssl_parse_certificate_request( ssl );
2568  break;
2569 
2570  case SSL_SERVER_HELLO_DONE:
2571  ret = ssl_parse_server_hello_done( ssl );
2572  break;
2573 
2574  /*
2575  * ==> ( Certificate/Alert )
2576  * ClientKeyExchange
2577  * ( CertificateVerify )
2578  * ChangeCipherSpec
2579  * Finished
2580  */
2582  ret = ssl_write_certificate( ssl );
2583  break;
2584 
2586  ret = ssl_write_client_key_exchange( ssl );
2587  break;
2588 
2590  ret = ssl_write_certificate_verify( ssl );
2591  break;
2592 
2594  ret = ssl_write_change_cipher_spec( ssl );
2595  break;
2596 
2597  case SSL_CLIENT_FINISHED:
2598  ret = ssl_write_finished( ssl );
2599  break;
2600 
2601  /*
2602  * <== ( NewSessionTicket )
2603  * ChangeCipherSpec
2604  * Finished
2605  */
2607 #if defined(POLARSSL_SSL_SESSION_TICKETS)
2608  if( ssl->handshake->new_session_ticket != 0 )
2609  ret = ssl_parse_new_session_ticket( ssl );
2610  else
2611 #endif
2612  ret = ssl_parse_change_cipher_spec( ssl );
2613  break;
2614 
2615  case SSL_SERVER_FINISHED:
2616  ret = ssl_parse_finished( ssl );
2617  break;
2618 
2619  case SSL_FLUSH_BUFFERS:
2620  SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2621  ssl->state = SSL_HANDSHAKE_WRAPUP;
2622  break;
2623 
2624  case SSL_HANDSHAKE_WRAPUP:
2625  ssl_handshake_wrapup( ssl );
2626  break;
2627 
2628  default:
2629  SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2631  }
2632 
2633  return( ret );
2634 }
2635 #endif /* POLARSSL_SSL_CLI_C */
#define SSL_HS_CLIENT_KEY_EXCHANGE
Definition: ssl.h:382
#define SSL_CERT_TYPE_ECDSA_SIGN
Definition: ssl.h:331
unsigned char * hostname
Definition: ssl.h:845
#define SSL_ALERT_LEVEL_FATAL
Definition: ssl.h:342
unsigned char mfl_code
Definition: ssl.h:783
size_t length
Definition: ssl.h:517
int ciphersuite
Definition: ssl.h:515
mpi P
Definition: dhm.h:159
int trunc_hmac
Definition: ssl.h:819
int ecdh_make_public(ecdh_context *ctx, size_t *olen, unsigned char *buf, size_t blen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Generate a public key and a TLS ClientKeyExchange payload.
size_t in_hslen
Definition: ssl.h:763
int ssl_send_alert_message(ssl_context *ssl, unsigned char level, unsigned char message)
Send an alert message.
int(* f_rng)(void *, unsigned char *, size_t)
Definition: ssl.h:701
#define TLS_EXT_SERVERNAME_HOSTNAME
Definition: ssl.h:389
#define POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE
Processing of the ServerKeyExchange handshake message failed.
Definition: ssl.h:129
int record_read
Definition: ssl.h:765
int major_ver
Definition: ssl.h:690
#define SSL_DEBUG_RET(level, text, ret)
Definition: debug.h:63
ecp_group_id grp_id
Definition: ecp.h:89
#define POLARSSL_PREMASTER_SIZE
Definition: ssl.h:451
SHA-1 context structure.
Definition: sha1.h:58
int compression
Definition: ssl.h:516
#define POLARSSL_ERR_SSL_PK_TYPE_MISMATCH
Public key type mismatch (eg, asked for RSA key exchange and presented EC key)
Definition: ssl.h:144
#define POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE
Processing of the ServerHelloDone handshake message failed.
Definition: ssl.h:130
#define POLARSSL_ECP_PF_COMPRESSED
Compressed point format.
Definition: ecp.h:234
pk_type_t ssl_pk_alg_from_sig(unsigned char sig)
int state
Definition: ssl.h:686
int ecdh_calc_secret(ecdh_context *ctx, size_t *olen, unsigned char *buf, size_t blen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Derive and export the shared secret.
char peer_verify_data[36]
Definition: ssl.h:864
#define SSL_HS_CLIENT_HELLO
Definition: ssl.h:374
const ecp_curve_info * ecp_curve_list(void)
Get the list of supported curves in order of preferrence (full information)
#define TLS_EXT_ALPN
Definition: ssl.h:400
Debug functions.
void sha1(const unsigned char *input, size_t ilen, unsigned char output[20])
Output = SHA-1( input buffer )
void(* calc_verify)(ssl_context *, unsigned char *)
Definition: ssl.h:633
void sha1_finish(sha1_context *ctx, unsigned char output[20])
SHA-1 final digest.
int md_starts(md_context_t *ctx)
Set-up the given context for a new message digest.
void md_init(md_context_t *ctx)
Initialize a md_context (as NONE)
#define SSL_HS_SERVER_KEY_EXCHANGE
Definition: ssl.h:378
const char * name
Definition: ecp.h:92
#define TLS_EXT_TRUNCATED_HMAC
Definition: ssl.h:393
#define polarssl_malloc
#define POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO
Processing of the ServerHello handshake message failed.
Definition: ssl.h:126
#define SSL_HS_NEW_SESSION_TICKET
Definition: ssl.h:376
size_t ticket_len
Definition: ssl.h:528
#define polarssl_free
#define SSL_HASH_SHA1
Definition: ssl.h:316
ssl_session * session_negotiate
Definition: ssl.h:737
int ssl_parse_certificate(ssl_context *ssl)
size_t out_msglen
Definition: ssl.h:776
mpi GX
Definition: dhm.h:162
#define SSL_SESSION_TICKETS_DISABLED
Definition: ssl.h:236
#define SSL_SIG_RSA
Definition: ssl.h:323
const int * ciphersuite_list[4]
Definition: ssl.h:814
int ssl_parse_finished(ssl_context *ssl)
void * p_rng
Definition: ssl.h:708
int md_init_ctx(md_context_t *ctx, const md_info_t *md_info)
Initialises and fills the message digest context structure with the appropriate values.
#define SSL_RENEGOTIATION
Definition: ssl.h:215
unsigned char premaster[POLARSSL_PREMASTER_SIZE]
Definition: ssl.h:642
#define POLARSSL_ECP_PF_UNCOMPRESSED
Uncompressed point format.
Definition: ecp.h:233
#define POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET
Processing of the NewSessionTicket handshake message failed.
Definition: ssl.h:142
int ssl_write_finished(ssl_context *ssl)
Configuration options (set of defines)
#define SSL_DEBUG_MSG(level, args)
Definition: debug.h:60
size_t psk_identity_len
Definition: ssl.h:838
mpi X
Definition: dhm.h:161
#define SSL_TRUNC_HMAC_ENABLED
Definition: ssl.h:233
void ssl_handshake_wrapup(ssl_context *ssl)
char own_verify_data[36]
Definition: ssl.h:863
int ecdh_get_params(ecdh_context *ctx, const ecp_keypair *key, ecdh_side side)
Setup an ECDH context from an EC key.
ECP key pair structure.
Definition: ecp.h:163
#define SSL_SIG_ECDSA
Definition: ssl.h:324
void md5_finish(md5_context *ctx, unsigned char output[16])
MD5 final digest.
#define SSL_MAX_MAJOR_VERSION
Definition: ssl.h:178
int secure_renegotiation
Definition: ssl.h:860
time_t start
Definition: ssl.h:513
PolarSSL Platform abstraction layer.
#define pk_ec(pk)
Quick access to an EC context inside a PK context.
Definition: pk.h:84
#define SSL_HASH_MD5
Definition: ssl.h:315
#define SSL_LEGACY_NO_RENEGOTIATION
Definition: ssl.h:228
#define SSL_MAJOR_VERSION_3
Definition: ssl.h:152
unsigned char id[32]
Definition: ssl.h:518
pk_type_t ssl_get_ciphersuite_sig_pk_alg(const ssl_ciphersuite_t *info)
const ssl_ciphersuite_t * ciphersuite_info
Definition: ssl.h:550
unsigned char * psk
Definition: ssl.h:835
size_t len
Definition: dhm.h:158
int max_major_ver
Definition: ssl.h:693
void md5_free(md5_context *ctx)
Clear MD5 context.
ecp_point Qp
Definition: ecdh.h:53
md_type_t
Definition: md.h:51
#define POLARSSL_MPI_MAX_SIZE
const char ** alpn_list
Definition: ssl.h:853
int max_minor_ver
Definition: ssl.h:694
const char * alpn_chosen
Definition: ssl.h:854
int dhm_read_params(dhm_context *ctx, unsigned char **p, const unsigned char *end)
Parse the ServerKeyExchange parameters.
const md_info_t * md_info_from_type(md_type_t md_type)
Returns the message digest information associated with the given digest type.
#define TLS_EXT_SIG_ALG
Definition: ssl.h:398
#define SSL_CERT_TYPE_RSA_SIGN
Definition: ssl.h:330
#define SSL_HS_CERTIFICATE_REQUEST
Definition: ssl.h:379
ssl_handshake_params * handshake
Definition: ssl.h:739
#define SSL_MSG_HANDSHAKE
Definition: ssl.h:338
int ssl_write_certificate(ssl_context *ssl)
#define SSL_ALERT_MSG_PROTOCOL_VERSION
Definition: ssl.h:363
#define POLARSSL_ERR_SSL_NO_RNG
No RNG was provided to the SSL module.
Definition: ssl.h:115
ecp_group_id id
Definition: ecp.h:138
#define SSL_TRUNC_HMAC_DISABLED
Definition: ssl.h:232
int in_msgtype
Definition: ssl.h:759
void md_free(md_context_t *ctx)
Free and clear the message-specific context of ctx.
size_t verify_data_len
Definition: ssl.h:862
mpi G
Definition: dhm.h:160
int point_format
Definition: ecdh.h:55
int min_minor_ver
Definition: ssl.h:696
unsigned char * out_msg
Definition: ssl.h:773
#define SSL_MINOR_VERSION_0
Definition: ssl.h:153
int client_auth
Definition: ssl.h:809
int pk_verify(pk_context *ctx, md_type_t md_alg, const unsigned char *hash, size_t hash_len, const unsigned char *sig, size_t sig_len)
Verify signature (including padding if relevant).
#define POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION
Handshake protocol not within min/max boundaries.
Definition: ssl.h:141
ecdh_context ecdh_ctx
Definition: ssl.h:597
#define SSL_HS_SERVER_HELLO_DONE
Definition: ssl.h:380
static x509_crt * ssl_own_cert(ssl_context *ssl)
Definition: ssl.h:1771
int ssl_handshake_client_step(ssl_context *ssl)
#define POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE
An unexpected message was received from our peer.
Definition: ssl.h:121
const ecp_curve_info * ecp_curve_info_from_grp_id(ecp_group_id grp_id)
Get curve information from an internal group identifier.
unsigned char * ticket
Definition: ssl.h:527
key_exchange_type_t key_exchange
int new_session_ticket
Definition: ssl.h:651
mpi GY
Definition: dhm.h:163
int trunc_hmac
Definition: ssl.h:537
void sha1_free(sha1_context *ctx)
Clear SHA-1 context.
Curve information for use by other modules.
Definition: ecp.h:87
int pk_can_do(pk_context *ctx, pk_type_t type)
Tell if a context can do the operation given by type.
void md5_starts(md5_context *ctx)
MD5 context setup.
unsigned char ssl_sig_from_pk(pk_context *pk)
int ssl_flush_output(ssl_context *ssl)
#define TLS_EXT_SESSION_TICKET
Definition: ssl.h:402
#define SSL_HS_SERVER_HELLO
Definition: ssl.h:375
#define SSL_COMPRESS_DEFLATE
Definition: ssl.h:208
unsigned char * in_msg
Definition: ssl.h:756
mpi z
Definition: ecdh.h:54
#define SSL_MINOR_VERSION_3
Definition: ssl.h:156
mpi K
Definition: dhm.h:164
MD5 context structure.
Definition: md5.h:58
#define TLS_EXT_RENEGOTIATION_INFO
Definition: ssl.h:404
pk_type_t
Public key types.
Definition: pk.h:95
#define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST
Processing of the CertificateRequest handshake message failed.
Definition: ssl.h:128
int ssl_parse_change_cipher_spec(ssl_context *ssl)
size_t hostname_len
Definition: ssl.h:846
int ecdh_read_params(ecdh_context *ctx, const unsigned char **buf, const unsigned char *end)
Parse and procress a TLS ServerKeyExhange payload.
void sha1_starts(sha1_context *ctx)
SHA-1 context setup.
int minor_ver
Definition: ssl.h:691
#define SSL_EMPTY_RENEGOTIATION_INFO
renegotiation info ext
Definition: ssl.h:308
This structure is used for storing ciphersuite information.
#define SSL_HASH_SHA256
Definition: ssl.h:318
void md5_init(md5_context *ctx)
Initialize MD5 context.
#define SSL_DEBUG_BUF(level, text, buf, len)
Definition: debug.h:66
#define POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED
The own private key or pre-shared key is not set, but needed.
Definition: ssl.h:119
#define SSL_INITIAL_HANDSHAKE
Definition: ssl.h:214
#define SSL_MAX_MINOR_VERSION
Definition: ssl.h:181
int session_tickets
Definition: ssl.h:822
int allow_legacy_renegotiation
Definition: ssl.h:812
#define SSL_COMPRESS_NULL
Definition: ssl.h:207
#define POLARSSL_ERR_SSL_INTERNAL_ERROR
Internal error (eg, unexpected failure in lower-level module)
Definition: ssl.h:146
const ssl_ciphersuite_t * ssl_ciphersuite_from_id(int ciphersuite_id)
int ssl_read_record(ssl_context *ssl)
ecp_group_id
Domain parameters (curve, subgroup and generator) identifiers.
Definition: ecp.h:57
int out_msgtype
Definition: ssl.h:775
#define TLS_EXT_MAX_FRAGMENT_LENGTH
Definition: ssl.h:391
size_t nbits
Definition: ecp.h:145
#define SSL_MAX_FRAG_LEN_NONE
Definition: ssl.h:198
#define SSL_HS_CERTIFICATE_VERIFY
Definition: ssl.h:381
#define TLS_EXT_SERVERNAME
Definition: ssl.h:388
int pk_sign(pk_context *ctx, md_type_t md_alg, const unsigned char *hash, size_t hash_len, unsigned char *sig, size_t *sig_len, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Make signature, including padding if relevant.
int dhm_make_public(dhm_context *ctx, int x_size, unsigned char *output, size_t olen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Create own private value X and export G^X.
#define SSL_DEBUG_MPI(level, text, X)
Definition: debug.h:70
size_t mpi_size(const mpi *X)
Return the total size in bytes.
#define SSL_MAX_CONTENT_LEN
#define SSL_LEGACY_BREAK_HANDSHAKE
Definition: ssl.h:230
int pk_encrypt(pk_context *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen, size_t osize, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Encrypt message (including padding if relevant).
int min_major_ver
Definition: ssl.h:695
pk_context pk
Container for the public key context.
Definition: x509_crt.h:75
ssl_transform * transform_negotiate
Definition: ssl.h:748
#define SSL_LEGACY_RENEGOTIATION
Definition: ssl.h:219
#define SSL_HASH_SHA224
Definition: ssl.h:317
uint32_t ticket_lifetime
Definition: ssl.h:529
#define SSL_SECURE_RENEGOTIATION
Definition: ssl.h:220
SSL/TLS functions.
void sha1_init(sha1_context *ctx)
Initialize SHA-1 context.
#define POLARSSL_ERR_SSL_MALLOC_FAILED
Memory allocation failed.
Definition: ssl.h:137
void sha1_update(sha1_context *ctx, const unsigned char *input, size_t ilen)
SHA-1 process buffer.
#define TLS_EXT_SUPPORTED_POINT_FORMATS
Definition: ssl.h:396
void md5_update(md5_context *ctx, const unsigned char *input, size_t ilen)
MD5 process buffer.
int ssl_write_change_cipher_spec(ssl_context *ssl)
#define SSL_DEBUG_ECP(level, text, X)
Definition: debug.h:75
uint16_t tls_id
Definition: ecp.h:90
int ssl_derive_keys(ssl_context *ssl)
static pk_context * ssl_own_key(ssl_context *ssl)
Definition: ssl.h:1765
int md_finish(md_context_t *ctx, unsigned char *output)
Generic message digest final digest.
int ssl_psk_derive_premaster(ssl_context *ssl, key_exchange_type_t key_ex)
int renegotiation
Definition: ssl.h:687
dhm_context dhm_ctx
Definition: ssl.h:594
static int safer_memcmp(const void *a, const void *b, size_t n)
Definition: ssl.h:1792
int ssl_send_fatal_handshake_failure(ssl_context *ssl)
unsigned char * psk_identity
Definition: ssl.h:837
#define TLS_EXT_SUPPORTED_ELLIPTIC_CURVES
Definition: ssl.h:395
void md5(const unsigned char *input, size_t ilen, unsigned char output[16])
Output = MD5( input buffer )
#define POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE
The requested feature is not available.
Definition: ssl.h:108
#define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE
Processing of the Certificate handshake message failed.
Definition: ssl.h:127
int md_update(md_context_t *ctx, const unsigned char *input, size_t ilen)
Generic message digest process buffer.
#define POLARSSL_ERR_SSL_BAD_INPUT_DATA
Bad input parameters to function.
Definition: ssl.h:109
#define SSL_HASH_SHA512
Definition: ssl.h:320
#define SSL_HASH_SHA384
Definition: ssl.h:319
int ssl_write_record(ssl_context *ssl)
unsigned char randbytes[64]
Definition: ssl.h:641
ecp_group grp
Definition: ecdh.h:50
Generic message digest context.
Definition: md.h:132
void ssl_optimize_checksum(ssl_context *ssl, const ssl_ciphersuite_t *ciphersuite_info)
ecp_point Q
Definition: ecdh.h:52
x509_crt * peer_cert
Definition: ssl.h:522
md_type_t ssl_md_alg_from_hash(unsigned char hash)
int dhm_calc_secret(dhm_context *ctx, unsigned char *output, size_t *olen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Derive and export the shared secret (G^Y)^X mod P.