blob: 78572ebf5ca5d79400effb13f56525588c374304 [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
Manuel Pégourié-Gonnardfb2d2232014-07-22 15:59:14 +0200458 /*
459 * When responding to a verify request, MUST reuse random (RFC 6347 4.2.1)
460 */
461#if defined(POLARSSL_SSL_PROTO_DTLS)
462 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
463 ssl->handshake->verify_cookie != NULL )
464 {
465 return( 0 );
466 }
467#endif
468
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200469#if defined(POLARSSL_HAVE_TIME)
470 t = time( NULL );
471 *p++ = (unsigned char)( t >> 24 );
472 *p++ = (unsigned char)( t >> 16 );
473 *p++ = (unsigned char)( t >> 8 );
474 *p++ = (unsigned char)( t );
475
476 SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
477#else
478 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
479 return( ret );
480
481 p += 4;
482#endif /* POLARSSL_HAVE_TIME */
483
484 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
485 return( ret );
486
487 return( 0 );
488}
489
Paul Bakker5121ce52009-01-03 21:22:43 +0000490static int ssl_write_client_hello( ssl_context *ssl )
491{
Paul Bakker23986e52011-04-24 08:57:21 +0000492 int ret;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100493 size_t i, n, olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000494 unsigned char *buf;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200495 unsigned char *p, *q;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200496 unsigned char offer_compress;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200497 const int *ciphersuites;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200498 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000499
500 SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
501
Paul Bakkera9a028e2013-11-21 17:31:06 +0100502 if( ssl->f_rng == NULL )
503 {
504 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
505 return( POLARSSL_ERR_SSL_NO_RNG );
506 }
507
Paul Bakker48916f92012-09-16 19:57:18 +0000508 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
509 {
Paul Bakker993d11d2012-09-28 15:00:12 +0000510 ssl->major_ver = ssl->min_major_ver;
511 ssl->minor_ver = ssl->min_minor_ver;
Paul Bakker48916f92012-09-16 19:57:18 +0000512 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000513
Paul Bakker490ecc82011-10-06 13:04:09 +0000514 if( ssl->max_major_ver == 0 && ssl->max_minor_ver == 0 )
515 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200516 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
517 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker490ecc82011-10-06 13:04:09 +0000518 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000519
520 /*
521 * 0 . 0 handshake type
522 * 1 . 3 handshake length
523 * 4 . 5 highest version supported
524 * 6 . 9 current UNIX time
525 * 10 . 37 random bytes
526 */
527 buf = ssl->out_msg;
528 p = buf + 4;
529
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +0100530 ssl_write_version( ssl->max_major_ver, ssl->max_minor_ver,
531 ssl->transport, p );
532 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000533
534 SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
535 buf[4], buf[5] ) );
536
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200537 if( ( ret = ssl_generate_random( ssl ) ) != 0 )
538 {
539 SSL_DEBUG_RET( 1, "ssl_generate_random", ret );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200540 return( ret );
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200541 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200542
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200543 memcpy( p, ssl->handshake->randbytes, 32 );
544 SSL_DEBUG_BUF( 3, "client hello, random bytes", p, 32 );
545 p += 32;
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
547 /*
548 * 38 . 38 session id length
549 * 39 . 39+n session id
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100550 * 39+n . 39+n DTLS only: cookie length (1 byte)
551 * 40+n . .. DTSL only: cookie
552 * .. . .. ciphersuitelist length (2 bytes)
553 * .. . .. ciphersuitelist
554 * .. . .. compression methods length (1 byte)
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000555 * .. . .. compression methods
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100556 * .. . .. extensions length (2 bytes)
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000557 * .. . .. extensions
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 */
Paul Bakker48916f92012-09-16 19:57:18 +0000559 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000560
Paul Bakker0a597072012-09-25 21:55:46 +0000561 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE || n < 16 || n > 32 ||
562 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200563 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 n = 0;
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200565 }
566
Paul Bakkera503a632013-08-14 13:48:06 +0200567#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200568 /*
569 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
570 * generate and include a Session ID in the TLS ClientHello."
571 */
572 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
573 ssl->session_negotiate->ticket != NULL &&
574 ssl->session_negotiate->ticket_len != 0 )
575 {
576 ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
577
578 if( ret != 0 )
579 return( ret );
580
581 ssl->session_negotiate->length = n = 32;
582 }
Paul Bakkera503a632013-08-14 13:48:06 +0200583#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
585 *p++ = (unsigned char) n;
586
587 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000588 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000589
590 SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
591 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
592
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100593 /*
594 * DTLS cookie
595 */
596#if defined(POLARSSL_SSL_PROTO_DTLS)
597 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
598 {
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200599 if( ssl->handshake->verify_cookie == NULL )
600 {
601 SSL_DEBUG_MSG( 3, ( "no verify cookie to send" ) );
602 *p++ = 0;
603 }
604 else
605 {
606 SSL_DEBUG_BUF( 3, "client hello, cookie",
607 ssl->handshake->verify_cookie,
608 ssl->handshake->verify_cookie_len );
609
610 *p++ = ssl->handshake->verify_cookie_len;
611 memcpy( p, ssl->handshake->verify_cookie,
612 ssl->handshake->verify_cookie_len );
613 p += ssl->handshake->verify_cookie_len;
614 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100615 }
616#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000617
Paul Bakker48916f92012-09-16 19:57:18 +0000618 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100619 * Ciphersuite list
Paul Bakker48916f92012-09-16 19:57:18 +0000620 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100621 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
622
623 /* Skip writing ciphersuite length for now */
624 n = 0;
625 q = p;
626 p += 2;
627
628 /* Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV */
Paul Bakker48916f92012-09-16 19:57:18 +0000629 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
630 {
631 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
632 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
Paul Bakker2fbefde2013-06-29 16:01:15 +0200633 n++;
Paul Bakker48916f92012-09-16 19:57:18 +0000634 }
635
Paul Bakker2fbefde2013-06-29 16:01:15 +0200636 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000637 {
Paul Bakker2fbefde2013-06-29 16:01:15 +0200638 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
639
640 if( ciphersuite_info == NULL )
641 continue;
642
643 if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
644 ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
645 continue;
646
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +0100647#if defined(POLARSSL_SSL_PROTO_DTLS)
648 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
649 ( ciphersuite_info->flags & POLARSSL_CIPHERSUITE_NODTLS ) )
650 continue;
651#endif
652
Paul Bakkere3166ce2011-01-27 17:40:50 +0000653 SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200654 ciphersuites[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000655
Paul Bakker2fbefde2013-06-29 16:01:15 +0200656 n++;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200657 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
658 *p++ = (unsigned char)( ciphersuites[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000659 }
660
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200661 /* Some versions of OpenSSL don't handle it correctly if not at end */
662#if defined(POLARSSL_SSL_FALLBACK_SCSV)
663 if( ssl->fallback == SSL_IS_FALLBACK )
664 {
665 SSL_DEBUG_MSG( 3, ( "adding FALLBACK_SCSV" ) );
666 *p++ = (unsigned char)( SSL_FALLBACK_SCSV >> 8 );
667 *p++ = (unsigned char)( SSL_FALLBACK_SCSV );
668 n++;
669 }
670#endif
671
Paul Bakker2fbefde2013-06-29 16:01:15 +0200672 *q++ = (unsigned char)( n >> 7 );
673 *q++ = (unsigned char)( n << 1 );
674
675 SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
676
Paul Bakker2770fbd2012-07-03 13:30:23 +0000677#if defined(POLARSSL_ZLIB_SUPPORT)
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200678 offer_compress = 1;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000679#else
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200680 offer_compress = 0;
681#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000682
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200683 /*
684 * We don't support compression with DTLS right now: is many records come
685 * in the same datagram, uncompressing one could overwrite the next one.
686 * We don't want to add complexity for handling that case unless there is
687 * an actual need for it.
688 */
689#if defined(POLARSSL_SSL_PROTO_DTLS)
690 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
691 offer_compress = 0;
692#endif
693
694 if( offer_compress )
695 {
696 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
697 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
698 SSL_COMPRESS_DEFLATE, SSL_COMPRESS_NULL ) );
699
700 *p++ = 2;
701 *p++ = SSL_COMPRESS_DEFLATE;
702 *p++ = SSL_COMPRESS_NULL;
703 }
704 else
705 {
706 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
707 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d",
708 SSL_COMPRESS_NULL ) );
709
710 *p++ = 1;
711 *p++ = SSL_COMPRESS_NULL;
712 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000713
Paul Bakkerd3edc862013-03-20 16:07:17 +0100714 // First write extensions, then the total length
715 //
Paul Bakker0be444a2013-08-27 21:55:01 +0200716#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100717 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
718 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +0200719#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000720
Paul Bakkerd3edc862013-03-20 16:07:17 +0100721 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
722 ext_len += olen;
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000723
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200724#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100725 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
726 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200727#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000728
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200729#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100730 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
731 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100732
Paul Bakkerd3edc862013-03-20 16:07:17 +0100733 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
734 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100735#endif
736
Paul Bakker05decb22013-08-15 13:33:48 +0200737#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200738 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
739 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +0200740#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200741
Paul Bakker1f2bc622013-08-15 13:45:55 +0200742#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200743 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
744 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200745#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200746
Paul Bakkera503a632013-08-14 13:48:06 +0200747#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200748 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
749 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +0200750#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200751
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200752#if defined(POLARSSL_SSL_ALPN)
753 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
754 ext_len += olen;
755#endif
756
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000757 SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
758 ext_len ) );
759
Paul Bakkera7036632014-04-30 10:15:38 +0200760 if( ext_len > 0 )
761 {
762 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
763 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
764 p += ext_len;
765 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100766
Paul Bakker5121ce52009-01-03 21:22:43 +0000767 ssl->out_msglen = p - buf;
768 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
769 ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
770
771 ssl->state++;
772
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +0200773#if defined(POLARSSL_SSL_PROTO_DTLS)
774 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
775 ssl_send_flight_completed( ssl );
776#endif
777
Paul Bakker5121ce52009-01-03 21:22:43 +0000778 if( ( ret = ssl_write_record( ssl ) ) != 0 )
779 {
780 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
781 return( ret );
782 }
783
784 SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
785
786 return( 0 );
787}
788
Paul Bakker48916f92012-09-16 19:57:18 +0000789static int ssl_parse_renegotiation_info( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200790 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000791 size_t len )
792{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000793 int ret;
794
Paul Bakker48916f92012-09-16 19:57:18 +0000795 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
796 {
797 if( len != 1 || buf[0] != 0x0 )
798 {
799 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000800
801 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
802 return( ret );
803
Paul Bakker48916f92012-09-16 19:57:18 +0000804 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
805 }
806
807 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
808 }
809 else
810 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100811 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000812 if( len != 1 + ssl->verify_data_len * 2 ||
813 buf[0] != ssl->verify_data_len * 2 ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100814 safer_memcmp( buf + 1,
815 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
816 safer_memcmp( buf + 1 + ssl->verify_data_len,
817 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000818 {
819 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000820
821 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
822 return( ret );
823
Paul Bakker48916f92012-09-16 19:57:18 +0000824 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
825 }
826 }
827
828 return( 0 );
829}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200830
Paul Bakker05decb22013-08-15 13:33:48 +0200831#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200832static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200833 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200834 size_t len )
835{
836 /*
837 * server should use the extension only if we did,
838 * and if so the server's value should match ours (and len is always 1)
839 */
840 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
841 len != 1 ||
842 buf[0] != ssl->mfl_code )
843 {
844 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
845 }
846
847 return( 0 );
848}
Paul Bakker05decb22013-08-15 13:33:48 +0200849#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000850
Paul Bakker1f2bc622013-08-15 13:45:55 +0200851#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200852static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
853 const unsigned char *buf,
854 size_t len )
855{
856 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
857 len != 0 )
858 {
859 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
860 }
861
862 ((void) buf);
863
864 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
865
866 return( 0 );
867}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200868#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200869
Paul Bakkera503a632013-08-14 13:48:06 +0200870#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200871static int ssl_parse_session_ticket_ext( ssl_context *ssl,
872 const unsigned char *buf,
873 size_t len )
874{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200875 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ||
876 len != 0 )
877 {
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200878 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200879 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200880
881 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200882
883 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200884
885 return( 0 );
886}
Paul Bakkera503a632013-08-14 13:48:06 +0200887#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200888
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200889#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200890static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
891 const unsigned char *buf,
892 size_t len )
893{
894 size_t list_size;
895 const unsigned char *p;
896
897 list_size = buf[0];
898 if( list_size + 1 != len )
899 {
900 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
901 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
902 }
903
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200904 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200905 while( list_size > 0 )
906 {
907 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
908 p[0] == POLARSSL_ECP_PF_COMPRESSED )
909 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200910 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200911 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
912 return( 0 );
913 }
914
915 list_size--;
916 p++;
917 }
918
Manuel Pégourié-Gonnard5c1f0322014-06-23 14:24:43 +0200919 SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
920 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200921}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200922#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200923
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200924#if defined(POLARSSL_SSL_ALPN)
925static int ssl_parse_alpn_ext( ssl_context *ssl,
926 const unsigned char *buf, size_t len )
927{
928 size_t list_len, name_len;
929 const char **p;
930
931 /* If we didn't send it, the server shouldn't send it */
932 if( ssl->alpn_list == NULL )
933 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
934
935 /*
936 * opaque ProtocolName<1..2^8-1>;
937 *
938 * struct {
939 * ProtocolName protocol_name_list<2..2^16-1>
940 * } ProtocolNameList;
941 *
942 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
943 */
944
945 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
946 if( len < 4 )
947 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
948
949 list_len = ( buf[0] << 8 ) | buf[1];
950 if( list_len != len - 2 )
951 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
952
953 name_len = buf[2];
954 if( name_len != list_len - 1 )
955 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
956
957 /* Check that the server chosen protocol was in our list and save it */
958 for( p = ssl->alpn_list; *p != NULL; p++ )
959 {
960 if( name_len == strlen( *p ) &&
961 memcmp( buf + 3, *p, name_len ) == 0 )
962 {
963 ssl->alpn_chosen = *p;
964 return( 0 );
965 }
966 }
967
968 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
969}
970#endif /* POLARSSL_SSL_ALPN */
971
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200972/*
973 * Parse HelloVerifyRequest. Only called after verifying the HS type.
974 */
975#if defined(POLARSSL_SSL_PROTO_DTLS)
976static int ssl_parse_hello_verify_request( ssl_context *ssl )
977{
Manuel Pégourié-Gonnard069eb792014-09-10 20:08:29 +0200978 const unsigned char *p = ssl->in_msg + ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200979 int major_ver, minor_ver;
980 unsigned char cookie_len;
981
982 SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
983
984 /*
985 * struct {
986 * ProtocolVersion server_version;
987 * opaque cookie<0..2^8-1>;
988 * } HelloVerifyRequest;
989 */
990 SSL_DEBUG_BUF( 3, "server version", (unsigned char *) p, 2 );
991 ssl_read_version( &major_ver, &minor_ver, ssl->transport, p );
992 p += 2;
993
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +0200994 /*
995 * Since the RFC is not clear on this point, accept DTLS 1.0 (TLS 1.1)
996 * even is lower than our min version.
997 */
998 if( major_ver < SSL_MAJOR_VERSION_3 ||
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200999 minor_ver < SSL_MINOR_VERSION_2 ||
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02001000 major_ver > ssl->max_major_ver ||
1001 minor_ver > ssl->max_minor_ver )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001002 {
1003 SSL_DEBUG_MSG( 1, ( "bad server version" ) );
1004
1005 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1006 SSL_ALERT_MSG_PROTOCOL_VERSION );
1007
1008 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1009 }
1010
1011 cookie_len = *p++;
1012 SSL_DEBUG_BUF( 3, "cookie", (unsigned char *) p, cookie_len );
1013
1014 polarssl_free( ssl->handshake->verify_cookie );
1015
1016 ssl->handshake->verify_cookie = polarssl_malloc( cookie_len );
1017 if( ssl->handshake->verify_cookie == NULL )
1018 {
1019 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", cookie_len ) );
1020 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
1021 }
1022
1023 memcpy( ssl->handshake->verify_cookie, p, cookie_len );
1024 ssl->handshake->verify_cookie_len = cookie_len;
1025
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001026 /* Start over at ClientHello */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001027 ssl->state = SSL_CLIENT_HELLO;
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001028 ssl_reset_checksum( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001029
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001030 ssl_recv_flight_completed( ssl );
1031
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001032 SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
1033
1034 return( 0 );
1035}
1036#endif /* POLARSSL_SSL_PROTO_DTLS */
1037
Paul Bakker5121ce52009-01-03 21:22:43 +00001038static int ssl_parse_server_hello( ssl_context *ssl )
1039{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001040 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001041 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001042 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001043 unsigned char *buf, *ext;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001044 unsigned char comp, accept_comp;
Paul Bakker48916f92012-09-16 19:57:18 +00001045 int renegotiation_info_seen = 0;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001046 int handshake_failure = 0;
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001047#if defined(POLARSSL_DEBUG_C)
1048 uint32_t t;
1049#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001050
1051 SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
1052
Paul Bakker5121ce52009-01-03 21:22:43 +00001053 buf = ssl->in_msg;
1054
1055 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1056 {
1057 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1058 return( ret );
1059 }
1060
1061 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1062 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001063 if( ssl->renegotiation == SSL_RENEGOTIATION )
1064 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001065 ssl->renego_records_seen++;
1066
1067 if( ssl->renego_max_records >= 0 &&
1068 ssl->renego_records_seen > ssl->renego_max_records )
1069 {
1070 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
1071 "but not honored by server" ) );
1072 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1073 }
1074
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001075 SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
1076 return( POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
1077 }
1078
Paul Bakker5121ce52009-01-03 21:22:43 +00001079 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001080 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001081 }
1082
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001083#if defined(POLARSSL_SSL_PROTO_DTLS)
1084 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1085 {
1086 if( buf[0] == SSL_HS_HELLO_VERIFY_REQUEST )
1087 {
1088 SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
1089 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1090 return( ssl_parse_hello_verify_request( ssl ) );
1091 }
1092 else
1093 {
1094 /* We made it through the verification process */
1095 polarssl_free( ssl->handshake->verify_cookie );
1096 ssl->handshake->verify_cookie = NULL;
1097 ssl->handshake->verify_cookie_len = 0;
1098 }
1099 }
1100#endif /* POLARSSL_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001101
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001102 if( ssl->in_hslen < 38 + ssl_hs_hdr_len( ssl ) ||
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001103 buf[0] != SSL_HS_SERVER_HELLO )
Paul Bakker5121ce52009-01-03 21:22:43 +00001104 {
1105 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001106 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001107 }
1108
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001109 /*
1110 * 0 . 1 server_version
1111 * 2 . 33 random (maybe including 4 bytes of Unix time)
1112 * 34 . 34 session_id length = n
1113 * 35 . 34+n session_id
1114 * 35+n . 36+n cipher_suite
1115 * 37+n . 37+n compression_method
1116 *
1117 * 38+n . 39+n extensions length (optional)
1118 * 40+n . .. extensions
1119 */
1120 buf += ssl_hs_hdr_len( ssl );
1121
1122 SSL_DEBUG_BUF( 3, "server hello, version", buf + 0, 2 );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001123 ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001124 ssl->transport, buf + 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001125
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001126 if( ssl->major_ver < ssl->min_major_ver ||
1127 ssl->minor_ver < ssl->min_minor_ver ||
1128 ssl->major_ver > ssl->max_major_ver ||
1129 ssl->minor_ver > ssl->max_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001130 {
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001131 SSL_DEBUG_MSG( 1, ( "server version out of bounds - "
1132 " min: [%d:%d], server: [%d:%d], max: [%d:%d]",
1133 ssl->min_major_ver, ssl->min_minor_ver,
1134 ssl->major_ver, ssl->minor_ver,
1135 ssl->max_major_ver, ssl->max_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001136
1137 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1138 SSL_ALERT_MSG_PROTOCOL_VERSION );
1139
1140 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1141 }
1142
Paul Bakker1504af52012-02-11 16:17:43 +00001143#if defined(POLARSSL_DEBUG_C)
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001144 t = ( (uint32_t) buf[2] << 24 )
1145 | ( (uint32_t) buf[3] << 16 )
1146 | ( (uint32_t) buf[4] << 8 )
1147 | ( (uint32_t) buf[5] );
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001148 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakker87e5cda2012-01-14 18:14:15 +00001149#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001150
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001151 memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001152
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001153 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001154
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001155 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001156
Paul Bakker48916f92012-09-16 19:57:18 +00001157 if( n > 32 )
1158 {
1159 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1160 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1161 }
1162
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001163 if( ssl->in_hslen > 39 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001164 {
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001165 ext_len = ( ( buf[38 + n] << 8 )
1166 | ( buf[39 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001167
Paul Bakker48916f92012-09-16 19:57:18 +00001168 if( ( ext_len > 0 && ext_len < 4 ) ||
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001169 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
Paul Bakker48916f92012-09-16 19:57:18 +00001170 {
1171 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1172 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1173 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001174 }
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001175 else if( ssl->in_hslen == 38 + n )
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001176 {
1177 ext_len = 0;
1178 }
1179 else
1180 {
1181 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1182 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1183 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001184
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001185 /* ciphersuite (used later) */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001186 i = ( buf[35 + n] << 8 ) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001187
1188 /*
1189 * Read and check compression
1190 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001191 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001192
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001193#if defined(POLARSSL_ZLIB_SUPPORT)
1194 accept_comp = 1;
1195#else
1196 accept_comp = 0;
1197#endif
1198
1199 /* See comments in ssl_write_client_hello() */
1200#if defined(POLARSSL_SSL_PROTO_DTLS)
1201 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1202 accept_comp = 0;
1203#endif
1204
1205 if( ( accept_comp == 0 && comp != SSL_COMPRESS_NULL ) ||
1206 ( comp != SSL_COMPRESS_NULL && comp != SSL_COMPRESS_DEFLATE ) )
1207 {
1208 SSL_DEBUG_MSG( 1, ( "server hello, bad compression: %d", comp ) );
1209 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1210 }
1211
Paul Bakker380da532012-04-18 16:10:25 +00001212 /*
1213 * Initialize update checksum functions
1214 */
Paul Bakker68884e32013-01-07 18:20:04 +01001215 ssl->transform_negotiate->ciphersuite_info = ssl_ciphersuite_from_id( i );
1216
1217 if( ssl->transform_negotiate->ciphersuite_info == NULL )
1218 {
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001219 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001220 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1221 }
Paul Bakker380da532012-04-18 16:10:25 +00001222
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001223 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1224
Paul Bakker5121ce52009-01-03 21:22:43 +00001225 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001226 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001227
1228 /*
1229 * Check if the session can be resumed
1230 */
Paul Bakker0a597072012-09-25 21:55:46 +00001231 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
1232 ssl->handshake->resume == 0 || n == 0 ||
Paul Bakker48916f92012-09-16 19:57:18 +00001233 ssl->session_negotiate->ciphersuite != i ||
1234 ssl->session_negotiate->compression != comp ||
1235 ssl->session_negotiate->length != n ||
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001236 memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 {
1238 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001239 ssl->handshake->resume = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001240#if defined(POLARSSL_HAVE_TIME)
Paul Bakker48916f92012-09-16 19:57:18 +00001241 ssl->session_negotiate->start = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001242#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001243 ssl->session_negotiate->ciphersuite = i;
1244 ssl->session_negotiate->compression = comp;
1245 ssl->session_negotiate->length = n;
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001246 memcpy( ssl->session_negotiate->id, buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001247 }
1248 else
1249 {
1250 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001251
1252 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1253 {
1254 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1255 return( ret );
1256 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001257 }
1258
1259 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001260 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001261
Paul Bakkere3166ce2011-01-27 17:40:50 +00001262 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001263 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[37 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001264
1265 i = 0;
1266 while( 1 )
1267 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001268 if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001269 {
1270 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001271 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001272 }
1273
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001274 if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
1275 ssl->session_negotiate->ciphersuite )
1276 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001277 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001278 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001279 }
1280
Paul Bakker2770fbd2012-07-03 13:30:23 +00001281 if( comp != SSL_COMPRESS_NULL
1282#if defined(POLARSSL_ZLIB_SUPPORT)
1283 && comp != SSL_COMPRESS_DEFLATE
1284#endif
1285 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001286 {
1287 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001288 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001289 }
Paul Bakker48916f92012-09-16 19:57:18 +00001290 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001291
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001292 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001293
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001294 SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
1295
Paul Bakker48916f92012-09-16 19:57:18 +00001296 while( ext_len )
1297 {
1298 unsigned int ext_id = ( ( ext[0] << 8 )
1299 | ( ext[1] ) );
1300 unsigned int ext_size = ( ( ext[2] << 8 )
1301 | ( ext[3] ) );
1302
1303 if( ext_size + 4 > ext_len )
1304 {
1305 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1306 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1307 }
1308
1309 switch( ext_id )
1310 {
1311 case TLS_EXT_RENEGOTIATION_INFO:
1312 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1313 renegotiation_info_seen = 1;
1314
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001315 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1316 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001317 return( ret );
1318
1319 break;
1320
Paul Bakker05decb22013-08-15 13:33:48 +02001321#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001322 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1323 SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
1324
1325 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1326 ext + 4, ext_size ) ) != 0 )
1327 {
1328 return( ret );
1329 }
1330
1331 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001332#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001333
Paul Bakker1f2bc622013-08-15 13:45:55 +02001334#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001335 case TLS_EXT_TRUNCATED_HMAC:
1336 SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
1337
1338 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1339 ext + 4, ext_size ) ) != 0 )
1340 {
1341 return( ret );
1342 }
1343
1344 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001345#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001346
Paul Bakkera503a632013-08-14 13:48:06 +02001347#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001348 case TLS_EXT_SESSION_TICKET:
1349 SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
1350
1351 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1352 ext + 4, ext_size ) ) != 0 )
1353 {
1354 return( ret );
1355 }
1356
1357 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001358#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001359
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001360#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001361 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1362 SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
1363
1364 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1365 ext + 4, ext_size ) ) != 0 )
1366 {
1367 return( ret );
1368 }
1369
1370 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001371#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001372
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001373#if defined(POLARSSL_SSL_ALPN)
1374 case TLS_EXT_ALPN:
1375 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1376
1377 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1378 return( ret );
1379
1380 break;
1381#endif /* POLARSSL_SSL_ALPN */
1382
Paul Bakker48916f92012-09-16 19:57:18 +00001383 default:
1384 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1385 ext_id ) );
1386 }
1387
1388 ext_len -= 4 + ext_size;
1389 ext += 4 + ext_size;
1390
1391 if( ext_len > 0 && ext_len < 4 )
1392 {
1393 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1394 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1395 }
1396 }
1397
1398 /*
1399 * Renegotiation security checks
1400 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001401 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1402 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001403 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001404 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1405 handshake_failure = 1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001406 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001407 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1408 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1409 renegotiation_info_seen == 0 )
1410 {
1411 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1412 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001413 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001414 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1415 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1416 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001417 {
1418 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001419 handshake_failure = 1;
1420 }
1421 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1422 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1423 renegotiation_info_seen == 1 )
1424 {
1425 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1426 handshake_failure = 1;
1427 }
1428
1429 if( handshake_failure == 1 )
1430 {
1431 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1432 return( ret );
1433
Paul Bakker48916f92012-09-16 19:57:18 +00001434 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1435 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001436
1437 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1438
1439 return( 0 );
1440}
1441
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001442#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1443 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001444static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
1445 unsigned char *end )
1446{
1447 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1448
Paul Bakker29e1f122013-04-16 13:07:56 +02001449 /*
1450 * Ephemeral DH parameters:
1451 *
1452 * struct {
1453 * opaque dh_p<1..2^16-1>;
1454 * opaque dh_g<1..2^16-1>;
1455 * opaque dh_Ys<1..2^16-1>;
1456 * } ServerDHParams;
1457 */
1458 if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1459 {
1460 SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
1461 return( ret );
1462 }
1463
1464 if( ssl->handshake->dhm_ctx.len < 64 ||
1465 ssl->handshake->dhm_ctx.len > 512 )
1466 {
1467 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
1468 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1469 }
1470
1471 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1472 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1473 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001474
1475 return( ret );
1476}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001477#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1478 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001479
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001480#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001481 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001482 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1483 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1484 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1485static int ssl_check_server_ecdh_params( const ssl_context *ssl )
1486{
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001487 const ecp_curve_info *curve_info;
1488
1489 curve_info = ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
1490 if( curve_info == NULL )
1491 {
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001492 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1493 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001494 }
1495
1496 SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001497
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001498#if defined(POLARSSL_SSL_ECP_SET_CURVES)
1499 if( ! ssl_curve_is_acceptable( ssl, ssl->handshake->ecdh_ctx.grp.id ) )
1500#else
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001501 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1502 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001503#endif
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001504 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001505
1506 SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
1507
1508 return( 0 );
1509}
Paul Bakker9af723c2014-05-01 13:03:14 +02001510#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1511 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1512 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1513 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1514 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001515
1516#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1517 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001518 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001519static int ssl_parse_server_ecdh_params( ssl_context *ssl,
1520 unsigned char **p,
1521 unsigned char *end )
1522{
1523 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1524
Paul Bakker29e1f122013-04-16 13:07:56 +02001525 /*
1526 * Ephemeral ECDH parameters:
1527 *
1528 * struct {
1529 * ECParameters curve_params;
1530 * ECPoint public;
1531 * } ServerECDHParams;
1532 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001533 if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
1534 (const unsigned char **) p, end ) ) != 0 )
1535 {
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001536 SSL_DEBUG_RET( 1, ( "ecdh_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001537 return( ret );
1538 }
1539
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001540 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001541 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001542 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001543 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1544 }
1545
Paul Bakker29e1f122013-04-16 13:07:56 +02001546 return( ret );
1547}
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001548#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001549 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1550 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001551
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001552#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001553static int ssl_parse_server_psk_hint( ssl_context *ssl,
1554 unsigned char **p,
1555 unsigned char *end )
1556{
1557 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001558 size_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001559 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001560
1561 /*
1562 * PSK parameters:
1563 *
1564 * opaque psk_identity_hint<0..2^16-1>;
1565 */
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001566 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001567 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001568
1569 if( (*p) + len > end )
1570 {
1571 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
1572 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1573 }
1574
1575 // TODO: Retrieve PSK identity hint and callback to app
1576 //
1577 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001578 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001579
1580 return( ret );
1581}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001582#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001583
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001584#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1585 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1586/*
1587 * Generate a pre-master secret and encrypt it with the server's RSA key
1588 */
1589static int ssl_write_encrypted_pms( ssl_context *ssl,
1590 size_t offset, size_t *olen,
1591 size_t pms_offset )
1592{
1593 int ret;
1594 size_t len_bytes = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 0 : 2;
1595 unsigned char *p = ssl->handshake->premaster + pms_offset;
1596
1597 /*
1598 * Generate (part of) the pre-master as
1599 * struct {
1600 * ProtocolVersion client_version;
1601 * opaque random[46];
1602 * } PreMasterSecret;
1603 */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001604 ssl_write_version( ssl->max_major_ver, ssl->max_minor_ver,
1605 ssl->transport, p );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001606
1607 if( ( ret = ssl->f_rng( ssl->p_rng, p + 2, 46 ) ) != 0 )
1608 {
1609 SSL_DEBUG_RET( 1, "f_rng", ret );
1610 return( ret );
1611 }
1612
1613 ssl->handshake->pmslen = 48;
1614
1615 /*
1616 * Now write it out, encrypted
1617 */
1618 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1619 POLARSSL_PK_RSA ) )
1620 {
1621 SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1622 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1623 }
1624
1625 if( ( ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
1626 p, ssl->handshake->pmslen,
1627 ssl->out_msg + offset + len_bytes, olen,
1628 SSL_MAX_CONTENT_LEN - offset - len_bytes,
1629 ssl->f_rng, ssl->p_rng ) ) != 0 )
1630 {
1631 SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
1632 return( ret );
1633 }
1634
1635#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1636 defined(POLARSSL_SSL_PROTO_TLS1_2)
1637 if( len_bytes == 2 )
1638 {
1639 ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
1640 ssl->out_msg[offset+1] = (unsigned char)( *olen );
1641 *olen += 2;
1642 }
1643#endif
1644
1645 return( 0 );
1646}
1647#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
1648 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001649
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001650#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001651#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001652 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1653 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001654static int ssl_parse_signature_algorithm( ssl_context *ssl,
1655 unsigned char **p,
1656 unsigned char *end,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001657 md_type_t *md_alg,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001658 pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02001659{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001660 ((void) ssl);
Paul Bakker29e1f122013-04-16 13:07:56 +02001661 *md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001662 *pk_alg = POLARSSL_PK_NONE;
1663
1664 /* Only in TLS 1.2 */
1665 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1666 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001667 return( 0 );
1668 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001669
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001670 if( (*p) + 2 > end )
Paul Bakker29e1f122013-04-16 13:07:56 +02001671 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1672
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001673 /*
1674 * Get hash algorithm
1675 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001676 if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001677 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001678 SSL_DEBUG_MSG( 2, ( "Server used unsupported "
1679 "HashAlgorithm %d", *(p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001680 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1681 }
1682
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001683 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001684 * Get signature algorithm
1685 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001686 if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001687 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001688 SSL_DEBUG_MSG( 2, ( "server used unsupported "
1689 "SignatureAlgorithm %d", (*p)[1] ) );
1690 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001691 }
1692
1693 SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
1694 SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
1695 *p += 2;
1696
1697 return( 0 );
1698}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001699#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001700 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1701 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001702#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001703
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001704
1705#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1706 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1707static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
1708{
1709 int ret;
1710 const ecp_keypair *peer_key;
1711
1712 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1713 POLARSSL_PK_ECKEY ) )
1714 {
1715 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
1716 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1717 }
1718
1719 peer_key = pk_ec( ssl->session_negotiate->peer_cert->pk );
1720
1721 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
1722 POLARSSL_ECDH_THEIRS ) ) != 0 )
1723 {
1724 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
1725 return( ret );
1726 }
1727
1728 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1729 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001730 SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001731 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
1732 }
1733
1734 return( ret );
1735}
1736#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1737 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1738
Paul Bakker41c83d32013-03-20 14:39:14 +01001739static int ssl_parse_server_key_exchange( ssl_context *ssl )
1740{
Paul Bakker23986e52011-04-24 08:57:21 +00001741 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001742 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001743 unsigned char *p, *end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001744
1745 SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
1746
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001747#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001748 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001749 {
1750 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1751 ssl->state++;
1752 return( 0 );
1753 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001754 ((void) p);
1755 ((void) end);
1756#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001757
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001758#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1759 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1760 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
1761 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
1762 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001763 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
1764 {
1765 SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
1766 return( ret );
1767 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001768
1769 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1770 ssl->state++;
1771 return( 0 );
1772 }
1773 ((void) p);
1774 ((void) end);
Paul Bakker9af723c2014-05-01 13:03:14 +02001775#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1776 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001777
Paul Bakker5121ce52009-01-03 21:22:43 +00001778 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1779 {
1780 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1781 return( ret );
1782 }
1783
1784 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1785 {
1786 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001787 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001788 }
1789
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001790 /*
1791 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
1792 * doesn't use a psk_identity_hint
1793 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001794 if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
1795 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001796 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1797 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02001798 {
1799 ssl->record_read = 1;
1800 goto exit;
1801 }
1802
1803 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1804 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001805 }
1806
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00001807 p = ssl->in_msg + ssl_hs_hdr_len( ssl );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001808 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00001809 SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001810
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001811#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1812 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1813 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1814 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1815 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1816 {
1817 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1818 {
1819 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1820 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1821 }
1822 } /* FALLTROUGH */
1823#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
1824
1825#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
1826 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1827 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1828 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1829 ; /* nothing more to do */
1830 else
1831#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
1832 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
1833#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1834 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1835 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1836 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001837 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001838 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001839 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001840 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001841 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1842 }
1843 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001844 else
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001845#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1846 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001847#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001848 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001849 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1850 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001851 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001852 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001853 {
1854 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
1855 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001856 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1857 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1858 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001859 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001860 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001861#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001862 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001863 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001864 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001865 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001866 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001867 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001868
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001869#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001870 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1871 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001872 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001873 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1874 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001875 {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00001876 size_t sig_len, hashlen;
1877 unsigned char hash[64];
1878 md_type_t md_alg = POLARSSL_MD_NONE;
1879 pk_type_t pk_alg = POLARSSL_PK_NONE;
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00001880 unsigned char *params = ssl->in_msg + ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00001881 size_t params_len = p - params;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001882
Paul Bakker29e1f122013-04-16 13:07:56 +02001883 /*
1884 * Handle the digitally-signed structure
1885 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001886#if defined(POLARSSL_SSL_PROTO_TLS1_2)
1887 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001888 {
Paul Bakker9659dae2013-08-28 16:21:34 +02001889 if( ssl_parse_signature_algorithm( ssl, &p, end,
1890 &md_alg, &pk_alg ) != 0 )
1891 {
1892 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1893 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1894 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001895
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001896 if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001897 {
1898 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1899 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1900 }
1901 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001902 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001903#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001904#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1905 defined(POLARSSL_SSL_PROTO_TLS1_1)
1906 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001907 {
1908 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Paul Bakker1ef83d62012-04-11 12:09:53 +00001909
Paul Bakker9659dae2013-08-28 16:21:34 +02001910 /* Default hash for ECDSA is SHA-1 */
1911 if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
1912 md_alg = POLARSSL_MD_SHA1;
1913 }
1914 else
1915#endif
1916 {
1917 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001918 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker9659dae2013-08-28 16:21:34 +02001919 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001920
1921 /*
1922 * Read signature
1923 */
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001924 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00001925 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001926
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001927 if( end != p + sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01001928 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001929 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001930 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1931 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001932
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001933 SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02001934
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001935 /*
1936 * Compute the hash that has been signed
1937 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001938#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1939 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001940 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001941 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001942 md5_context md5;
1943 sha1_context sha1;
1944
Paul Bakker5b4af392014-06-26 12:09:34 +02001945 md5_init( &md5 );
1946 sha1_init( &sha1 );
1947
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001948 hashlen = 36;
1949
Paul Bakker29e1f122013-04-16 13:07:56 +02001950 /*
1951 * digitally-signed struct {
1952 * opaque md5_hash[16];
1953 * opaque sha_hash[20];
1954 * };
1955 *
1956 * md5_hash
1957 * MD5(ClientHello.random + ServerHello.random
1958 * + ServerParams);
1959 * sha_hash
1960 * SHA(ClientHello.random + ServerHello.random
1961 * + ServerParams);
1962 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001963 md5_starts( &md5 );
1964 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00001965 md5_update( &md5, params, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001966 md5_finish( &md5, hash );
1967
1968 sha1_starts( &sha1 );
1969 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00001970 sha1_update( &sha1, params, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001971 sha1_finish( &sha1, hash + 16 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001972
1973 md5_free( &md5 );
1974 sha1_free( &sha1 );
Paul Bakker29e1f122013-04-16 13:07:56 +02001975 }
1976 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001977#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
1978 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001979#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1980 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02001981 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001982 {
1983 md_context_t ctx;
1984
Paul Bakker84bbeb52014-07-01 14:53:22 +02001985 md_init( &ctx );
1986
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001987 /* Info from md_alg will be used instead */
1988 hashlen = 0;
Paul Bakker29e1f122013-04-16 13:07:56 +02001989
1990 /*
1991 * digitally-signed struct {
1992 * opaque client_random[32];
1993 * opaque server_random[32];
1994 * ServerDHParams params;
1995 * };
1996 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001997 if( ( ret = md_init_ctx( &ctx,
1998 md_info_from_type( md_alg ) ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001999 {
2000 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2001 return( ret );
2002 }
2003
2004 md_starts( &ctx );
2005 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00002006 md_update( &ctx, params, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002007 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002008 md_free( &ctx );
Paul Bakker29e1f122013-04-16 13:07:56 +02002009 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002010 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002011#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2012 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02002013 {
Paul Bakker577e0062013-08-28 11:57:20 +02002014 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002015 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002016 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002017
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002018 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2019 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker29e1f122013-04-16 13:07:56 +02002020
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002021 /*
2022 * Verify signature
2023 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02002024 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002025 {
2026 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2027 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2028 }
2029
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002030 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
2031 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002032 {
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002033 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002034 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002035 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002036 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002037#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002038 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2039 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002040
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002041exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002042 ssl->state++;
2043
2044 SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
2045
2046 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002047}
2048
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002049#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2050 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2051 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2052 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2053static int ssl_parse_certificate_request( ssl_context *ssl )
2054{
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002055 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2056
2057 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2058
2059 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2060 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2061 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2062 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2063 {
2064 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2065 ssl->state++;
2066 return( 0 );
2067 }
2068
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002069 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2070 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002071}
2072#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002073static int ssl_parse_certificate_request( ssl_context *ssl )
2074{
2075 int ret;
Paul Bakker926af752012-11-23 13:38:07 +01002076 unsigned char *buf, *p;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002077 size_t n = 0, m = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002078 size_t cert_type_len = 0, dn_len = 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002079 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002080
2081 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2082
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002083 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2084 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2085 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2086 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2087 {
2088 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2089 ssl->state++;
2090 return( 0 );
2091 }
2092
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002093 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002095 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2096 {
2097 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2098 return( ret );
2099 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002100
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002101 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2102 {
2103 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2104 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2105 }
2106
2107 ssl->record_read = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 }
2109
2110 ssl->client_auth = 0;
2111 ssl->state++;
2112
2113 if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST )
2114 ssl->client_auth++;
2115
2116 SSL_DEBUG_MSG( 3, ( "got %s certificate request",
2117 ssl->client_auth ? "a" : "no" ) );
2118
Paul Bakker926af752012-11-23 13:38:07 +01002119 if( ssl->client_auth == 0 )
2120 goto exit;
2121
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002122 ssl->record_read = 0;
2123
Paul Bakker926af752012-11-23 13:38:07 +01002124 // TODO: handshake_failure alert for an anonymous server to request
2125 // client authentication
2126
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002127 /*
2128 * struct {
2129 * ClientCertificateType certificate_types<1..2^8-1>;
2130 * SignatureAndHashAlgorithm
2131 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2132 * DistinguishedName certificate_authorities<0..2^16-1>;
2133 * } CertificateRequest;
2134 */
Paul Bakker926af752012-11-23 13:38:07 +01002135 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002136
Paul Bakker926af752012-11-23 13:38:07 +01002137 // Retrieve cert types
2138 //
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002139 cert_type_len = buf[ssl_hs_hdr_len( ssl )];
Paul Bakker926af752012-11-23 13:38:07 +01002140 n = cert_type_len;
2141
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002142 if( ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002143 {
2144 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2145 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2146 }
2147
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002148 p = buf + ssl_hs_hdr_len( ssl ) + 1;
Paul Bakker926af752012-11-23 13:38:07 +01002149 while( cert_type_len > 0 )
2150 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002151#if defined(POLARSSL_RSA_C)
2152 if( *p == SSL_CERT_TYPE_RSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002153 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker926af752012-11-23 13:38:07 +01002154 {
2155 ssl->handshake->cert_type = SSL_CERT_TYPE_RSA_SIGN;
2156 break;
2157 }
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002158 else
2159#endif
2160#if defined(POLARSSL_ECDSA_C)
2161 if( *p == SSL_CERT_TYPE_ECDSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002162 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002163 {
2164 ssl->handshake->cert_type = SSL_CERT_TYPE_ECDSA_SIGN;
2165 break;
2166 }
2167 else
2168#endif
2169 {
2170 ; /* Unsupported cert type, ignore */
2171 }
Paul Bakker926af752012-11-23 13:38:07 +01002172
2173 cert_type_len--;
2174 p++;
2175 }
2176
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002177#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002178 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2179 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002180 /* Ignored, see comments about hash in write_certificate_verify */
2181 // TODO: should check the signature part against our pk_key though
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002182 size_t sig_alg_len = ( ( buf[ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2183 | ( buf[ssl_hs_hdr_len( ssl ) + 2 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002184
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002185 p = buf + ssl_hs_hdr_len( ssl ) + 3 + n;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002186 m += 2;
Paul Bakker926af752012-11-23 13:38:07 +01002187 n += sig_alg_len;
2188
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002189 if( ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002190 {
2191 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2192 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2193 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002194 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002195#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker926af752012-11-23 13:38:07 +01002196
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002197 /* Ignore certificate_authorities, we only have one cert anyway */
2198 // TODO: should not send cert if no CA matches
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002199 dn_len = ( ( buf[ssl_hs_hdr_len( ssl ) + 1 + m + n] << 8 )
2200 | ( buf[ssl_hs_hdr_len( ssl ) + 2 + m + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002201
2202 n += dn_len;
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002203 if( ssl->in_hslen != ssl_hs_hdr_len( ssl ) + 3 + m + n )
Paul Bakker926af752012-11-23 13:38:07 +01002204 {
2205 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2206 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2207 }
2208
2209exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002210 SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2211
2212 return( 0 );
2213}
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002214#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2215 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2216 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2217 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002218
2219static int ssl_parse_server_hello_done( ssl_context *ssl )
2220{
2221 int ret;
2222
2223 SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
2224
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002225 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002226 {
2227 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2228 {
2229 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2230 return( ret );
2231 }
2232
2233 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2234 {
2235 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002236 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002237 }
2238 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002239 ssl->record_read = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002240
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002241 if( ssl->in_hslen != ssl_hs_hdr_len( ssl ) ||
Paul Bakker5121ce52009-01-03 21:22:43 +00002242 ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
2243 {
2244 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002245 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002246 }
2247
2248 ssl->state++;
2249
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002250#if defined(POLARSSL_SSL_PROTO_DTLS)
2251 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2252 ssl_recv_flight_completed( ssl );
2253#endif
2254
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
2256
2257 return( 0 );
2258}
2259
2260static int ssl_write_client_key_exchange( ssl_context *ssl )
2261{
Paul Bakker23986e52011-04-24 08:57:21 +00002262 int ret;
2263 size_t i, n;
Paul Bakker41c83d32013-03-20 14:39:14 +01002264 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002265
2266 SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
2267
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002268#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002269 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002270 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002271 /*
2272 * DHM key exchange -- send G^X mod P
2273 */
Paul Bakker48916f92012-09-16 19:57:18 +00002274 n = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002275
2276 ssl->out_msg[4] = (unsigned char)( n >> 8 );
2277 ssl->out_msg[5] = (unsigned char)( n );
2278 i = 6;
2279
Paul Bakker29b64762012-09-25 09:36:44 +00002280 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002281 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker5121ce52009-01-03 21:22:43 +00002282 &ssl->out_msg[i], n,
2283 ssl->f_rng, ssl->p_rng );
2284 if( ret != 0 )
2285 {
2286 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2287 return( ret );
2288 }
2289
Paul Bakker48916f92012-09-16 19:57:18 +00002290 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2291 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002292
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002293 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002294
Paul Bakker48916f92012-09-16 19:57:18 +00002295 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2296 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002297 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002298 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002299 {
2300 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2301 return( ret );
2302 }
2303
Paul Bakker48916f92012-09-16 19:57:18 +00002304 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002305 }
2306 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002307#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002308#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002309 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2310 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2311 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002312 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002313 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2314 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2315 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002316 {
2317 /*
2318 * ECDH key exchange -- send client public value
2319 */
2320 i = 4;
2321
2322 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
2323 &n,
2324 &ssl->out_msg[i], 1000,
2325 ssl->f_rng, ssl->p_rng );
2326 if( ret != 0 )
2327 {
2328 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2329 return( ret );
2330 }
2331
2332 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2333
2334 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2335 &ssl->handshake->pmslen,
2336 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002337 POLARSSL_MPI_MAX_SIZE,
2338 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002339 {
2340 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2341 return( ret );
2342 }
2343
2344 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2345 }
2346 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002347#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002348 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2349 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2350 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002351#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002352 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002353 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002354 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2355 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002356 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002357 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002358 * opaque psk_identity<0..2^16-1>;
2359 */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002360 if( ssl->psk == NULL || ssl->psk_identity == NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002361 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2362
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002363 i = 4;
2364 n = ssl->psk_identity_len;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002365 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2366 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002367
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002368 memcpy( ssl->out_msg + i, ssl->psk_identity, ssl->psk_identity_len );
2369 i += ssl->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002370
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002371#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002372 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002373 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002374 n = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002375 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002376 else
2377#endif
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002378#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2379 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2380 {
2381 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2382 return( ret );
2383 }
2384 else
2385#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002386#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002387 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002388 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002389 /*
2390 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
2391 */
2392 n = ssl->handshake->dhm_ctx.len;
2393 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2394 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002395
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002396 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakker68881672013-10-15 13:24:01 +02002397 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002398 &ssl->out_msg[i], n,
2399 ssl->f_rng, ssl->p_rng );
2400 if( ret != 0 )
2401 {
2402 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2403 return( ret );
2404 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002405 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002406 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002407#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002408#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002409 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002410 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002411 /*
2412 * ClientECDiffieHellmanPublic public;
2413 */
2414 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
2415 &ssl->out_msg[i], SSL_MAX_CONTENT_LEN - i,
2416 ssl->f_rng, ssl->p_rng );
2417 if( ret != 0 )
2418 {
2419 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2420 return( ret );
2421 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002422
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002423 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2424 }
2425 else
2426#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2427 {
2428 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002429 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002430 }
2431
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002432 if( ( ret = ssl_psk_derive_premaster( ssl,
2433 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002434 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002435 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002436 return( ret );
2437 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002438 }
2439 else
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002440#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002441#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +02002442 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002443 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002444 i = 4;
2445 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002446 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002447 }
Paul Bakkered27a042013-04-18 22:46:23 +02002448 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002449#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002450 {
2451 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002452 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002453 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02002454 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002455
Paul Bakkerff60ee62010-03-16 21:09:09 +00002456 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2457 {
2458 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2459 return( ret );
2460 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002461
2462 ssl->out_msglen = i + n;
2463 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2464 ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE;
2465
2466 ssl->state++;
2467
2468 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2469 {
2470 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2471 return( ret );
2472 }
2473
2474 SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
2475
2476 return( 0 );
2477}
2478
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002479#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2480 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002481 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2482 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002483static int ssl_write_certificate_verify( ssl_context *ssl )
2484{
Paul Bakkered27a042013-04-18 22:46:23 +02002485 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002486
2487 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2488
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002489 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002490 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002491 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002492 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002493 {
2494 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2495 ssl->state++;
2496 return( 0 );
2497 }
2498
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002499 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2500 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002501}
2502#else
2503static int ssl_write_certificate_verify( ssl_context *ssl )
2504{
2505 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2506 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2507 size_t n = 0, offset = 0;
2508 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002509 unsigned char *hash_start = hash;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002510 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002511 unsigned int hashlen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002512
2513 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2514
2515 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002516 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002517 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002518 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2519 {
2520 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2521 ssl->state++;
2522 return( 0 );
2523 }
2524
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002525 if( ssl->client_auth == 0 || ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002526 {
2527 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2528 ssl->state++;
2529 return( 0 );
2530 }
2531
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002532 if( ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002533 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00002534 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2535 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002536 }
2537
2538 /*
2539 * Make an RSA signature of the handshake digests
2540 */
Paul Bakker48916f92012-09-16 19:57:18 +00002541 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002542
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002543#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2544 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker926af752012-11-23 13:38:07 +01002545 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002546 {
Paul Bakker926af752012-11-23 13:38:07 +01002547 /*
2548 * digitally-signed struct {
2549 * opaque md5_hash[16];
2550 * opaque sha_hash[20];
2551 * };
2552 *
2553 * md5_hash
2554 * MD5(handshake_messages);
2555 *
2556 * sha_hash
2557 * SHA(handshake_messages);
2558 */
2559 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002560 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002561
2562 /*
2563 * For ECDSA, default hash is SHA-1 only
2564 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002565 if( pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002566 {
2567 hash_start += 16;
2568 hashlen -= 16;
2569 md_alg = POLARSSL_MD_SHA1;
2570 }
Paul Bakker926af752012-11-23 13:38:07 +01002571 }
2572 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002573#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2574 POLARSSL_SSL_PROTO_TLS1_1 */
2575#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2576 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002577 {
2578 /*
2579 * digitally-signed struct {
2580 * opaque handshake_messages[handshake_messages_length];
2581 * };
2582 *
2583 * Taking shortcut here. We assume that the server always allows the
2584 * PRF Hash function and has sent it in the allowed signature
2585 * algorithms list received in the Certificate Request message.
2586 *
2587 * Until we encounter a server that does not, we will take this
2588 * shortcut.
2589 *
2590 * Reason: Otherwise we should have running hashes for SHA512 and SHA224
2591 * in order to satisfy 'weird' needs from the server side.
2592 */
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002593 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2594 POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002595 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002596 md_alg = POLARSSL_MD_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002597 ssl->out_msg[4] = SSL_HASH_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002598 }
2599 else
2600 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002601 md_alg = POLARSSL_MD_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002602 ssl->out_msg[4] = SSL_HASH_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002603 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002604 ssl->out_msg[5] = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002605
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002606 /* Info from md_alg will be used instead */
2607 hashlen = 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002608 offset = 2;
2609 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002610 else
2611#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002612 {
2613 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002614 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002615 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002616
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002617 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002618 ssl->out_msg + 6 + offset, &n,
2619 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002620 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002621 SSL_DEBUG_RET( 1, "pk_sign", ret );
2622 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002623 }
Paul Bakker926af752012-11-23 13:38:07 +01002624
Paul Bakker1ef83d62012-04-11 12:09:53 +00002625 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
2626 ssl->out_msg[5 + offset] = (unsigned char)( n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002627
Paul Bakker1ef83d62012-04-11 12:09:53 +00002628 ssl->out_msglen = 6 + n + offset;
Paul Bakker5121ce52009-01-03 21:22:43 +00002629 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2630 ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY;
2631
2632 ssl->state++;
2633
2634 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2635 {
2636 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2637 return( ret );
2638 }
2639
2640 SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
2641
Paul Bakkered27a042013-04-18 22:46:23 +02002642 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002643}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002644#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2645 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2646 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002647
Paul Bakkera503a632013-08-14 13:48:06 +02002648#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002649static int ssl_parse_new_session_ticket( ssl_context *ssl )
2650{
2651 int ret;
2652 uint32_t lifetime;
2653 size_t ticket_len;
2654 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002655 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002656
2657 SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2658
2659 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2660 {
2661 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2662 return( ret );
2663 }
2664
2665 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2666 {
2667 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2668 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2669 }
2670
2671 /*
2672 * struct {
2673 * uint32 ticket_lifetime_hint;
2674 * opaque ticket<0..2^16-1>;
2675 * } NewSessionTicket;
2676 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002677 * 0 . 3 ticket_lifetime_hint
2678 * 4 . 5 ticket_len (n)
2679 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002680 */
2681 if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002682 ssl->in_hslen < 6 + ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002683 {
2684 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2685 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2686 }
2687
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002688 msg = ssl->in_msg + ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002689
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002690 lifetime = ( msg[0] << 24 ) | ( msg[1] << 16 ) |
2691 ( msg[2] << 8 ) | ( msg[3] );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002692
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002693 ticket_len = ( msg[4] << 8 ) | ( msg[5] );
2694
2695 if( ticket_len + 6 + ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002696 {
2697 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2698 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2699 }
2700
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002701 SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
2702
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002703 /* We're not waiting for a NewSessionTicket message any more */
2704 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02002705 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002706
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002707 /*
2708 * Zero-length ticket means the server changed his mind and doesn't want
2709 * to send a ticket after all, so just forget it
2710 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002711 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002712 return( 0 );
2713
Paul Bakker34617722014-06-13 17:20:13 +02002714 polarssl_zeroize( ssl->session_negotiate->ticket,
2715 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002716 polarssl_free( ssl->session_negotiate->ticket );
2717 ssl->session_negotiate->ticket = NULL;
2718 ssl->session_negotiate->ticket_len = 0;
2719
2720 if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
2721 {
2722 SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
2723 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2724 }
2725
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002726 memcpy( ticket, msg + 6, ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002727
2728 ssl->session_negotiate->ticket = ticket;
2729 ssl->session_negotiate->ticket_len = ticket_len;
2730 ssl->session_negotiate->ticket_lifetime = lifetime;
2731
2732 /*
2733 * RFC 5077 section 3.4:
2734 * "If the client receives a session ticket from the server, then it
2735 * discards any Session ID that was sent in the ServerHello."
2736 */
2737 SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
2738 ssl->session_negotiate->length = 0;
2739
2740 SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2741
2742 return( 0 );
2743}
Paul Bakkera503a632013-08-14 13:48:06 +02002744#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002745
Paul Bakker5121ce52009-01-03 21:22:43 +00002746/*
Paul Bakker1961b702013-01-25 14:49:24 +01002747 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002748 */
Paul Bakker1961b702013-01-25 14:49:24 +01002749int ssl_handshake_client_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002750{
2751 int ret = 0;
2752
Paul Bakker1961b702013-01-25 14:49:24 +01002753 if( ssl->state == SSL_HANDSHAKE_OVER )
2754 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002755
Paul Bakker1961b702013-01-25 14:49:24 +01002756 SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
2757
2758 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2759 return( ret );
2760
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002761#if defined(POLARSSL_SSL_PROTO_DTLS)
2762 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2763 ssl->handshake != NULL &&
2764 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
2765 {
2766 if( ( ret = ssl_resend( ssl ) ) != 0 )
2767 return( ret );
2768 }
2769#endif
2770
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02002771 /* Change state now, so that it is right in ssl_read_record(), used
2772 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
2773#if defined(POLARSSL_SSL_SESSION_TICKETS)
2774 if( ssl->state == SSL_SERVER_CHANGE_CIPHER_SPEC &&
2775 ssl->handshake->new_session_ticket != 0 )
2776 {
2777 ssl->state = SSL_SERVER_NEW_SESSION_TICKET;
2778 }
2779#endif
2780
Paul Bakker1961b702013-01-25 14:49:24 +01002781 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002782 {
Paul Bakker1961b702013-01-25 14:49:24 +01002783 case SSL_HELLO_REQUEST:
2784 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002785 break;
2786
Paul Bakker1961b702013-01-25 14:49:24 +01002787 /*
2788 * ==> ClientHello
2789 */
2790 case SSL_CLIENT_HELLO:
2791 ret = ssl_write_client_hello( ssl );
2792 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002793
Paul Bakker1961b702013-01-25 14:49:24 +01002794 /*
2795 * <== ServerHello
2796 * Certificate
2797 * ( ServerKeyExchange )
2798 * ( CertificateRequest )
2799 * ServerHelloDone
2800 */
2801 case SSL_SERVER_HELLO:
2802 ret = ssl_parse_server_hello( ssl );
2803 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002804
Paul Bakker1961b702013-01-25 14:49:24 +01002805 case SSL_SERVER_CERTIFICATE:
2806 ret = ssl_parse_certificate( ssl );
2807 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002808
Paul Bakker1961b702013-01-25 14:49:24 +01002809 case SSL_SERVER_KEY_EXCHANGE:
2810 ret = ssl_parse_server_key_exchange( ssl );
2811 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002812
Paul Bakker1961b702013-01-25 14:49:24 +01002813 case SSL_CERTIFICATE_REQUEST:
2814 ret = ssl_parse_certificate_request( ssl );
2815 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002816
Paul Bakker1961b702013-01-25 14:49:24 +01002817 case SSL_SERVER_HELLO_DONE:
2818 ret = ssl_parse_server_hello_done( ssl );
2819 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002820
Paul Bakker1961b702013-01-25 14:49:24 +01002821 /*
2822 * ==> ( Certificate/Alert )
2823 * ClientKeyExchange
2824 * ( CertificateVerify )
2825 * ChangeCipherSpec
2826 * Finished
2827 */
2828 case SSL_CLIENT_CERTIFICATE:
2829 ret = ssl_write_certificate( ssl );
2830 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002831
Paul Bakker1961b702013-01-25 14:49:24 +01002832 case SSL_CLIENT_KEY_EXCHANGE:
2833 ret = ssl_write_client_key_exchange( ssl );
2834 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002835
Paul Bakker1961b702013-01-25 14:49:24 +01002836 case SSL_CERTIFICATE_VERIFY:
2837 ret = ssl_write_certificate_verify( ssl );
2838 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002839
Paul Bakker1961b702013-01-25 14:49:24 +01002840 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2841 ret = ssl_write_change_cipher_spec( ssl );
2842 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002843
Paul Bakker1961b702013-01-25 14:49:24 +01002844 case SSL_CLIENT_FINISHED:
2845 ret = ssl_write_finished( ssl );
2846 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002847
Paul Bakker1961b702013-01-25 14:49:24 +01002848 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002849 * <== ( NewSessionTicket )
2850 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002851 * Finished
2852 */
Paul Bakkera503a632013-08-14 13:48:06 +02002853#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02002854 case SSL_SERVER_NEW_SESSION_TICKET:
2855 ret = ssl_parse_new_session_ticket( ssl );
2856 break;
Paul Bakkera503a632013-08-14 13:48:06 +02002857#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02002858
2859 case SSL_SERVER_CHANGE_CIPHER_SPEC:
2860 ret = ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002861 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002862
Paul Bakker1961b702013-01-25 14:49:24 +01002863 case SSL_SERVER_FINISHED:
2864 ret = ssl_parse_finished( ssl );
2865 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002866
Paul Bakker1961b702013-01-25 14:49:24 +01002867 case SSL_FLUSH_BUFFERS:
2868 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2869 ssl->state = SSL_HANDSHAKE_WRAPUP;
2870 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002871
Paul Bakker1961b702013-01-25 14:49:24 +01002872 case SSL_HANDSHAKE_WRAPUP:
2873 ssl_handshake_wrapup( ssl );
2874 break;
Paul Bakker48916f92012-09-16 19:57:18 +00002875
Paul Bakker1961b702013-01-25 14:49:24 +01002876 default:
2877 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2878 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2879 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002880
2881 return( ret );
2882}
Paul Bakker9af723c2014-05-01 13:03:14 +02002883#endif /* POLARSSL_SSL_CLI_C */