blob: 0db9e231125eed50e0747fb07c444b86b8308443 [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
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100145/*
146 * Only if we handle at least one key exchange that needs signatures.
147 */
148#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
149 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100150static void ssl_write_signature_algorithms_ext( ssl_context *ssl,
151 unsigned char *buf,
152 size_t *olen )
153{
154 unsigned char *p = buf;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100155 size_t sig_alg_len = 0;
Manuel Pégourié-Gonnard5bfd9682014-06-24 15:18:11 +0200156#if defined(POLARSSL_RSA_C) || defined(POLARSSL_ECDSA_C)
157 unsigned char *sig_alg_list = buf + 6;
158#endif
Paul Bakkerd3edc862013-03-20 16:07:17 +0100159
160 *olen = 0;
161
162 if( ssl->max_minor_ver != SSL_MINOR_VERSION_3 )
163 return;
164
165 SSL_DEBUG_MSG( 3, ( "client hello, adding signature_algorithms extension" ) );
166
167 /*
168 * Prepare signature_algorithms extension (TLS 1.2)
169 */
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200170#if defined(POLARSSL_RSA_C)
Paul Bakker9e36f042013-06-30 14:34:05 +0200171#if defined(POLARSSL_SHA512_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100172 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
173 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
174 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
175 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
176#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200177#if defined(POLARSSL_SHA256_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100178 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
179 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
180 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
181 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
182#endif
183#if defined(POLARSSL_SHA1_C)
184 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
185 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
186#endif
187#if defined(POLARSSL_MD5_C)
188 sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
189 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
190#endif
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200191#endif /* POLARSSL_RSA_C */
192#if defined(POLARSSL_ECDSA_C)
193#if defined(POLARSSL_SHA512_C)
194 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
195 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
196 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
197 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
198#endif
199#if defined(POLARSSL_SHA256_C)
200 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
201 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
202 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
203 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
204#endif
205#if defined(POLARSSL_SHA1_C)
206 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
207 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
208#endif
209#if defined(POLARSSL_MD5_C)
210 sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
211 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
212#endif
213#endif /* POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100214
215 /*
216 * enum {
217 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
218 * sha512(6), (255)
219 * } HashAlgorithm;
220 *
221 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
222 * SignatureAlgorithm;
223 *
224 * struct {
225 * HashAlgorithm hash;
226 * SignatureAlgorithm signature;
227 * } SignatureAndHashAlgorithm;
228 *
229 * SignatureAndHashAlgorithm
230 * supported_signature_algorithms<2..2^16-2>;
231 */
232 *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG >> 8 ) & 0xFF );
233 *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG ) & 0xFF );
234
235 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) >> 8 ) & 0xFF );
236 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) ) & 0xFF );
237
238 *p++ = (unsigned char)( ( sig_alg_len >> 8 ) & 0xFF );
239 *p++ = (unsigned char)( ( sig_alg_len ) & 0xFF );
240
Paul Bakkerd3edc862013-03-20 16:07:17 +0100241 *olen = 6 + sig_alg_len;
242}
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100243#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
244 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100245
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200246#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100247static void ssl_write_supported_elliptic_curves_ext( ssl_context *ssl,
248 unsigned char *buf,
249 size_t *olen )
250{
251 unsigned char *p = buf;
Manuel Pégourié-Gonnard8e205fc2014-01-23 17:27:10 +0100252 unsigned char *elliptic_curve_list = p + 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100253 size_t elliptic_curve_len = 0;
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100254 const ecp_curve_info *info;
255#if defined(POLARSSL_SSL_SET_CURVES)
256 const ecp_group_id *grp_id;
Paul Bakker0910f322014-02-06 13:41:18 +0100257#else
258 ((void) ssl);
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100259#endif
Paul Bakkerd3edc862013-03-20 16:07:17 +0100260
261 *olen = 0;
262
263 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_elliptic_curves extension" ) );
264
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100265#if defined(POLARSSL_SSL_SET_CURVES)
266 for( grp_id = ssl->curve_list; *grp_id != POLARSSL_ECP_DP_NONE; grp_id++ )
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200267 {
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100268 info = ecp_curve_info_from_grp_id( *grp_id );
269#else
270 for( info = ecp_curve_list(); info->grp_id != POLARSSL_ECP_DP_NONE; info++ )
271 {
272#endif
273
274 elliptic_curve_list[elliptic_curve_len++] = info->tls_id >> 8;
275 elliptic_curve_list[elliptic_curve_len++] = info->tls_id & 0xFF;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200276 }
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200277
278 if( elliptic_curve_len == 0 )
279 return;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100280
281 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES >> 8 ) & 0xFF );
282 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ) & 0xFF );
283
284 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF );
285 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) ) & 0xFF );
286
287 *p++ = (unsigned char)( ( ( elliptic_curve_len ) >> 8 ) & 0xFF );
288 *p++ = (unsigned char)( ( ( elliptic_curve_len ) ) & 0xFF );
289
Paul Bakkerd3edc862013-03-20 16:07:17 +0100290 *olen = 6 + elliptic_curve_len;
291}
292
293static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
294 unsigned char *buf,
295 size_t *olen )
296{
297 unsigned char *p = buf;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +0200298 ((void) ssl);
Paul Bakkerd3edc862013-03-20 16:07:17 +0100299
300 *olen = 0;
301
302 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_point_formats extension" ) );
303
304 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
305 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
306
307 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100308 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200309
310 *p++ = 1;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100311 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
312
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200313 *olen = 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100314}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200315#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100316
Paul Bakker05decb22013-08-15 13:33:48 +0200317#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200318static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
319 unsigned char *buf,
320 size_t *olen )
321{
322 unsigned char *p = buf;
323
324 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ) {
325 *olen = 0;
326 return;
327 }
328
329 SSL_DEBUG_MSG( 3, ( "client hello, adding max_fragment_length extension" ) );
330
331 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
332 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
333
334 *p++ = 0x00;
335 *p++ = 1;
336
337 *p++ = ssl->mfl_code;
338
339 *olen = 5;
340}
Paul Bakker05decb22013-08-15 13:33:48 +0200341#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200342
Paul Bakker1f2bc622013-08-15 13:45:55 +0200343#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200344static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
345 unsigned char *buf, size_t *olen )
346{
347 unsigned char *p = buf;
348
349 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
350 {
351 *olen = 0;
352 return;
353 }
354
355 SSL_DEBUG_MSG( 3, ( "client hello, adding truncated_hmac extension" ) );
356
357 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
358 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
359
360 *p++ = 0x00;
361 *p++ = 0x00;
362
363 *olen = 4;
364}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200365#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200366
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100367#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
368static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
369 unsigned char *buf, size_t *olen )
370{
371 unsigned char *p = buf;
372
373 if( ssl->encrypt_then_mac == SSL_ETM_DISABLED ||
374 ssl->max_minor_ver == SSL_MINOR_VERSION_0 )
375 {
376 *olen = 0;
377 return;
378 }
379
380 SSL_DEBUG_MSG( 3, ( "client hello, adding encrypt_then_mac "
381 "extension" ) );
382
383 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
384 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
385
386 *p++ = 0x00;
387 *p++ = 0x00;
388
389 *olen = 4;
390}
391#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
392
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200393#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
394static void ssl_write_extended_ms_ext( ssl_context *ssl,
395 unsigned char *buf, size_t *olen )
396{
397 unsigned char *p = buf;
398
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200399 if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED ||
400 ssl->max_minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200401 {
402 *olen = 0;
403 return;
404 }
405
406 SSL_DEBUG_MSG( 3, ( "client hello, adding extended_master_secret "
407 "extension" ) );
408
409 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
410 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
411
412 *p++ = 0x00;
413 *p++ = 0x00;
414
415 *olen = 4;
416}
417#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
418
Paul Bakkera503a632013-08-14 13:48:06 +0200419#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200420static void ssl_write_session_ticket_ext( ssl_context *ssl,
421 unsigned char *buf, size_t *olen )
422{
423 unsigned char *p = buf;
424 size_t tlen = ssl->session_negotiate->ticket_len;
425
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200426 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
427 {
428 *olen = 0;
429 return;
430 }
431
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200432 SSL_DEBUG_MSG( 3, ( "client hello, adding session ticket extension" ) );
433
434 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
435 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
436
437 *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF );
438 *p++ = (unsigned char)( ( tlen ) & 0xFF );
439
440 *olen = 4;
441
442 if( ssl->session_negotiate->ticket == NULL ||
443 ssl->session_negotiate->ticket_len == 0 )
444 {
445 return;
446 }
447
448 SSL_DEBUG_MSG( 3, ( "sending session ticket of length %d", tlen ) );
449
450 memcpy( p, ssl->session_negotiate->ticket, tlen );
451
452 *olen += tlen;
453}
Paul Bakkera503a632013-08-14 13:48:06 +0200454#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200455
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200456#if defined(POLARSSL_SSL_ALPN)
457static void ssl_write_alpn_ext( ssl_context *ssl,
458 unsigned char *buf, size_t *olen )
459{
460 unsigned char *p = buf;
461 const char **cur;
462
463 if( ssl->alpn_list == NULL )
464 {
465 *olen = 0;
466 return;
467 }
468
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +0200469 SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200470
471 *p++ = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
472 *p++ = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
473
474 /*
475 * opaque ProtocolName<1..2^8-1>;
476 *
477 * struct {
478 * ProtocolName protocol_name_list<2..2^16-1>
479 * } ProtocolNameList;
480 */
481
482 /* Skip writing extension and list length for now */
483 p += 4;
484
485 for( cur = ssl->alpn_list; *cur != NULL; cur++ )
486 {
487 *p = (unsigned char)( strlen( *cur ) & 0xFF );
488 memcpy( p + 1, *cur, *p );
489 p += 1 + *p;
490 }
491
492 *olen = p - buf;
493
494 /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
495 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
496 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
497
498 /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
499 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
500 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
501}
502#endif /* POLARSSL_SSL_ALPN */
503
Paul Bakker5121ce52009-01-03 21:22:43 +0000504static int ssl_write_client_hello( ssl_context *ssl )
505{
Paul Bakker23986e52011-04-24 08:57:21 +0000506 int ret;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100507 size_t i, n, olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000508 unsigned char *buf;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200509 unsigned char *p, *q;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200510#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000511 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200512#endif
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200513 const int *ciphersuites;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200514 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000515
516 SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
517
Paul Bakkera9a028e2013-11-21 17:31:06 +0100518 if( ssl->f_rng == NULL )
519 {
520 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
521 return( POLARSSL_ERR_SSL_NO_RNG );
522 }
523
Paul Bakker48916f92012-09-16 19:57:18 +0000524 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
525 {
Paul Bakker993d11d2012-09-28 15:00:12 +0000526 ssl->major_ver = ssl->min_major_ver;
527 ssl->minor_ver = ssl->min_minor_ver;
Paul Bakker48916f92012-09-16 19:57:18 +0000528 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000529
Paul Bakker490ecc82011-10-06 13:04:09 +0000530 if( ssl->max_major_ver == 0 && ssl->max_minor_ver == 0 )
531 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200532 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
533 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker490ecc82011-10-06 13:04:09 +0000534 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000535
536 /*
537 * 0 . 0 handshake type
538 * 1 . 3 handshake length
539 * 4 . 5 highest version supported
540 * 6 . 9 current UNIX time
541 * 10 . 37 random bytes
542 */
543 buf = ssl->out_msg;
544 p = buf + 4;
545
546 *p++ = (unsigned char) ssl->max_major_ver;
547 *p++ = (unsigned char) ssl->max_minor_ver;
548
549 SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
550 buf[4], buf[5] ) );
551
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200552#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000553 t = time( NULL );
554 *p++ = (unsigned char)( t >> 24 );
555 *p++ = (unsigned char)( t >> 16 );
556 *p++ = (unsigned char)( t >> 8 );
557 *p++ = (unsigned char)( t );
558
559 SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200560#else
561 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
562 return( ret );
563
564 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +0200565#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000566
Paul Bakkera3d195c2011-11-27 21:07:34 +0000567 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
568 return( ret );
569
570 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +0000571
Paul Bakker48916f92012-09-16 19:57:18 +0000572 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
574 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 6, 32 );
575
576 /*
577 * 38 . 38 session id length
578 * 39 . 39+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +0000579 * 40+n . 41+n ciphersuitelist length
580 * 42+n . .. ciphersuitelist
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000581 * .. . .. compression methods length
582 * .. . .. compression methods
583 * .. . .. extensions length
584 * .. . .. extensions
Paul Bakker5121ce52009-01-03 21:22:43 +0000585 */
Paul Bakker48916f92012-09-16 19:57:18 +0000586 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000587
Paul Bakker0a597072012-09-25 21:55:46 +0000588 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE || n < 16 || n > 32 ||
589 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200590 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000591 n = 0;
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200592 }
593
Paul Bakkera503a632013-08-14 13:48:06 +0200594#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200595 /*
596 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
597 * generate and include a Session ID in the TLS ClientHello."
598 */
599 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
600 ssl->session_negotiate->ticket != NULL &&
601 ssl->session_negotiate->ticket_len != 0 )
602 {
603 ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
604
605 if( ret != 0 )
606 return( ret );
607
608 ssl->session_negotiate->length = n = 32;
609 }
Paul Bakkera503a632013-08-14 13:48:06 +0200610#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000611
612 *p++ = (unsigned char) n;
613
614 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000615 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000616
617 SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
618 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
619
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200620 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Paul Bakker2fbefde2013-06-29 16:01:15 +0200621 n = 0;
622 q = p;
623
624 // Skip writing ciphersuite length for now
625 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000626
Paul Bakker48916f92012-09-16 19:57:18 +0000627 /*
628 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
629 */
630 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
631 {
632 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
633 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
Paul Bakker2fbefde2013-06-29 16:01:15 +0200634 n++;
Paul Bakker48916f92012-09-16 19:57:18 +0000635 }
636
Paul Bakker2fbefde2013-06-29 16:01:15 +0200637 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000638 {
Paul Bakker2fbefde2013-06-29 16:01:15 +0200639 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
640
641 if( ciphersuite_info == NULL )
642 continue;
643
644 if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
645 ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
646 continue;
647
Paul Bakkere3166ce2011-01-27 17:40:50 +0000648 SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200649 ciphersuites[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000650
Paul Bakker2fbefde2013-06-29 16:01:15 +0200651 n++;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200652 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
653 *p++ = (unsigned char)( ciphersuites[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000654 }
655
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200656 /* Some versions of OpenSSL don't handle it correctly if not at end */
657#if defined(POLARSSL_SSL_FALLBACK_SCSV)
658 if( ssl->fallback == SSL_IS_FALLBACK )
659 {
660 SSL_DEBUG_MSG( 3, ( "adding FALLBACK_SCSV" ) );
661 *p++ = (unsigned char)( SSL_FALLBACK_SCSV >> 8 );
662 *p++ = (unsigned char)( SSL_FALLBACK_SCSV );
663 n++;
664 }
665#endif
666
Paul Bakker2fbefde2013-06-29 16:01:15 +0200667 *q++ = (unsigned char)( n >> 7 );
668 *q++ = (unsigned char)( n << 1 );
669
670 SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
671
672
Paul Bakker2770fbd2012-07-03 13:30:23 +0000673#if defined(POLARSSL_ZLIB_SUPPORT)
674 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
675 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000676 SSL_COMPRESS_DEFLATE, SSL_COMPRESS_NULL ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000677
678 *p++ = 2;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000679 *p++ = SSL_COMPRESS_DEFLATE;
Paul Bakker48916f92012-09-16 19:57:18 +0000680 *p++ = SSL_COMPRESS_NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000681#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000682 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000683 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d", SSL_COMPRESS_NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000684
685 *p++ = 1;
686 *p++ = SSL_COMPRESS_NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200687#endif /* POLARSSL_ZLIB_SUPPORT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000688
Paul Bakkerd3edc862013-03-20 16:07:17 +0100689 // First write extensions, then the total length
690 //
Paul Bakker0be444a2013-08-27 21:55:01 +0200691#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100692 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
693 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +0200694#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000695
Paul Bakkerd3edc862013-03-20 16:07:17 +0100696 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
697 ext_len += olen;
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000698
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100699#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
700 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100701 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
702 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200703#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000704
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200705#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100706 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
707 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100708
Paul Bakkerd3edc862013-03-20 16:07:17 +0100709 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
710 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100711#endif
712
Paul Bakker05decb22013-08-15 13:33:48 +0200713#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200714 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
715 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +0200716#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200717
Paul Bakker1f2bc622013-08-15 13:45:55 +0200718#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200719 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
720 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200721#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200722
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100723#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
724 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
725 ext_len += olen;
726#endif
727
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200728#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
729 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
730 ext_len += olen;
731#endif
732
Paul Bakkera503a632013-08-14 13:48:06 +0200733#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200734 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
735 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +0200736#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200737
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200738#if defined(POLARSSL_SSL_ALPN)
739 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
740 ext_len += olen;
741#endif
742
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000743 SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
744 ext_len ) );
745
Paul Bakkera7036632014-04-30 10:15:38 +0200746 if( ext_len > 0 )
747 {
748 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
749 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
750 p += ext_len;
751 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100752
Paul Bakker5121ce52009-01-03 21:22:43 +0000753 ssl->out_msglen = p - buf;
754 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
755 ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
756
757 ssl->state++;
758
759 if( ( ret = ssl_write_record( ssl ) ) != 0 )
760 {
761 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
762 return( ret );
763 }
764
765 SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
766
767 return( 0 );
768}
769
Paul Bakker48916f92012-09-16 19:57:18 +0000770static int ssl_parse_renegotiation_info( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200771 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000772 size_t len )
773{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000774 int ret;
775
Paul Bakker48916f92012-09-16 19:57:18 +0000776 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
777 {
778 if( len != 1 || buf[0] != 0x0 )
779 {
780 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000781
782 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
783 return( ret );
784
Paul Bakker48916f92012-09-16 19:57:18 +0000785 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
786 }
787
788 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
789 }
790 else
791 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100792 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000793 if( len != 1 + ssl->verify_data_len * 2 ||
794 buf[0] != ssl->verify_data_len * 2 ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100795 safer_memcmp( buf + 1,
796 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
797 safer_memcmp( buf + 1 + ssl->verify_data_len,
798 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000799 {
800 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000801
802 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
803 return( ret );
804
Paul Bakker48916f92012-09-16 19:57:18 +0000805 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
806 }
807 }
808
809 return( 0 );
810}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200811
Paul Bakker05decb22013-08-15 13:33:48 +0200812#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200813static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200814 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200815 size_t len )
816{
817 /*
818 * server should use the extension only if we did,
819 * and if so the server's value should match ours (and len is always 1)
820 */
821 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
822 len != 1 ||
823 buf[0] != ssl->mfl_code )
824 {
825 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
826 }
827
828 return( 0 );
829}
Paul Bakker05decb22013-08-15 13:33:48 +0200830#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000831
Paul Bakker1f2bc622013-08-15 13:45:55 +0200832#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200833static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
834 const unsigned char *buf,
835 size_t len )
836{
837 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
838 len != 0 )
839 {
840 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
841 }
842
843 ((void) buf);
844
845 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
846
847 return( 0 );
848}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200849#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200850
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100851#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
852static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
853 const unsigned char *buf,
854 size_t len )
855{
856 if( ssl->encrypt_then_mac == SSL_ETM_DISABLED ||
857 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
858 len != 0 )
859 {
860 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
861 }
862
863 ((void) buf);
864
865 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
866
867 return( 0 );
868}
869#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
870
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200871#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
872static int ssl_parse_extended_ms_ext( ssl_context *ssl,
873 const unsigned char *buf,
874 size_t len )
875{
876 if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200877 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200878 len != 0 )
879 {
880 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
881 }
882
883 ((void) buf);
884
885 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
886
887 return( 0 );
888}
889#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
890
Paul Bakkera503a632013-08-14 13:48:06 +0200891#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200892static int ssl_parse_session_ticket_ext( ssl_context *ssl,
893 const unsigned char *buf,
894 size_t len )
895{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200896 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ||
897 len != 0 )
898 {
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200899 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200900 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200901
902 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200903
904 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200905
906 return( 0 );
907}
Paul Bakkera503a632013-08-14 13:48:06 +0200908#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200909
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200910#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200911static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
912 const unsigned char *buf,
913 size_t len )
914{
915 size_t list_size;
916 const unsigned char *p;
917
918 list_size = buf[0];
919 if( list_size + 1 != len )
920 {
921 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
922 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
923 }
924
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200925 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200926 while( list_size > 0 )
927 {
928 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
929 p[0] == POLARSSL_ECP_PF_COMPRESSED )
930 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200931 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200932 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
933 return( 0 );
934 }
935
936 list_size--;
937 p++;
938 }
939
Manuel Pégourié-Gonnard5c1f0322014-06-23 14:24:43 +0200940 SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
941 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200942}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200943#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200944
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200945#if defined(POLARSSL_SSL_ALPN)
946static int ssl_parse_alpn_ext( ssl_context *ssl,
947 const unsigned char *buf, size_t len )
948{
949 size_t list_len, name_len;
950 const char **p;
951
952 /* If we didn't send it, the server shouldn't send it */
953 if( ssl->alpn_list == NULL )
954 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
955
956 /*
957 * opaque ProtocolName<1..2^8-1>;
958 *
959 * struct {
960 * ProtocolName protocol_name_list<2..2^16-1>
961 * } ProtocolNameList;
962 *
963 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
964 */
965
966 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
967 if( len < 4 )
968 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
969
970 list_len = ( buf[0] << 8 ) | buf[1];
971 if( list_len != len - 2 )
972 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
973
974 name_len = buf[2];
975 if( name_len != list_len - 1 )
976 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
977
978 /* Check that the server chosen protocol was in our list and save it */
979 for( p = ssl->alpn_list; *p != NULL; p++ )
980 {
981 if( name_len == strlen( *p ) &&
982 memcmp( buf + 3, *p, name_len ) == 0 )
983 {
984 ssl->alpn_chosen = *p;
985 return( 0 );
986 }
987 }
988
989 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
990}
991#endif /* POLARSSL_SSL_ALPN */
992
Paul Bakker5121ce52009-01-03 21:22:43 +0000993static int ssl_parse_server_hello( ssl_context *ssl )
994{
Paul Bakker2770fbd2012-07-03 13:30:23 +0000995 int ret, i, comp;
Paul Bakker23986e52011-04-24 08:57:21 +0000996 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +0200997 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +0000998 unsigned char *buf, *ext;
999 int renegotiation_info_seen = 0;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001000 int handshake_failure = 0;
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001001#if defined(POLARSSL_DEBUG_C)
1002 uint32_t t;
1003#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001004
1005 SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
1006
1007 /*
1008 * 0 . 0 handshake type
1009 * 1 . 3 handshake length
1010 * 4 . 5 protocol version
1011 * 6 . 9 UNIX time()
1012 * 10 . 37 random bytes
1013 */
1014 buf = ssl->in_msg;
1015
1016 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1017 {
1018 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1019 return( ret );
1020 }
1021
1022 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1023 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001024 if( ssl->renegotiation == SSL_RENEGOTIATION )
1025 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001026 ssl->renego_records_seen++;
1027
1028 if( ssl->renego_max_records >= 0 &&
1029 ssl->renego_records_seen > ssl->renego_max_records )
1030 {
1031 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
1032 "but not honored by server" ) );
1033 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1034 }
1035
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001036 SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
1037 return( POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
1038 }
1039
Paul Bakker5121ce52009-01-03 21:22:43 +00001040 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001041 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001042 }
1043
1044 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1045 buf[4], buf[5] ) );
1046
1047 if( ssl->in_hslen < 42 ||
1048 buf[0] != SSL_HS_SERVER_HELLO ||
1049 buf[4] != SSL_MAJOR_VERSION_3 )
1050 {
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
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001055 if( buf[5] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00001056 {
1057 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001058 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001059 }
1060
1061 ssl->minor_ver = buf[5];
1062
Paul Bakker1d29fb52012-09-28 13:28:45 +00001063 if( ssl->minor_ver < ssl->min_minor_ver )
1064 {
1065 SSL_DEBUG_MSG( 1, ( "server only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001066 " [%d:%d] < [%d:%d]", ssl->major_ver,
1067 ssl->minor_ver, buf[4], buf[5] ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001068
1069 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1070 SSL_ALERT_MSG_PROTOCOL_VERSION );
1071
1072 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1073 }
1074
Paul Bakker1504af52012-02-11 16:17:43 +00001075#if defined(POLARSSL_DEBUG_C)
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001076 t = ( (uint32_t) buf[6] << 24 )
1077 | ( (uint32_t) buf[7] << 16 )
1078 | ( (uint32_t) buf[8] << 8 )
1079 | ( (uint32_t) buf[9] );
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001080 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakker87e5cda2012-01-14 18:14:15 +00001081#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001082
Paul Bakker48916f92012-09-16 19:57:18 +00001083 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001084
1085 n = buf[38];
1086
Paul Bakker5121ce52009-01-03 21:22:43 +00001087 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1088
Paul Bakker48916f92012-09-16 19:57:18 +00001089 if( n > 32 )
1090 {
1091 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1092 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1093 }
1094
Paul Bakker5121ce52009-01-03 21:22:43 +00001095 /*
1096 * 38 . 38 session id length
1097 * 39 . 38+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +00001098 * 39+n . 40+n chosen ciphersuite
Paul Bakker5121ce52009-01-03 21:22:43 +00001099 * 41+n . 41+n chosen compression alg.
1100 * 42+n . 43+n extensions length
1101 * 44+n . 44+n+m extensions
1102 */
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001103 if( ssl->in_hslen > 43 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001104 {
1105 ext_len = ( ( buf[42 + n] << 8 )
Paul Bakker48916f92012-09-16 19:57:18 +00001106 | ( buf[43 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001107
Paul Bakker48916f92012-09-16 19:57:18 +00001108 if( ( ext_len > 0 && ext_len < 4 ) ||
1109 ssl->in_hslen != 44 + n + ext_len )
1110 {
1111 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1112 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1113 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001114 }
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001115 else if( ssl->in_hslen == 42 + n )
1116 {
1117 ext_len = 0;
1118 }
1119 else
1120 {
1121 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1122 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1123 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001124
1125 i = ( buf[39 + n] << 8 ) | buf[40 + n];
Paul Bakker2770fbd2012-07-03 13:30:23 +00001126 comp = buf[41 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001127
Paul Bakker380da532012-04-18 16:10:25 +00001128 /*
1129 * Initialize update checksum functions
1130 */
Paul Bakker68884e32013-01-07 18:20:04 +01001131 ssl->transform_negotiate->ciphersuite_info = ssl_ciphersuite_from_id( i );
1132
1133 if( ssl->transform_negotiate->ciphersuite_info == NULL )
1134 {
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001135 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001136 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1137 }
Paul Bakker380da532012-04-18 16:10:25 +00001138
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001139 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1140
Paul Bakker5121ce52009-01-03 21:22:43 +00001141 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1142 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1143
1144 /*
1145 * Check if the session can be resumed
1146 */
Paul Bakker0a597072012-09-25 21:55:46 +00001147 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
1148 ssl->handshake->resume == 0 || n == 0 ||
Paul Bakker48916f92012-09-16 19:57:18 +00001149 ssl->session_negotiate->ciphersuite != i ||
1150 ssl->session_negotiate->compression != comp ||
1151 ssl->session_negotiate->length != n ||
1152 memcmp( ssl->session_negotiate->id, buf + 39, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001153 {
1154 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001155 ssl->handshake->resume = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001156#if defined(POLARSSL_HAVE_TIME)
Paul Bakker48916f92012-09-16 19:57:18 +00001157 ssl->session_negotiate->start = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001158#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001159 ssl->session_negotiate->ciphersuite = i;
1160 ssl->session_negotiate->compression = comp;
1161 ssl->session_negotiate->length = n;
1162 memcpy( ssl->session_negotiate->id, buf + 39, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001163 }
1164 else
1165 {
1166 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001167
1168 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1169 {
1170 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1171 return( ret );
1172 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001173 }
1174
1175 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001176 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001177
Paul Bakkere3166ce2011-01-27 17:40:50 +00001178 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001179 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[41 + n] ) );
1180
1181 i = 0;
1182 while( 1 )
1183 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001184 if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001185 {
1186 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001187 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001188 }
1189
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001190 if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
1191 ssl->session_negotiate->ciphersuite )
1192 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001193 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001194 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001195 }
1196
Paul Bakker2770fbd2012-07-03 13:30:23 +00001197 if( comp != SSL_COMPRESS_NULL
1198#if defined(POLARSSL_ZLIB_SUPPORT)
1199 && comp != SSL_COMPRESS_DEFLATE
1200#endif
1201 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001202 {
1203 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001204 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001205 }
Paul Bakker48916f92012-09-16 19:57:18 +00001206 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001207
Paul Bakker48916f92012-09-16 19:57:18 +00001208 ext = buf + 44 + n;
1209
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001210 SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
1211
Paul Bakker48916f92012-09-16 19:57:18 +00001212 while( ext_len )
1213 {
1214 unsigned int ext_id = ( ( ext[0] << 8 )
1215 | ( ext[1] ) );
1216 unsigned int ext_size = ( ( ext[2] << 8 )
1217 | ( ext[3] ) );
1218
1219 if( ext_size + 4 > ext_len )
1220 {
1221 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1222 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1223 }
1224
1225 switch( ext_id )
1226 {
1227 case TLS_EXT_RENEGOTIATION_INFO:
1228 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1229 renegotiation_info_seen = 1;
1230
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001231 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1232 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001233 return( ret );
1234
1235 break;
1236
Paul Bakker05decb22013-08-15 13:33:48 +02001237#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001238 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1239 SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
1240
1241 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1242 ext + 4, ext_size ) ) != 0 )
1243 {
1244 return( ret );
1245 }
1246
1247 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001248#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001249
Paul Bakker1f2bc622013-08-15 13:45:55 +02001250#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001251 case TLS_EXT_TRUNCATED_HMAC:
1252 SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
1253
1254 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1255 ext + 4, ext_size ) ) != 0 )
1256 {
1257 return( ret );
1258 }
1259
1260 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001261#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001262
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001263#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1264 case TLS_EXT_ENCRYPT_THEN_MAC:
1265 SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
1266
1267 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
1268 ext + 4, ext_size ) ) != 0 )
1269 {
1270 return( ret );
1271 }
1272
1273 break;
1274#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1275
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001276#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1277 case TLS_EXT_EXTENDED_MASTER_SECRET:
1278 SSL_DEBUG_MSG( 3, ( "found extended_master_secret extension" ) );
1279
1280 if( ( ret = ssl_parse_extended_ms_ext( ssl,
1281 ext + 4, ext_size ) ) != 0 )
1282 {
1283 return( ret );
1284 }
1285
1286 break;
1287#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1288
Paul Bakkera503a632013-08-14 13:48:06 +02001289#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001290 case TLS_EXT_SESSION_TICKET:
1291 SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
1292
1293 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1294 ext + 4, ext_size ) ) != 0 )
1295 {
1296 return( ret );
1297 }
1298
1299 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001300#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001301
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001302#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001303 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1304 SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
1305
1306 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1307 ext + 4, ext_size ) ) != 0 )
1308 {
1309 return( ret );
1310 }
1311
1312 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001313#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001314
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001315#if defined(POLARSSL_SSL_ALPN)
1316 case TLS_EXT_ALPN:
1317 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1318
1319 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1320 return( ret );
1321
1322 break;
1323#endif /* POLARSSL_SSL_ALPN */
1324
Paul Bakker48916f92012-09-16 19:57:18 +00001325 default:
1326 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1327 ext_id ) );
1328 }
1329
1330 ext_len -= 4 + ext_size;
1331 ext += 4 + ext_size;
1332
1333 if( ext_len > 0 && ext_len < 4 )
1334 {
1335 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1336 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1337 }
1338 }
1339
1340 /*
1341 * Renegotiation security checks
1342 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001343 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1344 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001345 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001346 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1347 handshake_failure = 1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001348 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001349 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1350 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1351 renegotiation_info_seen == 0 )
1352 {
1353 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1354 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001355 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001356 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1357 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1358 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001359 {
1360 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001361 handshake_failure = 1;
1362 }
1363 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1364 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1365 renegotiation_info_seen == 1 )
1366 {
1367 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1368 handshake_failure = 1;
1369 }
1370
1371 if( handshake_failure == 1 )
1372 {
1373 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1374 return( ret );
1375
Paul Bakker48916f92012-09-16 19:57:18 +00001376 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1377 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001378
1379 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1380
1381 return( 0 );
1382}
1383
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001384#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1385 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001386static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
1387 unsigned char *end )
1388{
1389 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1390
Paul Bakker29e1f122013-04-16 13:07:56 +02001391 /*
1392 * Ephemeral DH parameters:
1393 *
1394 * struct {
1395 * opaque dh_p<1..2^16-1>;
1396 * opaque dh_g<1..2^16-1>;
1397 * opaque dh_Ys<1..2^16-1>;
1398 * } ServerDHParams;
1399 */
1400 if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1401 {
1402 SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
1403 return( ret );
1404 }
1405
1406 if( ssl->handshake->dhm_ctx.len < 64 ||
1407 ssl->handshake->dhm_ctx.len > 512 )
1408 {
1409 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
1410 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1411 }
1412
1413 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1414 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1415 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001416
1417 return( ret );
1418}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001419#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1420 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001421
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001422#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001423 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001424 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1425 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1426 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1427static int ssl_check_server_ecdh_params( const ssl_context *ssl )
1428{
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01001429 const ecp_curve_info *curve_info;
1430
1431 curve_info = ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
1432 if( curve_info == NULL )
1433 {
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001434 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1435 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01001436 }
1437
1438 SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001439
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001440#if defined(POLARSSL_SSL_ECP_SET_CURVES)
1441 if( ! ssl_curve_is_acceptable( ssl, ssl->handshake->ecdh_ctx.grp.id ) )
1442#else
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001443 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1444 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001445#endif
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001446 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001447
1448 SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
1449
1450 return( 0 );
1451}
Paul Bakker9af723c2014-05-01 13:03:14 +02001452#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1453 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1454 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1455 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1456 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001457
1458#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1459 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001460 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001461static int ssl_parse_server_ecdh_params( ssl_context *ssl,
1462 unsigned char **p,
1463 unsigned char *end )
1464{
1465 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1466
Paul Bakker29e1f122013-04-16 13:07:56 +02001467 /*
1468 * Ephemeral ECDH parameters:
1469 *
1470 * struct {
1471 * ECParameters curve_params;
1472 * ECPoint public;
1473 * } ServerECDHParams;
1474 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001475 if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
1476 (const unsigned char **) p, end ) ) != 0 )
1477 {
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001478 SSL_DEBUG_RET( 1, ( "ecdh_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001479 return( ret );
1480 }
1481
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001482 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001483 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001484 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001485 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1486 }
1487
Paul Bakker29e1f122013-04-16 13:07:56 +02001488 return( ret );
1489}
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001490#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001491 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1492 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001493
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001494#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001495static int ssl_parse_server_psk_hint( ssl_context *ssl,
1496 unsigned char **p,
1497 unsigned char *end )
1498{
1499 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001500 size_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001501 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001502
1503 /*
1504 * PSK parameters:
1505 *
1506 * opaque psk_identity_hint<0..2^16-1>;
1507 */
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001508 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001509 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001510
1511 if( (*p) + len > end )
1512 {
1513 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
1514 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1515 }
1516
1517 // TODO: Retrieve PSK identity hint and callback to app
1518 //
1519 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001520 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001521
1522 return( ret );
1523}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001524#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001525
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001526#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1527 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1528/*
1529 * Generate a pre-master secret and encrypt it with the server's RSA key
1530 */
1531static int ssl_write_encrypted_pms( ssl_context *ssl,
1532 size_t offset, size_t *olen,
1533 size_t pms_offset )
1534{
1535 int ret;
1536 size_t len_bytes = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 0 : 2;
1537 unsigned char *p = ssl->handshake->premaster + pms_offset;
1538
1539 /*
1540 * Generate (part of) the pre-master as
1541 * struct {
1542 * ProtocolVersion client_version;
1543 * opaque random[46];
1544 * } PreMasterSecret;
1545 */
1546 p[0] = (unsigned char) ssl->max_major_ver;
1547 p[1] = (unsigned char) ssl->max_minor_ver;
1548
1549 if( ( ret = ssl->f_rng( ssl->p_rng, p + 2, 46 ) ) != 0 )
1550 {
1551 SSL_DEBUG_RET( 1, "f_rng", ret );
1552 return( ret );
1553 }
1554
1555 ssl->handshake->pmslen = 48;
1556
1557 /*
1558 * Now write it out, encrypted
1559 */
1560 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1561 POLARSSL_PK_RSA ) )
1562 {
1563 SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1564 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1565 }
1566
1567 if( ( ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
1568 p, ssl->handshake->pmslen,
1569 ssl->out_msg + offset + len_bytes, olen,
1570 SSL_MAX_CONTENT_LEN - offset - len_bytes,
1571 ssl->f_rng, ssl->p_rng ) ) != 0 )
1572 {
1573 SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
1574 return( ret );
1575 }
1576
1577#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1578 defined(POLARSSL_SSL_PROTO_TLS1_2)
1579 if( len_bytes == 2 )
1580 {
1581 ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
1582 ssl->out_msg[offset+1] = (unsigned char)( *olen );
1583 *olen += 2;
1584 }
1585#endif
1586
1587 return( 0 );
1588}
1589#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
1590 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001591
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001592#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001593#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001594 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1595 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001596static int ssl_parse_signature_algorithm( ssl_context *ssl,
1597 unsigned char **p,
1598 unsigned char *end,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001599 md_type_t *md_alg,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001600 pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02001601{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001602 ((void) ssl);
Paul Bakker29e1f122013-04-16 13:07:56 +02001603 *md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001604 *pk_alg = POLARSSL_PK_NONE;
1605
1606 /* Only in TLS 1.2 */
1607 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1608 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001609 return( 0 );
1610 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001611
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001612 if( (*p) + 2 > end )
Paul Bakker29e1f122013-04-16 13:07:56 +02001613 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1614
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001615 /*
1616 * Get hash algorithm
1617 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001618 if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001619 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001620 SSL_DEBUG_MSG( 2, ( "Server used unsupported "
1621 "HashAlgorithm %d", *(p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001622 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1623 }
1624
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001625 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001626 * Get signature algorithm
1627 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001628 if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001629 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001630 SSL_DEBUG_MSG( 2, ( "server used unsupported "
1631 "SignatureAlgorithm %d", (*p)[1] ) );
1632 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001633 }
1634
1635 SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
1636 SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
1637 *p += 2;
1638
1639 return( 0 );
1640}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001641#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001642 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1643 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001644#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001645
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001646
1647#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1648 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1649static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
1650{
1651 int ret;
1652 const ecp_keypair *peer_key;
1653
1654 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1655 POLARSSL_PK_ECKEY ) )
1656 {
1657 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
1658 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1659 }
1660
1661 peer_key = pk_ec( ssl->session_negotiate->peer_cert->pk );
1662
1663 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
1664 POLARSSL_ECDH_THEIRS ) ) != 0 )
1665 {
1666 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
1667 return( ret );
1668 }
1669
1670 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1671 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001672 SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001673 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
1674 }
1675
1676 return( ret );
1677}
1678#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1679 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1680
Paul Bakker41c83d32013-03-20 14:39:14 +01001681static int ssl_parse_server_key_exchange( ssl_context *ssl )
1682{
Paul Bakker23986e52011-04-24 08:57:21 +00001683 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001684 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001685 unsigned char *p, *end;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001686#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001687 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1688 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001689 size_t sig_len, params_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001690 unsigned char hash[64];
Paul Bakkerc70b9822013-04-07 22:00:46 +02001691 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001692 size_t hashlen;
1693 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001694#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001695
1696 SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
1697
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001698#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001699 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001700 {
1701 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1702 ssl->state++;
1703 return( 0 );
1704 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001705 ((void) p);
1706 ((void) end);
1707#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001708
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001709#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1710 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1711 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
1712 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
1713 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001714 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
1715 {
1716 SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
1717 return( ret );
1718 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001719
1720 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1721 ssl->state++;
1722 return( 0 );
1723 }
1724 ((void) p);
1725 ((void) end);
Paul Bakker9af723c2014-05-01 13:03:14 +02001726#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1727 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001728
Paul Bakker5121ce52009-01-03 21:22:43 +00001729 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1730 {
1731 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1732 return( ret );
1733 }
1734
1735 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1736 {
1737 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001738 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001739 }
1740
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001741 /*
1742 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
1743 * doesn't use a psk_identity_hint
1744 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001745 if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
1746 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001747 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1748 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02001749 {
1750 ssl->record_read = 1;
1751 goto exit;
1752 }
1753
1754 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1755 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001756 }
1757
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001758 p = ssl->in_msg + 4;
1759 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001760 SSL_DEBUG_BUF( 3, "server key exchange", p, ssl->in_hslen - 4 );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001761
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001762#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1763 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1764 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1765 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1766 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1767 {
1768 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1769 {
1770 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1771 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1772 }
1773 } /* FALLTROUGH */
1774#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
1775
1776#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
1777 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1778 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1779 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1780 ; /* nothing more to do */
1781 else
1782#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
1783 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
1784#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1785 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1786 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1787 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001788 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001789 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001790 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001791 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001792 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1793 }
1794 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001795 else
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001796#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1797 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001798#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001799 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001800 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1801 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001802 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001803 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001804 {
1805 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
1806 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001807 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1808 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1809 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001810 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001811 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001812#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001813 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001814 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001815 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001816 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001817 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001818 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001819
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001820#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001821 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1822 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001823 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001824 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1825 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001826 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001827 params_len = p - ( ssl->in_msg + 4 );
1828
Paul Bakker29e1f122013-04-16 13:07:56 +02001829 /*
1830 * Handle the digitally-signed structure
1831 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001832#if defined(POLARSSL_SSL_PROTO_TLS1_2)
1833 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001834 {
Paul Bakker9659dae2013-08-28 16:21:34 +02001835 if( ssl_parse_signature_algorithm( ssl, &p, end,
1836 &md_alg, &pk_alg ) != 0 )
1837 {
1838 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1839 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1840 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001841
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001842 if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001843 {
1844 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1845 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1846 }
1847 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001848 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001849#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001850#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1851 defined(POLARSSL_SSL_PROTO_TLS1_1)
1852 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001853 {
1854 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Paul Bakker1ef83d62012-04-11 12:09:53 +00001855
Paul Bakker9659dae2013-08-28 16:21:34 +02001856 /* Default hash for ECDSA is SHA-1 */
1857 if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
1858 md_alg = POLARSSL_MD_SHA1;
1859 }
1860 else
1861#endif
1862 {
1863 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001864 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker9659dae2013-08-28 16:21:34 +02001865 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001866
1867 /*
1868 * Read signature
1869 */
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001870 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00001871 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001872
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001873 if( end != p + sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01001874 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001875 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001876 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1877 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001878
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001879 SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02001880
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001881 /*
1882 * Compute the hash that has been signed
1883 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001884#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1885 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001886 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001887 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001888 md5_context md5;
1889 sha1_context sha1;
1890
Paul Bakker5b4af392014-06-26 12:09:34 +02001891 md5_init( &md5 );
1892 sha1_init( &sha1 );
1893
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001894 hashlen = 36;
1895
Paul Bakker29e1f122013-04-16 13:07:56 +02001896 /*
1897 * digitally-signed struct {
1898 * opaque md5_hash[16];
1899 * opaque sha_hash[20];
1900 * };
1901 *
1902 * md5_hash
1903 * MD5(ClientHello.random + ServerHello.random
1904 * + ServerParams);
1905 * sha_hash
1906 * SHA(ClientHello.random + ServerHello.random
1907 * + ServerParams);
1908 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001909 md5_starts( &md5 );
1910 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001911 md5_update( &md5, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001912 md5_finish( &md5, hash );
1913
1914 sha1_starts( &sha1 );
1915 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001916 sha1_update( &sha1, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001917 sha1_finish( &sha1, hash + 16 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001918
1919 md5_free( &md5 );
1920 sha1_free( &sha1 );
Paul Bakker29e1f122013-04-16 13:07:56 +02001921 }
1922 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001923#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
1924 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001925#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1926 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02001927 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001928 {
1929 md_context_t ctx;
1930
Paul Bakker84bbeb52014-07-01 14:53:22 +02001931 md_init( &ctx );
1932
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001933 /* Info from md_alg will be used instead */
1934 hashlen = 0;
Paul Bakker29e1f122013-04-16 13:07:56 +02001935
1936 /*
1937 * digitally-signed struct {
1938 * opaque client_random[32];
1939 * opaque server_random[32];
1940 * ServerDHParams params;
1941 * };
1942 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001943 if( ( ret = md_init_ctx( &ctx,
1944 md_info_from_type( md_alg ) ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001945 {
1946 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
1947 return( ret );
1948 }
1949
1950 md_starts( &ctx );
1951 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001952 md_update( &ctx, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001953 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02001954 md_free( &ctx );
Paul Bakker29e1f122013-04-16 13:07:56 +02001955 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001956 else
Paul Bakker9659dae2013-08-28 16:21:34 +02001957#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1958 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001959 {
Paul Bakker577e0062013-08-28 11:57:20 +02001960 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001961 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001962 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001963
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02001964 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
1965 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker29e1f122013-04-16 13:07:56 +02001966
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001967 /*
1968 * Verify signature
1969 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02001970 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001971 {
1972 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1973 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1974 }
1975
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001976 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
1977 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001978 {
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001979 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001980 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001981 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001982 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001983#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001984 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1985 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001986
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001987exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00001988 ssl->state++;
1989
1990 SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
1991
1992 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001993}
1994
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001995#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1996 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
1997 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1998 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1999static int ssl_parse_certificate_request( ssl_context *ssl )
2000{
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002001 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2002
2003 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2004
2005 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2006 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2007 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2008 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2009 {
2010 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2011 ssl->state++;
2012 return( 0 );
2013 }
2014
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002015 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2016 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002017}
2018#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002019static int ssl_parse_certificate_request( ssl_context *ssl )
2020{
2021 int ret;
Paul Bakker926af752012-11-23 13:38:07 +01002022 unsigned char *buf, *p;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002023 size_t n = 0, m = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002024 size_t cert_type_len = 0, dn_len = 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002025 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002026
2027 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2028
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002029 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2030 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2031 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2032 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2033 {
2034 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2035 ssl->state++;
2036 return( 0 );
2037 }
2038
Paul Bakker5121ce52009-01-03 21:22:43 +00002039 /*
2040 * 0 . 0 handshake type
2041 * 1 . 3 handshake length
Paul Bakker926af752012-11-23 13:38:07 +01002042 * 4 . 4 cert type count
2043 * 5 .. m-1 cert types
2044 * m .. m+1 sig alg length (TLS 1.2 only)
2045 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002046 * n .. n+1 length of all DNs
2047 * n+2 .. n+3 length of DN 1
2048 * n+4 .. ... Distinguished Name #1
2049 * ... .. ... length of DN 2, etc.
2050 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002051 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002052 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002053 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2054 {
2055 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2056 return( ret );
2057 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002058
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002059 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2060 {
2061 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2062 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2063 }
2064
2065 ssl->record_read = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002066 }
2067
2068 ssl->client_auth = 0;
2069 ssl->state++;
2070
2071 if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST )
2072 ssl->client_auth++;
2073
2074 SSL_DEBUG_MSG( 3, ( "got %s certificate request",
2075 ssl->client_auth ? "a" : "no" ) );
2076
Paul Bakker926af752012-11-23 13:38:07 +01002077 if( ssl->client_auth == 0 )
2078 goto exit;
2079
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002080 ssl->record_read = 0;
2081
Paul Bakker926af752012-11-23 13:38:07 +01002082 // TODO: handshake_failure alert for an anonymous server to request
2083 // client authentication
2084
2085 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002086
Paul Bakker926af752012-11-23 13:38:07 +01002087 // Retrieve cert types
2088 //
2089 cert_type_len = buf[4];
2090 n = cert_type_len;
2091
2092 if( ssl->in_hslen < 6 + n )
2093 {
2094 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2095 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2096 }
2097
Paul Bakker73d44312013-05-22 13:56:26 +02002098 p = buf + 5;
Paul Bakker926af752012-11-23 13:38:07 +01002099 while( cert_type_len > 0 )
2100 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002101#if defined(POLARSSL_RSA_C)
2102 if( *p == SSL_CERT_TYPE_RSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002103 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker926af752012-11-23 13:38:07 +01002104 {
2105 ssl->handshake->cert_type = SSL_CERT_TYPE_RSA_SIGN;
2106 break;
2107 }
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002108 else
2109#endif
2110#if defined(POLARSSL_ECDSA_C)
2111 if( *p == SSL_CERT_TYPE_ECDSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002112 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002113 {
2114 ssl->handshake->cert_type = SSL_CERT_TYPE_ECDSA_SIGN;
2115 break;
2116 }
2117 else
2118#endif
2119 {
2120 ; /* Unsupported cert type, ignore */
2121 }
Paul Bakker926af752012-11-23 13:38:07 +01002122
2123 cert_type_len--;
2124 p++;
2125 }
2126
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002127#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002128 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2129 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002130 /* Ignored, see comments about hash in write_certificate_verify */
2131 // TODO: should check the signature part against our pk_key though
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002132 size_t sig_alg_len = ( ( buf[5 + n] << 8 )
2133 | ( buf[6 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002134
2135 p = buf + 7 + n;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002136 m += 2;
Paul Bakker926af752012-11-23 13:38:07 +01002137 n += sig_alg_len;
2138
2139 if( ssl->in_hslen < 6 + n )
2140 {
2141 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2142 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2143 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002144 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002145#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker926af752012-11-23 13:38:07 +01002146
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002147 /* Ignore certificate_authorities, we only have one cert anyway */
2148 // TODO: should not send cert if no CA matches
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002149 dn_len = ( ( buf[5 + m + n] << 8 )
2150 | ( buf[6 + m + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002151
2152 n += dn_len;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002153 if( ssl->in_hslen != 7 + m + n )
Paul Bakker926af752012-11-23 13:38:07 +01002154 {
2155 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2156 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2157 }
2158
2159exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002160 SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2161
2162 return( 0 );
2163}
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002164#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2165 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2166 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2167 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002168
2169static int ssl_parse_server_hello_done( ssl_context *ssl )
2170{
2171 int ret;
2172
2173 SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
2174
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002175 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002176 {
2177 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2178 {
2179 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2180 return( ret );
2181 }
2182
2183 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2184 {
2185 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002186 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002187 }
2188 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002189 ssl->record_read = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002190
2191 if( ssl->in_hslen != 4 ||
2192 ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
2193 {
2194 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002195 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002196 }
2197
2198 ssl->state++;
2199
2200 SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
2201
2202 return( 0 );
2203}
2204
2205static int ssl_write_client_key_exchange( ssl_context *ssl )
2206{
Paul Bakker23986e52011-04-24 08:57:21 +00002207 int ret;
2208 size_t i, n;
Paul Bakker41c83d32013-03-20 14:39:14 +01002209 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002210
2211 SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
2212
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002213#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002214 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002215 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002216 /*
2217 * DHM key exchange -- send G^X mod P
2218 */
Paul Bakker48916f92012-09-16 19:57:18 +00002219 n = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002220
2221 ssl->out_msg[4] = (unsigned char)( n >> 8 );
2222 ssl->out_msg[5] = (unsigned char)( n );
2223 i = 6;
2224
Paul Bakker29b64762012-09-25 09:36:44 +00002225 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002226 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker5121ce52009-01-03 21:22:43 +00002227 &ssl->out_msg[i], n,
2228 ssl->f_rng, ssl->p_rng );
2229 if( ret != 0 )
2230 {
2231 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2232 return( ret );
2233 }
2234
Paul Bakker48916f92012-09-16 19:57:18 +00002235 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2236 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002237
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002238 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002239
Paul Bakker48916f92012-09-16 19:57:18 +00002240 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2241 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002242 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002243 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002244 {
2245 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2246 return( ret );
2247 }
2248
Paul Bakker48916f92012-09-16 19:57:18 +00002249 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002250 }
2251 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002252#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002253#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002254 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2255 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2256 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002257 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002258 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2259 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2260 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002261 {
2262 /*
2263 * ECDH key exchange -- send client public value
2264 */
2265 i = 4;
2266
2267 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
2268 &n,
2269 &ssl->out_msg[i], 1000,
2270 ssl->f_rng, ssl->p_rng );
2271 if( ret != 0 )
2272 {
2273 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2274 return( ret );
2275 }
2276
2277 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2278
2279 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2280 &ssl->handshake->pmslen,
2281 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002282 POLARSSL_MPI_MAX_SIZE,
2283 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002284 {
2285 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2286 return( ret );
2287 }
2288
2289 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2290 }
2291 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002292#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002293 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2294 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2295 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002296#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002297 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002298 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002299 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2300 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002301 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002302 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002303 * opaque psk_identity<0..2^16-1>;
2304 */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002305 if( ssl->psk == NULL || ssl->psk_identity == NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002306 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2307
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002308 i = 4;
2309 n = ssl->psk_identity_len;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002310 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2311 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002312
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002313 memcpy( ssl->out_msg + i, ssl->psk_identity, ssl->psk_identity_len );
2314 i += ssl->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002315
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002316#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002317 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002318 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002319 n = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002320 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002321 else
2322#endif
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002323#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2324 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2325 {
2326 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2327 return( ret );
2328 }
2329 else
2330#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002331#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002332 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002333 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002334 /*
2335 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
2336 */
2337 n = ssl->handshake->dhm_ctx.len;
2338 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2339 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002340
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002341 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakker68881672013-10-15 13:24:01 +02002342 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002343 &ssl->out_msg[i], n,
2344 ssl->f_rng, ssl->p_rng );
2345 if( ret != 0 )
2346 {
2347 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2348 return( ret );
2349 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002350 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002351 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002352#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002353#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002354 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002355 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002356 /*
2357 * ClientECDiffieHellmanPublic public;
2358 */
2359 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
2360 &ssl->out_msg[i], SSL_MAX_CONTENT_LEN - i,
2361 ssl->f_rng, ssl->p_rng );
2362 if( ret != 0 )
2363 {
2364 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2365 return( ret );
2366 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002367
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002368 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2369 }
2370 else
2371#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2372 {
2373 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002374 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002375 }
2376
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002377 if( ( ret = ssl_psk_derive_premaster( ssl,
2378 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002379 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002380 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002381 return( ret );
2382 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002383 }
2384 else
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002385#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002386#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +02002387 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002388 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002389 i = 4;
2390 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002391 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002392 }
Paul Bakkered27a042013-04-18 22:46:23 +02002393 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002394#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002395 {
2396 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002397 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002398 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02002399 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002400
Paul Bakker5121ce52009-01-03 21:22:43 +00002401 ssl->out_msglen = i + n;
2402 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2403 ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE;
2404
2405 ssl->state++;
2406
2407 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2408 {
2409 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2410 return( ret );
2411 }
2412
2413 SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
2414
2415 return( 0 );
2416}
2417
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002418#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2419 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002420 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2421 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002422static int ssl_write_certificate_verify( ssl_context *ssl )
2423{
Paul Bakkered27a042013-04-18 22:46:23 +02002424 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002425 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002426
2427 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2428
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002429 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2430 {
2431 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2432 return( ret );
2433 }
2434
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002435 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002436 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002437 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002438 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002439 {
2440 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2441 ssl->state++;
2442 return( 0 );
2443 }
2444
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002445 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2446 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002447}
2448#else
2449static int ssl_write_certificate_verify( ssl_context *ssl )
2450{
2451 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2452 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2453 size_t n = 0, offset = 0;
2454 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002455 unsigned char *hash_start = hash;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002456 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002457 unsigned int hashlen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002458
2459 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2460
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002461 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2462 {
2463 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2464 return( ret );
2465 }
2466
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002467 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002468 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002469 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002470 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2471 {
2472 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2473 ssl->state++;
2474 return( 0 );
2475 }
2476
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002477 if( ssl->client_auth == 0 || ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002478 {
2479 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2480 ssl->state++;
2481 return( 0 );
2482 }
2483
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002484 if( ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002485 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00002486 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2487 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002488 }
2489
2490 /*
2491 * Make an RSA signature of the handshake digests
2492 */
Paul Bakker48916f92012-09-16 19:57:18 +00002493 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002494
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002495#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2496 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker926af752012-11-23 13:38:07 +01002497 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002498 {
Paul Bakker926af752012-11-23 13:38:07 +01002499 /*
2500 * digitally-signed struct {
2501 * opaque md5_hash[16];
2502 * opaque sha_hash[20];
2503 * };
2504 *
2505 * md5_hash
2506 * MD5(handshake_messages);
2507 *
2508 * sha_hash
2509 * SHA(handshake_messages);
2510 */
2511 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002512 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002513
2514 /*
2515 * For ECDSA, default hash is SHA-1 only
2516 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002517 if( pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002518 {
2519 hash_start += 16;
2520 hashlen -= 16;
2521 md_alg = POLARSSL_MD_SHA1;
2522 }
Paul Bakker926af752012-11-23 13:38:07 +01002523 }
2524 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002525#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2526 POLARSSL_SSL_PROTO_TLS1_1 */
2527#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2528 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002529 {
2530 /*
2531 * digitally-signed struct {
2532 * opaque handshake_messages[handshake_messages_length];
2533 * };
2534 *
2535 * Taking shortcut here. We assume that the server always allows the
2536 * PRF Hash function and has sent it in the allowed signature
2537 * algorithms list received in the Certificate Request message.
2538 *
2539 * Until we encounter a server that does not, we will take this
2540 * shortcut.
2541 *
2542 * Reason: Otherwise we should have running hashes for SHA512 and SHA224
2543 * in order to satisfy 'weird' needs from the server side.
2544 */
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002545 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2546 POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002547 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002548 md_alg = POLARSSL_MD_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002549 ssl->out_msg[4] = SSL_HASH_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002550 }
2551 else
2552 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002553 md_alg = POLARSSL_MD_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002554 ssl->out_msg[4] = SSL_HASH_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002555 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002556 ssl->out_msg[5] = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002557
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002558 /* Info from md_alg will be used instead */
2559 hashlen = 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002560 offset = 2;
2561 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002562 else
2563#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002564 {
2565 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002566 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002567 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002568
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002569 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002570 ssl->out_msg + 6 + offset, &n,
2571 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002572 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002573 SSL_DEBUG_RET( 1, "pk_sign", ret );
2574 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002575 }
Paul Bakker926af752012-11-23 13:38:07 +01002576
Paul Bakker1ef83d62012-04-11 12:09:53 +00002577 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
2578 ssl->out_msg[5 + offset] = (unsigned char)( n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002579
Paul Bakker1ef83d62012-04-11 12:09:53 +00002580 ssl->out_msglen = 6 + n + offset;
Paul Bakker5121ce52009-01-03 21:22:43 +00002581 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2582 ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY;
2583
2584 ssl->state++;
2585
2586 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2587 {
2588 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2589 return( ret );
2590 }
2591
2592 SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
2593
Paul Bakkered27a042013-04-18 22:46:23 +02002594 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002595}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002596#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2597 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2598 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002599
Paul Bakkera503a632013-08-14 13:48:06 +02002600#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002601static int ssl_parse_new_session_ticket( ssl_context *ssl )
2602{
2603 int ret;
2604 uint32_t lifetime;
2605 size_t ticket_len;
2606 unsigned char *ticket;
2607
2608 SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2609
2610 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2611 {
2612 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2613 return( ret );
2614 }
2615
2616 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2617 {
2618 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2619 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2620 }
2621
2622 /*
2623 * struct {
2624 * uint32 ticket_lifetime_hint;
2625 * opaque ticket<0..2^16-1>;
2626 * } NewSessionTicket;
2627 *
2628 * 0 . 0 handshake message type
2629 * 1 . 3 handshake message length
2630 * 4 . 7 ticket_lifetime_hint
2631 * 8 . 9 ticket_len (n)
2632 * 10 . 9+n ticket content
2633 */
2634 if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
2635 ssl->in_hslen < 10 )
2636 {
2637 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2638 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2639 }
2640
2641 lifetime = ( ssl->in_msg[4] << 24 ) | ( ssl->in_msg[5] << 16 ) |
2642 ( ssl->in_msg[6] << 8 ) | ( ssl->in_msg[7] );
2643
2644 ticket_len = ( ssl->in_msg[8] << 8 ) | ( ssl->in_msg[9] );
2645
2646 if( ticket_len + 10 != ssl->in_hslen )
2647 {
2648 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2649 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2650 }
2651
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002652 SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
2653
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002654 /* We're not waiting for a NewSessionTicket message any more */
2655 ssl->handshake->new_session_ticket = 0;
2656
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002657 /*
2658 * Zero-length ticket means the server changed his mind and doesn't want
2659 * to send a ticket after all, so just forget it
2660 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002661 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002662 return( 0 );
2663
Paul Bakker34617722014-06-13 17:20:13 +02002664 polarssl_zeroize( ssl->session_negotiate->ticket,
2665 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002666 polarssl_free( ssl->session_negotiate->ticket );
2667 ssl->session_negotiate->ticket = NULL;
2668 ssl->session_negotiate->ticket_len = 0;
2669
2670 if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
2671 {
2672 SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
2673 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2674 }
2675
2676 memcpy( ticket, ssl->in_msg + 10, ticket_len );
2677
2678 ssl->session_negotiate->ticket = ticket;
2679 ssl->session_negotiate->ticket_len = ticket_len;
2680 ssl->session_negotiate->ticket_lifetime = lifetime;
2681
2682 /*
2683 * RFC 5077 section 3.4:
2684 * "If the client receives a session ticket from the server, then it
2685 * discards any Session ID that was sent in the ServerHello."
2686 */
2687 SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
2688 ssl->session_negotiate->length = 0;
2689
2690 SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2691
2692 return( 0 );
2693}
Paul Bakkera503a632013-08-14 13:48:06 +02002694#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002695
Paul Bakker5121ce52009-01-03 21:22:43 +00002696/*
Paul Bakker1961b702013-01-25 14:49:24 +01002697 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002698 */
Paul Bakker1961b702013-01-25 14:49:24 +01002699int ssl_handshake_client_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002700{
2701 int ret = 0;
2702
Paul Bakker1961b702013-01-25 14:49:24 +01002703 if( ssl->state == SSL_HANDSHAKE_OVER )
2704 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002705
Paul Bakker1961b702013-01-25 14:49:24 +01002706 SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
2707
2708 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2709 return( ret );
2710
2711 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002712 {
Paul Bakker1961b702013-01-25 14:49:24 +01002713 case SSL_HELLO_REQUEST:
2714 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002715 break;
2716
Paul Bakker1961b702013-01-25 14:49:24 +01002717 /*
2718 * ==> ClientHello
2719 */
2720 case SSL_CLIENT_HELLO:
2721 ret = ssl_write_client_hello( ssl );
2722 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002723
Paul Bakker1961b702013-01-25 14:49:24 +01002724 /*
2725 * <== ServerHello
2726 * Certificate
2727 * ( ServerKeyExchange )
2728 * ( CertificateRequest )
2729 * ServerHelloDone
2730 */
2731 case SSL_SERVER_HELLO:
2732 ret = ssl_parse_server_hello( ssl );
2733 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002734
Paul Bakker1961b702013-01-25 14:49:24 +01002735 case SSL_SERVER_CERTIFICATE:
2736 ret = ssl_parse_certificate( ssl );
2737 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002738
Paul Bakker1961b702013-01-25 14:49:24 +01002739 case SSL_SERVER_KEY_EXCHANGE:
2740 ret = ssl_parse_server_key_exchange( ssl );
2741 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002742
Paul Bakker1961b702013-01-25 14:49:24 +01002743 case SSL_CERTIFICATE_REQUEST:
2744 ret = ssl_parse_certificate_request( ssl );
2745 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002746
Paul Bakker1961b702013-01-25 14:49:24 +01002747 case SSL_SERVER_HELLO_DONE:
2748 ret = ssl_parse_server_hello_done( ssl );
2749 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002750
Paul Bakker1961b702013-01-25 14:49:24 +01002751 /*
2752 * ==> ( Certificate/Alert )
2753 * ClientKeyExchange
2754 * ( CertificateVerify )
2755 * ChangeCipherSpec
2756 * Finished
2757 */
2758 case SSL_CLIENT_CERTIFICATE:
2759 ret = ssl_write_certificate( ssl );
2760 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002761
Paul Bakker1961b702013-01-25 14:49:24 +01002762 case SSL_CLIENT_KEY_EXCHANGE:
2763 ret = ssl_write_client_key_exchange( ssl );
2764 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002765
Paul Bakker1961b702013-01-25 14:49:24 +01002766 case SSL_CERTIFICATE_VERIFY:
2767 ret = ssl_write_certificate_verify( ssl );
2768 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002769
Paul Bakker1961b702013-01-25 14:49:24 +01002770 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2771 ret = ssl_write_change_cipher_spec( ssl );
2772 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002773
Paul Bakker1961b702013-01-25 14:49:24 +01002774 case SSL_CLIENT_FINISHED:
2775 ret = ssl_write_finished( ssl );
2776 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002777
Paul Bakker1961b702013-01-25 14:49:24 +01002778 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002779 * <== ( NewSessionTicket )
2780 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002781 * Finished
2782 */
2783 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002784#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002785 if( ssl->handshake->new_session_ticket != 0 )
2786 ret = ssl_parse_new_session_ticket( ssl );
2787 else
Paul Bakkera503a632013-08-14 13:48:06 +02002788#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002789 ret = ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002790 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002791
Paul Bakker1961b702013-01-25 14:49:24 +01002792 case SSL_SERVER_FINISHED:
2793 ret = ssl_parse_finished( ssl );
2794 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002795
Paul Bakker1961b702013-01-25 14:49:24 +01002796 case SSL_FLUSH_BUFFERS:
2797 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2798 ssl->state = SSL_HANDSHAKE_WRAPUP;
2799 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002800
Paul Bakker1961b702013-01-25 14:49:24 +01002801 case SSL_HANDSHAKE_WRAPUP:
2802 ssl_handshake_wrapup( ssl );
2803 break;
Paul Bakker48916f92012-09-16 19:57:18 +00002804
Paul Bakker1961b702013-01-25 14:49:24 +01002805 default:
2806 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2807 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2808 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002809
2810 return( ret );
2811}
Paul Bakker9af723c2014-05-01 13:03:14 +02002812#endif /* POLARSSL_SSL_CLI_C */