blob: 7a0cde6b196c78387dc9ff5eb48b701524c88831 [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
Paul Bakker5121ce52009-01-03 21:22:43 +0000447static int ssl_write_client_hello( ssl_context *ssl )
448{
Paul Bakker23986e52011-04-24 08:57:21 +0000449 int ret;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100450 size_t i, n, olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000451 unsigned char *buf;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200452 unsigned char *p, *q;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200453#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200455#endif
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200456 const int *ciphersuites;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200457 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000458
459 SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
460
Paul Bakkera9a028e2013-11-21 17:31:06 +0100461 if( ssl->f_rng == NULL )
462 {
463 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
464 return( POLARSSL_ERR_SSL_NO_RNG );
465 }
466
Paul Bakker48916f92012-09-16 19:57:18 +0000467 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
468 {
Paul Bakker993d11d2012-09-28 15:00:12 +0000469 ssl->major_ver = ssl->min_major_ver;
470 ssl->minor_ver = ssl->min_minor_ver;
Paul Bakker48916f92012-09-16 19:57:18 +0000471 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
Paul Bakker490ecc82011-10-06 13:04:09 +0000473 if( ssl->max_major_ver == 0 && ssl->max_minor_ver == 0 )
474 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200475 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
476 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker490ecc82011-10-06 13:04:09 +0000477 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000478
479 /*
480 * 0 . 0 handshake type
481 * 1 . 3 handshake length
482 * 4 . 5 highest version supported
483 * 6 . 9 current UNIX time
484 * 10 . 37 random bytes
485 */
486 buf = ssl->out_msg;
487 p = buf + 4;
488
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +0100489 ssl_write_version( ssl->max_major_ver, ssl->max_minor_ver,
490 ssl->transport, p );
491 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000492
493 SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
494 buf[4], buf[5] ) );
495
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200496#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 t = time( NULL );
498 *p++ = (unsigned char)( t >> 24 );
499 *p++ = (unsigned char)( t >> 16 );
500 *p++ = (unsigned char)( t >> 8 );
501 *p++ = (unsigned char)( t );
502
503 SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200504#else
505 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
506 return( ret );
507
508 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +0200509#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000510
Paul Bakkera3d195c2011-11-27 21:07:34 +0000511 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
512 return( ret );
513
514 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +0000515
Paul Bakker48916f92012-09-16 19:57:18 +0000516 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000517
518 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 6, 32 );
519
520 /*
521 * 38 . 38 session id length
522 * 39 . 39+n session id
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100523 * 39+n . 39+n DTLS only: cookie length (1 byte)
524 * 40+n . .. DTSL only: cookie
525 * .. . .. ciphersuitelist length (2 bytes)
526 * .. . .. ciphersuitelist
527 * .. . .. compression methods length (1 byte)
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000528 * .. . .. compression methods
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100529 * .. . .. extensions length (2 bytes)
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000530 * .. . .. extensions
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 */
Paul Bakker48916f92012-09-16 19:57:18 +0000532 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000533
Paul Bakker0a597072012-09-25 21:55:46 +0000534 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE || n < 16 || n > 32 ||
535 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200536 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 n = 0;
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200538 }
539
Paul Bakkera503a632013-08-14 13:48:06 +0200540#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200541 /*
542 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
543 * generate and include a Session ID in the TLS ClientHello."
544 */
545 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
546 ssl->session_negotiate->ticket != NULL &&
547 ssl->session_negotiate->ticket_len != 0 )
548 {
549 ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
550
551 if( ret != 0 )
552 return( ret );
553
554 ssl->session_negotiate->length = n = 32;
555 }
Paul Bakkera503a632013-08-14 13:48:06 +0200556#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000557
558 *p++ = (unsigned char) n;
559
560 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000561 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000562
563 SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
564 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
565
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100566 /*
567 * DTLS cookie
568 */
569#if defined(POLARSSL_SSL_PROTO_DTLS)
570 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
571 {
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200572 if( ssl->handshake->verify_cookie == NULL )
573 {
574 SSL_DEBUG_MSG( 3, ( "no verify cookie to send" ) );
575 *p++ = 0;
576 }
577 else
578 {
579 SSL_DEBUG_BUF( 3, "client hello, cookie",
580 ssl->handshake->verify_cookie,
581 ssl->handshake->verify_cookie_len );
582
583 *p++ = ssl->handshake->verify_cookie_len;
584 memcpy( p, ssl->handshake->verify_cookie,
585 ssl->handshake->verify_cookie_len );
586 p += ssl->handshake->verify_cookie_len;
587 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100588 }
589#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
Paul Bakker48916f92012-09-16 19:57:18 +0000591 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100592 * Ciphersuite list
Paul Bakker48916f92012-09-16 19:57:18 +0000593 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100594 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
595
596 /* Skip writing ciphersuite length for now */
597 n = 0;
598 q = p;
599 p += 2;
600
601 /* Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV */
Paul Bakker48916f92012-09-16 19:57:18 +0000602 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
603 {
604 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
605 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
Paul Bakker2fbefde2013-06-29 16:01:15 +0200606 n++;
Paul Bakker48916f92012-09-16 19:57:18 +0000607 }
608
Paul Bakker2fbefde2013-06-29 16:01:15 +0200609 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000610 {
Paul Bakker2fbefde2013-06-29 16:01:15 +0200611 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
612
613 if( ciphersuite_info == NULL )
614 continue;
615
616 if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
617 ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
618 continue;
619
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +0100620#if defined(POLARSSL_SSL_PROTO_DTLS)
621 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
622 ( ciphersuite_info->flags & POLARSSL_CIPHERSUITE_NODTLS ) )
623 continue;
624#endif
625
Paul Bakkere3166ce2011-01-27 17:40:50 +0000626 SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200627 ciphersuites[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000628
Paul Bakker2fbefde2013-06-29 16:01:15 +0200629 n++;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200630 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
631 *p++ = (unsigned char)( ciphersuites[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000632 }
633
Paul Bakker2fbefde2013-06-29 16:01:15 +0200634 *q++ = (unsigned char)( n >> 7 );
635 *q++ = (unsigned char)( n << 1 );
636
637 SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
638
639
Paul Bakker2770fbd2012-07-03 13:30:23 +0000640#if defined(POLARSSL_ZLIB_SUPPORT)
641 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
642 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000643 SSL_COMPRESS_DEFLATE, SSL_COMPRESS_NULL ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000644
645 *p++ = 2;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000646 *p++ = SSL_COMPRESS_DEFLATE;
Paul Bakker48916f92012-09-16 19:57:18 +0000647 *p++ = SSL_COMPRESS_NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000648#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000649 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000650 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d", SSL_COMPRESS_NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000651
652 *p++ = 1;
653 *p++ = SSL_COMPRESS_NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200654#endif /* POLARSSL_ZLIB_SUPPORT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000655
Paul Bakkerd3edc862013-03-20 16:07:17 +0100656 // First write extensions, then the total length
657 //
Paul Bakker0be444a2013-08-27 21:55:01 +0200658#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100659 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
660 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +0200661#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000662
Paul Bakkerd3edc862013-03-20 16:07:17 +0100663 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
664 ext_len += olen;
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000665
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200666#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100667 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
668 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200669#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000670
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200671#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100672 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
673 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100674
Paul Bakkerd3edc862013-03-20 16:07:17 +0100675 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
676 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100677#endif
678
Paul Bakker05decb22013-08-15 13:33:48 +0200679#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200680 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
681 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +0200682#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200683
Paul Bakker1f2bc622013-08-15 13:45:55 +0200684#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200685 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
686 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200687#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200688
Paul Bakkera503a632013-08-14 13:48:06 +0200689#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200690 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
691 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +0200692#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200693
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200694#if defined(POLARSSL_SSL_ALPN)
695 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
696 ext_len += olen;
697#endif
698
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000699 SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
700 ext_len ) );
701
Paul Bakkera7036632014-04-30 10:15:38 +0200702 if( ext_len > 0 )
703 {
704 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
705 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
706 p += ext_len;
707 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100708
Paul Bakker5121ce52009-01-03 21:22:43 +0000709 ssl->out_msglen = p - buf;
710 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
711 ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
712
713 ssl->state++;
714
715 if( ( ret = ssl_write_record( ssl ) ) != 0 )
716 {
717 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
718 return( ret );
719 }
720
721 SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
722
723 return( 0 );
724}
725
Paul Bakker48916f92012-09-16 19:57:18 +0000726static int ssl_parse_renegotiation_info( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200727 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000728 size_t len )
729{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000730 int ret;
731
Paul Bakker48916f92012-09-16 19:57:18 +0000732 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
733 {
734 if( len != 1 || buf[0] != 0x0 )
735 {
736 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000737
738 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
739 return( ret );
740
Paul Bakker48916f92012-09-16 19:57:18 +0000741 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
742 }
743
744 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
745 }
746 else
747 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100748 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000749 if( len != 1 + ssl->verify_data_len * 2 ||
750 buf[0] != ssl->verify_data_len * 2 ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100751 safer_memcmp( buf + 1,
752 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
753 safer_memcmp( buf + 1 + ssl->verify_data_len,
754 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000755 {
756 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000757
758 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
759 return( ret );
760
Paul Bakker48916f92012-09-16 19:57:18 +0000761 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
762 }
763 }
764
765 return( 0 );
766}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200767
Paul Bakker05decb22013-08-15 13:33:48 +0200768#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200769static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200770 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200771 size_t len )
772{
773 /*
774 * server should use the extension only if we did,
775 * and if so the server's value should match ours (and len is always 1)
776 */
777 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
778 len != 1 ||
779 buf[0] != ssl->mfl_code )
780 {
781 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
782 }
783
784 return( 0 );
785}
Paul Bakker05decb22013-08-15 13:33:48 +0200786#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000787
Paul Bakker1f2bc622013-08-15 13:45:55 +0200788#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200789static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
790 const unsigned char *buf,
791 size_t len )
792{
793 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
794 len != 0 )
795 {
796 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
797 }
798
799 ((void) buf);
800
801 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
802
803 return( 0 );
804}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200805#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200806
Paul Bakkera503a632013-08-14 13:48:06 +0200807#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200808static int ssl_parse_session_ticket_ext( ssl_context *ssl,
809 const unsigned char *buf,
810 size_t len )
811{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200812 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ||
813 len != 0 )
814 {
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200815 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200816 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200817
818 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200819
820 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200821
822 return( 0 );
823}
Paul Bakkera503a632013-08-14 13:48:06 +0200824#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200825
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200826#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200827static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
828 const unsigned char *buf,
829 size_t len )
830{
831 size_t list_size;
832 const unsigned char *p;
833
834 list_size = buf[0];
835 if( list_size + 1 != len )
836 {
837 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
838 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
839 }
840
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200841 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200842 while( list_size > 0 )
843 {
844 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
845 p[0] == POLARSSL_ECP_PF_COMPRESSED )
846 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200847 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200848 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
849 return( 0 );
850 }
851
852 list_size--;
853 p++;
854 }
855
Manuel Pégourié-Gonnard5c1f0322014-06-23 14:24:43 +0200856 SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
857 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200858}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200859#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200860
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200861#if defined(POLARSSL_SSL_ALPN)
862static int ssl_parse_alpn_ext( ssl_context *ssl,
863 const unsigned char *buf, size_t len )
864{
865 size_t list_len, name_len;
866 const char **p;
867
868 /* If we didn't send it, the server shouldn't send it */
869 if( ssl->alpn_list == NULL )
870 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
871
872 /*
873 * opaque ProtocolName<1..2^8-1>;
874 *
875 * struct {
876 * ProtocolName protocol_name_list<2..2^16-1>
877 * } ProtocolNameList;
878 *
879 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
880 */
881
882 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
883 if( len < 4 )
884 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
885
886 list_len = ( buf[0] << 8 ) | buf[1];
887 if( list_len != len - 2 )
888 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
889
890 name_len = buf[2];
891 if( name_len != list_len - 1 )
892 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
893
894 /* Check that the server chosen protocol was in our list and save it */
895 for( p = ssl->alpn_list; *p != NULL; p++ )
896 {
897 if( name_len == strlen( *p ) &&
898 memcmp( buf + 3, *p, name_len ) == 0 )
899 {
900 ssl->alpn_chosen = *p;
901 return( 0 );
902 }
903 }
904
905 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
906}
907#endif /* POLARSSL_SSL_ALPN */
908
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200909/*
910 * Parse HelloVerifyRequest. Only called after verifying the HS type.
911 */
912#if defined(POLARSSL_SSL_PROTO_DTLS)
913static int ssl_parse_hello_verify_request( ssl_context *ssl )
914{
915 const unsigned char *p = ssl->in_msg + 4;
916 int major_ver, minor_ver;
917 unsigned char cookie_len;
918
919 SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
920
921 /*
922 * struct {
923 * ProtocolVersion server_version;
924 * opaque cookie<0..2^8-1>;
925 * } HelloVerifyRequest;
926 */
927 SSL_DEBUG_BUF( 3, "server version", (unsigned char *) p, 2 );
928 ssl_read_version( &major_ver, &minor_ver, ssl->transport, p );
929 p += 2;
930
931 if( major_ver != SSL_MAJOR_VERSION_3 ||
932 minor_ver < SSL_MINOR_VERSION_2 ||
933 minor_ver > SSL_MINOR_VERSION_3 )
934 {
935 SSL_DEBUG_MSG( 1, ( "bad server version" ) );
936
937 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
938 SSL_ALERT_MSG_PROTOCOL_VERSION );
939
940 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
941 }
942
943 cookie_len = *p++;
944 SSL_DEBUG_BUF( 3, "cookie", (unsigned char *) p, cookie_len );
945
946 polarssl_free( ssl->handshake->verify_cookie );
947
948 ssl->handshake->verify_cookie = polarssl_malloc( cookie_len );
949 if( ssl->handshake->verify_cookie == NULL )
950 {
951 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", cookie_len ) );
952 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
953 }
954
955 memcpy( ssl->handshake->verify_cookie, p, cookie_len );
956 ssl->handshake->verify_cookie_len = cookie_len;
957
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +0200958 /* Start over at ClientHello */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200959 ssl->state = SSL_CLIENT_HELLO;
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +0200960 ssl_reset_checksum( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200961
962 SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
963
964 return( 0 );
965}
966#endif /* POLARSSL_SSL_PROTO_DTLS */
967
Paul Bakker5121ce52009-01-03 21:22:43 +0000968static int ssl_parse_server_hello( ssl_context *ssl )
969{
Paul Bakker2770fbd2012-07-03 13:30:23 +0000970 int ret, i, comp;
Paul Bakker23986e52011-04-24 08:57:21 +0000971 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +0200972 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +0000973 unsigned char *buf, *ext;
974 int renegotiation_info_seen = 0;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000975 int handshake_failure = 0;
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +0200976#if defined(POLARSSL_DEBUG_C)
977 uint32_t t;
978#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000979
980 SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
981
982 /*
983 * 0 . 0 handshake type
984 * 1 . 3 handshake length
985 * 4 . 5 protocol version
986 * 6 . 9 UNIX time()
987 * 10 . 37 random bytes
988 */
989 buf = ssl->in_msg;
990
991 if( ( ret = ssl_read_record( ssl ) ) != 0 )
992 {
993 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
994 return( ret );
995 }
996
997 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
998 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +0200999 if( ssl->renegotiation == SSL_RENEGOTIATION )
1000 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001001 ssl->renego_records_seen++;
1002
1003 if( ssl->renego_max_records >= 0 &&
1004 ssl->renego_records_seen > ssl->renego_max_records )
1005 {
1006 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
1007 "but not honored by server" ) );
1008 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1009 }
1010
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001011 SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
1012 return( POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
1013 }
1014
Paul Bakker5121ce52009-01-03 21:22:43 +00001015 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001016 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001017 }
1018
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001019#if defined(POLARSSL_SSL_PROTO_DTLS)
1020 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1021 {
1022 if( buf[0] == SSL_HS_HELLO_VERIFY_REQUEST )
1023 {
1024 SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
1025 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1026 return( ssl_parse_hello_verify_request( ssl ) );
1027 }
1028 else
1029 {
1030 /* We made it through the verification process */
1031 polarssl_free( ssl->handshake->verify_cookie );
1032 ssl->handshake->verify_cookie = NULL;
1033 ssl->handshake->verify_cookie_len = 0;
1034 }
1035 }
1036#endif /* POLARSSL_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001037
1038 if( ssl->in_hslen < 42 ||
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001039 buf[0] != SSL_HS_SERVER_HELLO )
Paul Bakker5121ce52009-01-03 21:22:43 +00001040 {
1041 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001042 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001043 }
1044
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001045 SSL_DEBUG_BUF( 3, "server hello, version", buf + 4, 2 );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001046 ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
1047 ssl->transport, buf + 4 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001048
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001049 if( ssl->major_ver < ssl->min_major_ver ||
1050 ssl->minor_ver < ssl->min_minor_ver ||
1051 ssl->major_ver > ssl->max_major_ver ||
1052 ssl->minor_ver > ssl->max_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001053 {
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001054 SSL_DEBUG_MSG( 1, ( "server version out of bounds - "
1055 " min: [%d:%d], server: [%d:%d], max: [%d:%d]",
1056 ssl->min_major_ver, ssl->min_minor_ver,
1057 ssl->major_ver, ssl->minor_ver,
1058 ssl->max_major_ver, ssl->max_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001059
1060 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1061 SSL_ALERT_MSG_PROTOCOL_VERSION );
1062
1063 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1064 }
1065
Paul Bakker1504af52012-02-11 16:17:43 +00001066#if defined(POLARSSL_DEBUG_C)
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001067 t = ( (uint32_t) buf[6] << 24 )
1068 | ( (uint32_t) buf[7] << 16 )
1069 | ( (uint32_t) buf[8] << 8 )
1070 | ( (uint32_t) buf[9] );
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001071 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakker87e5cda2012-01-14 18:14:15 +00001072#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001073
Paul Bakker48916f92012-09-16 19:57:18 +00001074 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001075
1076 n = buf[38];
1077
Paul Bakker5121ce52009-01-03 21:22:43 +00001078 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1079
Paul Bakker48916f92012-09-16 19:57:18 +00001080 if( n > 32 )
1081 {
1082 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1083 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1084 }
1085
Paul Bakker5121ce52009-01-03 21:22:43 +00001086 /*
1087 * 38 . 38 session id length
1088 * 39 . 38+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +00001089 * 39+n . 40+n chosen ciphersuite
Paul Bakker5121ce52009-01-03 21:22:43 +00001090 * 41+n . 41+n chosen compression alg.
1091 * 42+n . 43+n extensions length
1092 * 44+n . 44+n+m extensions
1093 */
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001094 if( ssl->in_hslen > 43 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001095 {
1096 ext_len = ( ( buf[42 + n] << 8 )
Paul Bakker48916f92012-09-16 19:57:18 +00001097 | ( buf[43 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001098
Paul Bakker48916f92012-09-16 19:57:18 +00001099 if( ( ext_len > 0 && ext_len < 4 ) ||
1100 ssl->in_hslen != 44 + n + ext_len )
1101 {
1102 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1103 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1104 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001105 }
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001106 else if( ssl->in_hslen == 42 + n )
1107 {
1108 ext_len = 0;
1109 }
1110 else
1111 {
1112 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1113 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1114 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001115
1116 i = ( buf[39 + n] << 8 ) | buf[40 + n];
Paul Bakker2770fbd2012-07-03 13:30:23 +00001117 comp = buf[41 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001118
Paul Bakker380da532012-04-18 16:10:25 +00001119 /*
1120 * Initialize update checksum functions
1121 */
Paul Bakker68884e32013-01-07 18:20:04 +01001122 ssl->transform_negotiate->ciphersuite_info = ssl_ciphersuite_from_id( i );
1123
1124 if( ssl->transform_negotiate->ciphersuite_info == NULL )
1125 {
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001126 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001127 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1128 }
Paul Bakker380da532012-04-18 16:10:25 +00001129
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001130 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1131
Paul Bakker5121ce52009-01-03 21:22:43 +00001132 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1133 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1134
1135 /*
1136 * Check if the session can be resumed
1137 */
Paul Bakker0a597072012-09-25 21:55:46 +00001138 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
1139 ssl->handshake->resume == 0 || n == 0 ||
Paul Bakker48916f92012-09-16 19:57:18 +00001140 ssl->session_negotiate->ciphersuite != i ||
1141 ssl->session_negotiate->compression != comp ||
1142 ssl->session_negotiate->length != n ||
1143 memcmp( ssl->session_negotiate->id, buf + 39, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001144 {
1145 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001146 ssl->handshake->resume = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001147#if defined(POLARSSL_HAVE_TIME)
Paul Bakker48916f92012-09-16 19:57:18 +00001148 ssl->session_negotiate->start = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001149#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001150 ssl->session_negotiate->ciphersuite = i;
1151 ssl->session_negotiate->compression = comp;
1152 ssl->session_negotiate->length = n;
1153 memcpy( ssl->session_negotiate->id, buf + 39, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001154 }
1155 else
1156 {
1157 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001158
1159 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1160 {
1161 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1162 return( ret );
1163 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001164 }
1165
1166 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001167 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001168
Paul Bakkere3166ce2011-01-27 17:40:50 +00001169 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001170 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[41 + n] ) );
1171
1172 i = 0;
1173 while( 1 )
1174 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001175 if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001176 {
1177 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001178 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001179 }
1180
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001181 if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
1182 ssl->session_negotiate->ciphersuite )
1183 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001184 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001185 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001186 }
1187
Paul Bakker2770fbd2012-07-03 13:30:23 +00001188 if( comp != SSL_COMPRESS_NULL
1189#if defined(POLARSSL_ZLIB_SUPPORT)
1190 && comp != SSL_COMPRESS_DEFLATE
1191#endif
1192 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001193 {
1194 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001195 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001196 }
Paul Bakker48916f92012-09-16 19:57:18 +00001197 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001198
Paul Bakker48916f92012-09-16 19:57:18 +00001199 ext = buf + 44 + n;
1200
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001201 SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
1202
Paul Bakker48916f92012-09-16 19:57:18 +00001203 while( ext_len )
1204 {
1205 unsigned int ext_id = ( ( ext[0] << 8 )
1206 | ( ext[1] ) );
1207 unsigned int ext_size = ( ( ext[2] << 8 )
1208 | ( ext[3] ) );
1209
1210 if( ext_size + 4 > ext_len )
1211 {
1212 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1213 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1214 }
1215
1216 switch( ext_id )
1217 {
1218 case TLS_EXT_RENEGOTIATION_INFO:
1219 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1220 renegotiation_info_seen = 1;
1221
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001222 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1223 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001224 return( ret );
1225
1226 break;
1227
Paul Bakker05decb22013-08-15 13:33:48 +02001228#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001229 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1230 SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
1231
1232 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1233 ext + 4, ext_size ) ) != 0 )
1234 {
1235 return( ret );
1236 }
1237
1238 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001239#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001240
Paul Bakker1f2bc622013-08-15 13:45:55 +02001241#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001242 case TLS_EXT_TRUNCATED_HMAC:
1243 SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
1244
1245 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1246 ext + 4, ext_size ) ) != 0 )
1247 {
1248 return( ret );
1249 }
1250
1251 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001252#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001253
Paul Bakkera503a632013-08-14 13:48:06 +02001254#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001255 case TLS_EXT_SESSION_TICKET:
1256 SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
1257
1258 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1259 ext + 4, ext_size ) ) != 0 )
1260 {
1261 return( ret );
1262 }
1263
1264 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001265#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001266
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001267#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001268 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1269 SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
1270
1271 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1272 ext + 4, ext_size ) ) != 0 )
1273 {
1274 return( ret );
1275 }
1276
1277 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001278#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001279
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001280#if defined(POLARSSL_SSL_ALPN)
1281 case TLS_EXT_ALPN:
1282 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1283
1284 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1285 return( ret );
1286
1287 break;
1288#endif /* POLARSSL_SSL_ALPN */
1289
Paul Bakker48916f92012-09-16 19:57:18 +00001290 default:
1291 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1292 ext_id ) );
1293 }
1294
1295 ext_len -= 4 + ext_size;
1296 ext += 4 + ext_size;
1297
1298 if( ext_len > 0 && ext_len < 4 )
1299 {
1300 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1301 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1302 }
1303 }
1304
1305 /*
1306 * Renegotiation security checks
1307 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001308 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1309 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001310 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001311 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1312 handshake_failure = 1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001313 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001314 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1315 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1316 renegotiation_info_seen == 0 )
1317 {
1318 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1319 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001320 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001321 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1322 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1323 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001324 {
1325 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001326 handshake_failure = 1;
1327 }
1328 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1329 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1330 renegotiation_info_seen == 1 )
1331 {
1332 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1333 handshake_failure = 1;
1334 }
1335
1336 if( handshake_failure == 1 )
1337 {
1338 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1339 return( ret );
1340
Paul Bakker48916f92012-09-16 19:57:18 +00001341 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1342 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001343
1344 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1345
1346 return( 0 );
1347}
1348
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001349#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1350 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001351static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
1352 unsigned char *end )
1353{
1354 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1355
Paul Bakker29e1f122013-04-16 13:07:56 +02001356 /*
1357 * Ephemeral DH parameters:
1358 *
1359 * struct {
1360 * opaque dh_p<1..2^16-1>;
1361 * opaque dh_g<1..2^16-1>;
1362 * opaque dh_Ys<1..2^16-1>;
1363 * } ServerDHParams;
1364 */
1365 if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1366 {
1367 SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
1368 return( ret );
1369 }
1370
1371 if( ssl->handshake->dhm_ctx.len < 64 ||
1372 ssl->handshake->dhm_ctx.len > 512 )
1373 {
1374 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
1375 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1376 }
1377
1378 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1379 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1380 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001381
1382 return( ret );
1383}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001384#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1385 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001386
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001387#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001388 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001389 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1390 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1391 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1392static int ssl_check_server_ecdh_params( const ssl_context *ssl )
1393{
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001394 const ecp_curve_info *curve_info;
1395
1396 curve_info = ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
1397 if( curve_info == NULL )
1398 {
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001399 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1400 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001401 }
1402
1403 SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001404
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001405#if defined(POLARSSL_SSL_ECP_SET_CURVES)
1406 if( ! ssl_curve_is_acceptable( ssl, ssl->handshake->ecdh_ctx.grp.id ) )
1407#else
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001408 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1409 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001410#endif
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001411 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001412
1413 SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
1414
1415 return( 0 );
1416}
Paul Bakker9af723c2014-05-01 13:03:14 +02001417#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1418 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1419 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1420 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1421 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001422
1423#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1424 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001425 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001426static int ssl_parse_server_ecdh_params( ssl_context *ssl,
1427 unsigned char **p,
1428 unsigned char *end )
1429{
1430 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1431
Paul Bakker29e1f122013-04-16 13:07:56 +02001432 /*
1433 * Ephemeral ECDH parameters:
1434 *
1435 * struct {
1436 * ECParameters curve_params;
1437 * ECPoint public;
1438 * } ServerECDHParams;
1439 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001440 if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
1441 (const unsigned char **) p, end ) ) != 0 )
1442 {
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001443 SSL_DEBUG_RET( 1, ( "ecdh_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001444 return( ret );
1445 }
1446
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001447 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001448 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001449 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001450 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1451 }
1452
Paul Bakker29e1f122013-04-16 13:07:56 +02001453 return( ret );
1454}
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001455#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001456 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1457 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001458
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001459#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001460static int ssl_parse_server_psk_hint( ssl_context *ssl,
1461 unsigned char **p,
1462 unsigned char *end )
1463{
1464 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001465 size_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001466 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001467
1468 /*
1469 * PSK parameters:
1470 *
1471 * opaque psk_identity_hint<0..2^16-1>;
1472 */
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001473 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001474 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001475
1476 if( (*p) + len > end )
1477 {
1478 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
1479 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1480 }
1481
1482 // TODO: Retrieve PSK identity hint and callback to app
1483 //
1484 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001485 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001486
1487 return( ret );
1488}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001489#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001490
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001491#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1492 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1493/*
1494 * Generate a pre-master secret and encrypt it with the server's RSA key
1495 */
1496static int ssl_write_encrypted_pms( ssl_context *ssl,
1497 size_t offset, size_t *olen,
1498 size_t pms_offset )
1499{
1500 int ret;
1501 size_t len_bytes = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 0 : 2;
1502 unsigned char *p = ssl->handshake->premaster + pms_offset;
1503
1504 /*
1505 * Generate (part of) the pre-master as
1506 * struct {
1507 * ProtocolVersion client_version;
1508 * opaque random[46];
1509 * } PreMasterSecret;
1510 */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001511 ssl_write_version( ssl->max_major_ver, ssl->max_minor_ver,
1512 ssl->transport, p );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001513
1514 if( ( ret = ssl->f_rng( ssl->p_rng, p + 2, 46 ) ) != 0 )
1515 {
1516 SSL_DEBUG_RET( 1, "f_rng", ret );
1517 return( ret );
1518 }
1519
1520 ssl->handshake->pmslen = 48;
1521
1522 /*
1523 * Now write it out, encrypted
1524 */
1525 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1526 POLARSSL_PK_RSA ) )
1527 {
1528 SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1529 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1530 }
1531
1532 if( ( ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
1533 p, ssl->handshake->pmslen,
1534 ssl->out_msg + offset + len_bytes, olen,
1535 SSL_MAX_CONTENT_LEN - offset - len_bytes,
1536 ssl->f_rng, ssl->p_rng ) ) != 0 )
1537 {
1538 SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
1539 return( ret );
1540 }
1541
1542#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1543 defined(POLARSSL_SSL_PROTO_TLS1_2)
1544 if( len_bytes == 2 )
1545 {
1546 ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
1547 ssl->out_msg[offset+1] = (unsigned char)( *olen );
1548 *olen += 2;
1549 }
1550#endif
1551
1552 return( 0 );
1553}
1554#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
1555 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001556
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001557#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001558#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001559 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1560 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001561static int ssl_parse_signature_algorithm( ssl_context *ssl,
1562 unsigned char **p,
1563 unsigned char *end,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001564 md_type_t *md_alg,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001565 pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02001566{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001567 ((void) ssl);
Paul Bakker29e1f122013-04-16 13:07:56 +02001568 *md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001569 *pk_alg = POLARSSL_PK_NONE;
1570
1571 /* Only in TLS 1.2 */
1572 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1573 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001574 return( 0 );
1575 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001576
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001577 if( (*p) + 2 > end )
Paul Bakker29e1f122013-04-16 13:07:56 +02001578 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1579
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001580 /*
1581 * Get hash algorithm
1582 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001583 if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001584 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001585 SSL_DEBUG_MSG( 2, ( "Server used unsupported "
1586 "HashAlgorithm %d", *(p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001587 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1588 }
1589
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001590 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001591 * Get signature algorithm
1592 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001593 if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001594 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001595 SSL_DEBUG_MSG( 2, ( "server used unsupported "
1596 "SignatureAlgorithm %d", (*p)[1] ) );
1597 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001598 }
1599
1600 SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
1601 SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
1602 *p += 2;
1603
1604 return( 0 );
1605}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001606#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001607 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1608 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001609#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001610
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001611
1612#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1613 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1614static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
1615{
1616 int ret;
1617 const ecp_keypair *peer_key;
1618
1619 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1620 POLARSSL_PK_ECKEY ) )
1621 {
1622 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
1623 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1624 }
1625
1626 peer_key = pk_ec( ssl->session_negotiate->peer_cert->pk );
1627
1628 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
1629 POLARSSL_ECDH_THEIRS ) ) != 0 )
1630 {
1631 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
1632 return( ret );
1633 }
1634
1635 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1636 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001637 SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001638 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
1639 }
1640
1641 return( ret );
1642}
1643#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1644 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1645
Paul Bakker41c83d32013-03-20 14:39:14 +01001646static int ssl_parse_server_key_exchange( ssl_context *ssl )
1647{
Paul Bakker23986e52011-04-24 08:57:21 +00001648 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001649 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001650 unsigned char *p, *end;
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)
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001654 size_t sig_len, params_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001655 unsigned char hash[64];
Paul Bakkerc70b9822013-04-07 22:00:46 +02001656 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001657 size_t hashlen;
1658 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001659#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001660
1661 SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
1662
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001663#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001664 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001665 {
1666 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1667 ssl->state++;
1668 return( 0 );
1669 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001670 ((void) p);
1671 ((void) end);
1672#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001673
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001674#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1675 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1676 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
1677 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
1678 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001679 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
1680 {
1681 SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
1682 return( ret );
1683 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001684
1685 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1686 ssl->state++;
1687 return( 0 );
1688 }
1689 ((void) p);
1690 ((void) end);
Paul Bakker9af723c2014-05-01 13:03:14 +02001691#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1692 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001693
Paul Bakker5121ce52009-01-03 21:22:43 +00001694 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1695 {
1696 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1697 return( ret );
1698 }
1699
1700 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1701 {
1702 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001703 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001704 }
1705
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001706 /*
1707 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
1708 * doesn't use a psk_identity_hint
1709 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001710 if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
1711 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001712 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1713 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02001714 {
1715 ssl->record_read = 1;
1716 goto exit;
1717 }
1718
1719 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1720 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001721 }
1722
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001723 p = ssl->in_msg + 4;
1724 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001725 SSL_DEBUG_BUF( 3, "server key exchange", p, ssl->in_hslen - 4 );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001726
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001727#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1728 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1729 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1730 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1731 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1732 {
1733 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1734 {
1735 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1736 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1737 }
1738 } /* FALLTROUGH */
1739#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
1740
1741#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
1742 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1743 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1744 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1745 ; /* nothing more to do */
1746 else
1747#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
1748 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
1749#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1750 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1751 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1752 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001753 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001754 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001755 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001756 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001757 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1758 }
1759 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001760 else
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001761#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1762 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001763#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001764 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001765 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1766 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001767 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001768 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001769 {
1770 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
1771 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001772 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1773 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1774 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001775 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001776 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001777#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001778 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001779 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001780 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001781 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001782 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001783 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001784
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001785#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001786 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1787 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001788 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001789 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1790 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001791 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001792 params_len = p - ( ssl->in_msg + 4 );
1793
Paul Bakker29e1f122013-04-16 13:07:56 +02001794 /*
1795 * Handle the digitally-signed structure
1796 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001797#if defined(POLARSSL_SSL_PROTO_TLS1_2)
1798 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001799 {
Paul Bakker9659dae2013-08-28 16:21:34 +02001800 if( ssl_parse_signature_algorithm( ssl, &p, end,
1801 &md_alg, &pk_alg ) != 0 )
1802 {
1803 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1804 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1805 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001806
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001807 if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001808 {
1809 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1810 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1811 }
1812 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001813 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001814#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001815#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1816 defined(POLARSSL_SSL_PROTO_TLS1_1)
1817 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001818 {
1819 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Paul Bakker1ef83d62012-04-11 12:09:53 +00001820
Paul Bakker9659dae2013-08-28 16:21:34 +02001821 /* Default hash for ECDSA is SHA-1 */
1822 if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
1823 md_alg = POLARSSL_MD_SHA1;
1824 }
1825 else
1826#endif
1827 {
1828 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001829 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker9659dae2013-08-28 16:21:34 +02001830 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001831
1832 /*
1833 * Read signature
1834 */
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001835 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00001836 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001837
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001838 if( end != p + sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01001839 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001840 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001841 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1842 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001843
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001844 SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02001845
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001846 /*
1847 * Compute the hash that has been signed
1848 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001849#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1850 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001851 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001852 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001853 md5_context md5;
1854 sha1_context sha1;
1855
Paul Bakker5b4af392014-06-26 12:09:34 +02001856 md5_init( &md5 );
1857 sha1_init( &sha1 );
1858
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001859 hashlen = 36;
1860
Paul Bakker29e1f122013-04-16 13:07:56 +02001861 /*
1862 * digitally-signed struct {
1863 * opaque md5_hash[16];
1864 * opaque sha_hash[20];
1865 * };
1866 *
1867 * md5_hash
1868 * MD5(ClientHello.random + ServerHello.random
1869 * + ServerParams);
1870 * sha_hash
1871 * SHA(ClientHello.random + ServerHello.random
1872 * + ServerParams);
1873 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001874 md5_starts( &md5 );
1875 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001876 md5_update( &md5, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001877 md5_finish( &md5, hash );
1878
1879 sha1_starts( &sha1 );
1880 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001881 sha1_update( &sha1, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001882 sha1_finish( &sha1, hash + 16 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001883
1884 md5_free( &md5 );
1885 sha1_free( &sha1 );
Paul Bakker29e1f122013-04-16 13:07:56 +02001886 }
1887 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001888#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
1889 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001890#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1891 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02001892 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001893 {
1894 md_context_t ctx;
1895
Paul Bakker84bbeb52014-07-01 14:53:22 +02001896 md_init( &ctx );
1897
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001898 /* Info from md_alg will be used instead */
1899 hashlen = 0;
Paul Bakker29e1f122013-04-16 13:07:56 +02001900
1901 /*
1902 * digitally-signed struct {
1903 * opaque client_random[32];
1904 * opaque server_random[32];
1905 * ServerDHParams params;
1906 * };
1907 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001908 if( ( ret = md_init_ctx( &ctx,
1909 md_info_from_type( md_alg ) ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001910 {
1911 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
1912 return( ret );
1913 }
1914
1915 md_starts( &ctx );
1916 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001917 md_update( &ctx, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001918 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02001919 md_free( &ctx );
Paul Bakker29e1f122013-04-16 13:07:56 +02001920 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001921 else
Paul Bakker9659dae2013-08-28 16:21:34 +02001922#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1923 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001924 {
Paul Bakker577e0062013-08-28 11:57:20 +02001925 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001926 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001927 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001928
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02001929 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
1930 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker29e1f122013-04-16 13:07:56 +02001931
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001932 /*
1933 * Verify signature
1934 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02001935 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001936 {
1937 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1938 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1939 }
1940
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001941 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
1942 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001943 {
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001944 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001945 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001946 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001947 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001948#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001949 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1950 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001951
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001952exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00001953 ssl->state++;
1954
1955 SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
1956
1957 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001958}
1959
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001960#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1961 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
1962 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1963 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1964static int ssl_parse_certificate_request( ssl_context *ssl )
1965{
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001966 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
1967
1968 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1969
1970 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1971 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1972 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1973 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1974 {
1975 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
1976 ssl->state++;
1977 return( 0 );
1978 }
1979
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001980 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1981 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001982}
1983#else
Paul Bakker5121ce52009-01-03 21:22:43 +00001984static int ssl_parse_certificate_request( ssl_context *ssl )
1985{
1986 int ret;
Paul Bakker926af752012-11-23 13:38:07 +01001987 unsigned char *buf, *p;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01001988 size_t n = 0, m = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001989 size_t cert_type_len = 0, dn_len = 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001990 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001991
1992 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1993
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001994 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1995 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1996 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1997 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1998 {
1999 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2000 ssl->state++;
2001 return( 0 );
2002 }
2003
Paul Bakker5121ce52009-01-03 21:22:43 +00002004 /*
2005 * 0 . 0 handshake type
2006 * 1 . 3 handshake length
Paul Bakker926af752012-11-23 13:38:07 +01002007 * 4 . 4 cert type count
2008 * 5 .. m-1 cert types
2009 * m .. m+1 sig alg length (TLS 1.2 only)
2010 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002011 * n .. n+1 length of all DNs
2012 * n+2 .. n+3 length of DN 1
2013 * n+4 .. ... Distinguished Name #1
2014 * ... .. ... length of DN 2, etc.
2015 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002016 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002017 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002018 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2019 {
2020 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2021 return( ret );
2022 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002023
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002024 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2025 {
2026 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2027 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2028 }
2029
2030 ssl->record_read = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002031 }
2032
2033 ssl->client_auth = 0;
2034 ssl->state++;
2035
2036 if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST )
2037 ssl->client_auth++;
2038
2039 SSL_DEBUG_MSG( 3, ( "got %s certificate request",
2040 ssl->client_auth ? "a" : "no" ) );
2041
Paul Bakker926af752012-11-23 13:38:07 +01002042 if( ssl->client_auth == 0 )
2043 goto exit;
2044
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002045 ssl->record_read = 0;
2046
Paul Bakker926af752012-11-23 13:38:07 +01002047 // TODO: handshake_failure alert for an anonymous server to request
2048 // client authentication
2049
2050 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002051
Paul Bakker926af752012-11-23 13:38:07 +01002052 // Retrieve cert types
2053 //
2054 cert_type_len = buf[4];
2055 n = cert_type_len;
2056
2057 if( ssl->in_hslen < 6 + n )
2058 {
2059 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2060 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2061 }
2062
Paul Bakker73d44312013-05-22 13:56:26 +02002063 p = buf + 5;
Paul Bakker926af752012-11-23 13:38:07 +01002064 while( cert_type_len > 0 )
2065 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002066#if defined(POLARSSL_RSA_C)
2067 if( *p == SSL_CERT_TYPE_RSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002068 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker926af752012-11-23 13:38:07 +01002069 {
2070 ssl->handshake->cert_type = SSL_CERT_TYPE_RSA_SIGN;
2071 break;
2072 }
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002073 else
2074#endif
2075#if defined(POLARSSL_ECDSA_C)
2076 if( *p == SSL_CERT_TYPE_ECDSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002077 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002078 {
2079 ssl->handshake->cert_type = SSL_CERT_TYPE_ECDSA_SIGN;
2080 break;
2081 }
2082 else
2083#endif
2084 {
2085 ; /* Unsupported cert type, ignore */
2086 }
Paul Bakker926af752012-11-23 13:38:07 +01002087
2088 cert_type_len--;
2089 p++;
2090 }
2091
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002092#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002093 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2094 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002095 /* Ignored, see comments about hash in write_certificate_verify */
2096 // TODO: should check the signature part against our pk_key though
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002097 size_t sig_alg_len = ( ( buf[5 + n] << 8 )
2098 | ( buf[6 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002099
2100 p = buf + 7 + n;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002101 m += 2;
Paul Bakker926af752012-11-23 13:38:07 +01002102 n += sig_alg_len;
2103
2104 if( ssl->in_hslen < 6 + n )
2105 {
2106 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2107 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2108 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002109 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002110#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker926af752012-11-23 13:38:07 +01002111
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002112 /* Ignore certificate_authorities, we only have one cert anyway */
2113 // TODO: should not send cert if no CA matches
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002114 dn_len = ( ( buf[5 + m + n] << 8 )
2115 | ( buf[6 + m + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002116
2117 n += dn_len;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002118 if( ssl->in_hslen != 7 + m + n )
Paul Bakker926af752012-11-23 13:38:07 +01002119 {
2120 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2121 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2122 }
2123
2124exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2126
2127 return( 0 );
2128}
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002129#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2130 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2131 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2132 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002133
2134static int ssl_parse_server_hello_done( ssl_context *ssl )
2135{
2136 int ret;
2137
2138 SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
2139
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002140 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002141 {
2142 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2143 {
2144 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2145 return( ret );
2146 }
2147
2148 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2149 {
2150 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002151 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002152 }
2153 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002154 ssl->record_read = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002155
2156 if( ssl->in_hslen != 4 ||
2157 ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
2158 {
2159 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002160 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002161 }
2162
2163 ssl->state++;
2164
2165 SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
2166
2167 return( 0 );
2168}
2169
2170static int ssl_write_client_key_exchange( ssl_context *ssl )
2171{
Paul Bakker23986e52011-04-24 08:57:21 +00002172 int ret;
2173 size_t i, n;
Paul Bakker41c83d32013-03-20 14:39:14 +01002174 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002175
2176 SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
2177
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002178#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002179 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002180 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002181 /*
2182 * DHM key exchange -- send G^X mod P
2183 */
Paul Bakker48916f92012-09-16 19:57:18 +00002184 n = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002185
2186 ssl->out_msg[4] = (unsigned char)( n >> 8 );
2187 ssl->out_msg[5] = (unsigned char)( n );
2188 i = 6;
2189
Paul Bakker29b64762012-09-25 09:36:44 +00002190 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002191 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker5121ce52009-01-03 21:22:43 +00002192 &ssl->out_msg[i], n,
2193 ssl->f_rng, ssl->p_rng );
2194 if( ret != 0 )
2195 {
2196 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2197 return( ret );
2198 }
2199
Paul Bakker48916f92012-09-16 19:57:18 +00002200 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2201 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002202
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002203 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002204
Paul Bakker48916f92012-09-16 19:57:18 +00002205 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2206 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002207 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002208 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002209 {
2210 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2211 return( ret );
2212 }
2213
Paul Bakker48916f92012-09-16 19:57:18 +00002214 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002215 }
2216 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002217#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002218#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002219 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2220 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2221 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002222 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002223 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2224 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2225 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002226 {
2227 /*
2228 * ECDH key exchange -- send client public value
2229 */
2230 i = 4;
2231
2232 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
2233 &n,
2234 &ssl->out_msg[i], 1000,
2235 ssl->f_rng, ssl->p_rng );
2236 if( ret != 0 )
2237 {
2238 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2239 return( ret );
2240 }
2241
2242 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2243
2244 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2245 &ssl->handshake->pmslen,
2246 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002247 POLARSSL_MPI_MAX_SIZE,
2248 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002249 {
2250 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2251 return( ret );
2252 }
2253
2254 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2255 }
2256 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002257#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002258 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2259 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2260 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002261#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002262 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002263 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002264 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2265 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002266 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002267 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002268 * opaque psk_identity<0..2^16-1>;
2269 */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002270 if( ssl->psk == NULL || ssl->psk_identity == NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002271 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2272
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002273 i = 4;
2274 n = ssl->psk_identity_len;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002275 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2276 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002277
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002278 memcpy( ssl->out_msg + i, ssl->psk_identity, ssl->psk_identity_len );
2279 i += ssl->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002280
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002281#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002282 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002283 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002284 n = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002285 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002286 else
2287#endif
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002288#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2289 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2290 {
2291 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2292 return( ret );
2293 }
2294 else
2295#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002296#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002297 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002298 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002299 /*
2300 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
2301 */
2302 n = ssl->handshake->dhm_ctx.len;
2303 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2304 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002305
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002306 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakker68881672013-10-15 13:24:01 +02002307 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002308 &ssl->out_msg[i], n,
2309 ssl->f_rng, ssl->p_rng );
2310 if( ret != 0 )
2311 {
2312 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2313 return( ret );
2314 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002315 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002316 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002317#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002318#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002319 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002320 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002321 /*
2322 * ClientECDiffieHellmanPublic public;
2323 */
2324 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
2325 &ssl->out_msg[i], SSL_MAX_CONTENT_LEN - i,
2326 ssl->f_rng, ssl->p_rng );
2327 if( ret != 0 )
2328 {
2329 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2330 return( ret );
2331 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002332
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002333 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2334 }
2335 else
2336#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2337 {
2338 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002339 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002340 }
2341
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002342 if( ( ret = ssl_psk_derive_premaster( ssl,
2343 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002344 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002345 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002346 return( ret );
2347 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002348 }
2349 else
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002350#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002351#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +02002352 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002353 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002354 i = 4;
2355 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002356 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002357 }
Paul Bakkered27a042013-04-18 22:46:23 +02002358 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002359#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002360 {
2361 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002362 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002363 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02002364 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002365
Paul Bakkerff60ee62010-03-16 21:09:09 +00002366 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2367 {
2368 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2369 return( ret );
2370 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002371
2372 ssl->out_msglen = i + n;
2373 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2374 ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE;
2375
2376 ssl->state++;
2377
2378 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2379 {
2380 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2381 return( ret );
2382 }
2383
2384 SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
2385
2386 return( 0 );
2387}
2388
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002389#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2390 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002391 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2392 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002393static int ssl_write_certificate_verify( ssl_context *ssl )
2394{
Paul Bakkered27a042013-04-18 22:46:23 +02002395 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002396
2397 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2398
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002399 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002400 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002401 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002402 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002403 {
2404 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2405 ssl->state++;
2406 return( 0 );
2407 }
2408
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002409 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2410 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002411}
2412#else
2413static int ssl_write_certificate_verify( ssl_context *ssl )
2414{
2415 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2416 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2417 size_t n = 0, offset = 0;
2418 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002419 unsigned char *hash_start = hash;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002420 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002421 unsigned int hashlen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002422
2423 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2424
2425 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002426 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002427 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002428 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2429 {
2430 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2431 ssl->state++;
2432 return( 0 );
2433 }
2434
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002435 if( ssl->client_auth == 0 || ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002436 {
2437 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2438 ssl->state++;
2439 return( 0 );
2440 }
2441
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002442 if( ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002443 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00002444 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2445 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002446 }
2447
2448 /*
2449 * Make an RSA signature of the handshake digests
2450 */
Paul Bakker48916f92012-09-16 19:57:18 +00002451 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002452
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002453#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2454 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker926af752012-11-23 13:38:07 +01002455 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002456 {
Paul Bakker926af752012-11-23 13:38:07 +01002457 /*
2458 * digitally-signed struct {
2459 * opaque md5_hash[16];
2460 * opaque sha_hash[20];
2461 * };
2462 *
2463 * md5_hash
2464 * MD5(handshake_messages);
2465 *
2466 * sha_hash
2467 * SHA(handshake_messages);
2468 */
2469 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002470 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002471
2472 /*
2473 * For ECDSA, default hash is SHA-1 only
2474 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002475 if( pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002476 {
2477 hash_start += 16;
2478 hashlen -= 16;
2479 md_alg = POLARSSL_MD_SHA1;
2480 }
Paul Bakker926af752012-11-23 13:38:07 +01002481 }
2482 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002483#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2484 POLARSSL_SSL_PROTO_TLS1_1 */
2485#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2486 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002487 {
2488 /*
2489 * digitally-signed struct {
2490 * opaque handshake_messages[handshake_messages_length];
2491 * };
2492 *
2493 * Taking shortcut here. We assume that the server always allows the
2494 * PRF Hash function and has sent it in the allowed signature
2495 * algorithms list received in the Certificate Request message.
2496 *
2497 * Until we encounter a server that does not, we will take this
2498 * shortcut.
2499 *
2500 * Reason: Otherwise we should have running hashes for SHA512 and SHA224
2501 * in order to satisfy 'weird' needs from the server side.
2502 */
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002503 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2504 POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002505 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002506 md_alg = POLARSSL_MD_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002507 ssl->out_msg[4] = SSL_HASH_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002508 }
2509 else
2510 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002511 md_alg = POLARSSL_MD_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002512 ssl->out_msg[4] = SSL_HASH_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002513 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002514 ssl->out_msg[5] = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002515
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002516 /* Info from md_alg will be used instead */
2517 hashlen = 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002518 offset = 2;
2519 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002520 else
2521#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002522 {
2523 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002524 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002525 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002526
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002527 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002528 ssl->out_msg + 6 + offset, &n,
2529 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002530 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002531 SSL_DEBUG_RET( 1, "pk_sign", ret );
2532 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002533 }
Paul Bakker926af752012-11-23 13:38:07 +01002534
Paul Bakker1ef83d62012-04-11 12:09:53 +00002535 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
2536 ssl->out_msg[5 + offset] = (unsigned char)( n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002537
Paul Bakker1ef83d62012-04-11 12:09:53 +00002538 ssl->out_msglen = 6 + n + offset;
Paul Bakker5121ce52009-01-03 21:22:43 +00002539 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2540 ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY;
2541
2542 ssl->state++;
2543
2544 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2545 {
2546 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2547 return( ret );
2548 }
2549
2550 SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
2551
Paul Bakkered27a042013-04-18 22:46:23 +02002552 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002553}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002554#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2555 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2556 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002557
Paul Bakkera503a632013-08-14 13:48:06 +02002558#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002559static int ssl_parse_new_session_ticket( ssl_context *ssl )
2560{
2561 int ret;
2562 uint32_t lifetime;
2563 size_t ticket_len;
2564 unsigned char *ticket;
2565
2566 SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2567
2568 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2569 {
2570 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2571 return( ret );
2572 }
2573
2574 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2575 {
2576 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2577 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2578 }
2579
2580 /*
2581 * struct {
2582 * uint32 ticket_lifetime_hint;
2583 * opaque ticket<0..2^16-1>;
2584 * } NewSessionTicket;
2585 *
2586 * 0 . 0 handshake message type
2587 * 1 . 3 handshake message length
2588 * 4 . 7 ticket_lifetime_hint
2589 * 8 . 9 ticket_len (n)
2590 * 10 . 9+n ticket content
2591 */
2592 if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
2593 ssl->in_hslen < 10 )
2594 {
2595 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2596 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2597 }
2598
2599 lifetime = ( ssl->in_msg[4] << 24 ) | ( ssl->in_msg[5] << 16 ) |
2600 ( ssl->in_msg[6] << 8 ) | ( ssl->in_msg[7] );
2601
2602 ticket_len = ( ssl->in_msg[8] << 8 ) | ( ssl->in_msg[9] );
2603
2604 if( ticket_len + 10 != ssl->in_hslen )
2605 {
2606 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2607 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2608 }
2609
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002610 SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
2611
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002612 /* We're not waiting for a NewSessionTicket message any more */
2613 ssl->handshake->new_session_ticket = 0;
2614
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002615 /*
2616 * Zero-length ticket means the server changed his mind and doesn't want
2617 * to send a ticket after all, so just forget it
2618 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002619 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002620 return( 0 );
2621
Paul Bakker34617722014-06-13 17:20:13 +02002622 polarssl_zeroize( ssl->session_negotiate->ticket,
2623 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002624 polarssl_free( ssl->session_negotiate->ticket );
2625 ssl->session_negotiate->ticket = NULL;
2626 ssl->session_negotiate->ticket_len = 0;
2627
2628 if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
2629 {
2630 SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
2631 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2632 }
2633
2634 memcpy( ticket, ssl->in_msg + 10, ticket_len );
2635
2636 ssl->session_negotiate->ticket = ticket;
2637 ssl->session_negotiate->ticket_len = ticket_len;
2638 ssl->session_negotiate->ticket_lifetime = lifetime;
2639
2640 /*
2641 * RFC 5077 section 3.4:
2642 * "If the client receives a session ticket from the server, then it
2643 * discards any Session ID that was sent in the ServerHello."
2644 */
2645 SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
2646 ssl->session_negotiate->length = 0;
2647
2648 SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2649
2650 return( 0 );
2651}
Paul Bakkera503a632013-08-14 13:48:06 +02002652#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002653
Paul Bakker5121ce52009-01-03 21:22:43 +00002654/*
Paul Bakker1961b702013-01-25 14:49:24 +01002655 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002656 */
Paul Bakker1961b702013-01-25 14:49:24 +01002657int ssl_handshake_client_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002658{
2659 int ret = 0;
2660
Paul Bakker1961b702013-01-25 14:49:24 +01002661 if( ssl->state == SSL_HANDSHAKE_OVER )
2662 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002663
Paul Bakker1961b702013-01-25 14:49:24 +01002664 SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
2665
2666 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2667 return( ret );
2668
2669 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002670 {
Paul Bakker1961b702013-01-25 14:49:24 +01002671 case SSL_HELLO_REQUEST:
2672 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002673 break;
2674
Paul Bakker1961b702013-01-25 14:49:24 +01002675 /*
2676 * ==> ClientHello
2677 */
2678 case SSL_CLIENT_HELLO:
2679 ret = ssl_write_client_hello( ssl );
2680 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002681
Paul Bakker1961b702013-01-25 14:49:24 +01002682 /*
2683 * <== ServerHello
2684 * Certificate
2685 * ( ServerKeyExchange )
2686 * ( CertificateRequest )
2687 * ServerHelloDone
2688 */
2689 case SSL_SERVER_HELLO:
2690 ret = ssl_parse_server_hello( ssl );
2691 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002692
Paul Bakker1961b702013-01-25 14:49:24 +01002693 case SSL_SERVER_CERTIFICATE:
2694 ret = ssl_parse_certificate( ssl );
2695 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002696
Paul Bakker1961b702013-01-25 14:49:24 +01002697 case SSL_SERVER_KEY_EXCHANGE:
2698 ret = ssl_parse_server_key_exchange( ssl );
2699 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002700
Paul Bakker1961b702013-01-25 14:49:24 +01002701 case SSL_CERTIFICATE_REQUEST:
2702 ret = ssl_parse_certificate_request( ssl );
2703 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002704
Paul Bakker1961b702013-01-25 14:49:24 +01002705 case SSL_SERVER_HELLO_DONE:
2706 ret = ssl_parse_server_hello_done( ssl );
2707 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002708
Paul Bakker1961b702013-01-25 14:49:24 +01002709 /*
2710 * ==> ( Certificate/Alert )
2711 * ClientKeyExchange
2712 * ( CertificateVerify )
2713 * ChangeCipherSpec
2714 * Finished
2715 */
2716 case SSL_CLIENT_CERTIFICATE:
2717 ret = ssl_write_certificate( ssl );
2718 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002719
Paul Bakker1961b702013-01-25 14:49:24 +01002720 case SSL_CLIENT_KEY_EXCHANGE:
2721 ret = ssl_write_client_key_exchange( ssl );
2722 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002723
Paul Bakker1961b702013-01-25 14:49:24 +01002724 case SSL_CERTIFICATE_VERIFY:
2725 ret = ssl_write_certificate_verify( ssl );
2726 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002727
Paul Bakker1961b702013-01-25 14:49:24 +01002728 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2729 ret = ssl_write_change_cipher_spec( ssl );
2730 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002731
Paul Bakker1961b702013-01-25 14:49:24 +01002732 case SSL_CLIENT_FINISHED:
2733 ret = ssl_write_finished( ssl );
2734 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002735
Paul Bakker1961b702013-01-25 14:49:24 +01002736 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002737 * <== ( NewSessionTicket )
2738 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002739 * Finished
2740 */
2741 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002742#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002743 if( ssl->handshake->new_session_ticket != 0 )
2744 ret = ssl_parse_new_session_ticket( ssl );
2745 else
Paul Bakkera503a632013-08-14 13:48:06 +02002746#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002747 ret = ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002748 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002749
Paul Bakker1961b702013-01-25 14:49:24 +01002750 case SSL_SERVER_FINISHED:
2751 ret = ssl_parse_finished( ssl );
2752 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002753
Paul Bakker1961b702013-01-25 14:49:24 +01002754 case SSL_FLUSH_BUFFERS:
2755 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2756 ssl->state = SSL_HANDSHAKE_WRAPUP;
2757 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002758
Paul Bakker1961b702013-01-25 14:49:24 +01002759 case SSL_HANDSHAKE_WRAPUP:
2760 ssl_handshake_wrapup( ssl );
2761 break;
Paul Bakker48916f92012-09-16 19:57:18 +00002762
Paul Bakker1961b702013-01-25 14:49:24 +01002763 default:
2764 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2765 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2766 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002767
2768 return( ret );
2769}
Paul Bakker9af723c2014-05-01 13:03:14 +02002770#endif /* POLARSSL_SSL_CLI_C */