blob: 5f5beecb58e2e2feba0562c51c341669039b619a [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 Butcher643a9222015-10-01 01:17:10 +010078 if( end < p || (size_t)( end - p ) < ssl->hostname_len + 9 )
Simon Butcherb1e325d2015-10-01 00:24:36 +010079 {
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
Manuel Pégourié-Gonnardf3e6e4b2015-10-02 09:53:52 +0200136 if( end < p || (size_t)(end - p) < 5 + ssl->verify_data_len )
Simon Butcherb1e325d2015-10-01 00:24:36 +0100137 {
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
Simon Butcher14400c82016-01-02 00:08:13 +0000194#if defined(POLARSSL_MD5_C) && defined(POLARSSL_SSL_ENABLE_MD5_SIGNATURES)
Simon Butcherb1e325d2015-10-01 00:24:36 +0100195 /* 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
Simon Butcher14400c82016-01-02 00:08:13 +0000212#if defined(POLARSSL_MD5_C) && defined(POLARSSL_SSL_ENABLE_MD5_SIGNATURES)
Simon Butcherb1e325d2015-10-01 00:24:36 +0100213 /* 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
Simon Butcher14400c82016-01-02 00:08:13 +0000246#if defined(POLARSSL_MD5_C) && defined(POLARSSL_SSL_ENABLE_MD5_SIGNATURES)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100247 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
Simon Butcher14400c82016-01-02 00:08:13 +0000268#if defined(POLARSSL_MD5_C) && defined(POLARSSL_SSL_ENABLE_MD5_SIGNATURES)
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200269 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
Janos Follath4e034392016-04-21 23:37:09 +0100333 if( info == NULL )
334 {
Simon Butcher01660392016-04-22 10:05:50 +0100335 SSL_DEBUG_MSG( 1, ( "invalid curve in ssl configuration" ) );
Janos Follath4e034392016-04-21 23:37:09 +0100336 return;
337 }
338
Simon Butcherb1e325d2015-10-01 00:24:36 +0100339 elliptic_curve_len += 2;
340 }
341
342 if( end < p || (size_t)( end - p ) < 6 + elliptic_curve_len )
343 {
344 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
345 return;
346 }
347
348 elliptic_curve_len = 0;
349
350#if defined(POLARSSL_SSL_SET_CURVES)
351 for( grp_id = ssl->curve_list; *grp_id != POLARSSL_ECP_DP_NONE; grp_id++ )
352 {
353 info = ecp_curve_info_from_grp_id( *grp_id );
354#else
355 for( info = ecp_curve_list(); info->grp_id != POLARSSL_ECP_DP_NONE; info++ )
356 {
357#endif
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100358 elliptic_curve_list[elliptic_curve_len++] = info->tls_id >> 8;
359 elliptic_curve_list[elliptic_curve_len++] = info->tls_id & 0xFF;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200360 }
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200361
362 if( elliptic_curve_len == 0 )
363 return;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100364
365 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES >> 8 ) & 0xFF );
366 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ) & 0xFF );
367
368 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF );
369 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) ) & 0xFF );
370
371 *p++ = (unsigned char)( ( ( elliptic_curve_len ) >> 8 ) & 0xFF );
372 *p++ = (unsigned char)( ( ( elliptic_curve_len ) ) & 0xFF );
373
Paul Bakkerd3edc862013-03-20 16:07:17 +0100374 *olen = 6 + elliptic_curve_len;
375}
376
377static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
378 unsigned char *buf,
379 size_t *olen )
380{
381 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100382 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100383
384 *olen = 0;
385
386 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_point_formats extension" ) );
387
Simon Butcherb1e325d2015-10-01 00:24:36 +0100388 if( end < p || (size_t)( end - p ) < 6 )
389 {
390 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
391 return;
392 }
393
Paul Bakkerd3edc862013-03-20 16:07:17 +0100394 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
395 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
396
397 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100398 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200399
400 *p++ = 1;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100401 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
402
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200403 *olen = 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100404}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200405#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100406
Paul Bakker05decb22013-08-15 13:33:48 +0200407#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200408static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
409 unsigned char *buf,
410 size_t *olen )
411{
412 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100413 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200414
Simon Butcherb1e325d2015-10-01 00:24:36 +0100415 *olen = 0;
416
417 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE )
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200418 return;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200419
420 SSL_DEBUG_MSG( 3, ( "client hello, adding max_fragment_length extension" ) );
421
Simon Butcherb1e325d2015-10-01 00:24:36 +0100422 if( end < p || (size_t)( end - p ) < 5 )
423 {
424 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
425 return;
426 }
427
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200428 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
429 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
430
431 *p++ = 0x00;
432 *p++ = 1;
433
434 *p++ = ssl->mfl_code;
435
436 *olen = 5;
437}
Paul Bakker05decb22013-08-15 13:33:48 +0200438#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200439
Paul Bakker1f2bc622013-08-15 13:45:55 +0200440#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200441static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
442 unsigned char *buf, size_t *olen )
443{
444 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100445 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
446
447 *olen = 0;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200448
449 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200450 return;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200451
452 SSL_DEBUG_MSG( 3, ( "client hello, adding truncated_hmac extension" ) );
453
Simon Butcherb1e325d2015-10-01 00:24:36 +0100454 if( end < p || (size_t)( end - p ) < 4 )
455 {
456 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
457 return;
458 }
459
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200460 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
461 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
462
463 *p++ = 0x00;
464 *p++ = 0x00;
465
466 *olen = 4;
467}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200468#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200469
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100470#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
471static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
472 unsigned char *buf, size_t *olen )
473{
474 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100475 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
476
477 *olen = 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100478
479 if( ssl->encrypt_then_mac == SSL_ETM_DISABLED ||
480 ssl->max_minor_ver == SSL_MINOR_VERSION_0 )
481 {
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100482 return;
483 }
484
485 SSL_DEBUG_MSG( 3, ( "client hello, adding encrypt_then_mac "
486 "extension" ) );
487
Simon Butcherb1e325d2015-10-01 00:24:36 +0100488 if( end < p || (size_t)( end - p ) < 4 )
489 {
490 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
491 return;
492 }
493
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100494 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
495 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
496
497 *p++ = 0x00;
498 *p++ = 0x00;
499
500 *olen = 4;
501}
502#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
503
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200504#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
505static void ssl_write_extended_ms_ext( ssl_context *ssl,
506 unsigned char *buf, size_t *olen )
507{
508 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100509 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
510
511 *olen = 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200512
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200513 if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED ||
514 ssl->max_minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200515 {
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200516 return;
517 }
518
519 SSL_DEBUG_MSG( 3, ( "client hello, adding extended_master_secret "
520 "extension" ) );
521
Simon Butcherb1e325d2015-10-01 00:24:36 +0100522 if( end < p || (size_t)( end - p ) < 4 )
523 {
524 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
525 return;
526 }
527
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200528 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
529 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
530
531 *p++ = 0x00;
532 *p++ = 0x00;
533
534 *olen = 4;
535}
536#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
537
Paul Bakkera503a632013-08-14 13:48:06 +0200538#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200539static void ssl_write_session_ticket_ext( ssl_context *ssl,
540 unsigned char *buf, size_t *olen )
541{
542 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100543 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200544 size_t tlen = ssl->session_negotiate->ticket_len;
545
Simon Butcherb1e325d2015-10-01 00:24:36 +0100546 *olen = 0;
547
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200548 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200549 return;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200550
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200551 SSL_DEBUG_MSG( 3, ( "client hello, adding session ticket extension" ) );
552
Simon Butcherb1e325d2015-10-01 00:24:36 +0100553 if( end < p || (size_t)( end - p ) < 4 + tlen )
554 {
555 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
556 return;
557 }
558
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200559 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
560 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
561
562 *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF );
563 *p++ = (unsigned char)( ( tlen ) & 0xFF );
564
565 *olen = 4;
566
567 if( ssl->session_negotiate->ticket == NULL ||
568 ssl->session_negotiate->ticket_len == 0 )
569 {
570 return;
571 }
572
573 SSL_DEBUG_MSG( 3, ( "sending session ticket of length %d", tlen ) );
574
575 memcpy( p, ssl->session_negotiate->ticket, tlen );
576
577 *olen += tlen;
578}
Paul Bakkera503a632013-08-14 13:48:06 +0200579#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200580
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200581#if defined(POLARSSL_SSL_ALPN)
582static void ssl_write_alpn_ext( ssl_context *ssl,
583 unsigned char *buf, size_t *olen )
584{
585 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100586 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
587 size_t alpnlen = 0;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200588 const char **cur;
589
Simon Butcherb1e325d2015-10-01 00:24:36 +0100590 *olen = 0;
591
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200592 if( ssl->alpn_list == NULL )
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200593 return;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200594
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +0200595 SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200596
Simon Butcherb1e325d2015-10-01 00:24:36 +0100597 for( cur = ssl->alpn_list; *cur != NULL; cur++ )
598 alpnlen += (unsigned char)( strlen( *cur ) & 0xFF ) + 1;
599
600 if( end < p || (size_t)( end - p ) < 6 + alpnlen )
601 {
602 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
603 return;
604 }
605
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200606 *p++ = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
607 *p++ = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
608
609 /*
610 * opaque ProtocolName<1..2^8-1>;
611 *
612 * struct {
613 * ProtocolName protocol_name_list<2..2^16-1>
614 * } ProtocolNameList;
615 */
616
617 /* Skip writing extension and list length for now */
618 p += 4;
619
620 for( cur = ssl->alpn_list; *cur != NULL; cur++ )
621 {
622 *p = (unsigned char)( strlen( *cur ) & 0xFF );
623 memcpy( p + 1, *cur, *p );
624 p += 1 + *p;
625 }
626
627 *olen = p - buf;
628
629 /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
630 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
631 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
632
633 /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
634 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
635 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
636}
637#endif /* POLARSSL_SSL_ALPN */
638
Paul Bakker5121ce52009-01-03 21:22:43 +0000639static int ssl_write_client_hello( ssl_context *ssl )
640{
Paul Bakker23986e52011-04-24 08:57:21 +0000641 int ret;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100642 size_t i, n, olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000643 unsigned char *buf;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200644 unsigned char *p, *q;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200645#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000646 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200647#endif
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200648 const int *ciphersuites;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200649 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000650
651 SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
652
Paul Bakkera9a028e2013-11-21 17:31:06 +0100653 if( ssl->f_rng == NULL )
654 {
655 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
656 return( POLARSSL_ERR_SSL_NO_RNG );
657 }
658
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100659#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +0000660 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100661#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000662 {
Paul Bakker993d11d2012-09-28 15:00:12 +0000663 ssl->major_ver = ssl->min_major_ver;
664 ssl->minor_ver = ssl->min_minor_ver;
Paul Bakker48916f92012-09-16 19:57:18 +0000665 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000666
Paul Bakker490ecc82011-10-06 13:04:09 +0000667 if( ssl->max_major_ver == 0 && ssl->max_minor_ver == 0 )
668 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200669 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
670 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker490ecc82011-10-06 13:04:09 +0000671 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000672
673 /*
674 * 0 . 0 handshake type
675 * 1 . 3 handshake length
676 * 4 . 5 highest version supported
677 * 6 . 9 current UNIX time
678 * 10 . 37 random bytes
679 */
680 buf = ssl->out_msg;
681 p = buf + 4;
682
683 *p++ = (unsigned char) ssl->max_major_ver;
684 *p++ = (unsigned char) ssl->max_minor_ver;
685
686 SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
687 buf[4], buf[5] ) );
688
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200689#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000690 t = time( NULL );
691 *p++ = (unsigned char)( t >> 24 );
692 *p++ = (unsigned char)( t >> 16 );
693 *p++ = (unsigned char)( t >> 8 );
694 *p++ = (unsigned char)( t );
695
696 SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200697#else
698 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
699 return( ret );
700
701 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +0200702#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000703
Paul Bakkera3d195c2011-11-27 21:07:34 +0000704 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
705 return( ret );
706
707 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +0000708
Paul Bakker48916f92012-09-16 19:57:18 +0000709 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000710
711 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 6, 32 );
712
713 /*
714 * 38 . 38 session id length
715 * 39 . 39+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +0000716 * 40+n . 41+n ciphersuitelist length
717 * 42+n . .. ciphersuitelist
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000718 * .. . .. compression methods length
719 * .. . .. compression methods
720 * .. . .. extensions length
721 * .. . .. extensions
Paul Bakker5121ce52009-01-03 21:22:43 +0000722 */
Paul Bakker48916f92012-09-16 19:57:18 +0000723 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000724
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100725 if( n < 16 || n > 32 ||
726#if defined(POLARSSL_SSL_RENEGOTIATION)
727 ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
728#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000729 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200730 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000731 n = 0;
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200732 }
733
Paul Bakkera503a632013-08-14 13:48:06 +0200734#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200735 /*
736 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
737 * generate and include a Session ID in the TLS ClientHello."
738 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100739#if defined(POLARSSL_SSL_RENEGOTIATION)
740 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000741#endif
Manuel Pégourié-Gonnard51bccd32015-03-10 16:09:08 +0000742 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000743 if( ssl->session_negotiate->ticket != NULL &&
744 ssl->session_negotiate->ticket_len != 0 )
745 {
746 ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200747
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000748 if( ret != 0 )
749 return( ret );
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200750
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000751 ssl->session_negotiate->length = n = 32;
752 }
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200753 }
Paul Bakkera503a632013-08-14 13:48:06 +0200754#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
756 *p++ = (unsigned char) n;
757
758 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000759 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000760
761 SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
762 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
763
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200764 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Paul Bakker2fbefde2013-06-29 16:01:15 +0200765 n = 0;
766 q = p;
767
768 // Skip writing ciphersuite length for now
769 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000770
Paul Bakker2fbefde2013-06-29 16:01:15 +0200771 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000772 {
Paul Bakker2fbefde2013-06-29 16:01:15 +0200773 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
774
775 if( ciphersuite_info == NULL )
776 continue;
777
778 if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
779 ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
780 continue;
781
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100782 if( ssl->arc4_disabled == SSL_ARC4_DISABLED &&
783 ciphersuite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
784 continue;
785
Paul Bakkere3166ce2011-01-27 17:40:50 +0000786 SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200787 ciphersuites[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000788
Paul Bakker2fbefde2013-06-29 16:01:15 +0200789 n++;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200790 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
791 *p++ = (unsigned char)( ciphersuites[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000792 }
793
Manuel Pégourié-Gonnard5d9cde22015-01-22 10:49:41 +0000794 /*
795 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
796 */
797#if defined(POLARSSL_SSL_RENEGOTIATION)
798 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
799#endif
800 {
801 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
802 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
803 n++;
804 }
805
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200806 /* Some versions of OpenSSL don't handle it correctly if not at end */
807#if defined(POLARSSL_SSL_FALLBACK_SCSV)
808 if( ssl->fallback == SSL_IS_FALLBACK )
809 {
810 SSL_DEBUG_MSG( 3, ( "adding FALLBACK_SCSV" ) );
811 *p++ = (unsigned char)( SSL_FALLBACK_SCSV >> 8 );
812 *p++ = (unsigned char)( SSL_FALLBACK_SCSV );
813 n++;
814 }
815#endif
816
Paul Bakker2fbefde2013-06-29 16:01:15 +0200817 *q++ = (unsigned char)( n >> 7 );
818 *q++ = (unsigned char)( n << 1 );
819
820 SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
821
822
Paul Bakker2770fbd2012-07-03 13:30:23 +0000823#if defined(POLARSSL_ZLIB_SUPPORT)
824 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
825 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000826 SSL_COMPRESS_DEFLATE, SSL_COMPRESS_NULL ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000827
828 *p++ = 2;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000829 *p++ = SSL_COMPRESS_DEFLATE;
Paul Bakker48916f92012-09-16 19:57:18 +0000830 *p++ = SSL_COMPRESS_NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000831#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000832 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000833 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d", SSL_COMPRESS_NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000834
835 *p++ = 1;
836 *p++ = SSL_COMPRESS_NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200837#endif /* POLARSSL_ZLIB_SUPPORT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000838
Paul Bakkerd3edc862013-03-20 16:07:17 +0100839 // First write extensions, then the total length
840 //
Paul Bakker0be444a2013-08-27 21:55:01 +0200841#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100842 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
843 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +0200844#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000845
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100846#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100847 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
848 ext_len += olen;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100849#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000850
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100851#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
852 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100853 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
854 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200855#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000856
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200857#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100858 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
859 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100860
Paul Bakkerd3edc862013-03-20 16:07:17 +0100861 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
862 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100863#endif
864
Paul Bakker05decb22013-08-15 13:33:48 +0200865#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200866 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
867 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +0200868#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200869
Paul Bakker1f2bc622013-08-15 13:45:55 +0200870#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200871 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
872 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200873#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200874
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100875#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
876 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
877 ext_len += olen;
878#endif
879
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200880#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
881 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
882 ext_len += olen;
883#endif
884
Simon Butcher643a9222015-10-01 01:17:10 +0100885#if defined(POLARSSL_SSL_ALPN)
886 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200887 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +0200888#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200889
Simon Butcher643a9222015-10-01 01:17:10 +0100890#if defined(POLARSSL_SSL_SESSION_TICKETS)
891 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200892 ext_len += olen;
893#endif
894
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +0100895 /* olen unused if all extensions are disabled */
896 ((void) olen);
897
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000898 SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
899 ext_len ) );
900
Paul Bakkera7036632014-04-30 10:15:38 +0200901 if( ext_len > 0 )
902 {
903 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
904 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
905 p += ext_len;
906 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100907
Paul Bakker5121ce52009-01-03 21:22:43 +0000908 ssl->out_msglen = p - buf;
909 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
910 ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
911
912 ssl->state++;
913
914 if( ( ret = ssl_write_record( ssl ) ) != 0 )
915 {
916 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
917 return( ret );
918 }
919
920 SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
921
922 return( 0 );
923}
924
Paul Bakker48916f92012-09-16 19:57:18 +0000925static int ssl_parse_renegotiation_info( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200926 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000927 size_t len )
928{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000929 int ret;
930
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100931#if defined(POLARSSL_SSL_RENEGOTIATION)
932 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +0000933 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100934 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000935 if( len != 1 + ssl->verify_data_len * 2 ||
936 buf[0] != ssl->verify_data_len * 2 ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100937 safer_memcmp( buf + 1,
938 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
939 safer_memcmp( buf + 1 + ssl->verify_data_len,
940 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000941 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100942 SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000943
944 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
945 return( ret );
946
Paul Bakker48916f92012-09-16 19:57:18 +0000947 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
948 }
949 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100950 else
951#endif /* POLARSSL_SSL_RENEGOTIATION */
952 {
953 if( len != 1 || buf[0] != 0x00 )
954 {
955 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
956
957 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
958 return( ret );
959
960 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
961 }
962
963 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
964 }
Paul Bakker48916f92012-09-16 19:57:18 +0000965
966 return( 0 );
967}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200968
Paul Bakker05decb22013-08-15 13:33:48 +0200969#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200970static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200971 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200972 size_t len )
973{
974 /*
975 * server should use the extension only if we did,
976 * and if so the server's value should match ours (and len is always 1)
977 */
978 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
979 len != 1 ||
980 buf[0] != ssl->mfl_code )
981 {
982 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
983 }
984
985 return( 0 );
986}
Paul Bakker05decb22013-08-15 13:33:48 +0200987#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000988
Paul Bakker1f2bc622013-08-15 13:45:55 +0200989#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200990static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
991 const unsigned char *buf,
992 size_t len )
993{
994 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
995 len != 0 )
996 {
997 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
998 }
999
1000 ((void) buf);
1001
1002 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
1003
1004 return( 0 );
1005}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001006#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001007
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001008#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1009static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
1010 const unsigned char *buf,
1011 size_t len )
1012{
1013 if( ssl->encrypt_then_mac == SSL_ETM_DISABLED ||
1014 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
1015 len != 0 )
1016 {
1017 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1018 }
1019
1020 ((void) buf);
1021
1022 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
1023
1024 return( 0 );
1025}
1026#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1027
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001028#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1029static int ssl_parse_extended_ms_ext( ssl_context *ssl,
1030 const unsigned char *buf,
1031 size_t len )
1032{
1033 if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +02001034 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001035 len != 0 )
1036 {
1037 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1038 }
1039
1040 ((void) buf);
1041
1042 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
1043
1044 return( 0 );
1045}
1046#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1047
Paul Bakkera503a632013-08-14 13:48:06 +02001048#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001049static int ssl_parse_session_ticket_ext( ssl_context *ssl,
1050 const unsigned char *buf,
1051 size_t len )
1052{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001053 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ||
1054 len != 0 )
1055 {
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001056 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001057 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001058
1059 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02001060
1061 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001062
1063 return( 0 );
1064}
Paul Bakkera503a632013-08-14 13:48:06 +02001065#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001066
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001067#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001068static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
1069 const unsigned char *buf,
1070 size_t len )
1071{
1072 size_t list_size;
1073 const unsigned char *p;
1074
1075 list_size = buf[0];
1076 if( list_size + 1 != len )
1077 {
1078 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1079 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1080 }
1081
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +02001082 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001083 while( list_size > 0 )
1084 {
1085 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
1086 p[0] == POLARSSL_ECP_PF_COMPRESSED )
1087 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +02001088 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001089 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
1090 return( 0 );
1091 }
1092
1093 list_size--;
1094 p++;
1095 }
1096
Manuel Pégourié-Gonnard5c1f0322014-06-23 14:24:43 +02001097 SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
1098 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001099}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001100#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001101
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001102#if defined(POLARSSL_SSL_ALPN)
1103static int ssl_parse_alpn_ext( ssl_context *ssl,
1104 const unsigned char *buf, size_t len )
1105{
1106 size_t list_len, name_len;
1107 const char **p;
1108
1109 /* If we didn't send it, the server shouldn't send it */
1110 if( ssl->alpn_list == NULL )
1111 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1112
1113 /*
1114 * opaque ProtocolName<1..2^8-1>;
1115 *
1116 * struct {
1117 * ProtocolName protocol_name_list<2..2^16-1>
1118 * } ProtocolNameList;
1119 *
1120 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
1121 */
1122
1123 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
1124 if( len < 4 )
1125 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1126
1127 list_len = ( buf[0] << 8 ) | buf[1];
1128 if( list_len != len - 2 )
1129 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1130
1131 name_len = buf[2];
1132 if( name_len != list_len - 1 )
1133 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1134
1135 /* Check that the server chosen protocol was in our list and save it */
1136 for( p = ssl->alpn_list; *p != NULL; p++ )
1137 {
1138 if( name_len == strlen( *p ) &&
1139 memcmp( buf + 3, *p, name_len ) == 0 )
1140 {
1141 ssl->alpn_chosen = *p;
1142 return( 0 );
1143 }
1144 }
1145
1146 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1147}
1148#endif /* POLARSSL_SSL_ALPN */
1149
Paul Bakker5121ce52009-01-03 21:22:43 +00001150static int ssl_parse_server_hello( ssl_context *ssl )
1151{
Paul Bakker2770fbd2012-07-03 13:30:23 +00001152 int ret, i, comp;
Paul Bakker23986e52011-04-24 08:57:21 +00001153 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001154 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001155 unsigned char *buf, *ext;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001156#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001157 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001158#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001159 int handshake_failure = 0;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001160 const ssl_ciphersuite_t *suite_info;
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001161#if defined(POLARSSL_DEBUG_C)
1162 uint32_t t;
1163#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001164
1165 SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
1166
1167 /*
1168 * 0 . 0 handshake type
1169 * 1 . 3 handshake length
1170 * 4 . 5 protocol version
1171 * 6 . 9 UNIX time()
1172 * 10 . 37 random bytes
1173 */
1174 buf = ssl->in_msg;
1175
1176 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1177 {
1178 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1179 return( ret );
1180 }
1181
1182 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1183 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001184#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001185 if( ssl->renegotiation == SSL_RENEGOTIATION )
1186 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001187 ssl->renego_records_seen++;
1188
1189 if( ssl->renego_max_records >= 0 &&
1190 ssl->renego_records_seen > ssl->renego_max_records )
1191 {
1192 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
1193 "but not honored by server" ) );
1194 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1195 }
1196
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001197 SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
Hanno Beckerd37839e2017-06-08 15:56:50 +01001198
1199 ssl->keep_current_message = 1;
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001200 return( POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
1201 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001202#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001203
Paul Bakker5121ce52009-01-03 21:22:43 +00001204 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001205 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001206 }
1207
1208 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1209 buf[4], buf[5] ) );
1210
1211 if( ssl->in_hslen < 42 ||
1212 buf[0] != SSL_HS_SERVER_HELLO ||
1213 buf[4] != SSL_MAJOR_VERSION_3 )
1214 {
1215 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001216 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001217 }
1218
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001219 if( buf[5] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00001220 {
1221 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001222 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001223 }
1224
1225 ssl->minor_ver = buf[5];
1226
Paul Bakker1d29fb52012-09-28 13:28:45 +00001227 if( ssl->minor_ver < ssl->min_minor_ver )
1228 {
1229 SSL_DEBUG_MSG( 1, ( "server only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001230 " [%d:%d] < [%d:%d]", ssl->major_ver,
1231 ssl->minor_ver, buf[4], buf[5] ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001232
1233 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1234 SSL_ALERT_MSG_PROTOCOL_VERSION );
1235
1236 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1237 }
1238
Paul Bakker1504af52012-02-11 16:17:43 +00001239#if defined(POLARSSL_DEBUG_C)
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001240 t = ( (uint32_t) buf[6] << 24 )
1241 | ( (uint32_t) buf[7] << 16 )
1242 | ( (uint32_t) buf[8] << 8 )
1243 | ( (uint32_t) buf[9] );
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001244 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakker87e5cda2012-01-14 18:14:15 +00001245#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001246
Paul Bakker48916f92012-09-16 19:57:18 +00001247 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001248
1249 n = buf[38];
1250
Paul Bakker5121ce52009-01-03 21:22:43 +00001251 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1252
Paul Bakker48916f92012-09-16 19:57:18 +00001253 if( n > 32 )
1254 {
1255 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1256 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1257 }
1258
Paul Bakker5121ce52009-01-03 21:22:43 +00001259 /*
1260 * 38 . 38 session id length
1261 * 39 . 38+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +00001262 * 39+n . 40+n chosen ciphersuite
Paul Bakker5121ce52009-01-03 21:22:43 +00001263 * 41+n . 41+n chosen compression alg.
1264 * 42+n . 43+n extensions length
1265 * 44+n . 44+n+m extensions
1266 */
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001267 if( ssl->in_hslen > 43 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001268 {
1269 ext_len = ( ( buf[42 + n] << 8 )
Paul Bakker48916f92012-09-16 19:57:18 +00001270 | ( buf[43 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001271
Paul Bakker48916f92012-09-16 19:57:18 +00001272 if( ( ext_len > 0 && ext_len < 4 ) ||
1273 ssl->in_hslen != 44 + n + ext_len )
1274 {
1275 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1276 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1277 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001278 }
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001279 else if( ssl->in_hslen == 42 + n )
1280 {
1281 ext_len = 0;
1282 }
1283 else
1284 {
1285 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1286 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1287 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001288
1289 i = ( buf[39 + n] << 8 ) | buf[40 + n];
Paul Bakker2770fbd2012-07-03 13:30:23 +00001290 comp = buf[41 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001291
Paul Bakker380da532012-04-18 16:10:25 +00001292 /*
1293 * Initialize update checksum functions
1294 */
Paul Bakker68884e32013-01-07 18:20:04 +01001295 ssl->transform_negotiate->ciphersuite_info = ssl_ciphersuite_from_id( i );
1296
1297 if( ssl->transform_negotiate->ciphersuite_info == NULL )
1298 {
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001299 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001300 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1301 }
Paul Bakker380da532012-04-18 16:10:25 +00001302
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001303 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1304
Paul Bakker5121ce52009-01-03 21:22:43 +00001305 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1306 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1307
1308 /*
1309 * Check if the session can be resumed
1310 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001311 if( ssl->handshake->resume == 0 || n == 0 ||
1312#if defined(POLARSSL_SSL_RENEGOTIATION)
1313 ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
1314#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001315 ssl->session_negotiate->ciphersuite != i ||
1316 ssl->session_negotiate->compression != comp ||
1317 ssl->session_negotiate->length != n ||
1318 memcmp( ssl->session_negotiate->id, buf + 39, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001319 {
1320 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001321 ssl->handshake->resume = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001322#if defined(POLARSSL_HAVE_TIME)
Paul Bakker48916f92012-09-16 19:57:18 +00001323 ssl->session_negotiate->start = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001324#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001325 ssl->session_negotiate->ciphersuite = i;
1326 ssl->session_negotiate->compression = comp;
1327 ssl->session_negotiate->length = n;
1328 memcpy( ssl->session_negotiate->id, buf + 39, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001329 }
1330 else
1331 {
1332 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001333
1334 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1335 {
1336 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1337 return( ret );
1338 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 }
1340
1341 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001342 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001343
Paul Bakkere3166ce2011-01-27 17:40:50 +00001344 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001345 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[41 + n] ) );
1346
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001347 suite_info = ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite );
1348 if( suite_info == NULL ||
1349 ( ssl->arc4_disabled &&
1350 suite_info->cipher == POLARSSL_CIPHER_ARC4_128 ) )
1351 {
1352 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1353 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1354 }
1355
1356
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 i = 0;
1358 while( 1 )
1359 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001360 if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001361 {
1362 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001363 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001364 }
1365
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001366 if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
1367 ssl->session_negotiate->ciphersuite )
1368 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001369 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001370 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001371 }
1372
Paul Bakker2770fbd2012-07-03 13:30:23 +00001373 if( comp != SSL_COMPRESS_NULL
1374#if defined(POLARSSL_ZLIB_SUPPORT)
1375 && comp != SSL_COMPRESS_DEFLATE
1376#endif
1377 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001378 {
1379 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001380 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001381 }
Paul Bakker48916f92012-09-16 19:57:18 +00001382 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001383
Paul Bakker48916f92012-09-16 19:57:18 +00001384 ext = buf + 44 + n;
1385
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001386 SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
1387
Paul Bakker48916f92012-09-16 19:57:18 +00001388 while( ext_len )
1389 {
1390 unsigned int ext_id = ( ( ext[0] << 8 )
1391 | ( ext[1] ) );
1392 unsigned int ext_size = ( ( ext[2] << 8 )
1393 | ( ext[3] ) );
1394
1395 if( ext_size + 4 > ext_len )
1396 {
1397 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1398 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1399 }
1400
1401 switch( ext_id )
1402 {
1403 case TLS_EXT_RENEGOTIATION_INFO:
1404 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001405#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001406 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001407#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001408
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001409 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1410 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001411 return( ret );
1412
1413 break;
1414
Paul Bakker05decb22013-08-15 13:33:48 +02001415#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001416 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1417 SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
1418
1419 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1420 ext + 4, ext_size ) ) != 0 )
1421 {
1422 return( ret );
1423 }
1424
1425 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001426#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001427
Paul Bakker1f2bc622013-08-15 13:45:55 +02001428#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001429 case TLS_EXT_TRUNCATED_HMAC:
1430 SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
1431
1432 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1433 ext + 4, ext_size ) ) != 0 )
1434 {
1435 return( ret );
1436 }
1437
1438 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001439#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001440
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001441#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1442 case TLS_EXT_ENCRYPT_THEN_MAC:
1443 SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
1444
1445 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
1446 ext + 4, ext_size ) ) != 0 )
1447 {
1448 return( ret );
1449 }
1450
1451 break;
1452#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1453
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001454#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1455 case TLS_EXT_EXTENDED_MASTER_SECRET:
1456 SSL_DEBUG_MSG( 3, ( "found extended_master_secret extension" ) );
1457
1458 if( ( ret = ssl_parse_extended_ms_ext( ssl,
1459 ext + 4, ext_size ) ) != 0 )
1460 {
1461 return( ret );
1462 }
1463
1464 break;
1465#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1466
Paul Bakkera503a632013-08-14 13:48:06 +02001467#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001468 case TLS_EXT_SESSION_TICKET:
1469 SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
1470
1471 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1472 ext + 4, ext_size ) ) != 0 )
1473 {
1474 return( ret );
1475 }
1476
1477 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001478#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001479
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001480#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001481 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1482 SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
1483
1484 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1485 ext + 4, ext_size ) ) != 0 )
1486 {
1487 return( ret );
1488 }
1489
1490 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001491#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001492
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001493#if defined(POLARSSL_SSL_ALPN)
1494 case TLS_EXT_ALPN:
1495 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1496
1497 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1498 return( ret );
1499
1500 break;
1501#endif /* POLARSSL_SSL_ALPN */
1502
Paul Bakker48916f92012-09-16 19:57:18 +00001503 default:
1504 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1505 ext_id ) );
1506 }
1507
1508 ext_len -= 4 + ext_size;
1509 ext += 4 + ext_size;
1510
1511 if( ext_len > 0 && ext_len < 4 )
1512 {
1513 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1514 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1515 }
1516 }
1517
1518 /*
1519 * Renegotiation security checks
1520 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001521 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1522 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001523 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001524 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1525 handshake_failure = 1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001526 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001527#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001528 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1529 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1530 renegotiation_info_seen == 0 )
1531 {
1532 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1533 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001534 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001535 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1536 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1537 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001538 {
1539 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001540 handshake_failure = 1;
1541 }
1542 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1543 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1544 renegotiation_info_seen == 1 )
1545 {
1546 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1547 handshake_failure = 1;
1548 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001549#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001550
1551 if( handshake_failure == 1 )
1552 {
1553 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1554 return( ret );
1555
Paul Bakker48916f92012-09-16 19:57:18 +00001556 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1557 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001558
1559 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1560
1561 return( 0 );
1562}
1563
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001564#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1565 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001566static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
1567 unsigned char *end )
1568{
1569 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1570
Paul Bakker29e1f122013-04-16 13:07:56 +02001571 /*
1572 * Ephemeral DH parameters:
1573 *
1574 * struct {
1575 * opaque dh_p<1..2^16-1>;
1576 * opaque dh_g<1..2^16-1>;
1577 * opaque dh_Ys<1..2^16-1>;
1578 * } ServerDHParams;
1579 */
1580 if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1581 {
1582 SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
1583 return( ret );
1584 }
1585
Manuel Pégourié-Gonnard9ea1b232015-06-29 15:27:52 +02001586 if( ssl->handshake->dhm_ctx.len < SSL_MIN_DHM_BYTES ||
Paul Bakker29e1f122013-04-16 13:07:56 +02001587 ssl->handshake->dhm_ctx.len > 512 )
1588 {
1589 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
1590 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1591 }
1592
1593 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1594 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1595 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001596
1597 return( ret );
1598}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001599#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1600 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001601
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001602#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001603 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001604 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1605 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1606 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1607static int ssl_check_server_ecdh_params( const ssl_context *ssl )
1608{
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001609 const ecp_curve_info *curve_info;
1610
1611 curve_info = ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
1612 if( curve_info == NULL )
1613 {
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001614 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1615 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001616 }
1617
1618 SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001619
Manuel Pégourié-Gonnard29f777e2015-04-03 17:26:50 +02001620#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001621 if( ! ssl_curve_is_acceptable( ssl, ssl->handshake->ecdh_ctx.grp.id ) )
1622#else
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001623 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1624 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001625#endif
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001626 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001627
1628 SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
1629
1630 return( 0 );
1631}
Paul Bakker9af723c2014-05-01 13:03:14 +02001632#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1633 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1634 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1635 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1636 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001637
1638#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1639 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001640 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001641static int ssl_parse_server_ecdh_params( ssl_context *ssl,
1642 unsigned char **p,
1643 unsigned char *end )
1644{
1645 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1646
Paul Bakker29e1f122013-04-16 13:07:56 +02001647 /*
1648 * Ephemeral ECDH parameters:
1649 *
1650 * struct {
1651 * ECParameters curve_params;
1652 * ECPoint public;
1653 * } ServerECDHParams;
1654 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001655 if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
1656 (const unsigned char **) p, end ) ) != 0 )
1657 {
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001658 SSL_DEBUG_RET( 1, ( "ecdh_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001659 return( ret );
1660 }
1661
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001662 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001663 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001664 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001665 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1666 }
1667
Paul Bakker29e1f122013-04-16 13:07:56 +02001668 return( ret );
1669}
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001670#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001671 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1672 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001673
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001674#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001675static int ssl_parse_server_psk_hint( ssl_context *ssl,
1676 unsigned char **p,
1677 unsigned char *end )
1678{
1679 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001680 size_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001681 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001682
1683 /*
1684 * PSK parameters:
1685 *
1686 * opaque psk_identity_hint<0..2^16-1>;
1687 */
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001688 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001689 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001690
1691 if( (*p) + len > end )
1692 {
1693 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
1694 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1695 }
1696
1697 // TODO: Retrieve PSK identity hint and callback to app
1698 //
1699 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001700 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001701
1702 return( ret );
1703}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001704#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001705
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001706#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1707 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1708/*
1709 * Generate a pre-master secret and encrypt it with the server's RSA key
1710 */
1711static int ssl_write_encrypted_pms( ssl_context *ssl,
1712 size_t offset, size_t *olen,
1713 size_t pms_offset )
1714{
1715 int ret;
1716 size_t len_bytes = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 0 : 2;
1717 unsigned char *p = ssl->handshake->premaster + pms_offset;
1718
Manuel Pégourié-Gonnard65125542015-08-27 16:37:35 +02001719 if( offset + len_bytes > SSL_MAX_CONTENT_LEN )
1720 {
1721 SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
1722 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1723 }
1724
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001725 /*
1726 * Generate (part of) the pre-master as
1727 * struct {
1728 * ProtocolVersion client_version;
1729 * opaque random[46];
1730 * } PreMasterSecret;
1731 */
1732 p[0] = (unsigned char) ssl->max_major_ver;
1733 p[1] = (unsigned char) ssl->max_minor_ver;
1734
1735 if( ( ret = ssl->f_rng( ssl->p_rng, p + 2, 46 ) ) != 0 )
1736 {
1737 SSL_DEBUG_RET( 1, "f_rng", ret );
1738 return( ret );
1739 }
1740
1741 ssl->handshake->pmslen = 48;
1742
Manuel Pégourié-Gonnardbb564e02015-09-03 10:44:32 +02001743 if( ssl->session_negotiate->peer_cert == NULL )
1744 {
1745 SSL_DEBUG_MSG( 2, ( "certificate required" ) );
1746 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1747 }
1748
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001749 /*
1750 * Now write it out, encrypted
1751 */
1752 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1753 POLARSSL_PK_RSA ) )
1754 {
1755 SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1756 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1757 }
1758
1759 if( ( ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
1760 p, ssl->handshake->pmslen,
1761 ssl->out_msg + offset + len_bytes, olen,
1762 SSL_MAX_CONTENT_LEN - offset - len_bytes,
1763 ssl->f_rng, ssl->p_rng ) ) != 0 )
1764 {
1765 SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
1766 return( ret );
1767 }
1768
1769#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1770 defined(POLARSSL_SSL_PROTO_TLS1_2)
1771 if( len_bytes == 2 )
1772 {
1773 ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
1774 ssl->out_msg[offset+1] = (unsigned char)( *olen );
1775 *olen += 2;
1776 }
1777#endif
1778
1779 return( 0 );
1780}
1781#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
1782 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001783
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001784#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001785#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001786 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1787 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001788static int ssl_parse_signature_algorithm( ssl_context *ssl,
1789 unsigned char **p,
1790 unsigned char *end,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001791 md_type_t *md_alg,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001792 pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02001793{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001794 ((void) ssl);
Paul Bakker29e1f122013-04-16 13:07:56 +02001795 *md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001796 *pk_alg = POLARSSL_PK_NONE;
1797
1798 /* Only in TLS 1.2 */
1799 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1800 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001801 return( 0 );
1802 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001803
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001804 if( (*p) + 2 > end )
Paul Bakker29e1f122013-04-16 13:07:56 +02001805 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1806
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001807 /*
1808 * Get hash algorithm
1809 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001810 if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001811 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001812 SSL_DEBUG_MSG( 2, ( "Server used unsupported "
1813 "HashAlgorithm %d", *(p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001814 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1815 }
1816
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001817 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001818 * Get signature algorithm
1819 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001820 if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001821 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001822 SSL_DEBUG_MSG( 2, ( "server used unsupported "
1823 "SignatureAlgorithm %d", (*p)[1] ) );
1824 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001825 }
1826
1827 SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
1828 SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
1829 *p += 2;
1830
1831 return( 0 );
1832}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001833#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001834 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1835 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001836#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001837
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001838
1839#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1840 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1841static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
1842{
1843 int ret;
1844 const ecp_keypair *peer_key;
1845
Manuel Pégourié-Gonnardbb564e02015-09-03 10:44:32 +02001846 if( ssl->session_negotiate->peer_cert == NULL )
1847 {
1848 SSL_DEBUG_MSG( 2, ( "certificate required" ) );
1849 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1850 }
1851
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001852 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1853 POLARSSL_PK_ECKEY ) )
1854 {
1855 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
1856 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1857 }
1858
1859 peer_key = pk_ec( ssl->session_negotiate->peer_cert->pk );
1860
1861 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
1862 POLARSSL_ECDH_THEIRS ) ) != 0 )
1863 {
1864 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
1865 return( ret );
1866 }
1867
1868 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1869 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001870 SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001871 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
1872 }
1873
1874 return( ret );
1875}
1876#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1877 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1878
Paul Bakker41c83d32013-03-20 14:39:14 +01001879static int ssl_parse_server_key_exchange( ssl_context *ssl )
1880{
Paul Bakker23986e52011-04-24 08:57:21 +00001881 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001882 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001883 unsigned char *p, *end;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001884#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001885 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1886 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001887 size_t sig_len, params_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001888 unsigned char hash[64];
Paul Bakkerc70b9822013-04-07 22:00:46 +02001889 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001890 size_t hashlen;
1891 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001892#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001893
1894 SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
1895
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001896#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001897 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001898 {
1899 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1900 ssl->state++;
1901 return( 0 );
1902 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001903 ((void) p);
1904 ((void) end);
1905#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001906
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001907#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1908 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1909 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
1910 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
1911 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001912 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
1913 {
1914 SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
1915 return( ret );
1916 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001917
1918 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1919 ssl->state++;
1920 return( 0 );
1921 }
1922 ((void) p);
1923 ((void) end);
Paul Bakker9af723c2014-05-01 13:03:14 +02001924#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1925 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001926
Paul Bakker5121ce52009-01-03 21:22:43 +00001927 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1928 {
1929 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1930 return( ret );
1931 }
1932
1933 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1934 {
1935 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001936 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001937 }
1938
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001939 /*
1940 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
1941 * doesn't use a psk_identity_hint
1942 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001943 if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
1944 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001945 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1946 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02001947 {
Hanno Becker10699cc2017-06-08 15:41:02 +01001948 /* Current message is probably either
1949 * CertificateRequest or ServerHelloDone */
1950 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02001951 goto exit;
1952 }
1953
1954 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1955 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001956 }
1957
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001958 p = ssl->in_msg + 4;
1959 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001960 SSL_DEBUG_BUF( 3, "server key exchange", p, ssl->in_hslen - 4 );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001961
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001962#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1963 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1964 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1965 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1966 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1967 {
1968 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1969 {
1970 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1971 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1972 }
1973 } /* FALLTROUGH */
1974#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
1975
1976#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
1977 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1978 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1979 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1980 ; /* nothing more to do */
1981 else
1982#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
1983 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
1984#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1985 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1986 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1987 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001988 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001989 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001990 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001991 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001992 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1993 }
1994 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001995 else
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001996#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1997 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001998#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001999 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002000 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2001 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002002 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002003 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002004 {
2005 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
2006 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002007 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2008 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2009 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002010 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002011 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002012#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002013 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002014 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002015 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002016 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002017 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002018 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002019
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002020#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002021 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2022 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02002023 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002024 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2025 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002026 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002027 params_len = p - ( ssl->in_msg + 4 );
2028
Paul Bakker29e1f122013-04-16 13:07:56 +02002029 /*
2030 * Handle the digitally-signed structure
2031 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002032#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2033 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002034 {
Paul Bakker9659dae2013-08-28 16:21:34 +02002035 if( ssl_parse_signature_algorithm( ssl, &p, end,
2036 &md_alg, &pk_alg ) != 0 )
2037 {
2038 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2039 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2040 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002041
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002042 if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002043 {
2044 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2045 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2046 }
Simon Butcher14400c82016-01-02 00:08:13 +00002047
2048#if !defined(POLARSSL_SSL_ENABLE_MD5_SIGNATURES)
2049 if( md_alg == POLARSSL_MD_MD5 )
2050 {
2051 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2052 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2053 }
2054#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002055 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002056 else
Paul Bakker9af723c2014-05-01 13:03:14 +02002057#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002058#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2059 defined(POLARSSL_SSL_PROTO_TLS1_1)
2060 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002061 {
2062 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002063
Paul Bakker9659dae2013-08-28 16:21:34 +02002064 /* Default hash for ECDSA is SHA-1 */
2065 if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
2066 md_alg = POLARSSL_MD_SHA1;
2067 }
2068 else
2069#endif
2070 {
2071 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002072 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker9659dae2013-08-28 16:21:34 +02002073 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002074
2075 /*
2076 * Read signature
2077 */
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002078 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002079 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002080
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002081 if( end != p + sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002082 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002083 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01002084 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2085 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002086
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002087 SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002088
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002089 /*
2090 * Compute the hash that has been signed
2091 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002092#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2093 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002094 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002095 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002096 md5_context md5;
2097 sha1_context sha1;
2098
Paul Bakker5b4af392014-06-26 12:09:34 +02002099 md5_init( &md5 );
2100 sha1_init( &sha1 );
2101
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002102 hashlen = 36;
2103
Paul Bakker29e1f122013-04-16 13:07:56 +02002104 /*
2105 * digitally-signed struct {
2106 * opaque md5_hash[16];
2107 * opaque sha_hash[20];
2108 * };
2109 *
2110 * md5_hash
2111 * MD5(ClientHello.random + ServerHello.random
2112 * + ServerParams);
2113 * sha_hash
2114 * SHA(ClientHello.random + ServerHello.random
2115 * + ServerParams);
2116 */
Paul Bakker29e1f122013-04-16 13:07:56 +02002117 md5_starts( &md5 );
2118 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002119 md5_update( &md5, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002120 md5_finish( &md5, hash );
2121
2122 sha1_starts( &sha1 );
2123 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002124 sha1_update( &sha1, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002125 sha1_finish( &sha1, hash + 16 );
Paul Bakker5b4af392014-06-26 12:09:34 +02002126
2127 md5_free( &md5 );
2128 sha1_free( &sha1 );
Paul Bakker29e1f122013-04-16 13:07:56 +02002129 }
2130 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002131#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2132 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002133#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2134 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002135 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002136 {
2137 md_context_t ctx;
2138
Paul Bakker84bbeb52014-07-01 14:53:22 +02002139 md_init( &ctx );
2140
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002141 /* Info from md_alg will be used instead */
2142 hashlen = 0;
Paul Bakker29e1f122013-04-16 13:07:56 +02002143
2144 /*
2145 * digitally-signed struct {
2146 * opaque client_random[32];
2147 * opaque server_random[32];
2148 * ServerDHParams params;
2149 * };
2150 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002151 if( ( ret = md_init_ctx( &ctx,
2152 md_info_from_type( md_alg ) ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002153 {
2154 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2155 return( ret );
2156 }
2157
2158 md_starts( &ctx );
2159 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002160 md_update( &ctx, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002161 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002162 md_free( &ctx );
Paul Bakker29e1f122013-04-16 13:07:56 +02002163 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002164 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002165#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2166 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02002167 {
Paul Bakker577e0062013-08-28 11:57:20 +02002168 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002169 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002170 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002171
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002172 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2173 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker29e1f122013-04-16 13:07:56 +02002174
Manuel Pégourié-Gonnardbb564e02015-09-03 10:44:32 +02002175 if( ssl->session_negotiate->peer_cert == NULL )
2176 {
2177 SSL_DEBUG_MSG( 2, ( "certificate required" ) );
2178 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2179 }
2180
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002181 /*
2182 * Verify signature
2183 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02002184 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002185 {
2186 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2187 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2188 }
2189
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002190 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
2191 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002192 {
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002193 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002194 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002195 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002196 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002197#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002198 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2199 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002200
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002201exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002202 ssl->state++;
2203
2204 SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
2205
2206 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002207}
2208
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002209#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2210 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Simon Butcher7458bc32016-09-05 11:18:39 +03002211 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002212 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
Simon Butcher7458bc32016-09-05 11:18:39 +03002213 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)&& \
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002214 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2215static int ssl_parse_certificate_request( ssl_context *ssl )
2216{
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002217 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2218
2219 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2220
2221 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2222 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2223 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2224 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2225 {
2226 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2227 ssl->state++;
2228 return( 0 );
2229 }
2230
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002231 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2232 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002233}
2234#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002235static int ssl_parse_certificate_request( ssl_context *ssl )
2236{
2237 int ret;
Paul Bakker926af752012-11-23 13:38:07 +01002238 unsigned char *buf, *p;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002239 size_t n = 0, m = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002240 size_t cert_type_len = 0, dn_len = 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002241 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002242
2243 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2244
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002245 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2246 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2247 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2248 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2249 {
2250 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2251 ssl->state++;
2252 return( 0 );
2253 }
2254
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 /*
2256 * 0 . 0 handshake type
2257 * 1 . 3 handshake length
Paul Bakker926af752012-11-23 13:38:07 +01002258 * 4 . 4 cert type count
2259 * 5 .. m-1 cert types
2260 * m .. m+1 sig alg length (TLS 1.2 only)
2261 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002262 * n .. n+1 length of all DNs
2263 * n+2 .. n+3 length of DN 1
2264 * n+4 .. ... Distinguished Name #1
2265 * ... .. ... length of DN 2, etc.
2266 */
Hanno Becker10699cc2017-06-08 15:41:02 +01002267
2268 if( ( ret = ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002269 {
Hanno Becker10699cc2017-06-08 15:41:02 +01002270 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2271 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002272 }
2273
Hanno Becker10699cc2017-06-08 15:41:02 +01002274 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2275 {
2276 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2277 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2278 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002279
Hanno Becker10699cc2017-06-08 15:41:02 +01002280 ssl->state++;
2281 ssl->client_auth = ( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST );
Paul Bakker5121ce52009-01-03 21:22:43 +00002282
2283 SSL_DEBUG_MSG( 3, ( "got %s certificate request",
2284 ssl->client_auth ? "a" : "no" ) );
2285
Paul Bakker926af752012-11-23 13:38:07 +01002286 if( ssl->client_auth == 0 )
Hanno Becker10699cc2017-06-08 15:41:02 +01002287 {
2288 /* Current message is probably the ServerHelloDone */
2289 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002290 goto exit;
Hanno Becker10699cc2017-06-08 15:41:02 +01002291 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002292
Paul Bakker926af752012-11-23 13:38:07 +01002293 // TODO: handshake_failure alert for an anonymous server to request
2294 // client authentication
2295
2296 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002297
Paul Bakker926af752012-11-23 13:38:07 +01002298 // Retrieve cert types
2299 //
2300 cert_type_len = buf[4];
2301 n = cert_type_len;
2302
2303 if( ssl->in_hslen < 6 + n )
2304 {
2305 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2306 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2307 }
2308
Paul Bakker73d44312013-05-22 13:56:26 +02002309 p = buf + 5;
Paul Bakker926af752012-11-23 13:38:07 +01002310 while( cert_type_len > 0 )
2311 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002312#if defined(POLARSSL_RSA_C)
2313 if( *p == SSL_CERT_TYPE_RSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002314 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker926af752012-11-23 13:38:07 +01002315 {
2316 ssl->handshake->cert_type = SSL_CERT_TYPE_RSA_SIGN;
2317 break;
2318 }
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002319 else
2320#endif
2321#if defined(POLARSSL_ECDSA_C)
2322 if( *p == SSL_CERT_TYPE_ECDSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002323 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002324 {
2325 ssl->handshake->cert_type = SSL_CERT_TYPE_ECDSA_SIGN;
2326 break;
2327 }
2328 else
2329#endif
2330 {
2331 ; /* Unsupported cert type, ignore */
2332 }
Paul Bakker926af752012-11-23 13:38:07 +01002333
2334 cert_type_len--;
2335 p++;
2336 }
2337
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002338#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002339 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2340 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002341 /* Ignored, see comments about hash in write_certificate_verify */
2342 // TODO: should check the signature part against our pk_key though
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002343 size_t sig_alg_len = ( ( buf[5 + n] << 8 )
2344 | ( buf[6 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002345
2346 p = buf + 7 + n;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002347 m += 2;
Paul Bakker926af752012-11-23 13:38:07 +01002348 n += sig_alg_len;
2349
2350 if( ssl->in_hslen < 6 + n )
2351 {
2352 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2353 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2354 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002355 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002356#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker926af752012-11-23 13:38:07 +01002357
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002358 /* Ignore certificate_authorities, we only have one cert anyway */
2359 // TODO: should not send cert if no CA matches
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002360 dn_len = ( ( buf[5 + m + n] << 8 )
2361 | ( buf[6 + m + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002362
2363 n += dn_len;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002364 if( ssl->in_hslen != 7 + m + n )
Paul Bakker926af752012-11-23 13:38:07 +01002365 {
2366 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2367 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2368 }
2369
2370exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002371 SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2372
2373 return( 0 );
2374}
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002375#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2376 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Simon Butcher7458bc32016-09-05 11:18:39 +03002377 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLE &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002378 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
Simon Butcher7458bc32016-09-05 11:18:39 +03002379 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002380 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002381
2382static int ssl_parse_server_hello_done( ssl_context *ssl )
2383{
2384 int ret;
2385
2386 SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
2387
Hanno Becker10699cc2017-06-08 15:41:02 +01002388 if( ( ret = ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002389 {
Hanno Becker10699cc2017-06-08 15:41:02 +01002390 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2391 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002392 }
Hanno Becker10699cc2017-06-08 15:41:02 +01002393
2394 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2395 {
2396 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
2397 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2398 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002399
2400 if( ssl->in_hslen != 4 ||
2401 ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
2402 {
2403 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002404 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002405 }
2406
2407 ssl->state++;
2408
2409 SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
2410
2411 return( 0 );
2412}
2413
2414static int ssl_write_client_key_exchange( ssl_context *ssl )
2415{
Paul Bakker23986e52011-04-24 08:57:21 +00002416 int ret;
2417 size_t i, n;
Paul Bakker41c83d32013-03-20 14:39:14 +01002418 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002419
2420 SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
2421
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002422#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002423 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002425 /*
2426 * DHM key exchange -- send G^X mod P
2427 */
Paul Bakker48916f92012-09-16 19:57:18 +00002428 n = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002429
2430 ssl->out_msg[4] = (unsigned char)( n >> 8 );
2431 ssl->out_msg[5] = (unsigned char)( n );
2432 i = 6;
2433
Paul Bakker29b64762012-09-25 09:36:44 +00002434 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002435 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker5121ce52009-01-03 21:22:43 +00002436 &ssl->out_msg[i], n,
2437 ssl->f_rng, ssl->p_rng );
2438 if( ret != 0 )
2439 {
2440 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2441 return( ret );
2442 }
2443
Paul Bakker48916f92012-09-16 19:57:18 +00002444 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2445 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002446
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002447 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002448
Paul Bakker48916f92012-09-16 19:57:18 +00002449 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2450 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002451 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002452 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002453 {
2454 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2455 return( ret );
2456 }
2457
Paul Bakker48916f92012-09-16 19:57:18 +00002458 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002459 }
2460 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002461#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002462#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002463 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2464 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2465 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002466 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002467 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2468 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2469 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002470 {
2471 /*
2472 * ECDH key exchange -- send client public value
2473 */
2474 i = 4;
2475
2476 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
2477 &n,
2478 &ssl->out_msg[i], 1000,
2479 ssl->f_rng, ssl->p_rng );
2480 if( ret != 0 )
2481 {
2482 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2483 return( ret );
2484 }
2485
2486 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2487
2488 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2489 &ssl->handshake->pmslen,
2490 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002491 POLARSSL_MPI_MAX_SIZE,
2492 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002493 {
2494 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2495 return( ret );
2496 }
2497
2498 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2499 }
2500 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002501#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002502 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2503 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2504 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002505#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002506 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002507 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002508 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2509 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002510 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002511 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002512 * opaque psk_identity<0..2^16-1>;
2513 */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002514 if( ssl->psk == NULL || ssl->psk_identity == NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002515 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2516
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002517 i = 4;
2518 n = ssl->psk_identity_len;
Manuel Pégourié-Gonnard65125542015-08-27 16:37:35 +02002519
2520 if( i + 2 + n > SSL_MAX_CONTENT_LEN )
2521 {
2522 SSL_DEBUG_MSG( 1, ( "psk identity too long or "
2523 "SSL buffer too short" ) );
2524 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2525 }
2526
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002527 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2528 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002529
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002530 memcpy( ssl->out_msg + i, ssl->psk_identity, ssl->psk_identity_len );
2531 i += ssl->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002532
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002533#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002534 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002535 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002536 n = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002537 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002538 else
2539#endif
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002540#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2541 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2542 {
2543 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2544 return( ret );
2545 }
2546 else
2547#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002548#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002549 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002550 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002551 /*
2552 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
2553 */
2554 n = ssl->handshake->dhm_ctx.len;
Manuel Pégourié-Gonnard65125542015-08-27 16:37:35 +02002555
2556 if( i + 2 + n > SSL_MAX_CONTENT_LEN )
2557 {
2558 SSL_DEBUG_MSG( 1, ( "psk identity or DHM size too long"
2559 " or SSL buffer too short" ) );
2560 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2561 }
2562
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002563 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2564 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002565
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002566 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakker68881672013-10-15 13:24:01 +02002567 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002568 &ssl->out_msg[i], n,
2569 ssl->f_rng, ssl->p_rng );
2570 if( ret != 0 )
2571 {
2572 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2573 return( ret );
2574 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002575 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002576 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002577#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002578#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002579 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002580 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002581 /*
2582 * ClientECDiffieHellmanPublic public;
2583 */
2584 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
2585 &ssl->out_msg[i], SSL_MAX_CONTENT_LEN - i,
2586 ssl->f_rng, ssl->p_rng );
2587 if( ret != 0 )
2588 {
2589 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2590 return( ret );
2591 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002592
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002593 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2594 }
2595 else
2596#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2597 {
2598 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002599 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002600 }
2601
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002602 if( ( ret = ssl_psk_derive_premaster( ssl,
2603 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002604 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002605 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002606 return( ret );
2607 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002608 }
2609 else
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002610#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002611#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +02002612 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002613 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002614 i = 4;
2615 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002616 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002617 }
Paul Bakkered27a042013-04-18 22:46:23 +02002618 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002619#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002620 {
2621 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002622 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002623 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02002624 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002625
Paul Bakker5121ce52009-01-03 21:22:43 +00002626 ssl->out_msglen = i + n;
2627 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2628 ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE;
2629
2630 ssl->state++;
2631
2632 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2633 {
2634 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2635 return( ret );
2636 }
2637
2638 SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
2639
2640 return( 0 );
2641}
2642
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002643#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2644 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Simon Butcher7458bc32016-09-05 11:18:39 +03002645 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002646 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
Simon Butcher7458bc32016-09-05 11:18:39 +03002647 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002648 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002649static int ssl_write_certificate_verify( ssl_context *ssl )
2650{
Paul Bakkered27a042013-04-18 22:46:23 +02002651 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002652 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002653
2654 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2655
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002656 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2657 {
2658 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2659 return( ret );
2660 }
2661
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002662 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002663 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002664 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002665 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002666 {
2667 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2668 ssl->state++;
2669 return( 0 );
2670 }
2671
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002672 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2673 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002674}
2675#else
2676static int ssl_write_certificate_verify( ssl_context *ssl )
2677{
2678 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2679 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2680 size_t n = 0, offset = 0;
2681 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002682 unsigned char *hash_start = hash;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002683 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002684 unsigned int hashlen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002685
2686 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2687
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002688 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2689 {
2690 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2691 return( ret );
2692 }
2693
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002694 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002695 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002696 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002697 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2698 {
2699 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2700 ssl->state++;
2701 return( 0 );
2702 }
2703
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002704 if( ssl->client_auth == 0 || ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002705 {
2706 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2707 ssl->state++;
2708 return( 0 );
2709 }
2710
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002711 if( ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002712 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00002713 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2714 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002715 }
2716
2717 /*
2718 * Make an RSA signature of the handshake digests
2719 */
Paul Bakker48916f92012-09-16 19:57:18 +00002720 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002721
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002722#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2723 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker926af752012-11-23 13:38:07 +01002724 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002725 {
Paul Bakker926af752012-11-23 13:38:07 +01002726 /*
2727 * digitally-signed struct {
2728 * opaque md5_hash[16];
2729 * opaque sha_hash[20];
2730 * };
2731 *
2732 * md5_hash
2733 * MD5(handshake_messages);
2734 *
2735 * sha_hash
2736 * SHA(handshake_messages);
2737 */
2738 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002739 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002740
2741 /*
2742 * For ECDSA, default hash is SHA-1 only
2743 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002744 if( pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002745 {
2746 hash_start += 16;
2747 hashlen -= 16;
2748 md_alg = POLARSSL_MD_SHA1;
2749 }
Paul Bakker926af752012-11-23 13:38:07 +01002750 }
2751 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002752#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2753 POLARSSL_SSL_PROTO_TLS1_1 */
2754#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2755 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002756 {
2757 /*
2758 * digitally-signed struct {
2759 * opaque handshake_messages[handshake_messages_length];
2760 * };
2761 *
2762 * Taking shortcut here. We assume that the server always allows the
2763 * PRF Hash function and has sent it in the allowed signature
2764 * algorithms list received in the Certificate Request message.
2765 *
2766 * Until we encounter a server that does not, we will take this
2767 * shortcut.
2768 *
2769 * Reason: Otherwise we should have running hashes for SHA512 and SHA224
2770 * in order to satisfy 'weird' needs from the server side.
2771 */
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002772 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2773 POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002774 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002775 md_alg = POLARSSL_MD_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002776 ssl->out_msg[4] = SSL_HASH_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002777 }
2778 else
2779 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002780 md_alg = POLARSSL_MD_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002781 ssl->out_msg[4] = SSL_HASH_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002782 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002783 ssl->out_msg[5] = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002784
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002785 /* Info from md_alg will be used instead */
2786 hashlen = 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002787 offset = 2;
2788 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002789 else
2790#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002791 {
2792 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002793 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002794 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002795
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002796 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002797 ssl->out_msg + 6 + offset, &n,
2798 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002799 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002800 SSL_DEBUG_RET( 1, "pk_sign", ret );
2801 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002802 }
Paul Bakker926af752012-11-23 13:38:07 +01002803
Paul Bakker1ef83d62012-04-11 12:09:53 +00002804 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
2805 ssl->out_msg[5 + offset] = (unsigned char)( n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002806
Paul Bakker1ef83d62012-04-11 12:09:53 +00002807 ssl->out_msglen = 6 + n + offset;
Paul Bakker5121ce52009-01-03 21:22:43 +00002808 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2809 ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY;
2810
2811 ssl->state++;
2812
2813 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2814 {
2815 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2816 return( ret );
2817 }
2818
2819 SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
2820
Paul Bakkered27a042013-04-18 22:46:23 +02002821 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002822}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002823#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2824 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Simon Butcher7458bc32016-09-05 11:18:39 +03002825 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED &&
2826 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2827 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED &&
2828 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002829
Paul Bakkera503a632013-08-14 13:48:06 +02002830#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002831static int ssl_parse_new_session_ticket( ssl_context *ssl )
2832{
2833 int ret;
2834 uint32_t lifetime;
2835 size_t ticket_len;
2836 unsigned char *ticket;
2837
2838 SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2839
2840 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2841 {
2842 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2843 return( ret );
2844 }
2845
2846 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2847 {
2848 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2849 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2850 }
2851
2852 /*
2853 * struct {
2854 * uint32 ticket_lifetime_hint;
2855 * opaque ticket<0..2^16-1>;
2856 * } NewSessionTicket;
2857 *
2858 * 0 . 0 handshake message type
2859 * 1 . 3 handshake message length
2860 * 4 . 7 ticket_lifetime_hint
2861 * 8 . 9 ticket_len (n)
2862 * 10 . 9+n ticket content
2863 */
2864 if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
2865 ssl->in_hslen < 10 )
2866 {
2867 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2868 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2869 }
2870
2871 lifetime = ( ssl->in_msg[4] << 24 ) | ( ssl->in_msg[5] << 16 ) |
2872 ( ssl->in_msg[6] << 8 ) | ( ssl->in_msg[7] );
2873
2874 ticket_len = ( ssl->in_msg[8] << 8 ) | ( ssl->in_msg[9] );
2875
2876 if( ticket_len + 10 != ssl->in_hslen )
2877 {
2878 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2879 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2880 }
2881
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002882 SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
2883
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002884 /* We're not waiting for a NewSessionTicket message any more */
2885 ssl->handshake->new_session_ticket = 0;
2886
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002887 /*
2888 * Zero-length ticket means the server changed his mind and doesn't want
2889 * to send a ticket after all, so just forget it
2890 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002891 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002892 return( 0 );
2893
Paul Bakker34617722014-06-13 17:20:13 +02002894 polarssl_zeroize( ssl->session_negotiate->ticket,
2895 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002896 polarssl_free( ssl->session_negotiate->ticket );
2897 ssl->session_negotiate->ticket = NULL;
2898 ssl->session_negotiate->ticket_len = 0;
2899
2900 if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
2901 {
2902 SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
2903 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2904 }
2905
2906 memcpy( ticket, ssl->in_msg + 10, ticket_len );
2907
2908 ssl->session_negotiate->ticket = ticket;
2909 ssl->session_negotiate->ticket_len = ticket_len;
2910 ssl->session_negotiate->ticket_lifetime = lifetime;
2911
2912 /*
2913 * RFC 5077 section 3.4:
2914 * "If the client receives a session ticket from the server, then it
2915 * discards any Session ID that was sent in the ServerHello."
2916 */
2917 SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
2918 ssl->session_negotiate->length = 0;
2919
2920 SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2921
2922 return( 0 );
2923}
Paul Bakkera503a632013-08-14 13:48:06 +02002924#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002925
Paul Bakker5121ce52009-01-03 21:22:43 +00002926/*
Paul Bakker1961b702013-01-25 14:49:24 +01002927 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002928 */
Paul Bakker1961b702013-01-25 14:49:24 +01002929int ssl_handshake_client_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002930{
2931 int ret = 0;
2932
Paul Bakker1961b702013-01-25 14:49:24 +01002933 if( ssl->state == SSL_HANDSHAKE_OVER )
2934 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002935
Paul Bakker1961b702013-01-25 14:49:24 +01002936 SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
2937
2938 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2939 return( ret );
2940
2941 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002942 {
Paul Bakker1961b702013-01-25 14:49:24 +01002943 case SSL_HELLO_REQUEST:
2944 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002945 break;
2946
Paul Bakker1961b702013-01-25 14:49:24 +01002947 /*
2948 * ==> ClientHello
2949 */
2950 case SSL_CLIENT_HELLO:
2951 ret = ssl_write_client_hello( ssl );
2952 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002953
Paul Bakker1961b702013-01-25 14:49:24 +01002954 /*
2955 * <== ServerHello
2956 * Certificate
2957 * ( ServerKeyExchange )
2958 * ( CertificateRequest )
2959 * ServerHelloDone
2960 */
2961 case SSL_SERVER_HELLO:
2962 ret = ssl_parse_server_hello( ssl );
2963 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002964
Paul Bakker1961b702013-01-25 14:49:24 +01002965 case SSL_SERVER_CERTIFICATE:
2966 ret = ssl_parse_certificate( ssl );
2967 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002968
Paul Bakker1961b702013-01-25 14:49:24 +01002969 case SSL_SERVER_KEY_EXCHANGE:
2970 ret = ssl_parse_server_key_exchange( ssl );
2971 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002972
Paul Bakker1961b702013-01-25 14:49:24 +01002973 case SSL_CERTIFICATE_REQUEST:
2974 ret = ssl_parse_certificate_request( ssl );
2975 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002976
Paul Bakker1961b702013-01-25 14:49:24 +01002977 case SSL_SERVER_HELLO_DONE:
2978 ret = ssl_parse_server_hello_done( ssl );
2979 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002980
Paul Bakker1961b702013-01-25 14:49:24 +01002981 /*
2982 * ==> ( Certificate/Alert )
2983 * ClientKeyExchange
2984 * ( CertificateVerify )
2985 * ChangeCipherSpec
2986 * Finished
2987 */
2988 case SSL_CLIENT_CERTIFICATE:
2989 ret = ssl_write_certificate( ssl );
2990 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002991
Paul Bakker1961b702013-01-25 14:49:24 +01002992 case SSL_CLIENT_KEY_EXCHANGE:
2993 ret = ssl_write_client_key_exchange( ssl );
2994 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002995
Paul Bakker1961b702013-01-25 14:49:24 +01002996 case SSL_CERTIFICATE_VERIFY:
2997 ret = ssl_write_certificate_verify( ssl );
2998 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002999
Paul Bakker1961b702013-01-25 14:49:24 +01003000 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3001 ret = ssl_write_change_cipher_spec( ssl );
3002 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003003
Paul Bakker1961b702013-01-25 14:49:24 +01003004 case SSL_CLIENT_FINISHED:
3005 ret = ssl_write_finished( ssl );
3006 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003007
Paul Bakker1961b702013-01-25 14:49:24 +01003008 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003009 * <== ( NewSessionTicket )
3010 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003011 * Finished
3012 */
3013 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003014#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003015 if( ssl->handshake->new_session_ticket != 0 )
3016 ret = ssl_parse_new_session_ticket( ssl );
3017 else
Paul Bakkera503a632013-08-14 13:48:06 +02003018#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003019 ret = ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003020 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003021
Paul Bakker1961b702013-01-25 14:49:24 +01003022 case SSL_SERVER_FINISHED:
3023 ret = ssl_parse_finished( ssl );
3024 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003025
Paul Bakker1961b702013-01-25 14:49:24 +01003026 case SSL_FLUSH_BUFFERS:
3027 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3028 ssl->state = SSL_HANDSHAKE_WRAPUP;
3029 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003030
Paul Bakker1961b702013-01-25 14:49:24 +01003031 case SSL_HANDSHAKE_WRAPUP:
3032 ssl_handshake_wrapup( ssl );
3033 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003034
Paul Bakker1961b702013-01-25 14:49:24 +01003035 default:
3036 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3037 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3038 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003039
3040 return( ret );
3041}
Paul Bakker9af723c2014-05-01 13:03:14 +02003042#endif /* POLARSSL_SSL_CLI_C */