blob: fd0a81165a0bf3a13b96fbbc9e88ab59d5ba5854 [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
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100362#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
363static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
364 unsigned char *buf, size_t *olen )
365{
366 unsigned char *p = buf;
367
368 if( ssl->encrypt_then_mac == SSL_ETM_DISABLED ||
369 ssl->max_minor_ver == SSL_MINOR_VERSION_0 )
370 {
371 *olen = 0;
372 return;
373 }
374
375 SSL_DEBUG_MSG( 3, ( "client hello, adding encrypt_then_mac "
376 "extension" ) );
377
378 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
379 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
380
381 *p++ = 0x00;
382 *p++ = 0x00;
383
384 *olen = 4;
385}
386#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
387
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200388#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
389static void ssl_write_extended_ms_ext( ssl_context *ssl,
390 unsigned char *buf, size_t *olen )
391{
392 unsigned char *p = buf;
393
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200394 if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED ||
395 ssl->max_minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200396 {
397 *olen = 0;
398 return;
399 }
400
401 SSL_DEBUG_MSG( 3, ( "client hello, adding extended_master_secret "
402 "extension" ) );
403
404 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
405 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
406
407 *p++ = 0x00;
408 *p++ = 0x00;
409
410 *olen = 4;
411}
412#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
413
Paul Bakkera503a632013-08-14 13:48:06 +0200414#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200415static void ssl_write_session_ticket_ext( ssl_context *ssl,
416 unsigned char *buf, size_t *olen )
417{
418 unsigned char *p = buf;
419 size_t tlen = ssl->session_negotiate->ticket_len;
420
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200421 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
422 {
423 *olen = 0;
424 return;
425 }
426
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200427 SSL_DEBUG_MSG( 3, ( "client hello, adding session ticket extension" ) );
428
429 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
430 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
431
432 *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF );
433 *p++ = (unsigned char)( ( tlen ) & 0xFF );
434
435 *olen = 4;
436
437 if( ssl->session_negotiate->ticket == NULL ||
438 ssl->session_negotiate->ticket_len == 0 )
439 {
440 return;
441 }
442
443 SSL_DEBUG_MSG( 3, ( "sending session ticket of length %d", tlen ) );
444
445 memcpy( p, ssl->session_negotiate->ticket, tlen );
446
447 *olen += tlen;
448}
Paul Bakkera503a632013-08-14 13:48:06 +0200449#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200450
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200451#if defined(POLARSSL_SSL_ALPN)
452static void ssl_write_alpn_ext( ssl_context *ssl,
453 unsigned char *buf, size_t *olen )
454{
455 unsigned char *p = buf;
456 const char **cur;
457
458 if( ssl->alpn_list == NULL )
459 {
460 *olen = 0;
461 return;
462 }
463
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +0200464 SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200465
466 *p++ = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
467 *p++ = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
468
469 /*
470 * opaque ProtocolName<1..2^8-1>;
471 *
472 * struct {
473 * ProtocolName protocol_name_list<2..2^16-1>
474 * } ProtocolNameList;
475 */
476
477 /* Skip writing extension and list length for now */
478 p += 4;
479
480 for( cur = ssl->alpn_list; *cur != NULL; cur++ )
481 {
482 *p = (unsigned char)( strlen( *cur ) & 0xFF );
483 memcpy( p + 1, *cur, *p );
484 p += 1 + *p;
485 }
486
487 *olen = p - buf;
488
489 /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
490 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
491 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
492
493 /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
494 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
495 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
496}
497#endif /* POLARSSL_SSL_ALPN */
498
Paul Bakker5121ce52009-01-03 21:22:43 +0000499static int ssl_write_client_hello( ssl_context *ssl )
500{
Paul Bakker23986e52011-04-24 08:57:21 +0000501 int ret;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100502 size_t i, n, olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000503 unsigned char *buf;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200504 unsigned char *p, *q;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200505#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200507#endif
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200508 const int *ciphersuites;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200509 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000510
511 SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
512
Paul Bakkera9a028e2013-11-21 17:31:06 +0100513 if( ssl->f_rng == NULL )
514 {
515 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
516 return( POLARSSL_ERR_SSL_NO_RNG );
517 }
518
Paul Bakker48916f92012-09-16 19:57:18 +0000519 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
520 {
Paul Bakker993d11d2012-09-28 15:00:12 +0000521 ssl->major_ver = ssl->min_major_ver;
522 ssl->minor_ver = ssl->min_minor_ver;
Paul Bakker48916f92012-09-16 19:57:18 +0000523 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
Paul Bakker490ecc82011-10-06 13:04:09 +0000525 if( ssl->max_major_ver == 0 && ssl->max_minor_ver == 0 )
526 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200527 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
528 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker490ecc82011-10-06 13:04:09 +0000529 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000530
531 /*
532 * 0 . 0 handshake type
533 * 1 . 3 handshake length
534 * 4 . 5 highest version supported
535 * 6 . 9 current UNIX time
536 * 10 . 37 random bytes
537 */
538 buf = ssl->out_msg;
539 p = buf + 4;
540
541 *p++ = (unsigned char) ssl->max_major_ver;
542 *p++ = (unsigned char) ssl->max_minor_ver;
543
544 SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
545 buf[4], buf[5] ) );
546
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200547#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000548 t = time( NULL );
549 *p++ = (unsigned char)( t >> 24 );
550 *p++ = (unsigned char)( t >> 16 );
551 *p++ = (unsigned char)( t >> 8 );
552 *p++ = (unsigned char)( t );
553
554 SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200555#else
556 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
557 return( ret );
558
559 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +0200560#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000561
Paul Bakkera3d195c2011-11-27 21:07:34 +0000562 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
563 return( ret );
564
565 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +0000566
Paul Bakker48916f92012-09-16 19:57:18 +0000567 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
569 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 6, 32 );
570
571 /*
572 * 38 . 38 session id length
573 * 39 . 39+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +0000574 * 40+n . 41+n ciphersuitelist length
575 * 42+n . .. ciphersuitelist
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000576 * .. . .. compression methods length
577 * .. . .. compression methods
578 * .. . .. extensions length
579 * .. . .. extensions
Paul Bakker5121ce52009-01-03 21:22:43 +0000580 */
Paul Bakker48916f92012-09-16 19:57:18 +0000581 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000582
Paul Bakker0a597072012-09-25 21:55:46 +0000583 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE || n < 16 || n > 32 ||
584 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200585 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000586 n = 0;
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200587 }
588
Paul Bakkera503a632013-08-14 13:48:06 +0200589#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200590 /*
591 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
592 * generate and include a Session ID in the TLS ClientHello."
593 */
594 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
595 ssl->session_negotiate->ticket != NULL &&
596 ssl->session_negotiate->ticket_len != 0 )
597 {
598 ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
599
600 if( ret != 0 )
601 return( ret );
602
603 ssl->session_negotiate->length = n = 32;
604 }
Paul Bakkera503a632013-08-14 13:48:06 +0200605#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000606
607 *p++ = (unsigned char) n;
608
609 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000610 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000611
612 SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
613 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
614
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200615 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Paul Bakker2fbefde2013-06-29 16:01:15 +0200616 n = 0;
617 q = p;
618
619 // Skip writing ciphersuite length for now
620 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000621
Paul Bakker48916f92012-09-16 19:57:18 +0000622 /*
623 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
624 */
625 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
626 {
627 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
628 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
Paul Bakker2fbefde2013-06-29 16:01:15 +0200629 n++;
Paul Bakker48916f92012-09-16 19:57:18 +0000630 }
631
Paul Bakker2fbefde2013-06-29 16:01:15 +0200632 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000633 {
Paul Bakker2fbefde2013-06-29 16:01:15 +0200634 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
635
636 if( ciphersuite_info == NULL )
637 continue;
638
639 if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
640 ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
641 continue;
642
Paul Bakkere3166ce2011-01-27 17:40:50 +0000643 SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200644 ciphersuites[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000645
Paul Bakker2fbefde2013-06-29 16:01:15 +0200646 n++;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200647 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
648 *p++ = (unsigned char)( ciphersuites[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000649 }
650
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200651 /* Some versions of OpenSSL don't handle it correctly if not at end */
652#if defined(POLARSSL_SSL_FALLBACK_SCSV)
653 if( ssl->fallback == SSL_IS_FALLBACK )
654 {
655 SSL_DEBUG_MSG( 3, ( "adding FALLBACK_SCSV" ) );
656 *p++ = (unsigned char)( SSL_FALLBACK_SCSV >> 8 );
657 *p++ = (unsigned char)( SSL_FALLBACK_SCSV );
658 n++;
659 }
660#endif
661
Paul Bakker2fbefde2013-06-29 16:01:15 +0200662 *q++ = (unsigned char)( n >> 7 );
663 *q++ = (unsigned char)( n << 1 );
664
665 SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
666
667
Paul Bakker2770fbd2012-07-03 13:30:23 +0000668#if defined(POLARSSL_ZLIB_SUPPORT)
669 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
670 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000671 SSL_COMPRESS_DEFLATE, SSL_COMPRESS_NULL ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000672
673 *p++ = 2;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000674 *p++ = SSL_COMPRESS_DEFLATE;
Paul Bakker48916f92012-09-16 19:57:18 +0000675 *p++ = SSL_COMPRESS_NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000676#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000677 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000678 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d", SSL_COMPRESS_NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000679
680 *p++ = 1;
681 *p++ = SSL_COMPRESS_NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200682#endif /* POLARSSL_ZLIB_SUPPORT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000683
Paul Bakkerd3edc862013-03-20 16:07:17 +0100684 // First write extensions, then the total length
685 //
Paul Bakker0be444a2013-08-27 21:55:01 +0200686#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100687 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
688 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +0200689#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000690
Paul Bakkerd3edc862013-03-20 16:07:17 +0100691 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
692 ext_len += olen;
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000693
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200694#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100695 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
696 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200697#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000698
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200699#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100700 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
701 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100702
Paul Bakkerd3edc862013-03-20 16:07:17 +0100703 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
704 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100705#endif
706
Paul Bakker05decb22013-08-15 13:33:48 +0200707#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200708 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
709 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +0200710#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200711
Paul Bakker1f2bc622013-08-15 13:45:55 +0200712#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200713 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
714 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200715#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200716
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100717#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
718 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
719 ext_len += olen;
720#endif
721
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200722#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
723 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
724 ext_len += olen;
725#endif
726
Paul Bakkera503a632013-08-14 13:48:06 +0200727#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200728 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
729 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +0200730#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200731
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200732#if defined(POLARSSL_SSL_ALPN)
733 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
734 ext_len += olen;
735#endif
736
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000737 SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
738 ext_len ) );
739
Paul Bakkera7036632014-04-30 10:15:38 +0200740 if( ext_len > 0 )
741 {
742 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
743 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
744 p += ext_len;
745 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100746
Paul Bakker5121ce52009-01-03 21:22:43 +0000747 ssl->out_msglen = p - buf;
748 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
749 ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
750
751 ssl->state++;
752
753 if( ( ret = ssl_write_record( ssl ) ) != 0 )
754 {
755 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
756 return( ret );
757 }
758
759 SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
760
761 return( 0 );
762}
763
Paul Bakker48916f92012-09-16 19:57:18 +0000764static int ssl_parse_renegotiation_info( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200765 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000766 size_t len )
767{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000768 int ret;
769
Paul Bakker48916f92012-09-16 19:57:18 +0000770 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
771 {
772 if( len != 1 || buf[0] != 0x0 )
773 {
774 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000775
776 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
777 return( ret );
778
Paul Bakker48916f92012-09-16 19:57:18 +0000779 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
780 }
781
782 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
783 }
784 else
785 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100786 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000787 if( len != 1 + ssl->verify_data_len * 2 ||
788 buf[0] != ssl->verify_data_len * 2 ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100789 safer_memcmp( buf + 1,
790 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
791 safer_memcmp( buf + 1 + ssl->verify_data_len,
792 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000793 {
794 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000795
796 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
797 return( ret );
798
Paul Bakker48916f92012-09-16 19:57:18 +0000799 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
800 }
801 }
802
803 return( 0 );
804}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200805
Paul Bakker05decb22013-08-15 13:33:48 +0200806#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200807static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200808 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200809 size_t len )
810{
811 /*
812 * server should use the extension only if we did,
813 * and if so the server's value should match ours (and len is always 1)
814 */
815 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
816 len != 1 ||
817 buf[0] != ssl->mfl_code )
818 {
819 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
820 }
821
822 return( 0 );
823}
Paul Bakker05decb22013-08-15 13:33:48 +0200824#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000825
Paul Bakker1f2bc622013-08-15 13:45:55 +0200826#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200827static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
828 const unsigned char *buf,
829 size_t len )
830{
831 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
832 len != 0 )
833 {
834 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
835 }
836
837 ((void) buf);
838
839 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
840
841 return( 0 );
842}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200843#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200844
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100845#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
846static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
847 const unsigned char *buf,
848 size_t len )
849{
850 if( ssl->encrypt_then_mac == SSL_ETM_DISABLED ||
851 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
852 len != 0 )
853 {
854 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
855 }
856
857 ((void) buf);
858
859 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
860
861 return( 0 );
862}
863#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
864
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200865#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
866static int ssl_parse_extended_ms_ext( ssl_context *ssl,
867 const unsigned char *buf,
868 size_t len )
869{
870 if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200871 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200872 len != 0 )
873 {
874 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
875 }
876
877 ((void) buf);
878
879 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
880
881 return( 0 );
882}
883#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
884
Paul Bakkera503a632013-08-14 13:48:06 +0200885#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200886static int ssl_parse_session_ticket_ext( ssl_context *ssl,
887 const unsigned char *buf,
888 size_t len )
889{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200890 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ||
891 len != 0 )
892 {
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200893 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200894 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200895
896 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200897
898 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200899
900 return( 0 );
901}
Paul Bakkera503a632013-08-14 13:48:06 +0200902#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200903
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200904#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200905static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
906 const unsigned char *buf,
907 size_t len )
908{
909 size_t list_size;
910 const unsigned char *p;
911
912 list_size = buf[0];
913 if( list_size + 1 != len )
914 {
915 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
916 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
917 }
918
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200919 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200920 while( list_size > 0 )
921 {
922 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
923 p[0] == POLARSSL_ECP_PF_COMPRESSED )
924 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200925 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200926 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
927 return( 0 );
928 }
929
930 list_size--;
931 p++;
932 }
933
Manuel Pégourié-Gonnard5c1f0322014-06-23 14:24:43 +0200934 SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
935 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200936}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200937#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200938
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200939#if defined(POLARSSL_SSL_ALPN)
940static int ssl_parse_alpn_ext( ssl_context *ssl,
941 const unsigned char *buf, size_t len )
942{
943 size_t list_len, name_len;
944 const char **p;
945
946 /* If we didn't send it, the server shouldn't send it */
947 if( ssl->alpn_list == NULL )
948 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
949
950 /*
951 * opaque ProtocolName<1..2^8-1>;
952 *
953 * struct {
954 * ProtocolName protocol_name_list<2..2^16-1>
955 * } ProtocolNameList;
956 *
957 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
958 */
959
960 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
961 if( len < 4 )
962 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
963
964 list_len = ( buf[0] << 8 ) | buf[1];
965 if( list_len != len - 2 )
966 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
967
968 name_len = buf[2];
969 if( name_len != list_len - 1 )
970 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
971
972 /* Check that the server chosen protocol was in our list and save it */
973 for( p = ssl->alpn_list; *p != NULL; p++ )
974 {
975 if( name_len == strlen( *p ) &&
976 memcmp( buf + 3, *p, name_len ) == 0 )
977 {
978 ssl->alpn_chosen = *p;
979 return( 0 );
980 }
981 }
982
983 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
984}
985#endif /* POLARSSL_SSL_ALPN */
986
Paul Bakker5121ce52009-01-03 21:22:43 +0000987static int ssl_parse_server_hello( ssl_context *ssl )
988{
Paul Bakker2770fbd2012-07-03 13:30:23 +0000989 int ret, i, comp;
Paul Bakker23986e52011-04-24 08:57:21 +0000990 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +0200991 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +0000992 unsigned char *buf, *ext;
993 int renegotiation_info_seen = 0;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000994 int handshake_failure = 0;
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +0200995#if defined(POLARSSL_DEBUG_C)
996 uint32_t t;
997#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000998
999 SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
1000
1001 /*
1002 * 0 . 0 handshake type
1003 * 1 . 3 handshake length
1004 * 4 . 5 protocol version
1005 * 6 . 9 UNIX time()
1006 * 10 . 37 random bytes
1007 */
1008 buf = ssl->in_msg;
1009
1010 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1011 {
1012 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1013 return( ret );
1014 }
1015
1016 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1017 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001018 if( ssl->renegotiation == SSL_RENEGOTIATION )
1019 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001020 ssl->renego_records_seen++;
1021
1022 if( ssl->renego_max_records >= 0 &&
1023 ssl->renego_records_seen > ssl->renego_max_records )
1024 {
1025 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
1026 "but not honored by server" ) );
1027 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1028 }
1029
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001030 SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
1031 return( POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
1032 }
1033
Paul Bakker5121ce52009-01-03 21:22:43 +00001034 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001035 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001036 }
1037
1038 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1039 buf[4], buf[5] ) );
1040
1041 if( ssl->in_hslen < 42 ||
1042 buf[0] != SSL_HS_SERVER_HELLO ||
1043 buf[4] != SSL_MAJOR_VERSION_3 )
1044 {
1045 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001046 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001047 }
1048
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001049 if( buf[5] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00001050 {
1051 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001052 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001053 }
1054
1055 ssl->minor_ver = buf[5];
1056
Paul Bakker1d29fb52012-09-28 13:28:45 +00001057 if( ssl->minor_ver < ssl->min_minor_ver )
1058 {
1059 SSL_DEBUG_MSG( 1, ( "server only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001060 " [%d:%d] < [%d:%d]", ssl->major_ver,
1061 ssl->minor_ver, buf[4], buf[5] ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001062
1063 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1064 SSL_ALERT_MSG_PROTOCOL_VERSION );
1065
1066 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1067 }
1068
Paul Bakker1504af52012-02-11 16:17:43 +00001069#if defined(POLARSSL_DEBUG_C)
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001070 t = ( (uint32_t) buf[6] << 24 )
1071 | ( (uint32_t) buf[7] << 16 )
1072 | ( (uint32_t) buf[8] << 8 )
1073 | ( (uint32_t) buf[9] );
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001074 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakker87e5cda2012-01-14 18:14:15 +00001075#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001076
Paul Bakker48916f92012-09-16 19:57:18 +00001077 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001078
1079 n = buf[38];
1080
Paul Bakker5121ce52009-01-03 21:22:43 +00001081 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1082
Paul Bakker48916f92012-09-16 19:57:18 +00001083 if( n > 32 )
1084 {
1085 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1086 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1087 }
1088
Paul Bakker5121ce52009-01-03 21:22:43 +00001089 /*
1090 * 38 . 38 session id length
1091 * 39 . 38+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +00001092 * 39+n . 40+n chosen ciphersuite
Paul Bakker5121ce52009-01-03 21:22:43 +00001093 * 41+n . 41+n chosen compression alg.
1094 * 42+n . 43+n extensions length
1095 * 44+n . 44+n+m extensions
1096 */
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001097 if( ssl->in_hslen > 43 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001098 {
1099 ext_len = ( ( buf[42 + n] << 8 )
Paul Bakker48916f92012-09-16 19:57:18 +00001100 | ( buf[43 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001101
Paul Bakker48916f92012-09-16 19:57:18 +00001102 if( ( ext_len > 0 && ext_len < 4 ) ||
1103 ssl->in_hslen != 44 + n + ext_len )
1104 {
1105 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1106 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1107 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001108 }
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001109 else if( ssl->in_hslen == 42 + n )
1110 {
1111 ext_len = 0;
1112 }
1113 else
1114 {
1115 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1116 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1117 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001118
1119 i = ( buf[39 + n] << 8 ) | buf[40 + n];
Paul Bakker2770fbd2012-07-03 13:30:23 +00001120 comp = buf[41 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001121
Paul Bakker380da532012-04-18 16:10:25 +00001122 /*
1123 * Initialize update checksum functions
1124 */
Paul Bakker68884e32013-01-07 18:20:04 +01001125 ssl->transform_negotiate->ciphersuite_info = ssl_ciphersuite_from_id( i );
1126
1127 if( ssl->transform_negotiate->ciphersuite_info == NULL )
1128 {
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001129 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001130 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1131 }
Paul Bakker380da532012-04-18 16:10:25 +00001132
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001133 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1134
Paul Bakker5121ce52009-01-03 21:22:43 +00001135 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1136 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1137
1138 /*
1139 * Check if the session can be resumed
1140 */
Paul Bakker0a597072012-09-25 21:55:46 +00001141 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
1142 ssl->handshake->resume == 0 || n == 0 ||
Paul Bakker48916f92012-09-16 19:57:18 +00001143 ssl->session_negotiate->ciphersuite != i ||
1144 ssl->session_negotiate->compression != comp ||
1145 ssl->session_negotiate->length != n ||
1146 memcmp( ssl->session_negotiate->id, buf + 39, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001147 {
1148 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001149 ssl->handshake->resume = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001150#if defined(POLARSSL_HAVE_TIME)
Paul Bakker48916f92012-09-16 19:57:18 +00001151 ssl->session_negotiate->start = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001152#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001153 ssl->session_negotiate->ciphersuite = i;
1154 ssl->session_negotiate->compression = comp;
1155 ssl->session_negotiate->length = n;
1156 memcpy( ssl->session_negotiate->id, buf + 39, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001157 }
1158 else
1159 {
1160 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001161
1162 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1163 {
1164 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1165 return( ret );
1166 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001167 }
1168
1169 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001170 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001171
Paul Bakkere3166ce2011-01-27 17:40:50 +00001172 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001173 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[41 + n] ) );
1174
1175 i = 0;
1176 while( 1 )
1177 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001178 if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001179 {
1180 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001181 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001182 }
1183
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001184 if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
1185 ssl->session_negotiate->ciphersuite )
1186 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001187 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001188 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001189 }
1190
Paul Bakker2770fbd2012-07-03 13:30:23 +00001191 if( comp != SSL_COMPRESS_NULL
1192#if defined(POLARSSL_ZLIB_SUPPORT)
1193 && comp != SSL_COMPRESS_DEFLATE
1194#endif
1195 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001196 {
1197 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001198 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001199 }
Paul Bakker48916f92012-09-16 19:57:18 +00001200 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001201
Paul Bakker48916f92012-09-16 19:57:18 +00001202 ext = buf + 44 + n;
1203
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001204 SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
1205
Paul Bakker48916f92012-09-16 19:57:18 +00001206 while( ext_len )
1207 {
1208 unsigned int ext_id = ( ( ext[0] << 8 )
1209 | ( ext[1] ) );
1210 unsigned int ext_size = ( ( ext[2] << 8 )
1211 | ( ext[3] ) );
1212
1213 if( ext_size + 4 > ext_len )
1214 {
1215 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1216 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1217 }
1218
1219 switch( ext_id )
1220 {
1221 case TLS_EXT_RENEGOTIATION_INFO:
1222 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1223 renegotiation_info_seen = 1;
1224
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001225 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1226 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001227 return( ret );
1228
1229 break;
1230
Paul Bakker05decb22013-08-15 13:33:48 +02001231#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001232 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1233 SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
1234
1235 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1236 ext + 4, ext_size ) ) != 0 )
1237 {
1238 return( ret );
1239 }
1240
1241 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001242#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001243
Paul Bakker1f2bc622013-08-15 13:45:55 +02001244#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001245 case TLS_EXT_TRUNCATED_HMAC:
1246 SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
1247
1248 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1249 ext + 4, ext_size ) ) != 0 )
1250 {
1251 return( ret );
1252 }
1253
1254 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001255#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001256
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001257#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1258 case TLS_EXT_ENCRYPT_THEN_MAC:
1259 SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
1260
1261 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
1262 ext + 4, ext_size ) ) != 0 )
1263 {
1264 return( ret );
1265 }
1266
1267 break;
1268#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1269
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001270#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1271 case TLS_EXT_EXTENDED_MASTER_SECRET:
1272 SSL_DEBUG_MSG( 3, ( "found extended_master_secret extension" ) );
1273
1274 if( ( ret = ssl_parse_extended_ms_ext( ssl,
1275 ext + 4, ext_size ) ) != 0 )
1276 {
1277 return( ret );
1278 }
1279
1280 break;
1281#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1282
Paul Bakkera503a632013-08-14 13:48:06 +02001283#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001284 case TLS_EXT_SESSION_TICKET:
1285 SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
1286
1287 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1288 ext + 4, ext_size ) ) != 0 )
1289 {
1290 return( ret );
1291 }
1292
1293 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001294#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001295
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001296#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001297 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1298 SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
1299
1300 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1301 ext + 4, ext_size ) ) != 0 )
1302 {
1303 return( ret );
1304 }
1305
1306 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001307#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001308
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001309#if defined(POLARSSL_SSL_ALPN)
1310 case TLS_EXT_ALPN:
1311 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1312
1313 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1314 return( ret );
1315
1316 break;
1317#endif /* POLARSSL_SSL_ALPN */
1318
Paul Bakker48916f92012-09-16 19:57:18 +00001319 default:
1320 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1321 ext_id ) );
1322 }
1323
1324 ext_len -= 4 + ext_size;
1325 ext += 4 + ext_size;
1326
1327 if( ext_len > 0 && ext_len < 4 )
1328 {
1329 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1330 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1331 }
1332 }
1333
1334 /*
1335 * Renegotiation security checks
1336 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001337 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1338 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001339 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001340 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1341 handshake_failure = 1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001342 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001343 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1344 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1345 renegotiation_info_seen == 0 )
1346 {
1347 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1348 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001349 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001350 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1351 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1352 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001353 {
1354 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001355 handshake_failure = 1;
1356 }
1357 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1358 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1359 renegotiation_info_seen == 1 )
1360 {
1361 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1362 handshake_failure = 1;
1363 }
1364
1365 if( handshake_failure == 1 )
1366 {
1367 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1368 return( ret );
1369
Paul Bakker48916f92012-09-16 19:57:18 +00001370 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1371 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001372
1373 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1374
1375 return( 0 );
1376}
1377
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001378#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1379 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001380static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
1381 unsigned char *end )
1382{
1383 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1384
Paul Bakker29e1f122013-04-16 13:07:56 +02001385 /*
1386 * Ephemeral DH parameters:
1387 *
1388 * struct {
1389 * opaque dh_p<1..2^16-1>;
1390 * opaque dh_g<1..2^16-1>;
1391 * opaque dh_Ys<1..2^16-1>;
1392 * } ServerDHParams;
1393 */
1394 if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1395 {
1396 SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
1397 return( ret );
1398 }
1399
1400 if( ssl->handshake->dhm_ctx.len < 64 ||
1401 ssl->handshake->dhm_ctx.len > 512 )
1402 {
1403 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
1404 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1405 }
1406
1407 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1408 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1409 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001410
1411 return( ret );
1412}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001413#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1414 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001415
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001416#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001417 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001418 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1419 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1420 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1421static int ssl_check_server_ecdh_params( const ssl_context *ssl )
1422{
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001423 const ecp_curve_info *curve_info;
1424
1425 curve_info = ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
1426 if( curve_info == NULL )
1427 {
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001428 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1429 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001430 }
1431
1432 SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001433
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001434#if defined(POLARSSL_SSL_ECP_SET_CURVES)
1435 if( ! ssl_curve_is_acceptable( ssl, ssl->handshake->ecdh_ctx.grp.id ) )
1436#else
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001437 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1438 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001439#endif
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001440 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001441
1442 SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
1443
1444 return( 0 );
1445}
Paul Bakker9af723c2014-05-01 13:03:14 +02001446#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1447 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1448 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1449 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1450 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001451
1452#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1453 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001454 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001455static int ssl_parse_server_ecdh_params( ssl_context *ssl,
1456 unsigned char **p,
1457 unsigned char *end )
1458{
1459 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1460
Paul Bakker29e1f122013-04-16 13:07:56 +02001461 /*
1462 * Ephemeral ECDH parameters:
1463 *
1464 * struct {
1465 * ECParameters curve_params;
1466 * ECPoint public;
1467 * } ServerECDHParams;
1468 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001469 if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
1470 (const unsigned char **) p, end ) ) != 0 )
1471 {
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001472 SSL_DEBUG_RET( 1, ( "ecdh_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001473 return( ret );
1474 }
1475
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001476 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001477 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001478 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001479 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1480 }
1481
Paul Bakker29e1f122013-04-16 13:07:56 +02001482 return( ret );
1483}
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001484#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001485 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1486 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001487
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001488#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001489static int ssl_parse_server_psk_hint( ssl_context *ssl,
1490 unsigned char **p,
1491 unsigned char *end )
1492{
1493 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001494 size_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001495 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001496
1497 /*
1498 * PSK parameters:
1499 *
1500 * opaque psk_identity_hint<0..2^16-1>;
1501 */
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001502 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001503 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001504
1505 if( (*p) + len > end )
1506 {
1507 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
1508 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1509 }
1510
1511 // TODO: Retrieve PSK identity hint and callback to app
1512 //
1513 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001514 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001515
1516 return( ret );
1517}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001518#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001519
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001520#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1521 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1522/*
1523 * Generate a pre-master secret and encrypt it with the server's RSA key
1524 */
1525static int ssl_write_encrypted_pms( ssl_context *ssl,
1526 size_t offset, size_t *olen,
1527 size_t pms_offset )
1528{
1529 int ret;
1530 size_t len_bytes = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 0 : 2;
1531 unsigned char *p = ssl->handshake->premaster + pms_offset;
1532
1533 /*
1534 * Generate (part of) the pre-master as
1535 * struct {
1536 * ProtocolVersion client_version;
1537 * opaque random[46];
1538 * } PreMasterSecret;
1539 */
1540 p[0] = (unsigned char) ssl->max_major_ver;
1541 p[1] = (unsigned char) ssl->max_minor_ver;
1542
1543 if( ( ret = ssl->f_rng( ssl->p_rng, p + 2, 46 ) ) != 0 )
1544 {
1545 SSL_DEBUG_RET( 1, "f_rng", ret );
1546 return( ret );
1547 }
1548
1549 ssl->handshake->pmslen = 48;
1550
1551 /*
1552 * Now write it out, encrypted
1553 */
1554 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1555 POLARSSL_PK_RSA ) )
1556 {
1557 SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1558 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1559 }
1560
1561 if( ( ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
1562 p, ssl->handshake->pmslen,
1563 ssl->out_msg + offset + len_bytes, olen,
1564 SSL_MAX_CONTENT_LEN - offset - len_bytes,
1565 ssl->f_rng, ssl->p_rng ) ) != 0 )
1566 {
1567 SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
1568 return( ret );
1569 }
1570
1571#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1572 defined(POLARSSL_SSL_PROTO_TLS1_2)
1573 if( len_bytes == 2 )
1574 {
1575 ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
1576 ssl->out_msg[offset+1] = (unsigned char)( *olen );
1577 *olen += 2;
1578 }
1579#endif
1580
1581 return( 0 );
1582}
1583#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
1584 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001585
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001586#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001587#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001588 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1589 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001590static int ssl_parse_signature_algorithm( ssl_context *ssl,
1591 unsigned char **p,
1592 unsigned char *end,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001593 md_type_t *md_alg,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001594 pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02001595{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001596 ((void) ssl);
Paul Bakker29e1f122013-04-16 13:07:56 +02001597 *md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001598 *pk_alg = POLARSSL_PK_NONE;
1599
1600 /* Only in TLS 1.2 */
1601 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1602 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001603 return( 0 );
1604 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001605
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001606 if( (*p) + 2 > end )
Paul Bakker29e1f122013-04-16 13:07:56 +02001607 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1608
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001609 /*
1610 * Get hash algorithm
1611 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001612 if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001613 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001614 SSL_DEBUG_MSG( 2, ( "Server used unsupported "
1615 "HashAlgorithm %d", *(p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001616 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1617 }
1618
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001619 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001620 * Get signature algorithm
1621 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001622 if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001623 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001624 SSL_DEBUG_MSG( 2, ( "server used unsupported "
1625 "SignatureAlgorithm %d", (*p)[1] ) );
1626 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001627 }
1628
1629 SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
1630 SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
1631 *p += 2;
1632
1633 return( 0 );
1634}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001635#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001636 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1637 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001638#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001639
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001640
1641#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1642 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1643static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
1644{
1645 int ret;
1646 const ecp_keypair *peer_key;
1647
1648 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1649 POLARSSL_PK_ECKEY ) )
1650 {
1651 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
1652 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1653 }
1654
1655 peer_key = pk_ec( ssl->session_negotiate->peer_cert->pk );
1656
1657 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
1658 POLARSSL_ECDH_THEIRS ) ) != 0 )
1659 {
1660 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
1661 return( ret );
1662 }
1663
1664 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1665 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001666 SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001667 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
1668 }
1669
1670 return( ret );
1671}
1672#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1673 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1674
Paul Bakker41c83d32013-03-20 14:39:14 +01001675static int ssl_parse_server_key_exchange( ssl_context *ssl )
1676{
Paul Bakker23986e52011-04-24 08:57:21 +00001677 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001678 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001679 unsigned char *p, *end;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001680#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001681 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1682 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001683 size_t sig_len, params_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001684 unsigned char hash[64];
Paul Bakkerc70b9822013-04-07 22:00:46 +02001685 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001686 size_t hashlen;
1687 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001688#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001689
1690 SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
1691
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001692#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001693 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001694 {
1695 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1696 ssl->state++;
1697 return( 0 );
1698 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001699 ((void) p);
1700 ((void) end);
1701#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001702
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001703#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1704 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1705 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
1706 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
1707 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001708 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
1709 {
1710 SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
1711 return( ret );
1712 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001713
1714 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1715 ssl->state++;
1716 return( 0 );
1717 }
1718 ((void) p);
1719 ((void) end);
Paul Bakker9af723c2014-05-01 13:03:14 +02001720#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1721 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001722
Paul Bakker5121ce52009-01-03 21:22:43 +00001723 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1724 {
1725 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1726 return( ret );
1727 }
1728
1729 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1730 {
1731 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001732 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001733 }
1734
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001735 /*
1736 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
1737 * doesn't use a psk_identity_hint
1738 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001739 if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
1740 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001741 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1742 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02001743 {
1744 ssl->record_read = 1;
1745 goto exit;
1746 }
1747
1748 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1749 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001750 }
1751
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001752 p = ssl->in_msg + 4;
1753 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001754 SSL_DEBUG_BUF( 3, "server key exchange", p, ssl->in_hslen - 4 );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001755
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001756#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1757 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1758 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1759 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1760 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1761 {
1762 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1763 {
1764 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1765 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1766 }
1767 } /* FALLTROUGH */
1768#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
1769
1770#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
1771 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1772 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1773 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1774 ; /* nothing more to do */
1775 else
1776#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
1777 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
1778#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1779 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1780 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1781 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001782 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001783 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001784 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001785 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001786 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1787 }
1788 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001789 else
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001790#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1791 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001792#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001793 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001794 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1795 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001796 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001797 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001798 {
1799 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
1800 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001801 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1802 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1803 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001804 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001805 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001806#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001807 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001808 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001809 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001810 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001811 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001812 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001813
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001814#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001815 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1816 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001817 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001818 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1819 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001820 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001821 params_len = p - ( ssl->in_msg + 4 );
1822
Paul Bakker29e1f122013-04-16 13:07:56 +02001823 /*
1824 * Handle the digitally-signed structure
1825 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001826#if defined(POLARSSL_SSL_PROTO_TLS1_2)
1827 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001828 {
Paul Bakker9659dae2013-08-28 16:21:34 +02001829 if( ssl_parse_signature_algorithm( ssl, &p, end,
1830 &md_alg, &pk_alg ) != 0 )
1831 {
1832 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1833 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1834 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001835
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001836 if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001837 {
1838 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1839 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1840 }
1841 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001842 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001843#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001844#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1845 defined(POLARSSL_SSL_PROTO_TLS1_1)
1846 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001847 {
1848 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Paul Bakker1ef83d62012-04-11 12:09:53 +00001849
Paul Bakker9659dae2013-08-28 16:21:34 +02001850 /* Default hash for ECDSA is SHA-1 */
1851 if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
1852 md_alg = POLARSSL_MD_SHA1;
1853 }
1854 else
1855#endif
1856 {
1857 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001858 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker9659dae2013-08-28 16:21:34 +02001859 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001860
1861 /*
1862 * Read signature
1863 */
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001864 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00001865 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001866
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001867 if( end != p + sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01001868 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001869 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001870 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1871 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001872
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001873 SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02001874
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001875 /*
1876 * Compute the hash that has been signed
1877 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001878#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1879 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001880 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001881 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001882 md5_context md5;
1883 sha1_context sha1;
1884
Paul Bakker5b4af392014-06-26 12:09:34 +02001885 md5_init( &md5 );
1886 sha1_init( &sha1 );
1887
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001888 hashlen = 36;
1889
Paul Bakker29e1f122013-04-16 13:07:56 +02001890 /*
1891 * digitally-signed struct {
1892 * opaque md5_hash[16];
1893 * opaque sha_hash[20];
1894 * };
1895 *
1896 * md5_hash
1897 * MD5(ClientHello.random + ServerHello.random
1898 * + ServerParams);
1899 * sha_hash
1900 * SHA(ClientHello.random + ServerHello.random
1901 * + ServerParams);
1902 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001903 md5_starts( &md5 );
1904 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001905 md5_update( &md5, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001906 md5_finish( &md5, hash );
1907
1908 sha1_starts( &sha1 );
1909 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001910 sha1_update( &sha1, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001911 sha1_finish( &sha1, hash + 16 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001912
1913 md5_free( &md5 );
1914 sha1_free( &sha1 );
Paul Bakker29e1f122013-04-16 13:07:56 +02001915 }
1916 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001917#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
1918 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001919#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1920 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02001921 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001922 {
1923 md_context_t ctx;
1924
Paul Bakker84bbeb52014-07-01 14:53:22 +02001925 md_init( &ctx );
1926
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001927 /* Info from md_alg will be used instead */
1928 hashlen = 0;
Paul Bakker29e1f122013-04-16 13:07:56 +02001929
1930 /*
1931 * digitally-signed struct {
1932 * opaque client_random[32];
1933 * opaque server_random[32];
1934 * ServerDHParams params;
1935 * };
1936 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001937 if( ( ret = md_init_ctx( &ctx,
1938 md_info_from_type( md_alg ) ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001939 {
1940 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
1941 return( ret );
1942 }
1943
1944 md_starts( &ctx );
1945 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001946 md_update( &ctx, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001947 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02001948 md_free( &ctx );
Paul Bakker29e1f122013-04-16 13:07:56 +02001949 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001950 else
Paul Bakker9659dae2013-08-28 16:21:34 +02001951#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1952 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001953 {
Paul Bakker577e0062013-08-28 11:57:20 +02001954 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001955 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001956 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001957
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02001958 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
1959 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker29e1f122013-04-16 13:07:56 +02001960
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001961 /*
1962 * Verify signature
1963 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02001964 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001965 {
1966 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1967 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1968 }
1969
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001970 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
1971 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001972 {
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001973 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001974 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001975 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001976 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001977#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001978 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1979 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001980
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001981exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00001982 ssl->state++;
1983
1984 SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
1985
1986 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001987}
1988
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001989#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1990 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
1991 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1992 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1993static int ssl_parse_certificate_request( ssl_context *ssl )
1994{
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001995 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
1996
1997 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1998
1999 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2000 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2001 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2002 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2003 {
2004 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2005 ssl->state++;
2006 return( 0 );
2007 }
2008
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002009 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2010 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002011}
2012#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002013static int ssl_parse_certificate_request( ssl_context *ssl )
2014{
2015 int ret;
Paul Bakker926af752012-11-23 13:38:07 +01002016 unsigned char *buf, *p;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002017 size_t n = 0, m = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002018 size_t cert_type_len = 0, dn_len = 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002019 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002020
2021 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2022
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002023 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2024 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2025 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2026 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2027 {
2028 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2029 ssl->state++;
2030 return( 0 );
2031 }
2032
Paul Bakker5121ce52009-01-03 21:22:43 +00002033 /*
2034 * 0 . 0 handshake type
2035 * 1 . 3 handshake length
Paul Bakker926af752012-11-23 13:38:07 +01002036 * 4 . 4 cert type count
2037 * 5 .. m-1 cert types
2038 * m .. m+1 sig alg length (TLS 1.2 only)
2039 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002040 * n .. n+1 length of all DNs
2041 * n+2 .. n+3 length of DN 1
2042 * n+4 .. ... Distinguished Name #1
2043 * ... .. ... length of DN 2, etc.
2044 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002045 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002046 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002047 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2048 {
2049 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2050 return( ret );
2051 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002052
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002053 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2054 {
2055 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2056 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2057 }
2058
2059 ssl->record_read = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002060 }
2061
2062 ssl->client_auth = 0;
2063 ssl->state++;
2064
2065 if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST )
2066 ssl->client_auth++;
2067
2068 SSL_DEBUG_MSG( 3, ( "got %s certificate request",
2069 ssl->client_auth ? "a" : "no" ) );
2070
Paul Bakker926af752012-11-23 13:38:07 +01002071 if( ssl->client_auth == 0 )
2072 goto exit;
2073
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002074 ssl->record_read = 0;
2075
Paul Bakker926af752012-11-23 13:38:07 +01002076 // TODO: handshake_failure alert for an anonymous server to request
2077 // client authentication
2078
2079 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002080
Paul Bakker926af752012-11-23 13:38:07 +01002081 // Retrieve cert types
2082 //
2083 cert_type_len = buf[4];
2084 n = cert_type_len;
2085
2086 if( ssl->in_hslen < 6 + n )
2087 {
2088 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2089 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2090 }
2091
Paul Bakker73d44312013-05-22 13:56:26 +02002092 p = buf + 5;
Paul Bakker926af752012-11-23 13:38:07 +01002093 while( cert_type_len > 0 )
2094 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002095#if defined(POLARSSL_RSA_C)
2096 if( *p == SSL_CERT_TYPE_RSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002097 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker926af752012-11-23 13:38:07 +01002098 {
2099 ssl->handshake->cert_type = SSL_CERT_TYPE_RSA_SIGN;
2100 break;
2101 }
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002102 else
2103#endif
2104#if defined(POLARSSL_ECDSA_C)
2105 if( *p == SSL_CERT_TYPE_ECDSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002106 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002107 {
2108 ssl->handshake->cert_type = SSL_CERT_TYPE_ECDSA_SIGN;
2109 break;
2110 }
2111 else
2112#endif
2113 {
2114 ; /* Unsupported cert type, ignore */
2115 }
Paul Bakker926af752012-11-23 13:38:07 +01002116
2117 cert_type_len--;
2118 p++;
2119 }
2120
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002121#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002122 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2123 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002124 /* Ignored, see comments about hash in write_certificate_verify */
2125 // TODO: should check the signature part against our pk_key though
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002126 size_t sig_alg_len = ( ( buf[5 + n] << 8 )
2127 | ( buf[6 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002128
2129 p = buf + 7 + n;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002130 m += 2;
Paul Bakker926af752012-11-23 13:38:07 +01002131 n += sig_alg_len;
2132
2133 if( ssl->in_hslen < 6 + n )
2134 {
2135 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2136 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2137 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002138 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002139#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker926af752012-11-23 13:38:07 +01002140
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002141 /* Ignore certificate_authorities, we only have one cert anyway */
2142 // TODO: should not send cert if no CA matches
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002143 dn_len = ( ( buf[5 + m + n] << 8 )
2144 | ( buf[6 + m + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002145
2146 n += dn_len;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002147 if( ssl->in_hslen != 7 + m + n )
Paul Bakker926af752012-11-23 13:38:07 +01002148 {
2149 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2150 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2151 }
2152
2153exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002154 SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2155
2156 return( 0 );
2157}
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002158#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2159 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2160 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2161 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002162
2163static int ssl_parse_server_hello_done( ssl_context *ssl )
2164{
2165 int ret;
2166
2167 SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
2168
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002169 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002170 {
2171 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2172 {
2173 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2174 return( ret );
2175 }
2176
2177 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2178 {
2179 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002180 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002181 }
2182 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002183 ssl->record_read = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002184
2185 if( ssl->in_hslen != 4 ||
2186 ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
2187 {
2188 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002189 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002190 }
2191
2192 ssl->state++;
2193
2194 SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
2195
2196 return( 0 );
2197}
2198
2199static int ssl_write_client_key_exchange( ssl_context *ssl )
2200{
Paul Bakker23986e52011-04-24 08:57:21 +00002201 int ret;
2202 size_t i, n;
Paul Bakker41c83d32013-03-20 14:39:14 +01002203 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002204
2205 SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
2206
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002207#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002208 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002209 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002210 /*
2211 * DHM key exchange -- send G^X mod P
2212 */
Paul Bakker48916f92012-09-16 19:57:18 +00002213 n = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002214
2215 ssl->out_msg[4] = (unsigned char)( n >> 8 );
2216 ssl->out_msg[5] = (unsigned char)( n );
2217 i = 6;
2218
Paul Bakker29b64762012-09-25 09:36:44 +00002219 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002220 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker5121ce52009-01-03 21:22:43 +00002221 &ssl->out_msg[i], n,
2222 ssl->f_rng, ssl->p_rng );
2223 if( ret != 0 )
2224 {
2225 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2226 return( ret );
2227 }
2228
Paul Bakker48916f92012-09-16 19:57:18 +00002229 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2230 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002231
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002232 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002233
Paul Bakker48916f92012-09-16 19:57:18 +00002234 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2235 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002236 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002237 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002238 {
2239 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2240 return( ret );
2241 }
2242
Paul Bakker48916f92012-09-16 19:57:18 +00002243 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002244 }
2245 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002246#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002247#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002248 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2249 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2250 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002251 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002252 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2253 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2254 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002255 {
2256 /*
2257 * ECDH key exchange -- send client public value
2258 */
2259 i = 4;
2260
2261 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
2262 &n,
2263 &ssl->out_msg[i], 1000,
2264 ssl->f_rng, ssl->p_rng );
2265 if( ret != 0 )
2266 {
2267 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2268 return( ret );
2269 }
2270
2271 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2272
2273 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2274 &ssl->handshake->pmslen,
2275 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002276 POLARSSL_MPI_MAX_SIZE,
2277 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002278 {
2279 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2280 return( ret );
2281 }
2282
2283 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2284 }
2285 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002286#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002287 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2288 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2289 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002290#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002291 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002292 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002293 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2294 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002295 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002296 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002297 * opaque psk_identity<0..2^16-1>;
2298 */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002299 if( ssl->psk == NULL || ssl->psk_identity == NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002300 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2301
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002302 i = 4;
2303 n = ssl->psk_identity_len;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002304 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2305 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002306
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002307 memcpy( ssl->out_msg + i, ssl->psk_identity, ssl->psk_identity_len );
2308 i += ssl->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002309
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002310#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002311 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002312 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002313 n = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002314 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002315 else
2316#endif
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002317#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2318 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2319 {
2320 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2321 return( ret );
2322 }
2323 else
2324#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002325#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002326 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002327 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002328 /*
2329 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
2330 */
2331 n = ssl->handshake->dhm_ctx.len;
2332 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2333 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002334
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002335 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakker68881672013-10-15 13:24:01 +02002336 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002337 &ssl->out_msg[i], n,
2338 ssl->f_rng, ssl->p_rng );
2339 if( ret != 0 )
2340 {
2341 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2342 return( ret );
2343 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002344 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002345 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002346#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002347#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002348 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002349 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002350 /*
2351 * ClientECDiffieHellmanPublic public;
2352 */
2353 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
2354 &ssl->out_msg[i], SSL_MAX_CONTENT_LEN - i,
2355 ssl->f_rng, ssl->p_rng );
2356 if( ret != 0 )
2357 {
2358 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2359 return( ret );
2360 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002361
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002362 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2363 }
2364 else
2365#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2366 {
2367 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002368 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002369 }
2370
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002371 if( ( ret = ssl_psk_derive_premaster( ssl,
2372 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002373 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002374 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002375 return( ret );
2376 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002377 }
2378 else
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002379#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002380#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +02002381 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002382 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002383 i = 4;
2384 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002385 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002386 }
Paul Bakkered27a042013-04-18 22:46:23 +02002387 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002388#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002389 {
2390 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002391 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002392 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02002393 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002394
Paul Bakker5121ce52009-01-03 21:22:43 +00002395 ssl->out_msglen = i + n;
2396 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2397 ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE;
2398
2399 ssl->state++;
2400
2401 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2402 {
2403 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2404 return( ret );
2405 }
2406
2407 SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
2408
2409 return( 0 );
2410}
2411
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002412#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2413 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002414 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2415 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002416static int ssl_write_certificate_verify( ssl_context *ssl )
2417{
Paul Bakkered27a042013-04-18 22:46:23 +02002418 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002419 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002420
2421 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2422
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002423 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2424 {
2425 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2426 return( ret );
2427 }
2428
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002429 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002430 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002431 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002432 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002433 {
2434 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2435 ssl->state++;
2436 return( 0 );
2437 }
2438
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002439 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2440 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002441}
2442#else
2443static int ssl_write_certificate_verify( ssl_context *ssl )
2444{
2445 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2446 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2447 size_t n = 0, offset = 0;
2448 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002449 unsigned char *hash_start = hash;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002450 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002451 unsigned int hashlen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002452
2453 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2454
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002455 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2456 {
2457 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2458 return( ret );
2459 }
2460
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002461 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002462 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002463 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002464 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2465 {
2466 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2467 ssl->state++;
2468 return( 0 );
2469 }
2470
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002471 if( ssl->client_auth == 0 || ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002472 {
2473 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2474 ssl->state++;
2475 return( 0 );
2476 }
2477
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002478 if( ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002479 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00002480 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2481 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002482 }
2483
2484 /*
2485 * Make an RSA signature of the handshake digests
2486 */
Paul Bakker48916f92012-09-16 19:57:18 +00002487 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002488
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002489#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2490 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker926af752012-11-23 13:38:07 +01002491 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002492 {
Paul Bakker926af752012-11-23 13:38:07 +01002493 /*
2494 * digitally-signed struct {
2495 * opaque md5_hash[16];
2496 * opaque sha_hash[20];
2497 * };
2498 *
2499 * md5_hash
2500 * MD5(handshake_messages);
2501 *
2502 * sha_hash
2503 * SHA(handshake_messages);
2504 */
2505 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002506 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002507
2508 /*
2509 * For ECDSA, default hash is SHA-1 only
2510 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002511 if( pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002512 {
2513 hash_start += 16;
2514 hashlen -= 16;
2515 md_alg = POLARSSL_MD_SHA1;
2516 }
Paul Bakker926af752012-11-23 13:38:07 +01002517 }
2518 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002519#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2520 POLARSSL_SSL_PROTO_TLS1_1 */
2521#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2522 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002523 {
2524 /*
2525 * digitally-signed struct {
2526 * opaque handshake_messages[handshake_messages_length];
2527 * };
2528 *
2529 * Taking shortcut here. We assume that the server always allows the
2530 * PRF Hash function and has sent it in the allowed signature
2531 * algorithms list received in the Certificate Request message.
2532 *
2533 * Until we encounter a server that does not, we will take this
2534 * shortcut.
2535 *
2536 * Reason: Otherwise we should have running hashes for SHA512 and SHA224
2537 * in order to satisfy 'weird' needs from the server side.
2538 */
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002539 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2540 POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002541 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002542 md_alg = POLARSSL_MD_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002543 ssl->out_msg[4] = SSL_HASH_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002544 }
2545 else
2546 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002547 md_alg = POLARSSL_MD_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002548 ssl->out_msg[4] = SSL_HASH_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002549 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002550 ssl->out_msg[5] = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002551
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002552 /* Info from md_alg will be used instead */
2553 hashlen = 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002554 offset = 2;
2555 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002556 else
2557#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002558 {
2559 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002560 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002561 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002562
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002563 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002564 ssl->out_msg + 6 + offset, &n,
2565 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002566 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002567 SSL_DEBUG_RET( 1, "pk_sign", ret );
2568 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002569 }
Paul Bakker926af752012-11-23 13:38:07 +01002570
Paul Bakker1ef83d62012-04-11 12:09:53 +00002571 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
2572 ssl->out_msg[5 + offset] = (unsigned char)( n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002573
Paul Bakker1ef83d62012-04-11 12:09:53 +00002574 ssl->out_msglen = 6 + n + offset;
Paul Bakker5121ce52009-01-03 21:22:43 +00002575 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2576 ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY;
2577
2578 ssl->state++;
2579
2580 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2581 {
2582 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2583 return( ret );
2584 }
2585
2586 SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
2587
Paul Bakkered27a042013-04-18 22:46:23 +02002588 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002589}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002590#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2591 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2592 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002593
Paul Bakkera503a632013-08-14 13:48:06 +02002594#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002595static int ssl_parse_new_session_ticket( ssl_context *ssl )
2596{
2597 int ret;
2598 uint32_t lifetime;
2599 size_t ticket_len;
2600 unsigned char *ticket;
2601
2602 SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2603
2604 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2605 {
2606 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2607 return( ret );
2608 }
2609
2610 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2611 {
2612 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2613 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2614 }
2615
2616 /*
2617 * struct {
2618 * uint32 ticket_lifetime_hint;
2619 * opaque ticket<0..2^16-1>;
2620 * } NewSessionTicket;
2621 *
2622 * 0 . 0 handshake message type
2623 * 1 . 3 handshake message length
2624 * 4 . 7 ticket_lifetime_hint
2625 * 8 . 9 ticket_len (n)
2626 * 10 . 9+n ticket content
2627 */
2628 if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
2629 ssl->in_hslen < 10 )
2630 {
2631 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2632 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2633 }
2634
2635 lifetime = ( ssl->in_msg[4] << 24 ) | ( ssl->in_msg[5] << 16 ) |
2636 ( ssl->in_msg[6] << 8 ) | ( ssl->in_msg[7] );
2637
2638 ticket_len = ( ssl->in_msg[8] << 8 ) | ( ssl->in_msg[9] );
2639
2640 if( ticket_len + 10 != ssl->in_hslen )
2641 {
2642 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2643 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2644 }
2645
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002646 SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
2647
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002648 /* We're not waiting for a NewSessionTicket message any more */
2649 ssl->handshake->new_session_ticket = 0;
2650
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002651 /*
2652 * Zero-length ticket means the server changed his mind and doesn't want
2653 * to send a ticket after all, so just forget it
2654 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002655 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002656 return( 0 );
2657
Paul Bakker34617722014-06-13 17:20:13 +02002658 polarssl_zeroize( ssl->session_negotiate->ticket,
2659 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002660 polarssl_free( ssl->session_negotiate->ticket );
2661 ssl->session_negotiate->ticket = NULL;
2662 ssl->session_negotiate->ticket_len = 0;
2663
2664 if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
2665 {
2666 SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
2667 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2668 }
2669
2670 memcpy( ticket, ssl->in_msg + 10, ticket_len );
2671
2672 ssl->session_negotiate->ticket = ticket;
2673 ssl->session_negotiate->ticket_len = ticket_len;
2674 ssl->session_negotiate->ticket_lifetime = lifetime;
2675
2676 /*
2677 * RFC 5077 section 3.4:
2678 * "If the client receives a session ticket from the server, then it
2679 * discards any Session ID that was sent in the ServerHello."
2680 */
2681 SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
2682 ssl->session_negotiate->length = 0;
2683
2684 SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2685
2686 return( 0 );
2687}
Paul Bakkera503a632013-08-14 13:48:06 +02002688#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002689
Paul Bakker5121ce52009-01-03 21:22:43 +00002690/*
Paul Bakker1961b702013-01-25 14:49:24 +01002691 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002692 */
Paul Bakker1961b702013-01-25 14:49:24 +01002693int ssl_handshake_client_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002694{
2695 int ret = 0;
2696
Paul Bakker1961b702013-01-25 14:49:24 +01002697 if( ssl->state == SSL_HANDSHAKE_OVER )
2698 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002699
Paul Bakker1961b702013-01-25 14:49:24 +01002700 SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
2701
2702 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2703 return( ret );
2704
2705 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002706 {
Paul Bakker1961b702013-01-25 14:49:24 +01002707 case SSL_HELLO_REQUEST:
2708 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002709 break;
2710
Paul Bakker1961b702013-01-25 14:49:24 +01002711 /*
2712 * ==> ClientHello
2713 */
2714 case SSL_CLIENT_HELLO:
2715 ret = ssl_write_client_hello( ssl );
2716 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002717
Paul Bakker1961b702013-01-25 14:49:24 +01002718 /*
2719 * <== ServerHello
2720 * Certificate
2721 * ( ServerKeyExchange )
2722 * ( CertificateRequest )
2723 * ServerHelloDone
2724 */
2725 case SSL_SERVER_HELLO:
2726 ret = ssl_parse_server_hello( ssl );
2727 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002728
Paul Bakker1961b702013-01-25 14:49:24 +01002729 case SSL_SERVER_CERTIFICATE:
2730 ret = ssl_parse_certificate( ssl );
2731 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002732
Paul Bakker1961b702013-01-25 14:49:24 +01002733 case SSL_SERVER_KEY_EXCHANGE:
2734 ret = ssl_parse_server_key_exchange( ssl );
2735 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002736
Paul Bakker1961b702013-01-25 14:49:24 +01002737 case SSL_CERTIFICATE_REQUEST:
2738 ret = ssl_parse_certificate_request( ssl );
2739 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002740
Paul Bakker1961b702013-01-25 14:49:24 +01002741 case SSL_SERVER_HELLO_DONE:
2742 ret = ssl_parse_server_hello_done( ssl );
2743 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002744
Paul Bakker1961b702013-01-25 14:49:24 +01002745 /*
2746 * ==> ( Certificate/Alert )
2747 * ClientKeyExchange
2748 * ( CertificateVerify )
2749 * ChangeCipherSpec
2750 * Finished
2751 */
2752 case SSL_CLIENT_CERTIFICATE:
2753 ret = ssl_write_certificate( ssl );
2754 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002755
Paul Bakker1961b702013-01-25 14:49:24 +01002756 case SSL_CLIENT_KEY_EXCHANGE:
2757 ret = ssl_write_client_key_exchange( ssl );
2758 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002759
Paul Bakker1961b702013-01-25 14:49:24 +01002760 case SSL_CERTIFICATE_VERIFY:
2761 ret = ssl_write_certificate_verify( ssl );
2762 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002763
Paul Bakker1961b702013-01-25 14:49:24 +01002764 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2765 ret = ssl_write_change_cipher_spec( ssl );
2766 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002767
Paul Bakker1961b702013-01-25 14:49:24 +01002768 case SSL_CLIENT_FINISHED:
2769 ret = ssl_write_finished( ssl );
2770 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002771
Paul Bakker1961b702013-01-25 14:49:24 +01002772 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002773 * <== ( NewSessionTicket )
2774 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002775 * Finished
2776 */
2777 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002778#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002779 if( ssl->handshake->new_session_ticket != 0 )
2780 ret = ssl_parse_new_session_ticket( ssl );
2781 else
Paul Bakkera503a632013-08-14 13:48:06 +02002782#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002783 ret = ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002784 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002785
Paul Bakker1961b702013-01-25 14:49:24 +01002786 case SSL_SERVER_FINISHED:
2787 ret = ssl_parse_finished( ssl );
2788 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002789
Paul Bakker1961b702013-01-25 14:49:24 +01002790 case SSL_FLUSH_BUFFERS:
2791 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2792 ssl->state = SSL_HANDSHAKE_WRAPUP;
2793 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002794
Paul Bakker1961b702013-01-25 14:49:24 +01002795 case SSL_HANDSHAKE_WRAPUP:
2796 ssl_handshake_wrapup( ssl );
2797 break;
Paul Bakker48916f92012-09-16 19:57:18 +00002798
Paul Bakker1961b702013-01-25 14:49:24 +01002799 default:
2800 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2801 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2802 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002803
2804 return( ret );
2805}
Paul Bakker9af723c2014-05-01 13:03:14 +02002806#endif /* POLARSSL_SSL_CLI_C */