blob: 88a4f5af58f62327b2f48d1d541fcc4f80d04000 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 client-side functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakker40e46942009-01-03 21:51:57 +000032#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/debug.h"
35#include "polarssl/ssl.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker7dc4c442014-02-01 22:50:26 +010037#if defined(POLARSSL_PLATFORM_C)
38#include "polarssl/platform.h"
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020039#else
40#define polarssl_malloc malloc
41#define polarssl_free free
42#endif
43
Paul Bakker5121ce52009-01-03 21:22:43 +000044#include <stdlib.h>
45#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020046
Paul Bakkerfa6a6202013-10-28 18:48:30 +010047#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakkerfa9b1002013-07-03 15:31:03 +020048#include <basetsd.h>
49typedef UINT32 uint32_t;
50#else
51#include <inttypes.h>
52#endif
53
54#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000055#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020056#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Paul Bakker34617722014-06-13 17:20:13 +020058#if defined(POLARSSL_SSL_SESSION_TICKETS)
59/* Implementation that should never be optimized out by the compiler */
60static void polarssl_zeroize( void *v, size_t n ) {
61 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
62}
63#endif
64
Paul Bakker0be444a2013-08-27 21:55:01 +020065#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +010066static 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
Paul Bakker66d5d072014-06-17 16:39:18 +020074 if( ssl->hostname == NULL )
Paul Bakkerd3edc862013-03-20 16:07:17 +010075 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}
Paul Bakker0be444a2013-08-27 21:55:01 +0200115#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100116
117static 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
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200145#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100146static void ssl_write_signature_algorithms_ext( ssl_context *ssl,
147 unsigned char *buf,
148 size_t *olen )
149{
150 unsigned char *p = buf;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100151 size_t sig_alg_len = 0;
Manuel Pégourié-Gonnard5bfd9682014-06-24 15:18:11 +0200152#if defined(POLARSSL_RSA_C) || defined(POLARSSL_ECDSA_C)
153 unsigned char *sig_alg_list = buf + 6;
154#endif
Paul Bakkerd3edc862013-03-20 16:07:17 +0100155
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 */
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200166#if defined(POLARSSL_RSA_C)
Paul Bakker9e36f042013-06-30 14:34:05 +0200167#if defined(POLARSSL_SHA512_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100168 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
Paul Bakker9e36f042013-06-30 14:34:05 +0200173#if defined(POLARSSL_SHA256_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100174 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
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200187#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 */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100210
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
Paul Bakkerd3edc862013-03-20 16:07:17 +0100237 *olen = 6 + sig_alg_len;
238}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200239#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100240
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200241#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100242static void ssl_write_supported_elliptic_curves_ext( ssl_context *ssl,
243 unsigned char *buf,
244 size_t *olen )
245{
246 unsigned char *p = buf;
Manuel Pégourié-Gonnard8e205fc2014-01-23 17:27:10 +0100247 unsigned char *elliptic_curve_list = p + 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100248 size_t elliptic_curve_len = 0;
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100249 const ecp_curve_info *info;
250#if defined(POLARSSL_SSL_SET_CURVES)
251 const ecp_group_id *grp_id;
Paul Bakker0910f322014-02-06 13:41:18 +0100252#else
253 ((void) ssl);
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100254#endif
Paul Bakkerd3edc862013-03-20 16:07:17 +0100255
256 *olen = 0;
257
258 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_elliptic_curves extension" ) );
259
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100260#if defined(POLARSSL_SSL_SET_CURVES)
261 for( grp_id = ssl->curve_list; *grp_id != POLARSSL_ECP_DP_NONE; grp_id++ )
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200262 {
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100263 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;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200271 }
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200272
273 if( elliptic_curve_len == 0 )
274 return;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100275
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
Paul Bakkerd3edc862013-03-20 16:07:17 +0100285 *olen = 6 + elliptic_curve_len;
286}
287
288static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
289 unsigned char *buf,
290 size_t *olen )
291{
292 unsigned char *p = buf;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +0200293 ((void) ssl);
Paul Bakkerd3edc862013-03-20 16:07:17 +0100294
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;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100303 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200304
305 *p++ = 1;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100306 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
307
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200308 *olen = 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100309}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200310#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100311
Paul Bakker05decb22013-08-15 13:33:48 +0200312#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200313static 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}
Paul Bakker05decb22013-08-15 13:33:48 +0200336#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200337
Paul Bakker1f2bc622013-08-15 13:45:55 +0200338#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200339static 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}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200360#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200361
Paul Bakkera503a632013-08-14 13:48:06 +0200362#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200363static 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
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200369 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
370 {
371 *olen = 0;
372 return;
373 }
374
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200375 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}
Paul Bakkera503a632013-08-14 13:48:06 +0200397#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200398
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200399#if defined(POLARSSL_SSL_ALPN)
400static 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
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +0200412 SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200413
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
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200447/*
448 * Generate random bytes for ClientHello
449 */
450static int ssl_generate_random( ssl_context *ssl )
451{
452 int ret;
453 unsigned char *p = ssl->handshake->randbytes;
454#if defined(POLARSSL_HAVE_TIME)
455 time_t t;
456#endif
457
458#if defined(POLARSSL_HAVE_TIME)
459 t = time( NULL );
460 *p++ = (unsigned char)( t >> 24 );
461 *p++ = (unsigned char)( t >> 16 );
462 *p++ = (unsigned char)( t >> 8 );
463 *p++ = (unsigned char)( t );
464
465 SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
466#else
467 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
468 return( ret );
469
470 p += 4;
471#endif /* POLARSSL_HAVE_TIME */
472
473 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
474 return( ret );
475
476 return( 0 );
477}
478
Paul Bakker5121ce52009-01-03 21:22:43 +0000479static int ssl_write_client_hello( ssl_context *ssl )
480{
Paul Bakker23986e52011-04-24 08:57:21 +0000481 int ret;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100482 size_t i, n, olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000483 unsigned char *buf;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200484 unsigned char *p, *q;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200485 unsigned char offer_compress;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200486 const int *ciphersuites;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200487 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000488
489 SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
490
Paul Bakkera9a028e2013-11-21 17:31:06 +0100491 if( ssl->f_rng == NULL )
492 {
493 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
494 return( POLARSSL_ERR_SSL_NO_RNG );
495 }
496
Paul Bakker48916f92012-09-16 19:57:18 +0000497 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
498 {
Paul Bakker993d11d2012-09-28 15:00:12 +0000499 ssl->major_ver = ssl->min_major_ver;
500 ssl->minor_ver = ssl->min_minor_ver;
Paul Bakker48916f92012-09-16 19:57:18 +0000501 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000502
Paul Bakker490ecc82011-10-06 13:04:09 +0000503 if( ssl->max_major_ver == 0 && ssl->max_minor_ver == 0 )
504 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200505 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
506 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker490ecc82011-10-06 13:04:09 +0000507 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000508
509 /*
510 * 0 . 0 handshake type
511 * 1 . 3 handshake length
512 * 4 . 5 highest version supported
513 * 6 . 9 current UNIX time
514 * 10 . 37 random bytes
515 */
516 buf = ssl->out_msg;
517 p = buf + 4;
518
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +0100519 ssl_write_version( ssl->max_major_ver, ssl->max_minor_ver,
520 ssl->transport, p );
521 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000522
523 SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
524 buf[4], buf[5] ) );
525
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200526 if( ( ret = ssl_generate_random( ssl ) ) != 0 )
527 {
528 SSL_DEBUG_RET( 1, "ssl_generate_random", ret );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200529 return( ret );
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200530 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200531
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200532 memcpy( p, ssl->handshake->randbytes, 32 );
533 SSL_DEBUG_BUF( 3, "client hello, random bytes", p, 32 );
534 p += 32;
Paul Bakker5121ce52009-01-03 21:22:43 +0000535
536 /*
537 * 38 . 38 session id length
538 * 39 . 39+n session id
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100539 * 39+n . 39+n DTLS only: cookie length (1 byte)
540 * 40+n . .. DTSL only: cookie
541 * .. . .. ciphersuitelist length (2 bytes)
542 * .. . .. ciphersuitelist
543 * .. . .. compression methods length (1 byte)
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000544 * .. . .. compression methods
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100545 * .. . .. extensions length (2 bytes)
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000546 * .. . .. extensions
Paul Bakker5121ce52009-01-03 21:22:43 +0000547 */
Paul Bakker48916f92012-09-16 19:57:18 +0000548 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
Paul Bakker0a597072012-09-25 21:55:46 +0000550 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE || n < 16 || n > 32 ||
551 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200552 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000553 n = 0;
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200554 }
555
Paul Bakkera503a632013-08-14 13:48:06 +0200556#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200557 /*
558 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
559 * generate and include a Session ID in the TLS ClientHello."
560 */
561 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
562 ssl->session_negotiate->ticket != NULL &&
563 ssl->session_negotiate->ticket_len != 0 )
564 {
565 ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
566
567 if( ret != 0 )
568 return( ret );
569
570 ssl->session_negotiate->length = n = 32;
571 }
Paul Bakkera503a632013-08-14 13:48:06 +0200572#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
574 *p++ = (unsigned char) n;
575
576 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000577 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
579 SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
580 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
581
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100582 /*
583 * DTLS cookie
584 */
585#if defined(POLARSSL_SSL_PROTO_DTLS)
586 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
587 {
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200588 if( ssl->handshake->verify_cookie == NULL )
589 {
590 SSL_DEBUG_MSG( 3, ( "no verify cookie to send" ) );
591 *p++ = 0;
592 }
593 else
594 {
595 SSL_DEBUG_BUF( 3, "client hello, cookie",
596 ssl->handshake->verify_cookie,
597 ssl->handshake->verify_cookie_len );
598
599 *p++ = ssl->handshake->verify_cookie_len;
600 memcpy( p, ssl->handshake->verify_cookie,
601 ssl->handshake->verify_cookie_len );
602 p += ssl->handshake->verify_cookie_len;
603 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100604 }
605#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000606
Paul Bakker48916f92012-09-16 19:57:18 +0000607 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100608 * Ciphersuite list
Paul Bakker48916f92012-09-16 19:57:18 +0000609 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100610 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
611
612 /* Skip writing ciphersuite length for now */
613 n = 0;
614 q = p;
615 p += 2;
616
617 /* Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV */
Paul Bakker48916f92012-09-16 19:57:18 +0000618 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
619 {
620 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
621 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
Paul Bakker2fbefde2013-06-29 16:01:15 +0200622 n++;
Paul Bakker48916f92012-09-16 19:57:18 +0000623 }
624
Paul Bakker2fbefde2013-06-29 16:01:15 +0200625 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000626 {
Paul Bakker2fbefde2013-06-29 16:01:15 +0200627 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
628
629 if( ciphersuite_info == NULL )
630 continue;
631
632 if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
633 ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
634 continue;
635
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +0100636#if defined(POLARSSL_SSL_PROTO_DTLS)
637 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
638 ( ciphersuite_info->flags & POLARSSL_CIPHERSUITE_NODTLS ) )
639 continue;
640#endif
641
Paul Bakkere3166ce2011-01-27 17:40:50 +0000642 SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200643 ciphersuites[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000644
Paul Bakker2fbefde2013-06-29 16:01:15 +0200645 n++;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200646 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
647 *p++ = (unsigned char)( ciphersuites[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000648 }
649
Paul Bakker2fbefde2013-06-29 16:01:15 +0200650 *q++ = (unsigned char)( n >> 7 );
651 *q++ = (unsigned char)( n << 1 );
652
653 SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
654
Paul Bakker2770fbd2012-07-03 13:30:23 +0000655#if defined(POLARSSL_ZLIB_SUPPORT)
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200656 offer_compress = 1;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000657#else
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200658 offer_compress = 0;
659#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000660
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200661 /*
662 * We don't support compression with DTLS right now: is many records come
663 * in the same datagram, uncompressing one could overwrite the next one.
664 * We don't want to add complexity for handling that case unless there is
665 * an actual need for it.
666 */
667#if defined(POLARSSL_SSL_PROTO_DTLS)
668 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
669 offer_compress = 0;
670#endif
671
672 if( offer_compress )
673 {
674 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
675 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
676 SSL_COMPRESS_DEFLATE, SSL_COMPRESS_NULL ) );
677
678 *p++ = 2;
679 *p++ = SSL_COMPRESS_DEFLATE;
680 *p++ = SSL_COMPRESS_NULL;
681 }
682 else
683 {
684 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
685 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d",
686 SSL_COMPRESS_NULL ) );
687
688 *p++ = 1;
689 *p++ = SSL_COMPRESS_NULL;
690 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000691
Paul Bakkerd3edc862013-03-20 16:07:17 +0100692 // First write extensions, then the total length
693 //
Paul Bakker0be444a2013-08-27 21:55:01 +0200694#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100695 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
696 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +0200697#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000698
Paul Bakkerd3edc862013-03-20 16:07:17 +0100699 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
700 ext_len += olen;
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000701
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200702#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100703 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
704 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200705#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000706
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200707#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100708 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
709 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100710
Paul Bakkerd3edc862013-03-20 16:07:17 +0100711 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
712 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100713#endif
714
Paul Bakker05decb22013-08-15 13:33:48 +0200715#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200716 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
717 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +0200718#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200719
Paul Bakker1f2bc622013-08-15 13:45:55 +0200720#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200721 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
722 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200723#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200724
Paul Bakkera503a632013-08-14 13:48:06 +0200725#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200726 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
727 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +0200728#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200729
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200730#if defined(POLARSSL_SSL_ALPN)
731 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
732 ext_len += olen;
733#endif
734
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000735 SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
736 ext_len ) );
737
Paul Bakkera7036632014-04-30 10:15:38 +0200738 if( ext_len > 0 )
739 {
740 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
741 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
742 p += ext_len;
743 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100744
Paul Bakker5121ce52009-01-03 21:22:43 +0000745 ssl->out_msglen = p - buf;
746 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
747 ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
748
749 ssl->state++;
750
751 if( ( ret = ssl_write_record( ssl ) ) != 0 )
752 {
753 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
754 return( ret );
755 }
756
757 SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
758
759 return( 0 );
760}
761
Paul Bakker48916f92012-09-16 19:57:18 +0000762static int ssl_parse_renegotiation_info( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200763 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000764 size_t len )
765{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000766 int ret;
767
Paul Bakker48916f92012-09-16 19:57:18 +0000768 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
769 {
770 if( len != 1 || buf[0] != 0x0 )
771 {
772 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000773
774 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
775 return( ret );
776
Paul Bakker48916f92012-09-16 19:57:18 +0000777 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
778 }
779
780 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
781 }
782 else
783 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100784 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000785 if( len != 1 + ssl->verify_data_len * 2 ||
786 buf[0] != ssl->verify_data_len * 2 ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100787 safer_memcmp( buf + 1,
788 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
789 safer_memcmp( buf + 1 + ssl->verify_data_len,
790 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000791 {
792 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000793
794 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
795 return( ret );
796
Paul Bakker48916f92012-09-16 19:57:18 +0000797 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
798 }
799 }
800
801 return( 0 );
802}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200803
Paul Bakker05decb22013-08-15 13:33:48 +0200804#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200805static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200806 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200807 size_t len )
808{
809 /*
810 * server should use the extension only if we did,
811 * and if so the server's value should match ours (and len is always 1)
812 */
813 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
814 len != 1 ||
815 buf[0] != ssl->mfl_code )
816 {
817 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
818 }
819
820 return( 0 );
821}
Paul Bakker05decb22013-08-15 13:33:48 +0200822#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000823
Paul Bakker1f2bc622013-08-15 13:45:55 +0200824#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200825static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
826 const unsigned char *buf,
827 size_t len )
828{
829 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
830 len != 0 )
831 {
832 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
833 }
834
835 ((void) buf);
836
837 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
838
839 return( 0 );
840}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200841#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200842
Paul Bakkera503a632013-08-14 13:48:06 +0200843#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200844static int ssl_parse_session_ticket_ext( ssl_context *ssl,
845 const unsigned char *buf,
846 size_t len )
847{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200848 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ||
849 len != 0 )
850 {
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200851 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200852 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200853
854 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200855
856 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200857
858 return( 0 );
859}
Paul Bakkera503a632013-08-14 13:48:06 +0200860#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200861
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200862#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200863static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
864 const unsigned char *buf,
865 size_t len )
866{
867 size_t list_size;
868 const unsigned char *p;
869
870 list_size = buf[0];
871 if( list_size + 1 != len )
872 {
873 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
874 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
875 }
876
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200877 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200878 while( list_size > 0 )
879 {
880 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
881 p[0] == POLARSSL_ECP_PF_COMPRESSED )
882 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200883 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200884 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
885 return( 0 );
886 }
887
888 list_size--;
889 p++;
890 }
891
Manuel Pégourié-Gonnard5c1f0322014-06-23 14:24:43 +0200892 SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
893 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200894}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200895#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200896
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200897#if defined(POLARSSL_SSL_ALPN)
898static int ssl_parse_alpn_ext( ssl_context *ssl,
899 const unsigned char *buf, size_t len )
900{
901 size_t list_len, name_len;
902 const char **p;
903
904 /* If we didn't send it, the server shouldn't send it */
905 if( ssl->alpn_list == NULL )
906 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
907
908 /*
909 * opaque ProtocolName<1..2^8-1>;
910 *
911 * struct {
912 * ProtocolName protocol_name_list<2..2^16-1>
913 * } ProtocolNameList;
914 *
915 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
916 */
917
918 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
919 if( len < 4 )
920 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
921
922 list_len = ( buf[0] << 8 ) | buf[1];
923 if( list_len != len - 2 )
924 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
925
926 name_len = buf[2];
927 if( name_len != list_len - 1 )
928 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
929
930 /* Check that the server chosen protocol was in our list and save it */
931 for( p = ssl->alpn_list; *p != NULL; p++ )
932 {
933 if( name_len == strlen( *p ) &&
934 memcmp( buf + 3, *p, name_len ) == 0 )
935 {
936 ssl->alpn_chosen = *p;
937 return( 0 );
938 }
939 }
940
941 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
942}
943#endif /* POLARSSL_SSL_ALPN */
944
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200945/*
946 * Parse HelloVerifyRequest. Only called after verifying the HS type.
947 */
948#if defined(POLARSSL_SSL_PROTO_DTLS)
949static int ssl_parse_hello_verify_request( ssl_context *ssl )
950{
951 const unsigned char *p = ssl->in_msg + 4;
952 int major_ver, minor_ver;
953 unsigned char cookie_len;
954
955 SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
956
957 /*
958 * struct {
959 * ProtocolVersion server_version;
960 * opaque cookie<0..2^8-1>;
961 * } HelloVerifyRequest;
962 */
963 SSL_DEBUG_BUF( 3, "server version", (unsigned char *) p, 2 );
964 ssl_read_version( &major_ver, &minor_ver, ssl->transport, p );
965 p += 2;
966
967 if( major_ver != SSL_MAJOR_VERSION_3 ||
968 minor_ver < SSL_MINOR_VERSION_2 ||
969 minor_ver > SSL_MINOR_VERSION_3 )
970 {
971 SSL_DEBUG_MSG( 1, ( "bad server version" ) );
972
973 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
974 SSL_ALERT_MSG_PROTOCOL_VERSION );
975
976 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
977 }
978
979 cookie_len = *p++;
980 SSL_DEBUG_BUF( 3, "cookie", (unsigned char *) p, cookie_len );
981
982 polarssl_free( ssl->handshake->verify_cookie );
983
984 ssl->handshake->verify_cookie = polarssl_malloc( cookie_len );
985 if( ssl->handshake->verify_cookie == NULL )
986 {
987 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", cookie_len ) );
988 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
989 }
990
991 memcpy( ssl->handshake->verify_cookie, p, cookie_len );
992 ssl->handshake->verify_cookie_len = cookie_len;
993
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +0200994 /* Start over at ClientHello */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200995 ssl->state = SSL_CLIENT_HELLO;
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +0200996 ssl_reset_checksum( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200997
998 SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
999
1000 return( 0 );
1001}
1002#endif /* POLARSSL_SSL_PROTO_DTLS */
1003
Paul Bakker5121ce52009-01-03 21:22:43 +00001004static int ssl_parse_server_hello( ssl_context *ssl )
1005{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001006 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001007 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001008 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001009 unsigned char *buf, *ext;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001010 unsigned char comp, accept_comp;
Paul Bakker48916f92012-09-16 19:57:18 +00001011 int renegotiation_info_seen = 0;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001012 int handshake_failure = 0;
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001013#if defined(POLARSSL_DEBUG_C)
1014 uint32_t t;
1015#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001016
1017 SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
1018
1019 /*
1020 * 0 . 0 handshake type
1021 * 1 . 3 handshake length
1022 * 4 . 5 protocol version
1023 * 6 . 9 UNIX time()
1024 * 10 . 37 random bytes
1025 */
1026 buf = ssl->in_msg;
1027
1028 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1029 {
1030 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1031 return( ret );
1032 }
1033
1034 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1035 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001036 if( ssl->renegotiation == SSL_RENEGOTIATION )
1037 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001038 ssl->renego_records_seen++;
1039
1040 if( ssl->renego_max_records >= 0 &&
1041 ssl->renego_records_seen > ssl->renego_max_records )
1042 {
1043 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
1044 "but not honored by server" ) );
1045 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1046 }
1047
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001048 SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
1049 return( POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
1050 }
1051
Paul Bakker5121ce52009-01-03 21:22:43 +00001052 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001053 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001054 }
1055
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001056#if defined(POLARSSL_SSL_PROTO_DTLS)
1057 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1058 {
1059 if( buf[0] == SSL_HS_HELLO_VERIFY_REQUEST )
1060 {
1061 SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
1062 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1063 return( ssl_parse_hello_verify_request( ssl ) );
1064 }
1065 else
1066 {
1067 /* We made it through the verification process */
1068 polarssl_free( ssl->handshake->verify_cookie );
1069 ssl->handshake->verify_cookie = NULL;
1070 ssl->handshake->verify_cookie_len = 0;
1071 }
1072 }
1073#endif /* POLARSSL_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001074
1075 if( ssl->in_hslen < 42 ||
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001076 buf[0] != SSL_HS_SERVER_HELLO )
Paul Bakker5121ce52009-01-03 21:22:43 +00001077 {
1078 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001079 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001080 }
1081
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001082 SSL_DEBUG_BUF( 3, "server hello, version", buf + 4, 2 );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001083 ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
1084 ssl->transport, buf + 4 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001085
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001086 if( ssl->major_ver < ssl->min_major_ver ||
1087 ssl->minor_ver < ssl->min_minor_ver ||
1088 ssl->major_ver > ssl->max_major_ver ||
1089 ssl->minor_ver > ssl->max_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001090 {
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001091 SSL_DEBUG_MSG( 1, ( "server version out of bounds - "
1092 " min: [%d:%d], server: [%d:%d], max: [%d:%d]",
1093 ssl->min_major_ver, ssl->min_minor_ver,
1094 ssl->major_ver, ssl->minor_ver,
1095 ssl->max_major_ver, ssl->max_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001096
1097 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1098 SSL_ALERT_MSG_PROTOCOL_VERSION );
1099
1100 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1101 }
1102
Paul Bakker1504af52012-02-11 16:17:43 +00001103#if defined(POLARSSL_DEBUG_C)
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001104 t = ( (uint32_t) buf[6] << 24 )
1105 | ( (uint32_t) buf[7] << 16 )
1106 | ( (uint32_t) buf[8] << 8 )
1107 | ( (uint32_t) buf[9] );
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001108 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakker87e5cda2012-01-14 18:14:15 +00001109#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001110
Paul Bakker48916f92012-09-16 19:57:18 +00001111 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001112
1113 n = buf[38];
1114
Paul Bakker5121ce52009-01-03 21:22:43 +00001115 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1116
Paul Bakker48916f92012-09-16 19:57:18 +00001117 if( n > 32 )
1118 {
1119 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1120 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1121 }
1122
Paul Bakker5121ce52009-01-03 21:22:43 +00001123 /*
1124 * 38 . 38 session id length
1125 * 39 . 38+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +00001126 * 39+n . 40+n chosen ciphersuite
Paul Bakker5121ce52009-01-03 21:22:43 +00001127 * 41+n . 41+n chosen compression alg.
1128 * 42+n . 43+n extensions length
1129 * 44+n . 44+n+m extensions
1130 */
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001131 if( ssl->in_hslen > 43 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001132 {
1133 ext_len = ( ( buf[42 + n] << 8 )
Paul Bakker48916f92012-09-16 19:57:18 +00001134 | ( buf[43 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001135
Paul Bakker48916f92012-09-16 19:57:18 +00001136 if( ( ext_len > 0 && ext_len < 4 ) ||
1137 ssl->in_hslen != 44 + n + ext_len )
1138 {
1139 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1140 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1141 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001142 }
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001143 else if( ssl->in_hslen == 42 + n )
1144 {
1145 ext_len = 0;
1146 }
1147 else
1148 {
1149 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1150 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1151 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001152
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001153 /* ciphersuite (used later) */
Paul Bakker5121ce52009-01-03 21:22:43 +00001154 i = ( buf[39 + n] << 8 ) | buf[40 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001155
1156 /*
1157 * Read and check compression
1158 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001159 comp = buf[41 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001160
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001161#if defined(POLARSSL_ZLIB_SUPPORT)
1162 accept_comp = 1;
1163#else
1164 accept_comp = 0;
1165#endif
1166
1167 /* See comments in ssl_write_client_hello() */
1168#if defined(POLARSSL_SSL_PROTO_DTLS)
1169 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1170 accept_comp = 0;
1171#endif
1172
1173 if( ( accept_comp == 0 && comp != SSL_COMPRESS_NULL ) ||
1174 ( comp != SSL_COMPRESS_NULL && comp != SSL_COMPRESS_DEFLATE ) )
1175 {
1176 SSL_DEBUG_MSG( 1, ( "server hello, bad compression: %d", comp ) );
1177 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1178 }
1179
Paul Bakker380da532012-04-18 16:10:25 +00001180 /*
1181 * Initialize update checksum functions
1182 */
Paul Bakker68884e32013-01-07 18:20:04 +01001183 ssl->transform_negotiate->ciphersuite_info = ssl_ciphersuite_from_id( i );
1184
1185 if( ssl->transform_negotiate->ciphersuite_info == NULL )
1186 {
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001187 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001188 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1189 }
Paul Bakker380da532012-04-18 16:10:25 +00001190
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001191 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1192
Paul Bakker5121ce52009-01-03 21:22:43 +00001193 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1194 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1195
1196 /*
1197 * Check if the session can be resumed
1198 */
Paul Bakker0a597072012-09-25 21:55:46 +00001199 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
1200 ssl->handshake->resume == 0 || n == 0 ||
Paul Bakker48916f92012-09-16 19:57:18 +00001201 ssl->session_negotiate->ciphersuite != i ||
1202 ssl->session_negotiate->compression != comp ||
1203 ssl->session_negotiate->length != n ||
1204 memcmp( ssl->session_negotiate->id, buf + 39, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001205 {
1206 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001207 ssl->handshake->resume = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001208#if defined(POLARSSL_HAVE_TIME)
Paul Bakker48916f92012-09-16 19:57:18 +00001209 ssl->session_negotiate->start = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001210#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001211 ssl->session_negotiate->ciphersuite = i;
1212 ssl->session_negotiate->compression = comp;
1213 ssl->session_negotiate->length = n;
1214 memcpy( ssl->session_negotiate->id, buf + 39, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001215 }
1216 else
1217 {
1218 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001219
1220 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1221 {
1222 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1223 return( ret );
1224 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001225 }
1226
1227 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001228 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001229
Paul Bakkere3166ce2011-01-27 17:40:50 +00001230 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001231 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[41 + n] ) );
1232
1233 i = 0;
1234 while( 1 )
1235 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001236 if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 {
1238 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001239 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001240 }
1241
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001242 if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
1243 ssl->session_negotiate->ciphersuite )
1244 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001245 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001246 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001247 }
1248
Paul Bakker2770fbd2012-07-03 13:30:23 +00001249 if( comp != SSL_COMPRESS_NULL
1250#if defined(POLARSSL_ZLIB_SUPPORT)
1251 && comp != SSL_COMPRESS_DEFLATE
1252#endif
1253 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001254 {
1255 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001256 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001257 }
Paul Bakker48916f92012-09-16 19:57:18 +00001258 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001259
Paul Bakker48916f92012-09-16 19:57:18 +00001260 ext = buf + 44 + n;
1261
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001262 SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
1263
Paul Bakker48916f92012-09-16 19:57:18 +00001264 while( ext_len )
1265 {
1266 unsigned int ext_id = ( ( ext[0] << 8 )
1267 | ( ext[1] ) );
1268 unsigned int ext_size = ( ( ext[2] << 8 )
1269 | ( ext[3] ) );
1270
1271 if( ext_size + 4 > ext_len )
1272 {
1273 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1274 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1275 }
1276
1277 switch( ext_id )
1278 {
1279 case TLS_EXT_RENEGOTIATION_INFO:
1280 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1281 renegotiation_info_seen = 1;
1282
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001283 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1284 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001285 return( ret );
1286
1287 break;
1288
Paul Bakker05decb22013-08-15 13:33:48 +02001289#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001290 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1291 SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
1292
1293 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1294 ext + 4, ext_size ) ) != 0 )
1295 {
1296 return( ret );
1297 }
1298
1299 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001300#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001301
Paul Bakker1f2bc622013-08-15 13:45:55 +02001302#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001303 case TLS_EXT_TRUNCATED_HMAC:
1304 SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
1305
1306 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1307 ext + 4, ext_size ) ) != 0 )
1308 {
1309 return( ret );
1310 }
1311
1312 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001313#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001314
Paul Bakkera503a632013-08-14 13:48:06 +02001315#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001316 case TLS_EXT_SESSION_TICKET:
1317 SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
1318
1319 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1320 ext + 4, ext_size ) ) != 0 )
1321 {
1322 return( ret );
1323 }
1324
1325 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001326#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001327
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001328#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001329 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1330 SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
1331
1332 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1333 ext + 4, ext_size ) ) != 0 )
1334 {
1335 return( ret );
1336 }
1337
1338 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001339#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001340
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001341#if defined(POLARSSL_SSL_ALPN)
1342 case TLS_EXT_ALPN:
1343 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1344
1345 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1346 return( ret );
1347
1348 break;
1349#endif /* POLARSSL_SSL_ALPN */
1350
Paul Bakker48916f92012-09-16 19:57:18 +00001351 default:
1352 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1353 ext_id ) );
1354 }
1355
1356 ext_len -= 4 + ext_size;
1357 ext += 4 + ext_size;
1358
1359 if( ext_len > 0 && ext_len < 4 )
1360 {
1361 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1362 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1363 }
1364 }
1365
1366 /*
1367 * Renegotiation security checks
1368 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001369 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1370 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001371 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001372 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1373 handshake_failure = 1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001374 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001375 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1376 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1377 renegotiation_info_seen == 0 )
1378 {
1379 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1380 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001381 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001382 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1383 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1384 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001385 {
1386 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001387 handshake_failure = 1;
1388 }
1389 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1390 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1391 renegotiation_info_seen == 1 )
1392 {
1393 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1394 handshake_failure = 1;
1395 }
1396
1397 if( handshake_failure == 1 )
1398 {
1399 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1400 return( ret );
1401
Paul Bakker48916f92012-09-16 19:57:18 +00001402 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1403 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001404
1405 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1406
1407 return( 0 );
1408}
1409
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001410#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1411 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001412static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
1413 unsigned char *end )
1414{
1415 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1416
Paul Bakker29e1f122013-04-16 13:07:56 +02001417 /*
1418 * Ephemeral DH parameters:
1419 *
1420 * struct {
1421 * opaque dh_p<1..2^16-1>;
1422 * opaque dh_g<1..2^16-1>;
1423 * opaque dh_Ys<1..2^16-1>;
1424 * } ServerDHParams;
1425 */
1426 if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1427 {
1428 SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
1429 return( ret );
1430 }
1431
1432 if( ssl->handshake->dhm_ctx.len < 64 ||
1433 ssl->handshake->dhm_ctx.len > 512 )
1434 {
1435 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
1436 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1437 }
1438
1439 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1440 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1441 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001442
1443 return( ret );
1444}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001445#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1446 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001447
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001448#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001449 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001450 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1451 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1452 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1453static int ssl_check_server_ecdh_params( const ssl_context *ssl )
1454{
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001455 const ecp_curve_info *curve_info;
1456
1457 curve_info = ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
1458 if( curve_info == NULL )
1459 {
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001460 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1461 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001462 }
1463
1464 SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001465
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001466#if defined(POLARSSL_SSL_ECP_SET_CURVES)
1467 if( ! ssl_curve_is_acceptable( ssl, ssl->handshake->ecdh_ctx.grp.id ) )
1468#else
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001469 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1470 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001471#endif
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001472 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001473
1474 SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
1475
1476 return( 0 );
1477}
Paul Bakker9af723c2014-05-01 13:03:14 +02001478#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1479 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1480 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1481 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1482 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001483
1484#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1485 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001486 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001487static int ssl_parse_server_ecdh_params( ssl_context *ssl,
1488 unsigned char **p,
1489 unsigned char *end )
1490{
1491 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1492
Paul Bakker29e1f122013-04-16 13:07:56 +02001493 /*
1494 * Ephemeral ECDH parameters:
1495 *
1496 * struct {
1497 * ECParameters curve_params;
1498 * ECPoint public;
1499 * } ServerECDHParams;
1500 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001501 if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
1502 (const unsigned char **) p, end ) ) != 0 )
1503 {
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001504 SSL_DEBUG_RET( 1, ( "ecdh_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001505 return( ret );
1506 }
1507
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001508 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001509 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001510 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001511 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1512 }
1513
Paul Bakker29e1f122013-04-16 13:07:56 +02001514 return( ret );
1515}
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001516#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001517 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1518 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001519
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001520#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001521static int ssl_parse_server_psk_hint( ssl_context *ssl,
1522 unsigned char **p,
1523 unsigned char *end )
1524{
1525 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001526 size_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001527 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001528
1529 /*
1530 * PSK parameters:
1531 *
1532 * opaque psk_identity_hint<0..2^16-1>;
1533 */
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001534 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001535 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001536
1537 if( (*p) + len > end )
1538 {
1539 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
1540 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1541 }
1542
1543 // TODO: Retrieve PSK identity hint and callback to app
1544 //
1545 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001546 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001547
1548 return( ret );
1549}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001550#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001551
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001552#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1553 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1554/*
1555 * Generate a pre-master secret and encrypt it with the server's RSA key
1556 */
1557static int ssl_write_encrypted_pms( ssl_context *ssl,
1558 size_t offset, size_t *olen,
1559 size_t pms_offset )
1560{
1561 int ret;
1562 size_t len_bytes = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 0 : 2;
1563 unsigned char *p = ssl->handshake->premaster + pms_offset;
1564
1565 /*
1566 * Generate (part of) the pre-master as
1567 * struct {
1568 * ProtocolVersion client_version;
1569 * opaque random[46];
1570 * } PreMasterSecret;
1571 */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001572 ssl_write_version( ssl->max_major_ver, ssl->max_minor_ver,
1573 ssl->transport, p );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001574
1575 if( ( ret = ssl->f_rng( ssl->p_rng, p + 2, 46 ) ) != 0 )
1576 {
1577 SSL_DEBUG_RET( 1, "f_rng", ret );
1578 return( ret );
1579 }
1580
1581 ssl->handshake->pmslen = 48;
1582
1583 /*
1584 * Now write it out, encrypted
1585 */
1586 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1587 POLARSSL_PK_RSA ) )
1588 {
1589 SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1590 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1591 }
1592
1593 if( ( ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
1594 p, ssl->handshake->pmslen,
1595 ssl->out_msg + offset + len_bytes, olen,
1596 SSL_MAX_CONTENT_LEN - offset - len_bytes,
1597 ssl->f_rng, ssl->p_rng ) ) != 0 )
1598 {
1599 SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
1600 return( ret );
1601 }
1602
1603#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1604 defined(POLARSSL_SSL_PROTO_TLS1_2)
1605 if( len_bytes == 2 )
1606 {
1607 ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
1608 ssl->out_msg[offset+1] = (unsigned char)( *olen );
1609 *olen += 2;
1610 }
1611#endif
1612
1613 return( 0 );
1614}
1615#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
1616 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001617
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001618#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001619#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001620 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1621 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001622static int ssl_parse_signature_algorithm( ssl_context *ssl,
1623 unsigned char **p,
1624 unsigned char *end,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001625 md_type_t *md_alg,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001626 pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02001627{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001628 ((void) ssl);
Paul Bakker29e1f122013-04-16 13:07:56 +02001629 *md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001630 *pk_alg = POLARSSL_PK_NONE;
1631
1632 /* Only in TLS 1.2 */
1633 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1634 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001635 return( 0 );
1636 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001637
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001638 if( (*p) + 2 > end )
Paul Bakker29e1f122013-04-16 13:07:56 +02001639 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1640
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001641 /*
1642 * Get hash algorithm
1643 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001644 if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001645 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001646 SSL_DEBUG_MSG( 2, ( "Server used unsupported "
1647 "HashAlgorithm %d", *(p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001648 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1649 }
1650
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001651 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001652 * Get signature algorithm
1653 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001654 if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001655 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001656 SSL_DEBUG_MSG( 2, ( "server used unsupported "
1657 "SignatureAlgorithm %d", (*p)[1] ) );
1658 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001659 }
1660
1661 SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
1662 SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
1663 *p += 2;
1664
1665 return( 0 );
1666}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001667#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001668 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1669 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001670#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001671
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001672
1673#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1674 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1675static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
1676{
1677 int ret;
1678 const ecp_keypair *peer_key;
1679
1680 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1681 POLARSSL_PK_ECKEY ) )
1682 {
1683 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
1684 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1685 }
1686
1687 peer_key = pk_ec( ssl->session_negotiate->peer_cert->pk );
1688
1689 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
1690 POLARSSL_ECDH_THEIRS ) ) != 0 )
1691 {
1692 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
1693 return( ret );
1694 }
1695
1696 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1697 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001698 SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001699 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
1700 }
1701
1702 return( ret );
1703}
1704#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1705 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1706
Paul Bakker41c83d32013-03-20 14:39:14 +01001707static int ssl_parse_server_key_exchange( ssl_context *ssl )
1708{
Paul Bakker23986e52011-04-24 08:57:21 +00001709 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001710 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001711 unsigned char *p, *end;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001712#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001713 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1714 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001715 size_t sig_len, params_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001716 unsigned char hash[64];
Paul Bakkerc70b9822013-04-07 22:00:46 +02001717 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001718 size_t hashlen;
1719 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001720#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001721
1722 SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
1723
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001724#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001725 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001726 {
1727 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1728 ssl->state++;
1729 return( 0 );
1730 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001731 ((void) p);
1732 ((void) end);
1733#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001734
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001735#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1736 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1737 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
1738 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
1739 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001740 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
1741 {
1742 SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
1743 return( ret );
1744 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001745
1746 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1747 ssl->state++;
1748 return( 0 );
1749 }
1750 ((void) p);
1751 ((void) end);
Paul Bakker9af723c2014-05-01 13:03:14 +02001752#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1753 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001754
Paul Bakker5121ce52009-01-03 21:22:43 +00001755 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1756 {
1757 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1758 return( ret );
1759 }
1760
1761 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1762 {
1763 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001764 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001765 }
1766
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001767 /*
1768 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
1769 * doesn't use a psk_identity_hint
1770 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001771 if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
1772 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001773 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1774 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02001775 {
1776 ssl->record_read = 1;
1777 goto exit;
1778 }
1779
1780 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1781 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001782 }
1783
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001784 p = ssl->in_msg + 4;
1785 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001786 SSL_DEBUG_BUF( 3, "server key exchange", p, ssl->in_hslen - 4 );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001787
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001788#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1789 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1790 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1791 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1792 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1793 {
1794 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1795 {
1796 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1797 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1798 }
1799 } /* FALLTROUGH */
1800#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
1801
1802#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
1803 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1804 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1805 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1806 ; /* nothing more to do */
1807 else
1808#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
1809 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
1810#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1811 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1812 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1813 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001814 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001815 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001816 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001817 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001818 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1819 }
1820 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001821 else
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001822#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1823 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001824#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001825 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001826 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1827 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001828 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001829 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001830 {
1831 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
1832 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001833 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1834 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1835 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001836 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001837 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001838#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001839 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001840 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001841 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001842 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001843 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001844 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001845
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001846#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001847 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1848 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001849 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001850 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1851 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001852 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001853 params_len = p - ( ssl->in_msg + 4 );
1854
Paul Bakker29e1f122013-04-16 13:07:56 +02001855 /*
1856 * Handle the digitally-signed structure
1857 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001858#if defined(POLARSSL_SSL_PROTO_TLS1_2)
1859 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001860 {
Paul Bakker9659dae2013-08-28 16:21:34 +02001861 if( ssl_parse_signature_algorithm( ssl, &p, end,
1862 &md_alg, &pk_alg ) != 0 )
1863 {
1864 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1865 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1866 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001867
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001868 if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001869 {
1870 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1871 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1872 }
1873 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001874 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001875#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001876#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1877 defined(POLARSSL_SSL_PROTO_TLS1_1)
1878 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001879 {
1880 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Paul Bakker1ef83d62012-04-11 12:09:53 +00001881
Paul Bakker9659dae2013-08-28 16:21:34 +02001882 /* Default hash for ECDSA is SHA-1 */
1883 if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
1884 md_alg = POLARSSL_MD_SHA1;
1885 }
1886 else
1887#endif
1888 {
1889 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001890 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker9659dae2013-08-28 16:21:34 +02001891 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001892
1893 /*
1894 * Read signature
1895 */
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001896 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00001897 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001898
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001899 if( end != p + sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01001900 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001901 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001902 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1903 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001904
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001905 SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02001906
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001907 /*
1908 * Compute the hash that has been signed
1909 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001910#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1911 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001912 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001913 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001914 md5_context md5;
1915 sha1_context sha1;
1916
Paul Bakker5b4af392014-06-26 12:09:34 +02001917 md5_init( &md5 );
1918 sha1_init( &sha1 );
1919
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001920 hashlen = 36;
1921
Paul Bakker29e1f122013-04-16 13:07:56 +02001922 /*
1923 * digitally-signed struct {
1924 * opaque md5_hash[16];
1925 * opaque sha_hash[20];
1926 * };
1927 *
1928 * md5_hash
1929 * MD5(ClientHello.random + ServerHello.random
1930 * + ServerParams);
1931 * sha_hash
1932 * SHA(ClientHello.random + ServerHello.random
1933 * + ServerParams);
1934 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001935 md5_starts( &md5 );
1936 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001937 md5_update( &md5, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001938 md5_finish( &md5, hash );
1939
1940 sha1_starts( &sha1 );
1941 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001942 sha1_update( &sha1, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001943 sha1_finish( &sha1, hash + 16 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001944
1945 md5_free( &md5 );
1946 sha1_free( &sha1 );
Paul Bakker29e1f122013-04-16 13:07:56 +02001947 }
1948 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001949#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
1950 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001951#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1952 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02001953 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001954 {
1955 md_context_t ctx;
1956
Paul Bakker84bbeb52014-07-01 14:53:22 +02001957 md_init( &ctx );
1958
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001959 /* Info from md_alg will be used instead */
1960 hashlen = 0;
Paul Bakker29e1f122013-04-16 13:07:56 +02001961
1962 /*
1963 * digitally-signed struct {
1964 * opaque client_random[32];
1965 * opaque server_random[32];
1966 * ServerDHParams params;
1967 * };
1968 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001969 if( ( ret = md_init_ctx( &ctx,
1970 md_info_from_type( md_alg ) ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001971 {
1972 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
1973 return( ret );
1974 }
1975
1976 md_starts( &ctx );
1977 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001978 md_update( &ctx, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001979 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02001980 md_free( &ctx );
Paul Bakker29e1f122013-04-16 13:07:56 +02001981 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001982 else
Paul Bakker9659dae2013-08-28 16:21:34 +02001983#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1984 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001985 {
Paul Bakker577e0062013-08-28 11:57:20 +02001986 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001987 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001988 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001989
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02001990 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
1991 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker29e1f122013-04-16 13:07:56 +02001992
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001993 /*
1994 * Verify signature
1995 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02001996 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001997 {
1998 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1999 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2000 }
2001
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002002 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
2003 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002004 {
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002005 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002006 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002007 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002008 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002009#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002010 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2011 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002012
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002013exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002014 ssl->state++;
2015
2016 SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
2017
2018 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002019}
2020
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002021#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2022 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2023 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2024 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2025static int ssl_parse_certificate_request( ssl_context *ssl )
2026{
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002027 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2028
2029 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2030
2031 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2032 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2033 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2034 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2035 {
2036 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2037 ssl->state++;
2038 return( 0 );
2039 }
2040
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002041 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2042 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002043}
2044#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002045static int ssl_parse_certificate_request( ssl_context *ssl )
2046{
2047 int ret;
Paul Bakker926af752012-11-23 13:38:07 +01002048 unsigned char *buf, *p;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002049 size_t n = 0, m = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002050 size_t cert_type_len = 0, dn_len = 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002051 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002052
2053 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2054
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002055 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2056 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2057 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2058 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2059 {
2060 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2061 ssl->state++;
2062 return( 0 );
2063 }
2064
Paul Bakker5121ce52009-01-03 21:22:43 +00002065 /*
2066 * 0 . 0 handshake type
2067 * 1 . 3 handshake length
Paul Bakker926af752012-11-23 13:38:07 +01002068 * 4 . 4 cert type count
2069 * 5 .. m-1 cert types
2070 * m .. m+1 sig alg length (TLS 1.2 only)
2071 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002072 * n .. n+1 length of all DNs
2073 * n+2 .. n+3 length of DN 1
2074 * n+4 .. ... Distinguished Name #1
2075 * ... .. ... length of DN 2, etc.
2076 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002077 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002078 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002079 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2080 {
2081 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2082 return( ret );
2083 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002084
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002085 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2086 {
2087 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2088 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2089 }
2090
2091 ssl->record_read = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 }
2093
2094 ssl->client_auth = 0;
2095 ssl->state++;
2096
2097 if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST )
2098 ssl->client_auth++;
2099
2100 SSL_DEBUG_MSG( 3, ( "got %s certificate request",
2101 ssl->client_auth ? "a" : "no" ) );
2102
Paul Bakker926af752012-11-23 13:38:07 +01002103 if( ssl->client_auth == 0 )
2104 goto exit;
2105
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002106 ssl->record_read = 0;
2107
Paul Bakker926af752012-11-23 13:38:07 +01002108 // TODO: handshake_failure alert for an anonymous server to request
2109 // client authentication
2110
2111 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002112
Paul Bakker926af752012-11-23 13:38:07 +01002113 // Retrieve cert types
2114 //
2115 cert_type_len = buf[4];
2116 n = cert_type_len;
2117
2118 if( ssl->in_hslen < 6 + n )
2119 {
2120 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2121 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2122 }
2123
Paul Bakker73d44312013-05-22 13:56:26 +02002124 p = buf + 5;
Paul Bakker926af752012-11-23 13:38:07 +01002125 while( cert_type_len > 0 )
2126 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002127#if defined(POLARSSL_RSA_C)
2128 if( *p == SSL_CERT_TYPE_RSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002129 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker926af752012-11-23 13:38:07 +01002130 {
2131 ssl->handshake->cert_type = SSL_CERT_TYPE_RSA_SIGN;
2132 break;
2133 }
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002134 else
2135#endif
2136#if defined(POLARSSL_ECDSA_C)
2137 if( *p == SSL_CERT_TYPE_ECDSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002138 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002139 {
2140 ssl->handshake->cert_type = SSL_CERT_TYPE_ECDSA_SIGN;
2141 break;
2142 }
2143 else
2144#endif
2145 {
2146 ; /* Unsupported cert type, ignore */
2147 }
Paul Bakker926af752012-11-23 13:38:07 +01002148
2149 cert_type_len--;
2150 p++;
2151 }
2152
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002153#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002154 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2155 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002156 /* Ignored, see comments about hash in write_certificate_verify */
2157 // TODO: should check the signature part against our pk_key though
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002158 size_t sig_alg_len = ( ( buf[5 + n] << 8 )
2159 | ( buf[6 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002160
2161 p = buf + 7 + n;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002162 m += 2;
Paul Bakker926af752012-11-23 13:38:07 +01002163 n += sig_alg_len;
2164
2165 if( ssl->in_hslen < 6 + n )
2166 {
2167 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2168 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2169 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002170 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002171#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker926af752012-11-23 13:38:07 +01002172
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002173 /* Ignore certificate_authorities, we only have one cert anyway */
2174 // TODO: should not send cert if no CA matches
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002175 dn_len = ( ( buf[5 + m + n] << 8 )
2176 | ( buf[6 + m + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002177
2178 n += dn_len;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002179 if( ssl->in_hslen != 7 + m + n )
Paul Bakker926af752012-11-23 13:38:07 +01002180 {
2181 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2182 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2183 }
2184
2185exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002186 SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2187
2188 return( 0 );
2189}
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002190#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2191 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2192 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2193 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002194
2195static int ssl_parse_server_hello_done( ssl_context *ssl )
2196{
2197 int ret;
2198
2199 SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
2200
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002201 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002202 {
2203 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2204 {
2205 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2206 return( ret );
2207 }
2208
2209 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2210 {
2211 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002212 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002213 }
2214 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002215 ssl->record_read = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002216
2217 if( ssl->in_hslen != 4 ||
2218 ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
2219 {
2220 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002221 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002222 }
2223
2224 ssl->state++;
2225
2226 SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
2227
2228 return( 0 );
2229}
2230
2231static int ssl_write_client_key_exchange( ssl_context *ssl )
2232{
Paul Bakker23986e52011-04-24 08:57:21 +00002233 int ret;
2234 size_t i, n;
Paul Bakker41c83d32013-03-20 14:39:14 +01002235 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002236
2237 SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
2238
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002239#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002240 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002241 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002242 /*
2243 * DHM key exchange -- send G^X mod P
2244 */
Paul Bakker48916f92012-09-16 19:57:18 +00002245 n = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002246
2247 ssl->out_msg[4] = (unsigned char)( n >> 8 );
2248 ssl->out_msg[5] = (unsigned char)( n );
2249 i = 6;
2250
Paul Bakker29b64762012-09-25 09:36:44 +00002251 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002252 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 &ssl->out_msg[i], n,
2254 ssl->f_rng, ssl->p_rng );
2255 if( ret != 0 )
2256 {
2257 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2258 return( ret );
2259 }
2260
Paul Bakker48916f92012-09-16 19:57:18 +00002261 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2262 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002263
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002264 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002265
Paul Bakker48916f92012-09-16 19:57:18 +00002266 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2267 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002268 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002269 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002270 {
2271 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2272 return( ret );
2273 }
2274
Paul Bakker48916f92012-09-16 19:57:18 +00002275 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002276 }
2277 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002278#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002279#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002280 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2281 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2282 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002283 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002284 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2285 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2286 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002287 {
2288 /*
2289 * ECDH key exchange -- send client public value
2290 */
2291 i = 4;
2292
2293 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
2294 &n,
2295 &ssl->out_msg[i], 1000,
2296 ssl->f_rng, ssl->p_rng );
2297 if( ret != 0 )
2298 {
2299 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2300 return( ret );
2301 }
2302
2303 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2304
2305 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2306 &ssl->handshake->pmslen,
2307 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002308 POLARSSL_MPI_MAX_SIZE,
2309 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002310 {
2311 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2312 return( ret );
2313 }
2314
2315 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2316 }
2317 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002318#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002319 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2320 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2321 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002322#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002323 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002324 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002325 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2326 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002327 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002328 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002329 * opaque psk_identity<0..2^16-1>;
2330 */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002331 if( ssl->psk == NULL || ssl->psk_identity == NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002332 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2333
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002334 i = 4;
2335 n = ssl->psk_identity_len;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002336 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2337 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002338
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002339 memcpy( ssl->out_msg + i, ssl->psk_identity, ssl->psk_identity_len );
2340 i += ssl->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002341
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002342#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002343 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002344 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002345 n = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002346 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002347 else
2348#endif
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002349#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2350 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2351 {
2352 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2353 return( ret );
2354 }
2355 else
2356#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002357#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002358 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002359 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002360 /*
2361 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
2362 */
2363 n = ssl->handshake->dhm_ctx.len;
2364 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2365 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002366
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002367 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakker68881672013-10-15 13:24:01 +02002368 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002369 &ssl->out_msg[i], n,
2370 ssl->f_rng, ssl->p_rng );
2371 if( ret != 0 )
2372 {
2373 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2374 return( ret );
2375 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002376 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002377 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002378#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002379#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002380 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002381 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002382 /*
2383 * ClientECDiffieHellmanPublic public;
2384 */
2385 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
2386 &ssl->out_msg[i], SSL_MAX_CONTENT_LEN - i,
2387 ssl->f_rng, ssl->p_rng );
2388 if( ret != 0 )
2389 {
2390 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2391 return( ret );
2392 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002393
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002394 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2395 }
2396 else
2397#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2398 {
2399 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002400 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002401 }
2402
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002403 if( ( ret = ssl_psk_derive_premaster( ssl,
2404 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002405 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002406 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002407 return( ret );
2408 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002409 }
2410 else
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002411#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002412#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +02002413 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002414 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002415 i = 4;
2416 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002417 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002418 }
Paul Bakkered27a042013-04-18 22:46:23 +02002419 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002420#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002421 {
2422 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002423 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002424 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02002425 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002426
Paul Bakkerff60ee62010-03-16 21:09:09 +00002427 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2428 {
2429 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2430 return( ret );
2431 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002432
2433 ssl->out_msglen = i + n;
2434 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2435 ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE;
2436
2437 ssl->state++;
2438
2439 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2440 {
2441 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2442 return( ret );
2443 }
2444
2445 SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
2446
2447 return( 0 );
2448}
2449
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002450#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2451 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002452 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2453 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002454static int ssl_write_certificate_verify( ssl_context *ssl )
2455{
Paul Bakkered27a042013-04-18 22:46:23 +02002456 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002457
2458 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2459
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002460 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002461 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002462 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002463 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002464 {
2465 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2466 ssl->state++;
2467 return( 0 );
2468 }
2469
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002470 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2471 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002472}
2473#else
2474static int ssl_write_certificate_verify( ssl_context *ssl )
2475{
2476 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2477 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2478 size_t n = 0, offset = 0;
2479 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002480 unsigned char *hash_start = hash;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002481 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002482 unsigned int hashlen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002483
2484 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2485
2486 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002487 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002488 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002489 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2490 {
2491 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2492 ssl->state++;
2493 return( 0 );
2494 }
2495
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002496 if( ssl->client_auth == 0 || ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002497 {
2498 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2499 ssl->state++;
2500 return( 0 );
2501 }
2502
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002503 if( ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002504 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00002505 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2506 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002507 }
2508
2509 /*
2510 * Make an RSA signature of the handshake digests
2511 */
Paul Bakker48916f92012-09-16 19:57:18 +00002512 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002513
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002514#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2515 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker926af752012-11-23 13:38:07 +01002516 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002517 {
Paul Bakker926af752012-11-23 13:38:07 +01002518 /*
2519 * digitally-signed struct {
2520 * opaque md5_hash[16];
2521 * opaque sha_hash[20];
2522 * };
2523 *
2524 * md5_hash
2525 * MD5(handshake_messages);
2526 *
2527 * sha_hash
2528 * SHA(handshake_messages);
2529 */
2530 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002531 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002532
2533 /*
2534 * For ECDSA, default hash is SHA-1 only
2535 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002536 if( pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002537 {
2538 hash_start += 16;
2539 hashlen -= 16;
2540 md_alg = POLARSSL_MD_SHA1;
2541 }
Paul Bakker926af752012-11-23 13:38:07 +01002542 }
2543 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002544#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2545 POLARSSL_SSL_PROTO_TLS1_1 */
2546#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2547 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002548 {
2549 /*
2550 * digitally-signed struct {
2551 * opaque handshake_messages[handshake_messages_length];
2552 * };
2553 *
2554 * Taking shortcut here. We assume that the server always allows the
2555 * PRF Hash function and has sent it in the allowed signature
2556 * algorithms list received in the Certificate Request message.
2557 *
2558 * Until we encounter a server that does not, we will take this
2559 * shortcut.
2560 *
2561 * Reason: Otherwise we should have running hashes for SHA512 and SHA224
2562 * in order to satisfy 'weird' needs from the server side.
2563 */
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002564 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2565 POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002566 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002567 md_alg = POLARSSL_MD_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002568 ssl->out_msg[4] = SSL_HASH_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002569 }
2570 else
2571 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002572 md_alg = POLARSSL_MD_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002573 ssl->out_msg[4] = SSL_HASH_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002574 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002575 ssl->out_msg[5] = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002576
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002577 /* Info from md_alg will be used instead */
2578 hashlen = 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002579 offset = 2;
2580 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002581 else
2582#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002583 {
2584 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002585 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002586 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002587
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002588 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002589 ssl->out_msg + 6 + offset, &n,
2590 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002591 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002592 SSL_DEBUG_RET( 1, "pk_sign", ret );
2593 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002594 }
Paul Bakker926af752012-11-23 13:38:07 +01002595
Paul Bakker1ef83d62012-04-11 12:09:53 +00002596 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
2597 ssl->out_msg[5 + offset] = (unsigned char)( n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002598
Paul Bakker1ef83d62012-04-11 12:09:53 +00002599 ssl->out_msglen = 6 + n + offset;
Paul Bakker5121ce52009-01-03 21:22:43 +00002600 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2601 ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY;
2602
2603 ssl->state++;
2604
2605 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2606 {
2607 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2608 return( ret );
2609 }
2610
2611 SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
2612
Paul Bakkered27a042013-04-18 22:46:23 +02002613 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002614}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002615#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2616 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2617 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002618
Paul Bakkera503a632013-08-14 13:48:06 +02002619#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002620static int ssl_parse_new_session_ticket( ssl_context *ssl )
2621{
2622 int ret;
2623 uint32_t lifetime;
2624 size_t ticket_len;
2625 unsigned char *ticket;
2626
2627 SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2628
2629 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2630 {
2631 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2632 return( ret );
2633 }
2634
2635 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2636 {
2637 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2638 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2639 }
2640
2641 /*
2642 * struct {
2643 * uint32 ticket_lifetime_hint;
2644 * opaque ticket<0..2^16-1>;
2645 * } NewSessionTicket;
2646 *
2647 * 0 . 0 handshake message type
2648 * 1 . 3 handshake message length
2649 * 4 . 7 ticket_lifetime_hint
2650 * 8 . 9 ticket_len (n)
2651 * 10 . 9+n ticket content
2652 */
2653 if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
2654 ssl->in_hslen < 10 )
2655 {
2656 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2657 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2658 }
2659
2660 lifetime = ( ssl->in_msg[4] << 24 ) | ( ssl->in_msg[5] << 16 ) |
2661 ( ssl->in_msg[6] << 8 ) | ( ssl->in_msg[7] );
2662
2663 ticket_len = ( ssl->in_msg[8] << 8 ) | ( ssl->in_msg[9] );
2664
2665 if( ticket_len + 10 != ssl->in_hslen )
2666 {
2667 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2668 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2669 }
2670
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002671 SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
2672
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002673 /* We're not waiting for a NewSessionTicket message any more */
2674 ssl->handshake->new_session_ticket = 0;
2675
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002676 /*
2677 * Zero-length ticket means the server changed his mind and doesn't want
2678 * to send a ticket after all, so just forget it
2679 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002680 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002681 return( 0 );
2682
Paul Bakker34617722014-06-13 17:20:13 +02002683 polarssl_zeroize( ssl->session_negotiate->ticket,
2684 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002685 polarssl_free( ssl->session_negotiate->ticket );
2686 ssl->session_negotiate->ticket = NULL;
2687 ssl->session_negotiate->ticket_len = 0;
2688
2689 if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
2690 {
2691 SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
2692 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2693 }
2694
2695 memcpy( ticket, ssl->in_msg + 10, ticket_len );
2696
2697 ssl->session_negotiate->ticket = ticket;
2698 ssl->session_negotiate->ticket_len = ticket_len;
2699 ssl->session_negotiate->ticket_lifetime = lifetime;
2700
2701 /*
2702 * RFC 5077 section 3.4:
2703 * "If the client receives a session ticket from the server, then it
2704 * discards any Session ID that was sent in the ServerHello."
2705 */
2706 SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
2707 ssl->session_negotiate->length = 0;
2708
2709 SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2710
2711 return( 0 );
2712}
Paul Bakkera503a632013-08-14 13:48:06 +02002713#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002714
Paul Bakker5121ce52009-01-03 21:22:43 +00002715/*
Paul Bakker1961b702013-01-25 14:49:24 +01002716 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002717 */
Paul Bakker1961b702013-01-25 14:49:24 +01002718int ssl_handshake_client_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002719{
2720 int ret = 0;
2721
Paul Bakker1961b702013-01-25 14:49:24 +01002722 if( ssl->state == SSL_HANDSHAKE_OVER )
2723 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002724
Paul Bakker1961b702013-01-25 14:49:24 +01002725 SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
2726
2727 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2728 return( ret );
2729
2730 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002731 {
Paul Bakker1961b702013-01-25 14:49:24 +01002732 case SSL_HELLO_REQUEST:
2733 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002734 break;
2735
Paul Bakker1961b702013-01-25 14:49:24 +01002736 /*
2737 * ==> ClientHello
2738 */
2739 case SSL_CLIENT_HELLO:
2740 ret = ssl_write_client_hello( ssl );
2741 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002742
Paul Bakker1961b702013-01-25 14:49:24 +01002743 /*
2744 * <== ServerHello
2745 * Certificate
2746 * ( ServerKeyExchange )
2747 * ( CertificateRequest )
2748 * ServerHelloDone
2749 */
2750 case SSL_SERVER_HELLO:
2751 ret = ssl_parse_server_hello( ssl );
2752 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002753
Paul Bakker1961b702013-01-25 14:49:24 +01002754 case SSL_SERVER_CERTIFICATE:
2755 ret = ssl_parse_certificate( ssl );
2756 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002757
Paul Bakker1961b702013-01-25 14:49:24 +01002758 case SSL_SERVER_KEY_EXCHANGE:
2759 ret = ssl_parse_server_key_exchange( ssl );
2760 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002761
Paul Bakker1961b702013-01-25 14:49:24 +01002762 case SSL_CERTIFICATE_REQUEST:
2763 ret = ssl_parse_certificate_request( ssl );
2764 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002765
Paul Bakker1961b702013-01-25 14:49:24 +01002766 case SSL_SERVER_HELLO_DONE:
2767 ret = ssl_parse_server_hello_done( ssl );
2768 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002769
Paul Bakker1961b702013-01-25 14:49:24 +01002770 /*
2771 * ==> ( Certificate/Alert )
2772 * ClientKeyExchange
2773 * ( CertificateVerify )
2774 * ChangeCipherSpec
2775 * Finished
2776 */
2777 case SSL_CLIENT_CERTIFICATE:
2778 ret = ssl_write_certificate( ssl );
2779 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002780
Paul Bakker1961b702013-01-25 14:49:24 +01002781 case SSL_CLIENT_KEY_EXCHANGE:
2782 ret = ssl_write_client_key_exchange( ssl );
2783 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002784
Paul Bakker1961b702013-01-25 14:49:24 +01002785 case SSL_CERTIFICATE_VERIFY:
2786 ret = ssl_write_certificate_verify( ssl );
2787 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002788
Paul Bakker1961b702013-01-25 14:49:24 +01002789 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2790 ret = ssl_write_change_cipher_spec( ssl );
2791 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002792
Paul Bakker1961b702013-01-25 14:49:24 +01002793 case SSL_CLIENT_FINISHED:
2794 ret = ssl_write_finished( ssl );
2795 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002796
Paul Bakker1961b702013-01-25 14:49:24 +01002797 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002798 * <== ( NewSessionTicket )
2799 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002800 * Finished
2801 */
2802 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002803#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002804 if( ssl->handshake->new_session_ticket != 0 )
2805 ret = ssl_parse_new_session_ticket( ssl );
2806 else
Paul Bakkera503a632013-08-14 13:48:06 +02002807#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002808 ret = ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002809 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002810
Paul Bakker1961b702013-01-25 14:49:24 +01002811 case SSL_SERVER_FINISHED:
2812 ret = ssl_parse_finished( ssl );
2813 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002814
Paul Bakker1961b702013-01-25 14:49:24 +01002815 case SSL_FLUSH_BUFFERS:
2816 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2817 ssl->state = SSL_HANDSHAKE_WRAPUP;
2818 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002819
Paul Bakker1961b702013-01-25 14:49:24 +01002820 case SSL_HANDSHAKE_WRAPUP:
2821 ssl_handshake_wrapup( ssl );
2822 break;
Paul Bakker48916f92012-09-16 19:57:18 +00002823
Paul Bakker1961b702013-01-25 14:49:24 +01002824 default:
2825 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2826 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2827 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002828
2829 return( ret );
2830}
Paul Bakker9af723c2014-05-01 13:03:14 +02002831#endif /* POLARSSL_SSL_CLI_C */