blob: deeee33908bb81ea47b997d0b3b6810f59ffc823 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 client-side functions
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Paul Bakker40e46942009-01-03 21:51:57 +000029#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Paul Bakker40e46942009-01-03 21:51:57 +000031#include "polarssl/debug.h"
32#include "polarssl/ssl.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Rich Evans00ab4702015-02-06 13:43:58 +000034#include <string.h>
35
Paul Bakker7dc4c442014-02-01 22:50:26 +010036#if defined(POLARSSL_PLATFORM_C)
37#include "polarssl/platform.h"
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020038#else
Rich Evans00ab4702015-02-06 13:43:58 +000039#include <stdlib.h>
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020040#define polarssl_malloc malloc
41#define polarssl_free free
42#endif
43
Paul Bakkerfa6a6202013-10-28 18:48:30 +010044#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakkerfa9b1002013-07-03 15:31:03 +020045#include <basetsd.h>
46typedef UINT32 uint32_t;
47#else
48#include <inttypes.h>
49#endif
50
51#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000052#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020053#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Paul Bakker34617722014-06-13 17:20:13 +020055#if defined(POLARSSL_SSL_SESSION_TICKETS)
56/* Implementation that should never be optimized out by the compiler */
57static void polarssl_zeroize( void *v, size_t n ) {
58 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
59}
60#endif
61
Paul Bakker0be444a2013-08-27 21:55:01 +020062#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +010063static void ssl_write_hostname_ext( ssl_context *ssl,
64 unsigned char *buf,
65 size_t *olen )
66{
67 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +010068 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Paul Bakkerd3edc862013-03-20 16:07:17 +010069
70 *olen = 0;
71
Paul Bakker66d5d072014-06-17 16:39:18 +020072 if( ssl->hostname == NULL )
Paul Bakkerd3edc862013-03-20 16:07:17 +010073 return;
74
75 SSL_DEBUG_MSG( 3, ( "client hello, adding server name extension: %s",
76 ssl->hostname ) );
77
Simon Butcherb1e325d2015-10-01 00:24:36 +010078 if( (size_t)(end - p) < ssl->hostname_len + 9 )
79 {
80 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
81 return;
82 }
83
Paul Bakkerd3edc862013-03-20 16:07:17 +010084 /*
85 * struct {
86 * NameType name_type;
87 * select (name_type) {
88 * case host_name: HostName;
89 * } name;
90 * } ServerName;
91 *
92 * enum {
93 * host_name(0), (255)
94 * } NameType;
95 *
96 * opaque HostName<1..2^16-1>;
97 *
98 * struct {
99 * ServerName server_name_list<1..2^16-1>
100 * } ServerNameList;
101 */
102 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME >> 8 ) & 0xFF );
103 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME ) & 0xFF );
104
105 *p++ = (unsigned char)( ( (ssl->hostname_len + 5) >> 8 ) & 0xFF );
106 *p++ = (unsigned char)( ( (ssl->hostname_len + 5) ) & 0xFF );
107
108 *p++ = (unsigned char)( ( (ssl->hostname_len + 3) >> 8 ) & 0xFF );
109 *p++ = (unsigned char)( ( (ssl->hostname_len + 3) ) & 0xFF );
110
111 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME_HOSTNAME ) & 0xFF );
112 *p++ = (unsigned char)( ( ssl->hostname_len >> 8 ) & 0xFF );
113 *p++ = (unsigned char)( ( ssl->hostname_len ) & 0xFF );
114
115 memcpy( p, ssl->hostname, ssl->hostname_len );
116
117 *olen = ssl->hostname_len + 9;
118}
Paul Bakker0be444a2013-08-27 21:55:01 +0200119#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100120
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100121#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100122static void ssl_write_renegotiation_ext( ssl_context *ssl,
123 unsigned char *buf,
124 size_t *olen )
125{
126 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100127 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100128
129 *olen = 0;
130
131 if( ssl->renegotiation != SSL_RENEGOTIATION )
132 return;
133
134 SSL_DEBUG_MSG( 3, ( "client hello, adding renegotiation extension" ) );
135
Simon Butcherb1e325d2015-10-01 00:24:36 +0100136 if( (size_t)(end - p) < 5 + ssl->verify_data_len )
137 {
138 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
139 return;
140 }
141
Paul Bakkerd3edc862013-03-20 16:07:17 +0100142 /*
143 * Secure renegotiation
144 */
145 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
146 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
147
148 *p++ = 0x00;
149 *p++ = ( ssl->verify_data_len + 1 ) & 0xFF;
150 *p++ = ssl->verify_data_len & 0xFF;
151
152 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
153
154 *olen = 5 + ssl->verify_data_len;
155}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100156#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100157
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100158/*
159 * Only if we handle at least one key exchange that needs signatures.
160 */
161#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
162 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100163static void ssl_write_signature_algorithms_ext( ssl_context *ssl,
164 unsigned char *buf,
165 size_t *olen )
166{
167 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100168 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100169 size_t sig_alg_len = 0;
Manuel Pégourié-Gonnard5bfd9682014-06-24 15:18:11 +0200170#if defined(POLARSSL_RSA_C) || defined(POLARSSL_ECDSA_C)
171 unsigned char *sig_alg_list = buf + 6;
172#endif
Paul Bakkerd3edc862013-03-20 16:07:17 +0100173
174 *olen = 0;
175
176 if( ssl->max_minor_ver != SSL_MINOR_VERSION_3 )
177 return;
178
179 SSL_DEBUG_MSG( 3, ( "client hello, adding signature_algorithms extension" ) );
180
Simon Butcherb1e325d2015-10-01 00:24:36 +0100181#if defined(POLARSSL_RSA_C)
182#if defined(POLARSSL_SHA512_C)
183 /* SHA512 + RSA signature, SHA384 + RSA signature */
184 sig_alg_len += 4;
185#endif
186#if defined(POLARSSL_SHA256_C)
187 /* SHA256 + RSA signature, SHA224 + RSA signature */
188 sig_alg_len += 4;
189#endif
190#if defined(POLARSSL_SHA1_C)
191 /* SHA1 + RSA signature */
192 sig_alg_len += 2;
193#endif
194#if defined(POLARSSL_MD5_C)
195 /* MD5 + RSA signature */
196 sig_alg_len += 2;
197#endif
198#endif /* POLARSSL_RSA_C */
199#if defined(POLARSSL_ECDSA_C)
200#if defined(POLARSSL_SHA512_C)
201 /* SHA512 + ECDSA signature, SHA384 + ECDSA signature */
202 sig_alg_len += 4;
203#endif
204#if defined(POLARSSL_SHA256_C)
205 /* SHA256 + ECDSA signature, SHA224 + ECDSA signature */
206 sig_alg_len += 4;
207#endif
208#if defined(POLARSSL_SHA1_C)
209 /* SHA1 + ECDSA signature */
210 sig_alg_len += 2;
211#endif
212#if defined(POLARSSL_MD5_C)
213 /* MD5 + ECDSA signature */
214 sig_alg_len += 2;
215#endif
216#endif /* POLARSSL_ECDSA_C */
217
218 if( end < p || (size_t)( end - p ) < sig_alg_len + 6 )
219 {
220 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
221 return;
222 }
223
Paul Bakkerd3edc862013-03-20 16:07:17 +0100224 /*
225 * Prepare signature_algorithms extension (TLS 1.2)
226 */
Simon Butcherb1e325d2015-10-01 00:24:36 +0100227 sig_alg_len = 0;
228
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200229#if defined(POLARSSL_RSA_C)
Paul Bakker9e36f042013-06-30 14:34:05 +0200230#if defined(POLARSSL_SHA512_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100231 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
232 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
233 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
234 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
235#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200236#if defined(POLARSSL_SHA256_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100237 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
238 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
239 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
240 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
241#endif
242#if defined(POLARSSL_SHA1_C)
243 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
244 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
245#endif
246#if defined(POLARSSL_MD5_C)
247 sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
248 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
249#endif
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200250#endif /* POLARSSL_RSA_C */
251#if defined(POLARSSL_ECDSA_C)
252#if defined(POLARSSL_SHA512_C)
253 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
254 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
255 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
256 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
257#endif
258#if defined(POLARSSL_SHA256_C)
259 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
260 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
261 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
262 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
263#endif
264#if defined(POLARSSL_SHA1_C)
265 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
266 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
267#endif
268#if defined(POLARSSL_MD5_C)
269 sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
270 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
271#endif
272#endif /* POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100273
274 /*
275 * enum {
276 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
277 * sha512(6), (255)
278 * } HashAlgorithm;
279 *
280 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
281 * SignatureAlgorithm;
282 *
283 * struct {
284 * HashAlgorithm hash;
285 * SignatureAlgorithm signature;
286 * } SignatureAndHashAlgorithm;
287 *
288 * SignatureAndHashAlgorithm
289 * supported_signature_algorithms<2..2^16-2>;
290 */
291 *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG >> 8 ) & 0xFF );
292 *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG ) & 0xFF );
293
294 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) >> 8 ) & 0xFF );
295 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) ) & 0xFF );
296
297 *p++ = (unsigned char)( ( sig_alg_len >> 8 ) & 0xFF );
298 *p++ = (unsigned char)( ( sig_alg_len ) & 0xFF );
299
Paul Bakkerd3edc862013-03-20 16:07:17 +0100300 *olen = 6 + sig_alg_len;
301}
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100302#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
303 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100304
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200305#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100306static void ssl_write_supported_elliptic_curves_ext( ssl_context *ssl,
307 unsigned char *buf,
308 size_t *olen )
309{
310 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100311 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Manuel Pégourié-Gonnard8e205fc2014-01-23 17:27:10 +0100312 unsigned char *elliptic_curve_list = p + 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100313 size_t elliptic_curve_len = 0;
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100314 const ecp_curve_info *info;
315#if defined(POLARSSL_SSL_SET_CURVES)
316 const ecp_group_id *grp_id;
Paul Bakker0910f322014-02-06 13:41:18 +0100317#else
318 ((void) ssl);
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100319#endif
Paul Bakkerd3edc862013-03-20 16:07:17 +0100320
321 *olen = 0;
322
323 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_elliptic_curves extension" ) );
324
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100325#if defined(POLARSSL_SSL_SET_CURVES)
326 for( grp_id = ssl->curve_list; *grp_id != POLARSSL_ECP_DP_NONE; grp_id++ )
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200327 {
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100328 info = ecp_curve_info_from_grp_id( *grp_id );
329#else
330 for( info = ecp_curve_list(); info->grp_id != POLARSSL_ECP_DP_NONE; info++ )
331 {
332#endif
Simon Butcherb1e325d2015-10-01 00:24:36 +0100333 elliptic_curve_len += 2;
334 }
335
336 if( end < p || (size_t)( end - p ) < 6 + elliptic_curve_len )
337 {
338 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
339 return;
340 }
341
342 elliptic_curve_len = 0;
343
344#if defined(POLARSSL_SSL_SET_CURVES)
345 for( grp_id = ssl->curve_list; *grp_id != POLARSSL_ECP_DP_NONE; grp_id++ )
346 {
347 info = ecp_curve_info_from_grp_id( *grp_id );
348#else
349 for( info = ecp_curve_list(); info->grp_id != POLARSSL_ECP_DP_NONE; info++ )
350 {
351#endif
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100352
353 elliptic_curve_list[elliptic_curve_len++] = info->tls_id >> 8;
354 elliptic_curve_list[elliptic_curve_len++] = info->tls_id & 0xFF;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200355 }
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200356
357 if( elliptic_curve_len == 0 )
358 return;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100359
360 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES >> 8 ) & 0xFF );
361 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ) & 0xFF );
362
363 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF );
364 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) ) & 0xFF );
365
366 *p++ = (unsigned char)( ( ( elliptic_curve_len ) >> 8 ) & 0xFF );
367 *p++ = (unsigned char)( ( ( elliptic_curve_len ) ) & 0xFF );
368
Paul Bakkerd3edc862013-03-20 16:07:17 +0100369 *olen = 6 + elliptic_curve_len;
370}
371
372static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
373 unsigned char *buf,
374 size_t *olen )
375{
376 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100377 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100378
379 *olen = 0;
380
381 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_point_formats extension" ) );
382
Simon Butcherb1e325d2015-10-01 00:24:36 +0100383 if( end < p || (size_t)( end - p ) < 6 )
384 {
385 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
386 return;
387 }
388
Paul Bakkerd3edc862013-03-20 16:07:17 +0100389 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
390 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
391
392 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100393 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200394
395 *p++ = 1;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100396 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
397
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200398 *olen = 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100399}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200400#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100401
Paul Bakker05decb22013-08-15 13:33:48 +0200402#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200403static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
404 unsigned char *buf,
405 size_t *olen )
406{
407 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100408 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200409
Simon Butcherb1e325d2015-10-01 00:24:36 +0100410 *olen = 0;
411
412 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE )
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200413 return;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200414
415 SSL_DEBUG_MSG( 3, ( "client hello, adding max_fragment_length extension" ) );
416
Simon Butcherb1e325d2015-10-01 00:24:36 +0100417 if( end < p || (size_t)( end - p ) < 5 )
418 {
419 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
420 return;
421 }
422
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200423 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
424 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
425
426 *p++ = 0x00;
427 *p++ = 1;
428
429 *p++ = ssl->mfl_code;
430
431 *olen = 5;
432}
Paul Bakker05decb22013-08-15 13:33:48 +0200433#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200434
Paul Bakker1f2bc622013-08-15 13:45:55 +0200435#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200436static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
437 unsigned char *buf, size_t *olen )
438{
439 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100440 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
441
442 *olen = 0;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200443
444 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200445 return;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200446
447 SSL_DEBUG_MSG( 3, ( "client hello, adding truncated_hmac extension" ) );
448
Simon Butcherb1e325d2015-10-01 00:24:36 +0100449 if( end < p || (size_t)( end - p ) < 4 )
450 {
451 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
452 return;
453 }
454
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200455 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
456 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
457
458 *p++ = 0x00;
459 *p++ = 0x00;
460
461 *olen = 4;
462}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200463#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200464
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100465#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
466static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
467 unsigned char *buf, size_t *olen )
468{
469 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100470 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
471
472 *olen = 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100473
474 if( ssl->encrypt_then_mac == SSL_ETM_DISABLED ||
475 ssl->max_minor_ver == SSL_MINOR_VERSION_0 )
476 {
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100477 return;
478 }
479
480 SSL_DEBUG_MSG( 3, ( "client hello, adding encrypt_then_mac "
481 "extension" ) );
482
Simon Butcherb1e325d2015-10-01 00:24:36 +0100483 if( end < p || (size_t)( end - p ) < 4 )
484 {
485 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
486 return;
487 }
488
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100489 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
490 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
491
492 *p++ = 0x00;
493 *p++ = 0x00;
494
495 *olen = 4;
496}
497#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
498
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200499#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
500static void ssl_write_extended_ms_ext( ssl_context *ssl,
501 unsigned char *buf, size_t *olen )
502{
503 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100504 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
505
506 *olen = 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200507
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200508 if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED ||
509 ssl->max_minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200510 {
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200511 return;
512 }
513
514 SSL_DEBUG_MSG( 3, ( "client hello, adding extended_master_secret "
515 "extension" ) );
516
Simon Butcherb1e325d2015-10-01 00:24:36 +0100517 if( end < p || (size_t)( end - p ) < 4 )
518 {
519 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
520 return;
521 }
522
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200523 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
524 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
525
526 *p++ = 0x00;
527 *p++ = 0x00;
528
529 *olen = 4;
530}
531#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
532
Paul Bakkera503a632013-08-14 13:48:06 +0200533#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200534static void ssl_write_session_ticket_ext( ssl_context *ssl,
535 unsigned char *buf, size_t *olen )
536{
537 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100538 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200539 size_t tlen = ssl->session_negotiate->ticket_len;
540
Simon Butcherb1e325d2015-10-01 00:24:36 +0100541 *olen = 0;
542
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200543 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200544 return;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200545
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200546 SSL_DEBUG_MSG( 3, ( "client hello, adding session ticket extension" ) );
547
Simon Butcherb1e325d2015-10-01 00:24:36 +0100548 if( end < p || (size_t)( end - p ) < 4 + tlen )
549 {
550 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
551 return;
552 }
553
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200554 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
555 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
556
557 *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF );
558 *p++ = (unsigned char)( ( tlen ) & 0xFF );
559
560 *olen = 4;
561
562 if( ssl->session_negotiate->ticket == NULL ||
563 ssl->session_negotiate->ticket_len == 0 )
564 {
565 return;
566 }
567
568 SSL_DEBUG_MSG( 3, ( "sending session ticket of length %d", tlen ) );
569
570 memcpy( p, ssl->session_negotiate->ticket, tlen );
571
572 *olen += tlen;
573}
Paul Bakkera503a632013-08-14 13:48:06 +0200574#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200575
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200576#if defined(POLARSSL_SSL_ALPN)
577static void ssl_write_alpn_ext( ssl_context *ssl,
578 unsigned char *buf, size_t *olen )
579{
580 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100581 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
582 size_t alpnlen = 0;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200583 const char **cur;
584
Simon Butcherb1e325d2015-10-01 00:24:36 +0100585 *olen = 0;
586
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200587 if( ssl->alpn_list == NULL )
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200588 return;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200589
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +0200590 SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200591
Simon Butcherb1e325d2015-10-01 00:24:36 +0100592 for( cur = ssl->alpn_list; *cur != NULL; cur++ )
593 alpnlen += (unsigned char)( strlen( *cur ) & 0xFF ) + 1;
594
595 if( end < p || (size_t)( end - p ) < 6 + alpnlen )
596 {
597 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
598 return;
599 }
600
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200601 *p++ = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
602 *p++ = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
603
604 /*
605 * opaque ProtocolName<1..2^8-1>;
606 *
607 * struct {
608 * ProtocolName protocol_name_list<2..2^16-1>
609 * } ProtocolNameList;
610 */
611
612 /* Skip writing extension and list length for now */
613 p += 4;
614
615 for( cur = ssl->alpn_list; *cur != NULL; cur++ )
616 {
617 *p = (unsigned char)( strlen( *cur ) & 0xFF );
618 memcpy( p + 1, *cur, *p );
619 p += 1 + *p;
620 }
621
622 *olen = p - buf;
623
624 /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
625 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
626 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
627
628 /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
629 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
630 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
631}
632#endif /* POLARSSL_SSL_ALPN */
633
Paul Bakker5121ce52009-01-03 21:22:43 +0000634static int ssl_write_client_hello( ssl_context *ssl )
635{
Paul Bakker23986e52011-04-24 08:57:21 +0000636 int ret;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100637 size_t i, n, olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000638 unsigned char *buf;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200639 unsigned char *p, *q;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200640#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000641 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200642#endif
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200643 const int *ciphersuites;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200644 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000645
646 SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
647
Paul Bakkera9a028e2013-11-21 17:31:06 +0100648 if( ssl->f_rng == NULL )
649 {
650 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
651 return( POLARSSL_ERR_SSL_NO_RNG );
652 }
653
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100654#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +0000655 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100656#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000657 {
Paul Bakker993d11d2012-09-28 15:00:12 +0000658 ssl->major_ver = ssl->min_major_ver;
659 ssl->minor_ver = ssl->min_minor_ver;
Paul Bakker48916f92012-09-16 19:57:18 +0000660 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000661
Paul Bakker490ecc82011-10-06 13:04:09 +0000662 if( ssl->max_major_ver == 0 && ssl->max_minor_ver == 0 )
663 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200664 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
665 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker490ecc82011-10-06 13:04:09 +0000666 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000667
668 /*
669 * 0 . 0 handshake type
670 * 1 . 3 handshake length
671 * 4 . 5 highest version supported
672 * 6 . 9 current UNIX time
673 * 10 . 37 random bytes
674 */
675 buf = ssl->out_msg;
676 p = buf + 4;
677
678 *p++ = (unsigned char) ssl->max_major_ver;
679 *p++ = (unsigned char) ssl->max_minor_ver;
680
681 SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
682 buf[4], buf[5] ) );
683
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200684#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000685 t = time( NULL );
686 *p++ = (unsigned char)( t >> 24 );
687 *p++ = (unsigned char)( t >> 16 );
688 *p++ = (unsigned char)( t >> 8 );
689 *p++ = (unsigned char)( t );
690
691 SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200692#else
693 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
694 return( ret );
695
696 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +0200697#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000698
Paul Bakkera3d195c2011-11-27 21:07:34 +0000699 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
700 return( ret );
701
702 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +0000703
Paul Bakker48916f92012-09-16 19:57:18 +0000704 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000705
706 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 6, 32 );
707
708 /*
709 * 38 . 38 session id length
710 * 39 . 39+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +0000711 * 40+n . 41+n ciphersuitelist length
712 * 42+n . .. ciphersuitelist
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000713 * .. . .. compression methods length
714 * .. . .. compression methods
715 * .. . .. extensions length
716 * .. . .. extensions
Paul Bakker5121ce52009-01-03 21:22:43 +0000717 */
Paul Bakker48916f92012-09-16 19:57:18 +0000718 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000719
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100720 if( n < 16 || n > 32 ||
721#if defined(POLARSSL_SSL_RENEGOTIATION)
722 ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
723#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000724 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200725 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000726 n = 0;
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200727 }
728
Paul Bakkera503a632013-08-14 13:48:06 +0200729#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200730 /*
731 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
732 * generate and include a Session ID in the TLS ClientHello."
733 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100734#if defined(POLARSSL_SSL_RENEGOTIATION)
735 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000736#endif
Manuel Pégourié-Gonnard51bccd32015-03-10 16:09:08 +0000737 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000738 if( ssl->session_negotiate->ticket != NULL &&
739 ssl->session_negotiate->ticket_len != 0 )
740 {
741 ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200742
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000743 if( ret != 0 )
744 return( ret );
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200745
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000746 ssl->session_negotiate->length = n = 32;
747 }
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200748 }
Paul Bakkera503a632013-08-14 13:48:06 +0200749#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000750
751 *p++ = (unsigned char) n;
752
753 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000754 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
756 SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
757 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
758
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200759 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Paul Bakker2fbefde2013-06-29 16:01:15 +0200760 n = 0;
761 q = p;
762
763 // Skip writing ciphersuite length for now
764 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000765
Paul Bakker2fbefde2013-06-29 16:01:15 +0200766 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000767 {
Paul Bakker2fbefde2013-06-29 16:01:15 +0200768 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
769
770 if( ciphersuite_info == NULL )
771 continue;
772
773 if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
774 ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
775 continue;
776
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100777 if( ssl->arc4_disabled == SSL_ARC4_DISABLED &&
778 ciphersuite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
779 continue;
780
Paul Bakkere3166ce2011-01-27 17:40:50 +0000781 SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200782 ciphersuites[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000783
Paul Bakker2fbefde2013-06-29 16:01:15 +0200784 n++;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200785 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
786 *p++ = (unsigned char)( ciphersuites[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000787 }
788
Manuel Pégourié-Gonnard5d9cde22015-01-22 10:49:41 +0000789 /*
790 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
791 */
792#if defined(POLARSSL_SSL_RENEGOTIATION)
793 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
794#endif
795 {
796 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
797 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
798 n++;
799 }
800
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200801 /* Some versions of OpenSSL don't handle it correctly if not at end */
802#if defined(POLARSSL_SSL_FALLBACK_SCSV)
803 if( ssl->fallback == SSL_IS_FALLBACK )
804 {
805 SSL_DEBUG_MSG( 3, ( "adding FALLBACK_SCSV" ) );
806 *p++ = (unsigned char)( SSL_FALLBACK_SCSV >> 8 );
807 *p++ = (unsigned char)( SSL_FALLBACK_SCSV );
808 n++;
809 }
810#endif
811
Paul Bakker2fbefde2013-06-29 16:01:15 +0200812 *q++ = (unsigned char)( n >> 7 );
813 *q++ = (unsigned char)( n << 1 );
814
815 SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
816
817
Paul Bakker2770fbd2012-07-03 13:30:23 +0000818#if defined(POLARSSL_ZLIB_SUPPORT)
819 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
820 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000821 SSL_COMPRESS_DEFLATE, SSL_COMPRESS_NULL ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000822
823 *p++ = 2;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000824 *p++ = SSL_COMPRESS_DEFLATE;
Paul Bakker48916f92012-09-16 19:57:18 +0000825 *p++ = SSL_COMPRESS_NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000826#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000827 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000828 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d", SSL_COMPRESS_NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000829
830 *p++ = 1;
831 *p++ = SSL_COMPRESS_NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200832#endif /* POLARSSL_ZLIB_SUPPORT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000833
Paul Bakkerd3edc862013-03-20 16:07:17 +0100834 // First write extensions, then the total length
835 //
Paul Bakker0be444a2013-08-27 21:55:01 +0200836#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100837 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
838 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +0200839#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000840
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100841#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100842 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
843 ext_len += olen;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100844#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000845
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100846#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
847 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100848 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
849 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200850#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000851
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200852#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100853 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
854 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100855
Paul Bakkerd3edc862013-03-20 16:07:17 +0100856 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
857 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100858#endif
859
Paul Bakker05decb22013-08-15 13:33:48 +0200860#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200861 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
862 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +0200863#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200864
Paul Bakker1f2bc622013-08-15 13:45:55 +0200865#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200866 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
867 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200868#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200869
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100870#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
871 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
872 ext_len += olen;
873#endif
874
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200875#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
876 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
877 ext_len += olen;
878#endif
879
Paul Bakkera503a632013-08-14 13:48:06 +0200880#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200881 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
882 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +0200883#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200884
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200885#if defined(POLARSSL_SSL_ALPN)
886 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
887 ext_len += olen;
888#endif
889
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +0100890 /* olen unused if all extensions are disabled */
891 ((void) olen);
892
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000893 SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
894 ext_len ) );
895
Paul Bakkera7036632014-04-30 10:15:38 +0200896 if( ext_len > 0 )
897 {
898 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
899 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
900 p += ext_len;
901 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100902
Paul Bakker5121ce52009-01-03 21:22:43 +0000903 ssl->out_msglen = p - buf;
904 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
905 ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
906
907 ssl->state++;
908
909 if( ( ret = ssl_write_record( ssl ) ) != 0 )
910 {
911 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
912 return( ret );
913 }
914
915 SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
916
917 return( 0 );
918}
919
Paul Bakker48916f92012-09-16 19:57:18 +0000920static int ssl_parse_renegotiation_info( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200921 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000922 size_t len )
923{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000924 int ret;
925
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100926#if defined(POLARSSL_SSL_RENEGOTIATION)
927 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +0000928 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100929 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000930 if( len != 1 + ssl->verify_data_len * 2 ||
931 buf[0] != ssl->verify_data_len * 2 ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100932 safer_memcmp( buf + 1,
933 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
934 safer_memcmp( buf + 1 + ssl->verify_data_len,
935 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000936 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100937 SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000938
939 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
940 return( ret );
941
Paul Bakker48916f92012-09-16 19:57:18 +0000942 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
943 }
944 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100945 else
946#endif /* POLARSSL_SSL_RENEGOTIATION */
947 {
948 if( len != 1 || buf[0] != 0x00 )
949 {
950 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
951
952 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
953 return( ret );
954
955 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
956 }
957
958 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
959 }
Paul Bakker48916f92012-09-16 19:57:18 +0000960
961 return( 0 );
962}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200963
Paul Bakker05decb22013-08-15 13:33:48 +0200964#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200965static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200966 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200967 size_t len )
968{
969 /*
970 * server should use the extension only if we did,
971 * and if so the server's value should match ours (and len is always 1)
972 */
973 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
974 len != 1 ||
975 buf[0] != ssl->mfl_code )
976 {
977 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
978 }
979
980 return( 0 );
981}
Paul Bakker05decb22013-08-15 13:33:48 +0200982#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000983
Paul Bakker1f2bc622013-08-15 13:45:55 +0200984#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200985static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
986 const unsigned char *buf,
987 size_t len )
988{
989 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
990 len != 0 )
991 {
992 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
993 }
994
995 ((void) buf);
996
997 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
998
999 return( 0 );
1000}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001001#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001002
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001003#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1004static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
1005 const unsigned char *buf,
1006 size_t len )
1007{
1008 if( ssl->encrypt_then_mac == SSL_ETM_DISABLED ||
1009 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
1010 len != 0 )
1011 {
1012 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1013 }
1014
1015 ((void) buf);
1016
1017 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
1018
1019 return( 0 );
1020}
1021#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1022
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001023#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1024static int ssl_parse_extended_ms_ext( ssl_context *ssl,
1025 const unsigned char *buf,
1026 size_t len )
1027{
1028 if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +02001029 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001030 len != 0 )
1031 {
1032 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1033 }
1034
1035 ((void) buf);
1036
1037 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
1038
1039 return( 0 );
1040}
1041#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1042
Paul Bakkera503a632013-08-14 13:48:06 +02001043#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001044static int ssl_parse_session_ticket_ext( ssl_context *ssl,
1045 const unsigned char *buf,
1046 size_t len )
1047{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001048 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ||
1049 len != 0 )
1050 {
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001051 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001052 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001053
1054 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02001055
1056 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001057
1058 return( 0 );
1059}
Paul Bakkera503a632013-08-14 13:48:06 +02001060#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001061
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001062#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001063static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
1064 const unsigned char *buf,
1065 size_t len )
1066{
1067 size_t list_size;
1068 const unsigned char *p;
1069
1070 list_size = buf[0];
1071 if( list_size + 1 != len )
1072 {
1073 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1074 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1075 }
1076
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +02001077 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001078 while( list_size > 0 )
1079 {
1080 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
1081 p[0] == POLARSSL_ECP_PF_COMPRESSED )
1082 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +02001083 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001084 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
1085 return( 0 );
1086 }
1087
1088 list_size--;
1089 p++;
1090 }
1091
Manuel Pégourié-Gonnard5c1f0322014-06-23 14:24:43 +02001092 SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
1093 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001094}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001095#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001096
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001097#if defined(POLARSSL_SSL_ALPN)
1098static int ssl_parse_alpn_ext( ssl_context *ssl,
1099 const unsigned char *buf, size_t len )
1100{
1101 size_t list_len, name_len;
1102 const char **p;
1103
1104 /* If we didn't send it, the server shouldn't send it */
1105 if( ssl->alpn_list == NULL )
1106 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1107
1108 /*
1109 * opaque ProtocolName<1..2^8-1>;
1110 *
1111 * struct {
1112 * ProtocolName protocol_name_list<2..2^16-1>
1113 * } ProtocolNameList;
1114 *
1115 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
1116 */
1117
1118 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
1119 if( len < 4 )
1120 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1121
1122 list_len = ( buf[0] << 8 ) | buf[1];
1123 if( list_len != len - 2 )
1124 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1125
1126 name_len = buf[2];
1127 if( name_len != list_len - 1 )
1128 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1129
1130 /* Check that the server chosen protocol was in our list and save it */
1131 for( p = ssl->alpn_list; *p != NULL; p++ )
1132 {
1133 if( name_len == strlen( *p ) &&
1134 memcmp( buf + 3, *p, name_len ) == 0 )
1135 {
1136 ssl->alpn_chosen = *p;
1137 return( 0 );
1138 }
1139 }
1140
1141 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1142}
1143#endif /* POLARSSL_SSL_ALPN */
1144
Paul Bakker5121ce52009-01-03 21:22:43 +00001145static int ssl_parse_server_hello( ssl_context *ssl )
1146{
Paul Bakker2770fbd2012-07-03 13:30:23 +00001147 int ret, i, comp;
Paul Bakker23986e52011-04-24 08:57:21 +00001148 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001149 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001150 unsigned char *buf, *ext;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001151#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001152 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001153#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001154 int handshake_failure = 0;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001155 const ssl_ciphersuite_t *suite_info;
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001156#if defined(POLARSSL_DEBUG_C)
1157 uint32_t t;
1158#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001159
1160 SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
1161
1162 /*
1163 * 0 . 0 handshake type
1164 * 1 . 3 handshake length
1165 * 4 . 5 protocol version
1166 * 6 . 9 UNIX time()
1167 * 10 . 37 random bytes
1168 */
1169 buf = ssl->in_msg;
1170
1171 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1172 {
1173 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1174 return( ret );
1175 }
1176
1177 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1178 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001179#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001180 if( ssl->renegotiation == SSL_RENEGOTIATION )
1181 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001182 ssl->renego_records_seen++;
1183
1184 if( ssl->renego_max_records >= 0 &&
1185 ssl->renego_records_seen > ssl->renego_max_records )
1186 {
1187 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
1188 "but not honored by server" ) );
1189 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1190 }
1191
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001192 SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
1193 return( POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
1194 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001195#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001196
Paul Bakker5121ce52009-01-03 21:22:43 +00001197 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001198 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001199 }
1200
1201 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1202 buf[4], buf[5] ) );
1203
1204 if( ssl->in_hslen < 42 ||
1205 buf[0] != SSL_HS_SERVER_HELLO ||
1206 buf[4] != SSL_MAJOR_VERSION_3 )
1207 {
1208 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001209 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001210 }
1211
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001212 if( buf[5] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00001213 {
1214 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001215 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001216 }
1217
1218 ssl->minor_ver = buf[5];
1219
Paul Bakker1d29fb52012-09-28 13:28:45 +00001220 if( ssl->minor_ver < ssl->min_minor_ver )
1221 {
1222 SSL_DEBUG_MSG( 1, ( "server only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001223 " [%d:%d] < [%d:%d]", ssl->major_ver,
1224 ssl->minor_ver, buf[4], buf[5] ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001225
1226 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1227 SSL_ALERT_MSG_PROTOCOL_VERSION );
1228
1229 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1230 }
1231
Paul Bakker1504af52012-02-11 16:17:43 +00001232#if defined(POLARSSL_DEBUG_C)
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001233 t = ( (uint32_t) buf[6] << 24 )
1234 | ( (uint32_t) buf[7] << 16 )
1235 | ( (uint32_t) buf[8] << 8 )
1236 | ( (uint32_t) buf[9] );
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001237 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakker87e5cda2012-01-14 18:14:15 +00001238#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001239
Paul Bakker48916f92012-09-16 19:57:18 +00001240 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001241
1242 n = buf[38];
1243
Paul Bakker5121ce52009-01-03 21:22:43 +00001244 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1245
Paul Bakker48916f92012-09-16 19:57:18 +00001246 if( n > 32 )
1247 {
1248 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1249 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1250 }
1251
Paul Bakker5121ce52009-01-03 21:22:43 +00001252 /*
1253 * 38 . 38 session id length
1254 * 39 . 38+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +00001255 * 39+n . 40+n chosen ciphersuite
Paul Bakker5121ce52009-01-03 21:22:43 +00001256 * 41+n . 41+n chosen compression alg.
1257 * 42+n . 43+n extensions length
1258 * 44+n . 44+n+m extensions
1259 */
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001260 if( ssl->in_hslen > 43 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 {
1262 ext_len = ( ( buf[42 + n] << 8 )
Paul Bakker48916f92012-09-16 19:57:18 +00001263 | ( buf[43 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001264
Paul Bakker48916f92012-09-16 19:57:18 +00001265 if( ( ext_len > 0 && ext_len < 4 ) ||
1266 ssl->in_hslen != 44 + n + ext_len )
1267 {
1268 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1269 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1270 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001271 }
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001272 else if( ssl->in_hslen == 42 + n )
1273 {
1274 ext_len = 0;
1275 }
1276 else
1277 {
1278 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1279 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1280 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001281
1282 i = ( buf[39 + n] << 8 ) | buf[40 + n];
Paul Bakker2770fbd2012-07-03 13:30:23 +00001283 comp = buf[41 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001284
Paul Bakker380da532012-04-18 16:10:25 +00001285 /*
1286 * Initialize update checksum functions
1287 */
Paul Bakker68884e32013-01-07 18:20:04 +01001288 ssl->transform_negotiate->ciphersuite_info = ssl_ciphersuite_from_id( i );
1289
1290 if( ssl->transform_negotiate->ciphersuite_info == NULL )
1291 {
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001292 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001293 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1294 }
Paul Bakker380da532012-04-18 16:10:25 +00001295
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001296 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1297
Paul Bakker5121ce52009-01-03 21:22:43 +00001298 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1299 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1300
1301 /*
1302 * Check if the session can be resumed
1303 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001304 if( ssl->handshake->resume == 0 || n == 0 ||
1305#if defined(POLARSSL_SSL_RENEGOTIATION)
1306 ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
1307#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001308 ssl->session_negotiate->ciphersuite != i ||
1309 ssl->session_negotiate->compression != comp ||
1310 ssl->session_negotiate->length != n ||
1311 memcmp( ssl->session_negotiate->id, buf + 39, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001312 {
1313 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001314 ssl->handshake->resume = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001315#if defined(POLARSSL_HAVE_TIME)
Paul Bakker48916f92012-09-16 19:57:18 +00001316 ssl->session_negotiate->start = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001317#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001318 ssl->session_negotiate->ciphersuite = i;
1319 ssl->session_negotiate->compression = comp;
1320 ssl->session_negotiate->length = n;
1321 memcpy( ssl->session_negotiate->id, buf + 39, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001322 }
1323 else
1324 {
1325 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001326
1327 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1328 {
1329 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1330 return( ret );
1331 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001332 }
1333
1334 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001335 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001336
Paul Bakkere3166ce2011-01-27 17:40:50 +00001337 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001338 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[41 + n] ) );
1339
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001340 suite_info = ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite );
1341 if( suite_info == NULL ||
1342 ( ssl->arc4_disabled &&
1343 suite_info->cipher == POLARSSL_CIPHER_ARC4_128 ) )
1344 {
1345 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1346 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1347 }
1348
1349
Paul Bakker5121ce52009-01-03 21:22:43 +00001350 i = 0;
1351 while( 1 )
1352 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001353 if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 {
1355 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001356 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 }
1358
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001359 if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
1360 ssl->session_negotiate->ciphersuite )
1361 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001362 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001363 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001364 }
1365
Paul Bakker2770fbd2012-07-03 13:30:23 +00001366 if( comp != SSL_COMPRESS_NULL
1367#if defined(POLARSSL_ZLIB_SUPPORT)
1368 && comp != SSL_COMPRESS_DEFLATE
1369#endif
1370 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001371 {
1372 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001373 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001374 }
Paul Bakker48916f92012-09-16 19:57:18 +00001375 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001376
Paul Bakker48916f92012-09-16 19:57:18 +00001377 ext = buf + 44 + n;
1378
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001379 SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
1380
Paul Bakker48916f92012-09-16 19:57:18 +00001381 while( ext_len )
1382 {
1383 unsigned int ext_id = ( ( ext[0] << 8 )
1384 | ( ext[1] ) );
1385 unsigned int ext_size = ( ( ext[2] << 8 )
1386 | ( ext[3] ) );
1387
1388 if( ext_size + 4 > ext_len )
1389 {
1390 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1391 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1392 }
1393
1394 switch( ext_id )
1395 {
1396 case TLS_EXT_RENEGOTIATION_INFO:
1397 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001398#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001399 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001400#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001401
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001402 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1403 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001404 return( ret );
1405
1406 break;
1407
Paul Bakker05decb22013-08-15 13:33:48 +02001408#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001409 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1410 SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
1411
1412 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1413 ext + 4, ext_size ) ) != 0 )
1414 {
1415 return( ret );
1416 }
1417
1418 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001419#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001420
Paul Bakker1f2bc622013-08-15 13:45:55 +02001421#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001422 case TLS_EXT_TRUNCATED_HMAC:
1423 SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
1424
1425 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1426 ext + 4, ext_size ) ) != 0 )
1427 {
1428 return( ret );
1429 }
1430
1431 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001432#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001433
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001434#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1435 case TLS_EXT_ENCRYPT_THEN_MAC:
1436 SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
1437
1438 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
1439 ext + 4, ext_size ) ) != 0 )
1440 {
1441 return( ret );
1442 }
1443
1444 break;
1445#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1446
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001447#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1448 case TLS_EXT_EXTENDED_MASTER_SECRET:
1449 SSL_DEBUG_MSG( 3, ( "found extended_master_secret extension" ) );
1450
1451 if( ( ret = ssl_parse_extended_ms_ext( ssl,
1452 ext + 4, ext_size ) ) != 0 )
1453 {
1454 return( ret );
1455 }
1456
1457 break;
1458#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1459
Paul Bakkera503a632013-08-14 13:48:06 +02001460#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001461 case TLS_EXT_SESSION_TICKET:
1462 SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
1463
1464 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1465 ext + 4, ext_size ) ) != 0 )
1466 {
1467 return( ret );
1468 }
1469
1470 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001471#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001472
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001473#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001474 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1475 SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
1476
1477 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1478 ext + 4, ext_size ) ) != 0 )
1479 {
1480 return( ret );
1481 }
1482
1483 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001484#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001485
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001486#if defined(POLARSSL_SSL_ALPN)
1487 case TLS_EXT_ALPN:
1488 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1489
1490 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1491 return( ret );
1492
1493 break;
1494#endif /* POLARSSL_SSL_ALPN */
1495
Paul Bakker48916f92012-09-16 19:57:18 +00001496 default:
1497 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1498 ext_id ) );
1499 }
1500
1501 ext_len -= 4 + ext_size;
1502 ext += 4 + ext_size;
1503
1504 if( ext_len > 0 && ext_len < 4 )
1505 {
1506 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1507 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1508 }
1509 }
1510
1511 /*
1512 * Renegotiation security checks
1513 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001514 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1515 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001516 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001517 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1518 handshake_failure = 1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001519 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001520#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001521 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1522 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1523 renegotiation_info_seen == 0 )
1524 {
1525 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1526 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001527 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001528 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1529 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1530 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001531 {
1532 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001533 handshake_failure = 1;
1534 }
1535 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1536 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1537 renegotiation_info_seen == 1 )
1538 {
1539 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1540 handshake_failure = 1;
1541 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001542#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001543
1544 if( handshake_failure == 1 )
1545 {
1546 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1547 return( ret );
1548
Paul Bakker48916f92012-09-16 19:57:18 +00001549 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1550 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001551
1552 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1553
1554 return( 0 );
1555}
1556
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001557#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1558 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001559static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
1560 unsigned char *end )
1561{
1562 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1563
Paul Bakker29e1f122013-04-16 13:07:56 +02001564 /*
1565 * Ephemeral DH parameters:
1566 *
1567 * struct {
1568 * opaque dh_p<1..2^16-1>;
1569 * opaque dh_g<1..2^16-1>;
1570 * opaque dh_Ys<1..2^16-1>;
1571 * } ServerDHParams;
1572 */
1573 if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1574 {
1575 SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
1576 return( ret );
1577 }
1578
Manuel Pégourié-Gonnard9ea1b232015-06-29 15:27:52 +02001579 if( ssl->handshake->dhm_ctx.len < SSL_MIN_DHM_BYTES ||
Paul Bakker29e1f122013-04-16 13:07:56 +02001580 ssl->handshake->dhm_ctx.len > 512 )
1581 {
1582 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
1583 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1584 }
1585
1586 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1587 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1588 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001589
1590 return( ret );
1591}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001592#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1593 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001594
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001595#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001596 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001597 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1598 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1599 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1600static int ssl_check_server_ecdh_params( const ssl_context *ssl )
1601{
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001602 const ecp_curve_info *curve_info;
1603
1604 curve_info = ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
1605 if( curve_info == NULL )
1606 {
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001607 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1608 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001609 }
1610
1611 SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001612
Manuel Pégourié-Gonnard29f777e2015-04-03 17:26:50 +02001613#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001614 if( ! ssl_curve_is_acceptable( ssl, ssl->handshake->ecdh_ctx.grp.id ) )
1615#else
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001616 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1617 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001618#endif
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001619 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001620
1621 SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
1622
1623 return( 0 );
1624}
Paul Bakker9af723c2014-05-01 13:03:14 +02001625#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1626 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1627 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1628 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1629 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001630
1631#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1632 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001633 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001634static int ssl_parse_server_ecdh_params( ssl_context *ssl,
1635 unsigned char **p,
1636 unsigned char *end )
1637{
1638 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1639
Paul Bakker29e1f122013-04-16 13:07:56 +02001640 /*
1641 * Ephemeral ECDH parameters:
1642 *
1643 * struct {
1644 * ECParameters curve_params;
1645 * ECPoint public;
1646 * } ServerECDHParams;
1647 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001648 if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
1649 (const unsigned char **) p, end ) ) != 0 )
1650 {
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001651 SSL_DEBUG_RET( 1, ( "ecdh_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001652 return( ret );
1653 }
1654
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001655 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001656 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001657 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001658 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1659 }
1660
Paul Bakker29e1f122013-04-16 13:07:56 +02001661 return( ret );
1662}
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001663#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001664 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1665 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001666
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001667#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001668static int ssl_parse_server_psk_hint( ssl_context *ssl,
1669 unsigned char **p,
1670 unsigned char *end )
1671{
1672 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001673 size_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001674 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001675
1676 /*
1677 * PSK parameters:
1678 *
1679 * opaque psk_identity_hint<0..2^16-1>;
1680 */
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001681 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001682 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001683
1684 if( (*p) + len > end )
1685 {
1686 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
1687 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1688 }
1689
1690 // TODO: Retrieve PSK identity hint and callback to app
1691 //
1692 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001693 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001694
1695 return( ret );
1696}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001697#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001698
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001699#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1700 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1701/*
1702 * Generate a pre-master secret and encrypt it with the server's RSA key
1703 */
1704static int ssl_write_encrypted_pms( ssl_context *ssl,
1705 size_t offset, size_t *olen,
1706 size_t pms_offset )
1707{
1708 int ret;
1709 size_t len_bytes = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 0 : 2;
1710 unsigned char *p = ssl->handshake->premaster + pms_offset;
1711
Manuel Pégourié-Gonnard65125542015-08-27 16:37:35 +02001712 if( offset + len_bytes > SSL_MAX_CONTENT_LEN )
1713 {
1714 SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
1715 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1716 }
1717
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001718 /*
1719 * Generate (part of) the pre-master as
1720 * struct {
1721 * ProtocolVersion client_version;
1722 * opaque random[46];
1723 * } PreMasterSecret;
1724 */
1725 p[0] = (unsigned char) ssl->max_major_ver;
1726 p[1] = (unsigned char) ssl->max_minor_ver;
1727
1728 if( ( ret = ssl->f_rng( ssl->p_rng, p + 2, 46 ) ) != 0 )
1729 {
1730 SSL_DEBUG_RET( 1, "f_rng", ret );
1731 return( ret );
1732 }
1733
1734 ssl->handshake->pmslen = 48;
1735
Manuel Pégourié-Gonnardbb564e02015-09-03 10:44:32 +02001736 if( ssl->session_negotiate->peer_cert == NULL )
1737 {
1738 SSL_DEBUG_MSG( 2, ( "certificate required" ) );
1739 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1740 }
1741
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001742 /*
1743 * Now write it out, encrypted
1744 */
1745 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1746 POLARSSL_PK_RSA ) )
1747 {
1748 SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1749 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1750 }
1751
1752 if( ( ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
1753 p, ssl->handshake->pmslen,
1754 ssl->out_msg + offset + len_bytes, olen,
1755 SSL_MAX_CONTENT_LEN - offset - len_bytes,
1756 ssl->f_rng, ssl->p_rng ) ) != 0 )
1757 {
1758 SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
1759 return( ret );
1760 }
1761
1762#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1763 defined(POLARSSL_SSL_PROTO_TLS1_2)
1764 if( len_bytes == 2 )
1765 {
1766 ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
1767 ssl->out_msg[offset+1] = (unsigned char)( *olen );
1768 *olen += 2;
1769 }
1770#endif
1771
1772 return( 0 );
1773}
1774#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
1775 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001776
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001777#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001778#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001779 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1780 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001781static int ssl_parse_signature_algorithm( ssl_context *ssl,
1782 unsigned char **p,
1783 unsigned char *end,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001784 md_type_t *md_alg,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001785 pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02001786{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001787 ((void) ssl);
Paul Bakker29e1f122013-04-16 13:07:56 +02001788 *md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001789 *pk_alg = POLARSSL_PK_NONE;
1790
1791 /* Only in TLS 1.2 */
1792 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1793 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001794 return( 0 );
1795 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001796
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001797 if( (*p) + 2 > end )
Paul Bakker29e1f122013-04-16 13:07:56 +02001798 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1799
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001800 /*
1801 * Get hash algorithm
1802 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001803 if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001804 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001805 SSL_DEBUG_MSG( 2, ( "Server used unsupported "
1806 "HashAlgorithm %d", *(p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001807 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1808 }
1809
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001810 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001811 * Get signature algorithm
1812 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001813 if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001814 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001815 SSL_DEBUG_MSG( 2, ( "server used unsupported "
1816 "SignatureAlgorithm %d", (*p)[1] ) );
1817 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001818 }
1819
1820 SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
1821 SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
1822 *p += 2;
1823
1824 return( 0 );
1825}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001826#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001827 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1828 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001829#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001830
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001831
1832#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1833 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1834static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
1835{
1836 int ret;
1837 const ecp_keypair *peer_key;
1838
Manuel Pégourié-Gonnardbb564e02015-09-03 10:44:32 +02001839 if( ssl->session_negotiate->peer_cert == NULL )
1840 {
1841 SSL_DEBUG_MSG( 2, ( "certificate required" ) );
1842 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1843 }
1844
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001845 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1846 POLARSSL_PK_ECKEY ) )
1847 {
1848 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
1849 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1850 }
1851
1852 peer_key = pk_ec( ssl->session_negotiate->peer_cert->pk );
1853
1854 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
1855 POLARSSL_ECDH_THEIRS ) ) != 0 )
1856 {
1857 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
1858 return( ret );
1859 }
1860
1861 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1862 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001863 SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001864 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
1865 }
1866
1867 return( ret );
1868}
1869#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1870 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1871
Paul Bakker41c83d32013-03-20 14:39:14 +01001872static int ssl_parse_server_key_exchange( ssl_context *ssl )
1873{
Paul Bakker23986e52011-04-24 08:57:21 +00001874 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001875 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001876 unsigned char *p, *end;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001877#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001878 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1879 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001880 size_t sig_len, params_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001881 unsigned char hash[64];
Paul Bakkerc70b9822013-04-07 22:00:46 +02001882 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001883 size_t hashlen;
1884 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001885#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001886
1887 SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
1888
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001889#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001890 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001891 {
1892 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1893 ssl->state++;
1894 return( 0 );
1895 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001896 ((void) p);
1897 ((void) end);
1898#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001899
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001900#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1901 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1902 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
1903 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
1904 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001905 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
1906 {
1907 SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
1908 return( ret );
1909 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001910
1911 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1912 ssl->state++;
1913 return( 0 );
1914 }
1915 ((void) p);
1916 ((void) end);
Paul Bakker9af723c2014-05-01 13:03:14 +02001917#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1918 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001919
Paul Bakker5121ce52009-01-03 21:22:43 +00001920 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1921 {
1922 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1923 return( ret );
1924 }
1925
1926 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1927 {
1928 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001929 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001930 }
1931
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001932 /*
1933 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
1934 * doesn't use a psk_identity_hint
1935 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001936 if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
1937 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001938 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1939 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02001940 {
1941 ssl->record_read = 1;
1942 goto exit;
1943 }
1944
1945 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1946 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001947 }
1948
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001949 p = ssl->in_msg + 4;
1950 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001951 SSL_DEBUG_BUF( 3, "server key exchange", p, ssl->in_hslen - 4 );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001952
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001953#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1954 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1955 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1956 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1957 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1958 {
1959 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1960 {
1961 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1962 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1963 }
1964 } /* FALLTROUGH */
1965#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
1966
1967#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
1968 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1969 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1970 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1971 ; /* nothing more to do */
1972 else
1973#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
1974 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
1975#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1976 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1977 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1978 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001979 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001980 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001981 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001982 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001983 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1984 }
1985 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001986 else
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001987#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1988 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001989#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001990 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001991 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1992 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001993 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001994 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001995 {
1996 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
1997 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001998 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1999 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2000 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002001 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002002 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002003#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002004 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002005 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002006 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002007 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002008 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002009 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002010
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002011#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002012 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2013 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02002014 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002015 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2016 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002017 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002018 params_len = p - ( ssl->in_msg + 4 );
2019
Paul Bakker29e1f122013-04-16 13:07:56 +02002020 /*
2021 * Handle the digitally-signed structure
2022 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002023#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2024 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002025 {
Paul Bakker9659dae2013-08-28 16:21:34 +02002026 if( ssl_parse_signature_algorithm( ssl, &p, end,
2027 &md_alg, &pk_alg ) != 0 )
2028 {
2029 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2030 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2031 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002032
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002033 if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002034 {
2035 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2036 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2037 }
2038 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002039 else
Paul Bakker9af723c2014-05-01 13:03:14 +02002040#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002041#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2042 defined(POLARSSL_SSL_PROTO_TLS1_1)
2043 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002044 {
2045 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002046
Paul Bakker9659dae2013-08-28 16:21:34 +02002047 /* Default hash for ECDSA is SHA-1 */
2048 if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
2049 md_alg = POLARSSL_MD_SHA1;
2050 }
2051 else
2052#endif
2053 {
2054 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002055 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker9659dae2013-08-28 16:21:34 +02002056 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002057
2058 /*
2059 * Read signature
2060 */
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002061 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002062 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002063
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002064 if( end != p + sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002065 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002066 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01002067 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2068 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002069
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002070 SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002071
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002072 /*
2073 * Compute the hash that has been signed
2074 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002075#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2076 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002077 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002078 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002079 md5_context md5;
2080 sha1_context sha1;
2081
Paul Bakker5b4af392014-06-26 12:09:34 +02002082 md5_init( &md5 );
2083 sha1_init( &sha1 );
2084
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002085 hashlen = 36;
2086
Paul Bakker29e1f122013-04-16 13:07:56 +02002087 /*
2088 * digitally-signed struct {
2089 * opaque md5_hash[16];
2090 * opaque sha_hash[20];
2091 * };
2092 *
2093 * md5_hash
2094 * MD5(ClientHello.random + ServerHello.random
2095 * + ServerParams);
2096 * sha_hash
2097 * SHA(ClientHello.random + ServerHello.random
2098 * + ServerParams);
2099 */
Paul Bakker29e1f122013-04-16 13:07:56 +02002100 md5_starts( &md5 );
2101 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002102 md5_update( &md5, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002103 md5_finish( &md5, hash );
2104
2105 sha1_starts( &sha1 );
2106 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002107 sha1_update( &sha1, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002108 sha1_finish( &sha1, hash + 16 );
Paul Bakker5b4af392014-06-26 12:09:34 +02002109
2110 md5_free( &md5 );
2111 sha1_free( &sha1 );
Paul Bakker29e1f122013-04-16 13:07:56 +02002112 }
2113 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002114#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2115 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002116#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2117 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002118 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002119 {
2120 md_context_t ctx;
2121
Paul Bakker84bbeb52014-07-01 14:53:22 +02002122 md_init( &ctx );
2123
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002124 /* Info from md_alg will be used instead */
2125 hashlen = 0;
Paul Bakker29e1f122013-04-16 13:07:56 +02002126
2127 /*
2128 * digitally-signed struct {
2129 * opaque client_random[32];
2130 * opaque server_random[32];
2131 * ServerDHParams params;
2132 * };
2133 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002134 if( ( ret = md_init_ctx( &ctx,
2135 md_info_from_type( md_alg ) ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002136 {
2137 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2138 return( ret );
2139 }
2140
2141 md_starts( &ctx );
2142 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002143 md_update( &ctx, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002144 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002145 md_free( &ctx );
Paul Bakker29e1f122013-04-16 13:07:56 +02002146 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002147 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002148#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2149 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02002150 {
Paul Bakker577e0062013-08-28 11:57:20 +02002151 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002152 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002153 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002154
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002155 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2156 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker29e1f122013-04-16 13:07:56 +02002157
Manuel Pégourié-Gonnardbb564e02015-09-03 10:44:32 +02002158 if( ssl->session_negotiate->peer_cert == NULL )
2159 {
2160 SSL_DEBUG_MSG( 2, ( "certificate required" ) );
2161 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2162 }
2163
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002164 /*
2165 * Verify signature
2166 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02002167 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002168 {
2169 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2170 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2171 }
2172
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002173 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
2174 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002175 {
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002176 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002177 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002178 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002179 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002180#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002181 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2182 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002183
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002184exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002185 ssl->state++;
2186
2187 SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
2188
2189 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002190}
2191
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002192#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2193 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2194 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2195 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2196static int ssl_parse_certificate_request( ssl_context *ssl )
2197{
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002198 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2199
2200 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2201
2202 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2203 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2204 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2205 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2206 {
2207 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2208 ssl->state++;
2209 return( 0 );
2210 }
2211
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002212 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2213 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002214}
2215#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002216static int ssl_parse_certificate_request( ssl_context *ssl )
2217{
2218 int ret;
Paul Bakker926af752012-11-23 13:38:07 +01002219 unsigned char *buf, *p;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002220 size_t n = 0, m = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002221 size_t cert_type_len = 0, dn_len = 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002222 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002223
2224 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2225
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002226 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2227 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2228 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2229 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2230 {
2231 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2232 ssl->state++;
2233 return( 0 );
2234 }
2235
Paul Bakker5121ce52009-01-03 21:22:43 +00002236 /*
2237 * 0 . 0 handshake type
2238 * 1 . 3 handshake length
Paul Bakker926af752012-11-23 13:38:07 +01002239 * 4 . 4 cert type count
2240 * 5 .. m-1 cert types
2241 * m .. m+1 sig alg length (TLS 1.2 only)
2242 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002243 * n .. n+1 length of all DNs
2244 * n+2 .. n+3 length of DN 1
2245 * n+4 .. ... Distinguished Name #1
2246 * ... .. ... length of DN 2, etc.
2247 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002248 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002249 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002250 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2251 {
2252 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2253 return( ret );
2254 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002255
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002256 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2257 {
2258 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2259 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2260 }
2261
2262 ssl->record_read = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002263 }
2264
2265 ssl->client_auth = 0;
2266 ssl->state++;
2267
2268 if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST )
2269 ssl->client_auth++;
2270
2271 SSL_DEBUG_MSG( 3, ( "got %s certificate request",
2272 ssl->client_auth ? "a" : "no" ) );
2273
Paul Bakker926af752012-11-23 13:38:07 +01002274 if( ssl->client_auth == 0 )
2275 goto exit;
2276
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002277 ssl->record_read = 0;
2278
Paul Bakker926af752012-11-23 13:38:07 +01002279 // TODO: handshake_failure alert for an anonymous server to request
2280 // client authentication
2281
2282 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002283
Paul Bakker926af752012-11-23 13:38:07 +01002284 // Retrieve cert types
2285 //
2286 cert_type_len = buf[4];
2287 n = cert_type_len;
2288
2289 if( ssl->in_hslen < 6 + n )
2290 {
2291 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2292 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2293 }
2294
Paul Bakker73d44312013-05-22 13:56:26 +02002295 p = buf + 5;
Paul Bakker926af752012-11-23 13:38:07 +01002296 while( cert_type_len > 0 )
2297 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002298#if defined(POLARSSL_RSA_C)
2299 if( *p == SSL_CERT_TYPE_RSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002300 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker926af752012-11-23 13:38:07 +01002301 {
2302 ssl->handshake->cert_type = SSL_CERT_TYPE_RSA_SIGN;
2303 break;
2304 }
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002305 else
2306#endif
2307#if defined(POLARSSL_ECDSA_C)
2308 if( *p == SSL_CERT_TYPE_ECDSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002309 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002310 {
2311 ssl->handshake->cert_type = SSL_CERT_TYPE_ECDSA_SIGN;
2312 break;
2313 }
2314 else
2315#endif
2316 {
2317 ; /* Unsupported cert type, ignore */
2318 }
Paul Bakker926af752012-11-23 13:38:07 +01002319
2320 cert_type_len--;
2321 p++;
2322 }
2323
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002324#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002325 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2326 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002327 /* Ignored, see comments about hash in write_certificate_verify */
2328 // TODO: should check the signature part against our pk_key though
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002329 size_t sig_alg_len = ( ( buf[5 + n] << 8 )
2330 | ( buf[6 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002331
2332 p = buf + 7 + n;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002333 m += 2;
Paul Bakker926af752012-11-23 13:38:07 +01002334 n += sig_alg_len;
2335
2336 if( ssl->in_hslen < 6 + n )
2337 {
2338 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2339 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2340 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002341 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002342#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker926af752012-11-23 13:38:07 +01002343
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002344 /* Ignore certificate_authorities, we only have one cert anyway */
2345 // TODO: should not send cert if no CA matches
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002346 dn_len = ( ( buf[5 + m + n] << 8 )
2347 | ( buf[6 + m + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002348
2349 n += dn_len;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002350 if( ssl->in_hslen != 7 + m + n )
Paul Bakker926af752012-11-23 13:38:07 +01002351 {
2352 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2353 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2354 }
2355
2356exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002357 SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2358
2359 return( 0 );
2360}
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002361#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2362 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2363 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2364 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002365
2366static int ssl_parse_server_hello_done( ssl_context *ssl )
2367{
2368 int ret;
2369
2370 SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
2371
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002372 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002373 {
2374 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2375 {
2376 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2377 return( ret );
2378 }
2379
2380 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2381 {
2382 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002383 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002384 }
2385 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002386 ssl->record_read = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002387
2388 if( ssl->in_hslen != 4 ||
2389 ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
2390 {
2391 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002392 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002393 }
2394
2395 ssl->state++;
2396
2397 SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
2398
2399 return( 0 );
2400}
2401
2402static int ssl_write_client_key_exchange( ssl_context *ssl )
2403{
Paul Bakker23986e52011-04-24 08:57:21 +00002404 int ret;
2405 size_t i, n;
Paul Bakker41c83d32013-03-20 14:39:14 +01002406 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002407
2408 SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
2409
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002410#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002411 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002412 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002413 /*
2414 * DHM key exchange -- send G^X mod P
2415 */
Paul Bakker48916f92012-09-16 19:57:18 +00002416 n = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002417
2418 ssl->out_msg[4] = (unsigned char)( n >> 8 );
2419 ssl->out_msg[5] = (unsigned char)( n );
2420 i = 6;
2421
Paul Bakker29b64762012-09-25 09:36:44 +00002422 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002423 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 &ssl->out_msg[i], n,
2425 ssl->f_rng, ssl->p_rng );
2426 if( ret != 0 )
2427 {
2428 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2429 return( ret );
2430 }
2431
Paul Bakker48916f92012-09-16 19:57:18 +00002432 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2433 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002434
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002435 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002436
Paul Bakker48916f92012-09-16 19:57:18 +00002437 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2438 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002439 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002440 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002441 {
2442 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2443 return( ret );
2444 }
2445
Paul Bakker48916f92012-09-16 19:57:18 +00002446 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002447 }
2448 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002449#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002450#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002451 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2452 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2453 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002454 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002455 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2456 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2457 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002458 {
2459 /*
2460 * ECDH key exchange -- send client public value
2461 */
2462 i = 4;
2463
2464 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
2465 &n,
2466 &ssl->out_msg[i], 1000,
2467 ssl->f_rng, ssl->p_rng );
2468 if( ret != 0 )
2469 {
2470 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2471 return( ret );
2472 }
2473
2474 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2475
2476 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2477 &ssl->handshake->pmslen,
2478 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002479 POLARSSL_MPI_MAX_SIZE,
2480 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002481 {
2482 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2483 return( ret );
2484 }
2485
2486 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2487 }
2488 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002489#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002490 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2491 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2492 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002493#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002494 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002495 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002496 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2497 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002498 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002499 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002500 * opaque psk_identity<0..2^16-1>;
2501 */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002502 if( ssl->psk == NULL || ssl->psk_identity == NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002503 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2504
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002505 i = 4;
2506 n = ssl->psk_identity_len;
Manuel Pégourié-Gonnard65125542015-08-27 16:37:35 +02002507
2508 if( i + 2 + n > SSL_MAX_CONTENT_LEN )
2509 {
2510 SSL_DEBUG_MSG( 1, ( "psk identity too long or "
2511 "SSL buffer too short" ) );
2512 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2513 }
2514
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002515 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2516 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002517
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002518 memcpy( ssl->out_msg + i, ssl->psk_identity, ssl->psk_identity_len );
2519 i += ssl->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002520
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002521#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002522 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002523 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002524 n = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002525 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002526 else
2527#endif
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002528#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2529 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2530 {
2531 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2532 return( ret );
2533 }
2534 else
2535#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002536#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002537 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002538 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002539 /*
2540 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
2541 */
2542 n = ssl->handshake->dhm_ctx.len;
Manuel Pégourié-Gonnard65125542015-08-27 16:37:35 +02002543
2544 if( i + 2 + n > SSL_MAX_CONTENT_LEN )
2545 {
2546 SSL_DEBUG_MSG( 1, ( "psk identity or DHM size too long"
2547 " or SSL buffer too short" ) );
2548 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2549 }
2550
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002551 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2552 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002553
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002554 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakker68881672013-10-15 13:24:01 +02002555 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002556 &ssl->out_msg[i], n,
2557 ssl->f_rng, ssl->p_rng );
2558 if( ret != 0 )
2559 {
2560 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2561 return( ret );
2562 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002563 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002564 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002565#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002566#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002567 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002568 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002569 /*
2570 * ClientECDiffieHellmanPublic public;
2571 */
2572 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
2573 &ssl->out_msg[i], SSL_MAX_CONTENT_LEN - i,
2574 ssl->f_rng, ssl->p_rng );
2575 if( ret != 0 )
2576 {
2577 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2578 return( ret );
2579 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002580
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002581 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2582 }
2583 else
2584#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2585 {
2586 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002587 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002588 }
2589
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002590 if( ( ret = ssl_psk_derive_premaster( ssl,
2591 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002592 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002593 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002594 return( ret );
2595 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002596 }
2597 else
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002598#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002599#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +02002600 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002601 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002602 i = 4;
2603 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002604 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002605 }
Paul Bakkered27a042013-04-18 22:46:23 +02002606 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002607#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002608 {
2609 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002610 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002611 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02002612 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002613
Paul Bakker5121ce52009-01-03 21:22:43 +00002614 ssl->out_msglen = i + n;
2615 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2616 ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE;
2617
2618 ssl->state++;
2619
2620 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2621 {
2622 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2623 return( ret );
2624 }
2625
2626 SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
2627
2628 return( 0 );
2629}
2630
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002631#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2632 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002633 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2634 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002635static int ssl_write_certificate_verify( ssl_context *ssl )
2636{
Paul Bakkered27a042013-04-18 22:46:23 +02002637 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002638 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002639
2640 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2641
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002642 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2643 {
2644 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2645 return( ret );
2646 }
2647
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002648 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002649 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002650 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002651 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002652 {
2653 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2654 ssl->state++;
2655 return( 0 );
2656 }
2657
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002658 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2659 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002660}
2661#else
2662static int ssl_write_certificate_verify( ssl_context *ssl )
2663{
2664 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2665 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2666 size_t n = 0, offset = 0;
2667 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002668 unsigned char *hash_start = hash;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002669 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002670 unsigned int hashlen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002671
2672 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2673
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002674 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2675 {
2676 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2677 return( ret );
2678 }
2679
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002680 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002681 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002682 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002683 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2684 {
2685 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2686 ssl->state++;
2687 return( 0 );
2688 }
2689
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002690 if( ssl->client_auth == 0 || ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002691 {
2692 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2693 ssl->state++;
2694 return( 0 );
2695 }
2696
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002697 if( ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002698 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00002699 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2700 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002701 }
2702
2703 /*
2704 * Make an RSA signature of the handshake digests
2705 */
Paul Bakker48916f92012-09-16 19:57:18 +00002706 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002707
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002708#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2709 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker926af752012-11-23 13:38:07 +01002710 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002711 {
Paul Bakker926af752012-11-23 13:38:07 +01002712 /*
2713 * digitally-signed struct {
2714 * opaque md5_hash[16];
2715 * opaque sha_hash[20];
2716 * };
2717 *
2718 * md5_hash
2719 * MD5(handshake_messages);
2720 *
2721 * sha_hash
2722 * SHA(handshake_messages);
2723 */
2724 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002725 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002726
2727 /*
2728 * For ECDSA, default hash is SHA-1 only
2729 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002730 if( pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002731 {
2732 hash_start += 16;
2733 hashlen -= 16;
2734 md_alg = POLARSSL_MD_SHA1;
2735 }
Paul Bakker926af752012-11-23 13:38:07 +01002736 }
2737 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002738#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2739 POLARSSL_SSL_PROTO_TLS1_1 */
2740#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2741 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002742 {
2743 /*
2744 * digitally-signed struct {
2745 * opaque handshake_messages[handshake_messages_length];
2746 * };
2747 *
2748 * Taking shortcut here. We assume that the server always allows the
2749 * PRF Hash function and has sent it in the allowed signature
2750 * algorithms list received in the Certificate Request message.
2751 *
2752 * Until we encounter a server that does not, we will take this
2753 * shortcut.
2754 *
2755 * Reason: Otherwise we should have running hashes for SHA512 and SHA224
2756 * in order to satisfy 'weird' needs from the server side.
2757 */
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002758 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2759 POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002760 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002761 md_alg = POLARSSL_MD_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002762 ssl->out_msg[4] = SSL_HASH_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002763 }
2764 else
2765 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002766 md_alg = POLARSSL_MD_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002767 ssl->out_msg[4] = SSL_HASH_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002768 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002769 ssl->out_msg[5] = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002770
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002771 /* Info from md_alg will be used instead */
2772 hashlen = 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002773 offset = 2;
2774 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002775 else
2776#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002777 {
2778 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002779 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002780 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002781
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002782 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002783 ssl->out_msg + 6 + offset, &n,
2784 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002785 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002786 SSL_DEBUG_RET( 1, "pk_sign", ret );
2787 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002788 }
Paul Bakker926af752012-11-23 13:38:07 +01002789
Paul Bakker1ef83d62012-04-11 12:09:53 +00002790 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
2791 ssl->out_msg[5 + offset] = (unsigned char)( n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002792
Paul Bakker1ef83d62012-04-11 12:09:53 +00002793 ssl->out_msglen = 6 + n + offset;
Paul Bakker5121ce52009-01-03 21:22:43 +00002794 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2795 ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY;
2796
2797 ssl->state++;
2798
2799 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2800 {
2801 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2802 return( ret );
2803 }
2804
2805 SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
2806
Paul Bakkered27a042013-04-18 22:46:23 +02002807 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002808}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002809#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2810 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2811 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002812
Paul Bakkera503a632013-08-14 13:48:06 +02002813#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002814static int ssl_parse_new_session_ticket( ssl_context *ssl )
2815{
2816 int ret;
2817 uint32_t lifetime;
2818 size_t ticket_len;
2819 unsigned char *ticket;
2820
2821 SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2822
2823 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2824 {
2825 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2826 return( ret );
2827 }
2828
2829 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2830 {
2831 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2832 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2833 }
2834
2835 /*
2836 * struct {
2837 * uint32 ticket_lifetime_hint;
2838 * opaque ticket<0..2^16-1>;
2839 * } NewSessionTicket;
2840 *
2841 * 0 . 0 handshake message type
2842 * 1 . 3 handshake message length
2843 * 4 . 7 ticket_lifetime_hint
2844 * 8 . 9 ticket_len (n)
2845 * 10 . 9+n ticket content
2846 */
2847 if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
2848 ssl->in_hslen < 10 )
2849 {
2850 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2851 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2852 }
2853
2854 lifetime = ( ssl->in_msg[4] << 24 ) | ( ssl->in_msg[5] << 16 ) |
2855 ( ssl->in_msg[6] << 8 ) | ( ssl->in_msg[7] );
2856
2857 ticket_len = ( ssl->in_msg[8] << 8 ) | ( ssl->in_msg[9] );
2858
2859 if( ticket_len + 10 != ssl->in_hslen )
2860 {
2861 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2862 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2863 }
2864
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002865 SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
2866
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002867 /* We're not waiting for a NewSessionTicket message any more */
2868 ssl->handshake->new_session_ticket = 0;
2869
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002870 /*
2871 * Zero-length ticket means the server changed his mind and doesn't want
2872 * to send a ticket after all, so just forget it
2873 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002874 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002875 return( 0 );
2876
Paul Bakker34617722014-06-13 17:20:13 +02002877 polarssl_zeroize( ssl->session_negotiate->ticket,
2878 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002879 polarssl_free( ssl->session_negotiate->ticket );
2880 ssl->session_negotiate->ticket = NULL;
2881 ssl->session_negotiate->ticket_len = 0;
2882
2883 if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
2884 {
2885 SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
2886 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2887 }
2888
2889 memcpy( ticket, ssl->in_msg + 10, ticket_len );
2890
2891 ssl->session_negotiate->ticket = ticket;
2892 ssl->session_negotiate->ticket_len = ticket_len;
2893 ssl->session_negotiate->ticket_lifetime = lifetime;
2894
2895 /*
2896 * RFC 5077 section 3.4:
2897 * "If the client receives a session ticket from the server, then it
2898 * discards any Session ID that was sent in the ServerHello."
2899 */
2900 SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
2901 ssl->session_negotiate->length = 0;
2902
2903 SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2904
2905 return( 0 );
2906}
Paul Bakkera503a632013-08-14 13:48:06 +02002907#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002908
Paul Bakker5121ce52009-01-03 21:22:43 +00002909/*
Paul Bakker1961b702013-01-25 14:49:24 +01002910 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002911 */
Paul Bakker1961b702013-01-25 14:49:24 +01002912int ssl_handshake_client_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002913{
2914 int ret = 0;
2915
Paul Bakker1961b702013-01-25 14:49:24 +01002916 if( ssl->state == SSL_HANDSHAKE_OVER )
2917 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002918
Paul Bakker1961b702013-01-25 14:49:24 +01002919 SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
2920
2921 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2922 return( ret );
2923
2924 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002925 {
Paul Bakker1961b702013-01-25 14:49:24 +01002926 case SSL_HELLO_REQUEST:
2927 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002928 break;
2929
Paul Bakker1961b702013-01-25 14:49:24 +01002930 /*
2931 * ==> ClientHello
2932 */
2933 case SSL_CLIENT_HELLO:
2934 ret = ssl_write_client_hello( ssl );
2935 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002936
Paul Bakker1961b702013-01-25 14:49:24 +01002937 /*
2938 * <== ServerHello
2939 * Certificate
2940 * ( ServerKeyExchange )
2941 * ( CertificateRequest )
2942 * ServerHelloDone
2943 */
2944 case SSL_SERVER_HELLO:
2945 ret = ssl_parse_server_hello( ssl );
2946 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002947
Paul Bakker1961b702013-01-25 14:49:24 +01002948 case SSL_SERVER_CERTIFICATE:
2949 ret = ssl_parse_certificate( ssl );
2950 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002951
Paul Bakker1961b702013-01-25 14:49:24 +01002952 case SSL_SERVER_KEY_EXCHANGE:
2953 ret = ssl_parse_server_key_exchange( ssl );
2954 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002955
Paul Bakker1961b702013-01-25 14:49:24 +01002956 case SSL_CERTIFICATE_REQUEST:
2957 ret = ssl_parse_certificate_request( ssl );
2958 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002959
Paul Bakker1961b702013-01-25 14:49:24 +01002960 case SSL_SERVER_HELLO_DONE:
2961 ret = ssl_parse_server_hello_done( ssl );
2962 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002963
Paul Bakker1961b702013-01-25 14:49:24 +01002964 /*
2965 * ==> ( Certificate/Alert )
2966 * ClientKeyExchange
2967 * ( CertificateVerify )
2968 * ChangeCipherSpec
2969 * Finished
2970 */
2971 case SSL_CLIENT_CERTIFICATE:
2972 ret = ssl_write_certificate( ssl );
2973 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002974
Paul Bakker1961b702013-01-25 14:49:24 +01002975 case SSL_CLIENT_KEY_EXCHANGE:
2976 ret = ssl_write_client_key_exchange( ssl );
2977 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002978
Paul Bakker1961b702013-01-25 14:49:24 +01002979 case SSL_CERTIFICATE_VERIFY:
2980 ret = ssl_write_certificate_verify( ssl );
2981 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002982
Paul Bakker1961b702013-01-25 14:49:24 +01002983 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2984 ret = ssl_write_change_cipher_spec( ssl );
2985 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002986
Paul Bakker1961b702013-01-25 14:49:24 +01002987 case SSL_CLIENT_FINISHED:
2988 ret = ssl_write_finished( ssl );
2989 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002990
Paul Bakker1961b702013-01-25 14:49:24 +01002991 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002992 * <== ( NewSessionTicket )
2993 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002994 * Finished
2995 */
2996 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002997#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002998 if( ssl->handshake->new_session_ticket != 0 )
2999 ret = ssl_parse_new_session_ticket( ssl );
3000 else
Paul Bakkera503a632013-08-14 13:48:06 +02003001#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003002 ret = ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003003 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003004
Paul Bakker1961b702013-01-25 14:49:24 +01003005 case SSL_SERVER_FINISHED:
3006 ret = ssl_parse_finished( ssl );
3007 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003008
Paul Bakker1961b702013-01-25 14:49:24 +01003009 case SSL_FLUSH_BUFFERS:
3010 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3011 ssl->state = SSL_HANDSHAKE_WRAPUP;
3012 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003013
Paul Bakker1961b702013-01-25 14:49:24 +01003014 case SSL_HANDSHAKE_WRAPUP:
3015 ssl_handshake_wrapup( ssl );
3016 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003017
Paul Bakker1961b702013-01-25 14:49:24 +01003018 default:
3019 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3020 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3021 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003022
3023 return( ret );
3024}
Paul Bakker9af723c2014-05-01 13:03:14 +02003025#endif /* POLARSSL_SSL_CLI_C */