blob: e1cebbf4b97ac1a12f669a9f594212dba0b0b031 [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 Follath3f819732016-03-22 15:49:23 +0000333 if( info == NULL )
334 {
335 SSL_DEBUG_MSG( 1, ( "invalid curve in ssl configuration" ) );
336 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
359 elliptic_curve_list[elliptic_curve_len++] = info->tls_id >> 8;
360 elliptic_curve_list[elliptic_curve_len++] = info->tls_id & 0xFF;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200361 }
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200362
363 if( elliptic_curve_len == 0 )
364 return;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100365
366 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES >> 8 ) & 0xFF );
367 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ) & 0xFF );
368
369 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF );
370 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) ) & 0xFF );
371
372 *p++ = (unsigned char)( ( ( elliptic_curve_len ) >> 8 ) & 0xFF );
373 *p++ = (unsigned char)( ( ( elliptic_curve_len ) ) & 0xFF );
374
Paul Bakkerd3edc862013-03-20 16:07:17 +0100375 *olen = 6 + elliptic_curve_len;
376}
377
378static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
379 unsigned char *buf,
380 size_t *olen )
381{
382 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100383 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100384
385 *olen = 0;
386
387 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_point_formats extension" ) );
388
Simon Butcherb1e325d2015-10-01 00:24:36 +0100389 if( end < p || (size_t)( end - p ) < 6 )
390 {
391 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
392 return;
393 }
394
Paul Bakkerd3edc862013-03-20 16:07:17 +0100395 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
396 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
397
398 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100399 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200400
401 *p++ = 1;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100402 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
403
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200404 *olen = 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100405}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200406#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100407
Paul Bakker05decb22013-08-15 13:33:48 +0200408#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200409static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
410 unsigned char *buf,
411 size_t *olen )
412{
413 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100414 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200415
Simon Butcherb1e325d2015-10-01 00:24:36 +0100416 *olen = 0;
417
418 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE )
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200419 return;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200420
421 SSL_DEBUG_MSG( 3, ( "client hello, adding max_fragment_length extension" ) );
422
Simon Butcherb1e325d2015-10-01 00:24:36 +0100423 if( end < p || (size_t)( end - p ) < 5 )
424 {
425 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
426 return;
427 }
428
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200429 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
430 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
431
432 *p++ = 0x00;
433 *p++ = 1;
434
435 *p++ = ssl->mfl_code;
436
437 *olen = 5;
438}
Paul Bakker05decb22013-08-15 13:33:48 +0200439#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200440
Paul Bakker1f2bc622013-08-15 13:45:55 +0200441#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200442static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
443 unsigned char *buf, size_t *olen )
444{
445 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100446 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
447
448 *olen = 0;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200449
450 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200451 return;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200452
453 SSL_DEBUG_MSG( 3, ( "client hello, adding truncated_hmac extension" ) );
454
Simon Butcherb1e325d2015-10-01 00:24:36 +0100455 if( end < p || (size_t)( end - p ) < 4 )
456 {
457 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
458 return;
459 }
460
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200461 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
462 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
463
464 *p++ = 0x00;
465 *p++ = 0x00;
466
467 *olen = 4;
468}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200469#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200470
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100471#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
472static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
473 unsigned char *buf, size_t *olen )
474{
475 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100476 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
477
478 *olen = 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100479
480 if( ssl->encrypt_then_mac == SSL_ETM_DISABLED ||
481 ssl->max_minor_ver == SSL_MINOR_VERSION_0 )
482 {
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100483 return;
484 }
485
486 SSL_DEBUG_MSG( 3, ( "client hello, adding encrypt_then_mac "
487 "extension" ) );
488
Simon Butcherb1e325d2015-10-01 00:24:36 +0100489 if( end < p || (size_t)( end - p ) < 4 )
490 {
491 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
492 return;
493 }
494
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100495 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
496 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
497
498 *p++ = 0x00;
499 *p++ = 0x00;
500
501 *olen = 4;
502}
503#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
504
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200505#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
506static void ssl_write_extended_ms_ext( ssl_context *ssl,
507 unsigned char *buf, size_t *olen )
508{
509 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100510 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
511
512 *olen = 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200513
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200514 if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED ||
515 ssl->max_minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200516 {
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200517 return;
518 }
519
520 SSL_DEBUG_MSG( 3, ( "client hello, adding extended_master_secret "
521 "extension" ) );
522
Simon Butcherb1e325d2015-10-01 00:24:36 +0100523 if( end < p || (size_t)( end - p ) < 4 )
524 {
525 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
526 return;
527 }
528
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200529 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
530 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
531
532 *p++ = 0x00;
533 *p++ = 0x00;
534
535 *olen = 4;
536}
537#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
538
Paul Bakkera503a632013-08-14 13:48:06 +0200539#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200540static void ssl_write_session_ticket_ext( ssl_context *ssl,
541 unsigned char *buf, size_t *olen )
542{
543 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100544 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200545 size_t tlen = ssl->session_negotiate->ticket_len;
546
Simon Butcherb1e325d2015-10-01 00:24:36 +0100547 *olen = 0;
548
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200549 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200550 return;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200551
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200552 SSL_DEBUG_MSG( 3, ( "client hello, adding session ticket extension" ) );
553
Simon Butcherb1e325d2015-10-01 00:24:36 +0100554 if( end < p || (size_t)( end - p ) < 4 + tlen )
555 {
556 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
557 return;
558 }
559
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200560 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
561 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
562
563 *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF );
564 *p++ = (unsigned char)( ( tlen ) & 0xFF );
565
566 *olen = 4;
567
568 if( ssl->session_negotiate->ticket == NULL ||
569 ssl->session_negotiate->ticket_len == 0 )
570 {
571 return;
572 }
573
574 SSL_DEBUG_MSG( 3, ( "sending session ticket of length %d", tlen ) );
575
576 memcpy( p, ssl->session_negotiate->ticket, tlen );
577
578 *olen += tlen;
579}
Paul Bakkera503a632013-08-14 13:48:06 +0200580#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200581
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200582#if defined(POLARSSL_SSL_ALPN)
583static void ssl_write_alpn_ext( ssl_context *ssl,
584 unsigned char *buf, size_t *olen )
585{
586 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100587 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
588 size_t alpnlen = 0;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200589 const char **cur;
590
Simon Butcherb1e325d2015-10-01 00:24:36 +0100591 *olen = 0;
592
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200593 if( ssl->alpn_list == NULL )
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200594 return;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200595
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +0200596 SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200597
Simon Butcherb1e325d2015-10-01 00:24:36 +0100598 for( cur = ssl->alpn_list; *cur != NULL; cur++ )
599 alpnlen += (unsigned char)( strlen( *cur ) & 0xFF ) + 1;
600
601 if( end < p || (size_t)( end - p ) < 6 + alpnlen )
602 {
603 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
604 return;
605 }
606
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200607 *p++ = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
608 *p++ = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
609
610 /*
611 * opaque ProtocolName<1..2^8-1>;
612 *
613 * struct {
614 * ProtocolName protocol_name_list<2..2^16-1>
615 * } ProtocolNameList;
616 */
617
618 /* Skip writing extension and list length for now */
619 p += 4;
620
621 for( cur = ssl->alpn_list; *cur != NULL; cur++ )
622 {
623 *p = (unsigned char)( strlen( *cur ) & 0xFF );
624 memcpy( p + 1, *cur, *p );
625 p += 1 + *p;
626 }
627
628 *olen = p - buf;
629
630 /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
631 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
632 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
633
634 /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
635 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
636 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
637}
638#endif /* POLARSSL_SSL_ALPN */
639
Paul Bakker5121ce52009-01-03 21:22:43 +0000640static int ssl_write_client_hello( ssl_context *ssl )
641{
Paul Bakker23986e52011-04-24 08:57:21 +0000642 int ret;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100643 size_t i, n, olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000644 unsigned char *buf;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200645 unsigned char *p, *q;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200646#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000647 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200648#endif
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200649 const int *ciphersuites;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200650 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000651
652 SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
653
Paul Bakkera9a028e2013-11-21 17:31:06 +0100654 if( ssl->f_rng == NULL )
655 {
656 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
657 return( POLARSSL_ERR_SSL_NO_RNG );
658 }
659
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100660#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +0000661 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100662#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000663 {
Paul Bakker993d11d2012-09-28 15:00:12 +0000664 ssl->major_ver = ssl->min_major_ver;
665 ssl->minor_ver = ssl->min_minor_ver;
Paul Bakker48916f92012-09-16 19:57:18 +0000666 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000667
Paul Bakker490ecc82011-10-06 13:04:09 +0000668 if( ssl->max_major_ver == 0 && ssl->max_minor_ver == 0 )
669 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200670 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
671 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker490ecc82011-10-06 13:04:09 +0000672 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000673
674 /*
675 * 0 . 0 handshake type
676 * 1 . 3 handshake length
677 * 4 . 5 highest version supported
678 * 6 . 9 current UNIX time
679 * 10 . 37 random bytes
680 */
681 buf = ssl->out_msg;
682 p = buf + 4;
683
684 *p++ = (unsigned char) ssl->max_major_ver;
685 *p++ = (unsigned char) ssl->max_minor_ver;
686
687 SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
688 buf[4], buf[5] ) );
689
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200690#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000691 t = time( NULL );
692 *p++ = (unsigned char)( t >> 24 );
693 *p++ = (unsigned char)( t >> 16 );
694 *p++ = (unsigned char)( t >> 8 );
695 *p++ = (unsigned char)( t );
696
697 SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200698#else
699 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
700 return( ret );
701
702 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +0200703#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000704
Paul Bakkera3d195c2011-11-27 21:07:34 +0000705 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
706 return( ret );
707
708 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +0000709
Paul Bakker48916f92012-09-16 19:57:18 +0000710 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000711
712 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 6, 32 );
713
714 /*
715 * 38 . 38 session id length
716 * 39 . 39+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +0000717 * 40+n . 41+n ciphersuitelist length
718 * 42+n . .. ciphersuitelist
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000719 * .. . .. compression methods length
720 * .. . .. compression methods
721 * .. . .. extensions length
722 * .. . .. extensions
Paul Bakker5121ce52009-01-03 21:22:43 +0000723 */
Paul Bakker48916f92012-09-16 19:57:18 +0000724 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000725
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100726 if( n < 16 || n > 32 ||
727#if defined(POLARSSL_SSL_RENEGOTIATION)
728 ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
729#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000730 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200731 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000732 n = 0;
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200733 }
734
Paul Bakkera503a632013-08-14 13:48:06 +0200735#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200736 /*
737 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
738 * generate and include a Session ID in the TLS ClientHello."
739 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100740#if defined(POLARSSL_SSL_RENEGOTIATION)
741 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000742#endif
Manuel Pégourié-Gonnard51bccd32015-03-10 16:09:08 +0000743 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000744 if( ssl->session_negotiate->ticket != NULL &&
745 ssl->session_negotiate->ticket_len != 0 )
746 {
747 ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200748
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000749 if( ret != 0 )
750 return( ret );
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200751
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000752 ssl->session_negotiate->length = n = 32;
753 }
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200754 }
Paul Bakkera503a632013-08-14 13:48:06 +0200755#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000756
757 *p++ = (unsigned char) n;
758
759 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000760 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000761
762 SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
763 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
764
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200765 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Paul Bakker2fbefde2013-06-29 16:01:15 +0200766 n = 0;
767 q = p;
768
769 // Skip writing ciphersuite length for now
770 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000771
Paul Bakker2fbefde2013-06-29 16:01:15 +0200772 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000773 {
Paul Bakker2fbefde2013-06-29 16:01:15 +0200774 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
775
776 if( ciphersuite_info == NULL )
777 continue;
778
779 if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
780 ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
781 continue;
782
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100783 if( ssl->arc4_disabled == SSL_ARC4_DISABLED &&
784 ciphersuite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
785 continue;
786
Paul Bakkere3166ce2011-01-27 17:40:50 +0000787 SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200788 ciphersuites[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000789
Paul Bakker2fbefde2013-06-29 16:01:15 +0200790 n++;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200791 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
792 *p++ = (unsigned char)( ciphersuites[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000793 }
794
Manuel Pégourié-Gonnard5d9cde22015-01-22 10:49:41 +0000795 /*
796 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
797 */
798#if defined(POLARSSL_SSL_RENEGOTIATION)
799 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
800#endif
801 {
802 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
803 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
804 n++;
805 }
806
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200807 /* Some versions of OpenSSL don't handle it correctly if not at end */
808#if defined(POLARSSL_SSL_FALLBACK_SCSV)
809 if( ssl->fallback == SSL_IS_FALLBACK )
810 {
811 SSL_DEBUG_MSG( 3, ( "adding FALLBACK_SCSV" ) );
812 *p++ = (unsigned char)( SSL_FALLBACK_SCSV >> 8 );
813 *p++ = (unsigned char)( SSL_FALLBACK_SCSV );
814 n++;
815 }
816#endif
817
Paul Bakker2fbefde2013-06-29 16:01:15 +0200818 *q++ = (unsigned char)( n >> 7 );
819 *q++ = (unsigned char)( n << 1 );
820
821 SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
822
823
Paul Bakker2770fbd2012-07-03 13:30:23 +0000824#if defined(POLARSSL_ZLIB_SUPPORT)
825 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
826 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000827 SSL_COMPRESS_DEFLATE, SSL_COMPRESS_NULL ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000828
829 *p++ = 2;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000830 *p++ = SSL_COMPRESS_DEFLATE;
Paul Bakker48916f92012-09-16 19:57:18 +0000831 *p++ = SSL_COMPRESS_NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000832#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000833 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000834 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d", SSL_COMPRESS_NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000835
836 *p++ = 1;
837 *p++ = SSL_COMPRESS_NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200838#endif /* POLARSSL_ZLIB_SUPPORT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000839
Paul Bakkerd3edc862013-03-20 16:07:17 +0100840 // First write extensions, then the total length
841 //
Paul Bakker0be444a2013-08-27 21:55:01 +0200842#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100843 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
844 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +0200845#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000846
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100847#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100848 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
849 ext_len += olen;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100850#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000851
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100852#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
853 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100854 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
855 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200856#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000857
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200858#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100859 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
860 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100861
Paul Bakkerd3edc862013-03-20 16:07:17 +0100862 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
863 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100864#endif
865
Paul Bakker05decb22013-08-15 13:33:48 +0200866#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200867 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
868 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +0200869#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200870
Paul Bakker1f2bc622013-08-15 13:45:55 +0200871#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200872 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
873 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200874#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200875
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100876#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
877 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
878 ext_len += olen;
879#endif
880
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200881#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
882 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
883 ext_len += olen;
884#endif
885
Simon Butcher643a9222015-10-01 01:17:10 +0100886#if defined(POLARSSL_SSL_ALPN)
887 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200888 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +0200889#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200890
Simon Butcher643a9222015-10-01 01:17:10 +0100891#if defined(POLARSSL_SSL_SESSION_TICKETS)
892 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200893 ext_len += olen;
894#endif
895
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +0100896 /* olen unused if all extensions are disabled */
897 ((void) olen);
898
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000899 SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
900 ext_len ) );
901
Paul Bakkera7036632014-04-30 10:15:38 +0200902 if( ext_len > 0 )
903 {
904 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
905 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
906 p += ext_len;
907 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100908
Paul Bakker5121ce52009-01-03 21:22:43 +0000909 ssl->out_msglen = p - buf;
910 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
911 ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
912
913 ssl->state++;
914
915 if( ( ret = ssl_write_record( ssl ) ) != 0 )
916 {
917 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
918 return( ret );
919 }
920
921 SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
922
923 return( 0 );
924}
925
Paul Bakker48916f92012-09-16 19:57:18 +0000926static int ssl_parse_renegotiation_info( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200927 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000928 size_t len )
929{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000930 int ret;
931
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100932#if defined(POLARSSL_SSL_RENEGOTIATION)
933 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +0000934 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100935 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000936 if( len != 1 + ssl->verify_data_len * 2 ||
937 buf[0] != ssl->verify_data_len * 2 ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100938 safer_memcmp( buf + 1,
939 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
940 safer_memcmp( buf + 1 + ssl->verify_data_len,
941 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000942 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100943 SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000944
945 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
946 return( ret );
947
Paul Bakker48916f92012-09-16 19:57:18 +0000948 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
949 }
950 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100951 else
952#endif /* POLARSSL_SSL_RENEGOTIATION */
953 {
954 if( len != 1 || buf[0] != 0x00 )
955 {
956 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
957
958 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
959 return( ret );
960
961 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
962 }
963
964 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
965 }
Paul Bakker48916f92012-09-16 19:57:18 +0000966
967 return( 0 );
968}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200969
Paul Bakker05decb22013-08-15 13:33:48 +0200970#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200971static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200972 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200973 size_t len )
974{
975 /*
976 * server should use the extension only if we did,
977 * and if so the server's value should match ours (and len is always 1)
978 */
979 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
980 len != 1 ||
981 buf[0] != ssl->mfl_code )
982 {
983 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
984 }
985
986 return( 0 );
987}
Paul Bakker05decb22013-08-15 13:33:48 +0200988#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000989
Paul Bakker1f2bc622013-08-15 13:45:55 +0200990#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200991static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
992 const unsigned char *buf,
993 size_t len )
994{
995 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
996 len != 0 )
997 {
998 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
999 }
1000
1001 ((void) buf);
1002
1003 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
1004
1005 return( 0 );
1006}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001007#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001008
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001009#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1010static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
1011 const unsigned char *buf,
1012 size_t len )
1013{
1014 if( ssl->encrypt_then_mac == SSL_ETM_DISABLED ||
1015 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
1016 len != 0 )
1017 {
1018 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1019 }
1020
1021 ((void) buf);
1022
1023 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
1024
1025 return( 0 );
1026}
1027#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1028
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001029#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1030static int ssl_parse_extended_ms_ext( ssl_context *ssl,
1031 const unsigned char *buf,
1032 size_t len )
1033{
1034 if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +02001035 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001036 len != 0 )
1037 {
1038 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1039 }
1040
1041 ((void) buf);
1042
1043 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
1044
1045 return( 0 );
1046}
1047#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1048
Paul Bakkera503a632013-08-14 13:48:06 +02001049#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001050static int ssl_parse_session_ticket_ext( ssl_context *ssl,
1051 const unsigned char *buf,
1052 size_t len )
1053{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001054 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ||
1055 len != 0 )
1056 {
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001057 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001058 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001059
1060 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02001061
1062 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001063
1064 return( 0 );
1065}
Paul Bakkera503a632013-08-14 13:48:06 +02001066#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001067
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001068#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001069static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
1070 const unsigned char *buf,
1071 size_t len )
1072{
1073 size_t list_size;
1074 const unsigned char *p;
1075
1076 list_size = buf[0];
1077 if( list_size + 1 != len )
1078 {
1079 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1080 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1081 }
1082
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +02001083 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001084 while( list_size > 0 )
1085 {
1086 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
1087 p[0] == POLARSSL_ECP_PF_COMPRESSED )
1088 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +02001089 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001090 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
1091 return( 0 );
1092 }
1093
1094 list_size--;
1095 p++;
1096 }
1097
Manuel Pégourié-Gonnard5c1f0322014-06-23 14:24:43 +02001098 SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
1099 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001100}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001101#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001102
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001103#if defined(POLARSSL_SSL_ALPN)
1104static int ssl_parse_alpn_ext( ssl_context *ssl,
1105 const unsigned char *buf, size_t len )
1106{
1107 size_t list_len, name_len;
1108 const char **p;
1109
1110 /* If we didn't send it, the server shouldn't send it */
1111 if( ssl->alpn_list == NULL )
1112 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1113
1114 /*
1115 * opaque ProtocolName<1..2^8-1>;
1116 *
1117 * struct {
1118 * ProtocolName protocol_name_list<2..2^16-1>
1119 * } ProtocolNameList;
1120 *
1121 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
1122 */
1123
1124 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
1125 if( len < 4 )
1126 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1127
1128 list_len = ( buf[0] << 8 ) | buf[1];
1129 if( list_len != len - 2 )
1130 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1131
1132 name_len = buf[2];
1133 if( name_len != list_len - 1 )
1134 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1135
1136 /* Check that the server chosen protocol was in our list and save it */
1137 for( p = ssl->alpn_list; *p != NULL; p++ )
1138 {
1139 if( name_len == strlen( *p ) &&
1140 memcmp( buf + 3, *p, name_len ) == 0 )
1141 {
1142 ssl->alpn_chosen = *p;
1143 return( 0 );
1144 }
1145 }
1146
1147 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1148}
1149#endif /* POLARSSL_SSL_ALPN */
1150
Paul Bakker5121ce52009-01-03 21:22:43 +00001151static int ssl_parse_server_hello( ssl_context *ssl )
1152{
Paul Bakker2770fbd2012-07-03 13:30:23 +00001153 int ret, i, comp;
Paul Bakker23986e52011-04-24 08:57:21 +00001154 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001155 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001156 unsigned char *buf, *ext;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001157#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001158 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001159#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001160 int handshake_failure = 0;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001161 const ssl_ciphersuite_t *suite_info;
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001162#if defined(POLARSSL_DEBUG_C)
1163 uint32_t t;
1164#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001165
1166 SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
1167
1168 /*
1169 * 0 . 0 handshake type
1170 * 1 . 3 handshake length
1171 * 4 . 5 protocol version
1172 * 6 . 9 UNIX time()
1173 * 10 . 37 random bytes
1174 */
1175 buf = ssl->in_msg;
1176
1177 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1178 {
1179 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1180 return( ret );
1181 }
1182
1183 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1184 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001185#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001186 if( ssl->renegotiation == SSL_RENEGOTIATION )
1187 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001188 ssl->renego_records_seen++;
1189
1190 if( ssl->renego_max_records >= 0 &&
1191 ssl->renego_records_seen > ssl->renego_max_records )
1192 {
1193 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
1194 "but not honored by server" ) );
1195 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1196 }
1197
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001198 SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
1199 return( POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
1200 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001201#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001202
Paul Bakker5121ce52009-01-03 21:22:43 +00001203 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001204 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001205 }
1206
1207 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1208 buf[4], buf[5] ) );
1209
1210 if( ssl->in_hslen < 42 ||
1211 buf[0] != SSL_HS_SERVER_HELLO ||
1212 buf[4] != SSL_MAJOR_VERSION_3 )
1213 {
1214 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001215 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001216 }
1217
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001218 if( buf[5] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00001219 {
1220 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001221 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001222 }
1223
1224 ssl->minor_ver = buf[5];
1225
Paul Bakker1d29fb52012-09-28 13:28:45 +00001226 if( ssl->minor_ver < ssl->min_minor_ver )
1227 {
1228 SSL_DEBUG_MSG( 1, ( "server only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001229 " [%d:%d] < [%d:%d]", ssl->major_ver,
1230 ssl->minor_ver, buf[4], buf[5] ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001231
1232 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1233 SSL_ALERT_MSG_PROTOCOL_VERSION );
1234
1235 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1236 }
1237
Paul Bakker1504af52012-02-11 16:17:43 +00001238#if defined(POLARSSL_DEBUG_C)
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001239 t = ( (uint32_t) buf[6] << 24 )
1240 | ( (uint32_t) buf[7] << 16 )
1241 | ( (uint32_t) buf[8] << 8 )
1242 | ( (uint32_t) buf[9] );
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001243 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakker87e5cda2012-01-14 18:14:15 +00001244#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001245
Paul Bakker48916f92012-09-16 19:57:18 +00001246 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001247
1248 n = buf[38];
1249
Paul Bakker5121ce52009-01-03 21:22:43 +00001250 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1251
Paul Bakker48916f92012-09-16 19:57:18 +00001252 if( n > 32 )
1253 {
1254 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1255 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1256 }
1257
Paul Bakker5121ce52009-01-03 21:22:43 +00001258 /*
1259 * 38 . 38 session id length
1260 * 39 . 38+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +00001261 * 39+n . 40+n chosen ciphersuite
Paul Bakker5121ce52009-01-03 21:22:43 +00001262 * 41+n . 41+n chosen compression alg.
1263 * 42+n . 43+n extensions length
1264 * 44+n . 44+n+m extensions
1265 */
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001266 if( ssl->in_hslen > 43 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001267 {
1268 ext_len = ( ( buf[42 + n] << 8 )
Paul Bakker48916f92012-09-16 19:57:18 +00001269 | ( buf[43 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001270
Paul Bakker48916f92012-09-16 19:57:18 +00001271 if( ( ext_len > 0 && ext_len < 4 ) ||
1272 ssl->in_hslen != 44 + n + ext_len )
1273 {
1274 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1275 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1276 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001277 }
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001278 else if( ssl->in_hslen == 42 + n )
1279 {
1280 ext_len = 0;
1281 }
1282 else
1283 {
1284 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1285 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1286 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001287
1288 i = ( buf[39 + n] << 8 ) | buf[40 + n];
Paul Bakker2770fbd2012-07-03 13:30:23 +00001289 comp = buf[41 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001290
Paul Bakker380da532012-04-18 16:10:25 +00001291 /*
1292 * Initialize update checksum functions
1293 */
Paul Bakker68884e32013-01-07 18:20:04 +01001294 ssl->transform_negotiate->ciphersuite_info = ssl_ciphersuite_from_id( i );
1295
1296 if( ssl->transform_negotiate->ciphersuite_info == NULL )
1297 {
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001298 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001299 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1300 }
Paul Bakker380da532012-04-18 16:10:25 +00001301
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001302 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1303
Paul Bakker5121ce52009-01-03 21:22:43 +00001304 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1305 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1306
1307 /*
1308 * Check if the session can be resumed
1309 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001310 if( ssl->handshake->resume == 0 || n == 0 ||
1311#if defined(POLARSSL_SSL_RENEGOTIATION)
1312 ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
1313#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001314 ssl->session_negotiate->ciphersuite != i ||
1315 ssl->session_negotiate->compression != comp ||
1316 ssl->session_negotiate->length != n ||
1317 memcmp( ssl->session_negotiate->id, buf + 39, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001318 {
1319 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001320 ssl->handshake->resume = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001321#if defined(POLARSSL_HAVE_TIME)
Paul Bakker48916f92012-09-16 19:57:18 +00001322 ssl->session_negotiate->start = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001323#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001324 ssl->session_negotiate->ciphersuite = i;
1325 ssl->session_negotiate->compression = comp;
1326 ssl->session_negotiate->length = n;
1327 memcpy( ssl->session_negotiate->id, buf + 39, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001328 }
1329 else
1330 {
1331 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001332
1333 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1334 {
1335 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1336 return( ret );
1337 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001338 }
1339
1340 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001341 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001342
Paul Bakkere3166ce2011-01-27 17:40:50 +00001343 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001344 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[41 + n] ) );
1345
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001346 suite_info = ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite );
1347 if( suite_info == NULL ||
1348 ( ssl->arc4_disabled &&
1349 suite_info->cipher == POLARSSL_CIPHER_ARC4_128 ) )
1350 {
1351 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1352 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1353 }
1354
1355
Paul Bakker5121ce52009-01-03 21:22:43 +00001356 i = 0;
1357 while( 1 )
1358 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001359 if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001360 {
1361 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001362 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001363 }
1364
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001365 if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
1366 ssl->session_negotiate->ciphersuite )
1367 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001368 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001369 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001370 }
1371
Paul Bakker2770fbd2012-07-03 13:30:23 +00001372 if( comp != SSL_COMPRESS_NULL
1373#if defined(POLARSSL_ZLIB_SUPPORT)
1374 && comp != SSL_COMPRESS_DEFLATE
1375#endif
1376 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001377 {
1378 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001379 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001380 }
Paul Bakker48916f92012-09-16 19:57:18 +00001381 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001382
Paul Bakker48916f92012-09-16 19:57:18 +00001383 ext = buf + 44 + n;
1384
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001385 SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
1386
Paul Bakker48916f92012-09-16 19:57:18 +00001387 while( ext_len )
1388 {
1389 unsigned int ext_id = ( ( ext[0] << 8 )
1390 | ( ext[1] ) );
1391 unsigned int ext_size = ( ( ext[2] << 8 )
1392 | ( ext[3] ) );
1393
1394 if( ext_size + 4 > ext_len )
1395 {
1396 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1397 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1398 }
1399
1400 switch( ext_id )
1401 {
1402 case TLS_EXT_RENEGOTIATION_INFO:
1403 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001404#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001405 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001406#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001407
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001408 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1409 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001410 return( ret );
1411
1412 break;
1413
Paul Bakker05decb22013-08-15 13:33:48 +02001414#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001415 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1416 SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
1417
1418 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1419 ext + 4, ext_size ) ) != 0 )
1420 {
1421 return( ret );
1422 }
1423
1424 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001425#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001426
Paul Bakker1f2bc622013-08-15 13:45:55 +02001427#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001428 case TLS_EXT_TRUNCATED_HMAC:
1429 SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
1430
1431 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1432 ext + 4, ext_size ) ) != 0 )
1433 {
1434 return( ret );
1435 }
1436
1437 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001438#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001439
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001440#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1441 case TLS_EXT_ENCRYPT_THEN_MAC:
1442 SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
1443
1444 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
1445 ext + 4, ext_size ) ) != 0 )
1446 {
1447 return( ret );
1448 }
1449
1450 break;
1451#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1452
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001453#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1454 case TLS_EXT_EXTENDED_MASTER_SECRET:
1455 SSL_DEBUG_MSG( 3, ( "found extended_master_secret extension" ) );
1456
1457 if( ( ret = ssl_parse_extended_ms_ext( ssl,
1458 ext + 4, ext_size ) ) != 0 )
1459 {
1460 return( ret );
1461 }
1462
1463 break;
1464#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1465
Paul Bakkera503a632013-08-14 13:48:06 +02001466#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001467 case TLS_EXT_SESSION_TICKET:
1468 SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
1469
1470 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1471 ext + 4, ext_size ) ) != 0 )
1472 {
1473 return( ret );
1474 }
1475
1476 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001477#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001478
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001479#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001480 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1481 SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
1482
1483 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1484 ext + 4, ext_size ) ) != 0 )
1485 {
1486 return( ret );
1487 }
1488
1489 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001490#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001491
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001492#if defined(POLARSSL_SSL_ALPN)
1493 case TLS_EXT_ALPN:
1494 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1495
1496 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1497 return( ret );
1498
1499 break;
1500#endif /* POLARSSL_SSL_ALPN */
1501
Paul Bakker48916f92012-09-16 19:57:18 +00001502 default:
1503 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1504 ext_id ) );
1505 }
1506
1507 ext_len -= 4 + ext_size;
1508 ext += 4 + ext_size;
1509
1510 if( ext_len > 0 && ext_len < 4 )
1511 {
1512 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1513 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1514 }
1515 }
1516
1517 /*
1518 * Renegotiation security checks
1519 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001520 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1521 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001522 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001523 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1524 handshake_failure = 1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001525 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001526#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001527 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1528 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1529 renegotiation_info_seen == 0 )
1530 {
1531 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1532 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001533 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001534 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1535 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1536 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001537 {
1538 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001539 handshake_failure = 1;
1540 }
1541 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1542 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1543 renegotiation_info_seen == 1 )
1544 {
1545 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1546 handshake_failure = 1;
1547 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001548#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001549
1550 if( handshake_failure == 1 )
1551 {
1552 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1553 return( ret );
1554
Paul Bakker48916f92012-09-16 19:57:18 +00001555 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1556 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001557
1558 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1559
1560 return( 0 );
1561}
1562
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001563#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1564 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001565static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
1566 unsigned char *end )
1567{
1568 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1569
Paul Bakker29e1f122013-04-16 13:07:56 +02001570 /*
1571 * Ephemeral DH parameters:
1572 *
1573 * struct {
1574 * opaque dh_p<1..2^16-1>;
1575 * opaque dh_g<1..2^16-1>;
1576 * opaque dh_Ys<1..2^16-1>;
1577 * } ServerDHParams;
1578 */
1579 if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1580 {
1581 SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
1582 return( ret );
1583 }
1584
Manuel Pégourié-Gonnard9ea1b232015-06-29 15:27:52 +02001585 if( ssl->handshake->dhm_ctx.len < SSL_MIN_DHM_BYTES ||
Paul Bakker29e1f122013-04-16 13:07:56 +02001586 ssl->handshake->dhm_ctx.len > 512 )
1587 {
1588 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
1589 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1590 }
1591
1592 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1593 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1594 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001595
1596 return( ret );
1597}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001598#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1599 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001600
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001601#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001602 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001603 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1604 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1605 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1606static int ssl_check_server_ecdh_params( const ssl_context *ssl )
1607{
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001608 const ecp_curve_info *curve_info;
1609
1610 curve_info = ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
1611 if( curve_info == NULL )
1612 {
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001613 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1614 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001615 }
1616
1617 SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001618
Manuel Pégourié-Gonnard29f777e2015-04-03 17:26:50 +02001619#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001620 if( ! ssl_curve_is_acceptable( ssl, ssl->handshake->ecdh_ctx.grp.id ) )
1621#else
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001622 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1623 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001624#endif
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001625 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001626
1627 SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
1628
1629 return( 0 );
1630}
Paul Bakker9af723c2014-05-01 13:03:14 +02001631#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1632 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1633 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1634 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1635 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001636
1637#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1638 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001639 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001640static int ssl_parse_server_ecdh_params( ssl_context *ssl,
1641 unsigned char **p,
1642 unsigned char *end )
1643{
1644 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1645
Paul Bakker29e1f122013-04-16 13:07:56 +02001646 /*
1647 * Ephemeral ECDH parameters:
1648 *
1649 * struct {
1650 * ECParameters curve_params;
1651 * ECPoint public;
1652 * } ServerECDHParams;
1653 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001654 if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
1655 (const unsigned char **) p, end ) ) != 0 )
1656 {
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001657 SSL_DEBUG_RET( 1, ( "ecdh_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001658 return( ret );
1659 }
1660
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001661 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001662 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001663 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001664 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1665 }
1666
Paul Bakker29e1f122013-04-16 13:07:56 +02001667 return( ret );
1668}
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001669#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001670 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1671 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001672
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001673#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001674static int ssl_parse_server_psk_hint( ssl_context *ssl,
1675 unsigned char **p,
1676 unsigned char *end )
1677{
1678 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001679 size_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001680 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001681
1682 /*
1683 * PSK parameters:
1684 *
1685 * opaque psk_identity_hint<0..2^16-1>;
1686 */
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001687 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001688 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001689
1690 if( (*p) + len > end )
1691 {
1692 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
1693 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1694 }
1695
1696 // TODO: Retrieve PSK identity hint and callback to app
1697 //
1698 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001699 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001700
1701 return( ret );
1702}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001703#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001704
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001705#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1706 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1707/*
1708 * Generate a pre-master secret and encrypt it with the server's RSA key
1709 */
1710static int ssl_write_encrypted_pms( ssl_context *ssl,
1711 size_t offset, size_t *olen,
1712 size_t pms_offset )
1713{
1714 int ret;
1715 size_t len_bytes = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 0 : 2;
1716 unsigned char *p = ssl->handshake->premaster + pms_offset;
1717
Manuel Pégourié-Gonnard65125542015-08-27 16:37:35 +02001718 if( offset + len_bytes > SSL_MAX_CONTENT_LEN )
1719 {
1720 SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
1721 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1722 }
1723
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001724 /*
1725 * Generate (part of) the pre-master as
1726 * struct {
1727 * ProtocolVersion client_version;
1728 * opaque random[46];
1729 * } PreMasterSecret;
1730 */
1731 p[0] = (unsigned char) ssl->max_major_ver;
1732 p[1] = (unsigned char) ssl->max_minor_ver;
1733
1734 if( ( ret = ssl->f_rng( ssl->p_rng, p + 2, 46 ) ) != 0 )
1735 {
1736 SSL_DEBUG_RET( 1, "f_rng", ret );
1737 return( ret );
1738 }
1739
1740 ssl->handshake->pmslen = 48;
1741
Manuel Pégourié-Gonnardbb564e02015-09-03 10:44:32 +02001742 if( ssl->session_negotiate->peer_cert == NULL )
1743 {
1744 SSL_DEBUG_MSG( 2, ( "certificate required" ) );
1745 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1746 }
1747
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001748 /*
1749 * Now write it out, encrypted
1750 */
1751 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1752 POLARSSL_PK_RSA ) )
1753 {
1754 SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1755 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1756 }
1757
1758 if( ( ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
1759 p, ssl->handshake->pmslen,
1760 ssl->out_msg + offset + len_bytes, olen,
1761 SSL_MAX_CONTENT_LEN - offset - len_bytes,
1762 ssl->f_rng, ssl->p_rng ) ) != 0 )
1763 {
1764 SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
1765 return( ret );
1766 }
1767
1768#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1769 defined(POLARSSL_SSL_PROTO_TLS1_2)
1770 if( len_bytes == 2 )
1771 {
1772 ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
1773 ssl->out_msg[offset+1] = (unsigned char)( *olen );
1774 *olen += 2;
1775 }
1776#endif
1777
1778 return( 0 );
1779}
1780#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
1781 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001782
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001783#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001784#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001785 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1786 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001787static int ssl_parse_signature_algorithm( ssl_context *ssl,
1788 unsigned char **p,
1789 unsigned char *end,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001790 md_type_t *md_alg,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001791 pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02001792{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001793 ((void) ssl);
Paul Bakker29e1f122013-04-16 13:07:56 +02001794 *md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001795 *pk_alg = POLARSSL_PK_NONE;
1796
1797 /* Only in TLS 1.2 */
1798 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1799 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001800 return( 0 );
1801 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001802
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001803 if( (*p) + 2 > end )
Paul Bakker29e1f122013-04-16 13:07:56 +02001804 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1805
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001806 /*
1807 * Get hash algorithm
1808 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001809 if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001810 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001811 SSL_DEBUG_MSG( 2, ( "Server used unsupported "
1812 "HashAlgorithm %d", *(p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001813 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1814 }
1815
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001816 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001817 * Get signature algorithm
1818 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001819 if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001820 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001821 SSL_DEBUG_MSG( 2, ( "server used unsupported "
1822 "SignatureAlgorithm %d", (*p)[1] ) );
1823 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001824 }
1825
1826 SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
1827 SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
1828 *p += 2;
1829
1830 return( 0 );
1831}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001832#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001833 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1834 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001835#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001836
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001837
1838#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1839 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1840static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
1841{
1842 int ret;
1843 const ecp_keypair *peer_key;
1844
Manuel Pégourié-Gonnardbb564e02015-09-03 10:44:32 +02001845 if( ssl->session_negotiate->peer_cert == NULL )
1846 {
1847 SSL_DEBUG_MSG( 2, ( "certificate required" ) );
1848 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1849 }
1850
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001851 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1852 POLARSSL_PK_ECKEY ) )
1853 {
1854 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
1855 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1856 }
1857
1858 peer_key = pk_ec( ssl->session_negotiate->peer_cert->pk );
1859
1860 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
1861 POLARSSL_ECDH_THEIRS ) ) != 0 )
1862 {
1863 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
1864 return( ret );
1865 }
1866
1867 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1868 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001869 SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001870 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
1871 }
1872
1873 return( ret );
1874}
1875#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1876 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1877
Paul Bakker41c83d32013-03-20 14:39:14 +01001878static int ssl_parse_server_key_exchange( ssl_context *ssl )
1879{
Paul Bakker23986e52011-04-24 08:57:21 +00001880 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001881 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001882 unsigned char *p, *end;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001883#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001884 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1885 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001886 size_t sig_len, params_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001887 unsigned char hash[64];
Paul Bakkerc70b9822013-04-07 22:00:46 +02001888 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001889 size_t hashlen;
1890 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001891#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001892
1893 SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
1894
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001895#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001896 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001897 {
1898 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1899 ssl->state++;
1900 return( 0 );
1901 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001902 ((void) p);
1903 ((void) end);
1904#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001905
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001906#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1907 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1908 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
1909 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
1910 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001911 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
1912 {
1913 SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
1914 return( ret );
1915 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001916
1917 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1918 ssl->state++;
1919 return( 0 );
1920 }
1921 ((void) p);
1922 ((void) end);
Paul Bakker9af723c2014-05-01 13:03:14 +02001923#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1924 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001925
Paul Bakker5121ce52009-01-03 21:22:43 +00001926 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1927 {
1928 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1929 return( ret );
1930 }
1931
1932 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1933 {
1934 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001935 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001936 }
1937
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001938 /*
1939 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
1940 * doesn't use a psk_identity_hint
1941 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001942 if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
1943 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001944 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1945 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02001946 {
1947 ssl->record_read = 1;
1948 goto exit;
1949 }
1950
1951 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1952 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001953 }
1954
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001955 p = ssl->in_msg + 4;
1956 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001957 SSL_DEBUG_BUF( 3, "server key exchange", p, ssl->in_hslen - 4 );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001958
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001959#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1960 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1961 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1962 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1963 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1964 {
1965 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1966 {
1967 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1968 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1969 }
1970 } /* FALLTROUGH */
1971#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
1972
1973#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
1974 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1975 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1976 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1977 ; /* nothing more to do */
1978 else
1979#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
1980 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
1981#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1982 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1983 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1984 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001985 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001986 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001987 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001988 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001989 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1990 }
1991 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001992 else
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001993#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1994 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001995#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001996 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001997 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1998 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001999 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002000 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002001 {
2002 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
2003 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002004 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2005 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2006 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002007 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002008 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002009#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002010 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002011 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002012 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002013 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002014 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002015 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002016
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002017#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002018 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2019 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02002020 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002021 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2022 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002023 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002024 params_len = p - ( ssl->in_msg + 4 );
2025
Paul Bakker29e1f122013-04-16 13:07:56 +02002026 /*
2027 * Handle the digitally-signed structure
2028 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002029#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2030 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002031 {
Paul Bakker9659dae2013-08-28 16:21:34 +02002032 if( ssl_parse_signature_algorithm( ssl, &p, end,
2033 &md_alg, &pk_alg ) != 0 )
2034 {
2035 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2036 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2037 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002038
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002039 if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002040 {
2041 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2042 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2043 }
Simon Butcher14400c82016-01-02 00:08:13 +00002044
2045#if !defined(POLARSSL_SSL_ENABLE_MD5_SIGNATURES)
2046 if( md_alg == POLARSSL_MD_MD5 )
2047 {
2048 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2049 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2050 }
2051#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002052 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002053 else
Paul Bakker9af723c2014-05-01 13:03:14 +02002054#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002055#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2056 defined(POLARSSL_SSL_PROTO_TLS1_1)
2057 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002058 {
2059 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002060
Paul Bakker9659dae2013-08-28 16:21:34 +02002061 /* Default hash for ECDSA is SHA-1 */
2062 if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
2063 md_alg = POLARSSL_MD_SHA1;
2064 }
2065 else
2066#endif
2067 {
2068 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002069 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker9659dae2013-08-28 16:21:34 +02002070 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002071
2072 /*
2073 * Read signature
2074 */
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002075 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002076 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002077
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002078 if( end != p + sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002079 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002080 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01002081 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2082 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002083
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002084 SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002085
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002086 /*
2087 * Compute the hash that has been signed
2088 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002089#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2090 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002091 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002092 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002093 md5_context md5;
2094 sha1_context sha1;
2095
Paul Bakker5b4af392014-06-26 12:09:34 +02002096 md5_init( &md5 );
2097 sha1_init( &sha1 );
2098
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002099 hashlen = 36;
2100
Paul Bakker29e1f122013-04-16 13:07:56 +02002101 /*
2102 * digitally-signed struct {
2103 * opaque md5_hash[16];
2104 * opaque sha_hash[20];
2105 * };
2106 *
2107 * md5_hash
2108 * MD5(ClientHello.random + ServerHello.random
2109 * + ServerParams);
2110 * sha_hash
2111 * SHA(ClientHello.random + ServerHello.random
2112 * + ServerParams);
2113 */
Paul Bakker29e1f122013-04-16 13:07:56 +02002114 md5_starts( &md5 );
2115 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002116 md5_update( &md5, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002117 md5_finish( &md5, hash );
2118
2119 sha1_starts( &sha1 );
2120 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002121 sha1_update( &sha1, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002122 sha1_finish( &sha1, hash + 16 );
Paul Bakker5b4af392014-06-26 12:09:34 +02002123
2124 md5_free( &md5 );
2125 sha1_free( &sha1 );
Paul Bakker29e1f122013-04-16 13:07:56 +02002126 }
2127 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002128#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2129 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002130#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2131 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002132 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002133 {
2134 md_context_t ctx;
2135
Paul Bakker84bbeb52014-07-01 14:53:22 +02002136 md_init( &ctx );
2137
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002138 /* Info from md_alg will be used instead */
2139 hashlen = 0;
Paul Bakker29e1f122013-04-16 13:07:56 +02002140
2141 /*
2142 * digitally-signed struct {
2143 * opaque client_random[32];
2144 * opaque server_random[32];
2145 * ServerDHParams params;
2146 * };
2147 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002148 if( ( ret = md_init_ctx( &ctx,
2149 md_info_from_type( md_alg ) ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002150 {
2151 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2152 return( ret );
2153 }
2154
2155 md_starts( &ctx );
2156 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002157 md_update( &ctx, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002158 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002159 md_free( &ctx );
Paul Bakker29e1f122013-04-16 13:07:56 +02002160 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002161 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002162#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2163 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02002164 {
Paul Bakker577e0062013-08-28 11:57:20 +02002165 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002166 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002167 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002168
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002169 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2170 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker29e1f122013-04-16 13:07:56 +02002171
Manuel Pégourié-Gonnardbb564e02015-09-03 10:44:32 +02002172 if( ssl->session_negotiate->peer_cert == NULL )
2173 {
2174 SSL_DEBUG_MSG( 2, ( "certificate required" ) );
2175 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2176 }
2177
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002178 /*
2179 * Verify signature
2180 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02002181 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002182 {
2183 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2184 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2185 }
2186
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002187 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
2188 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002189 {
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002190 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002191 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002192 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002193 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002194#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002195 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2196 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002197
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002198exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002199 ssl->state++;
2200
2201 SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
2202
2203 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002204}
2205
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002206#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2207 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2208 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2209 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2210static int ssl_parse_certificate_request( ssl_context *ssl )
2211{
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002212 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2213
2214 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2215
2216 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2217 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2218 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2219 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2220 {
2221 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2222 ssl->state++;
2223 return( 0 );
2224 }
2225
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002226 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2227 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002228}
2229#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002230static int ssl_parse_certificate_request( ssl_context *ssl )
2231{
2232 int ret;
Paul Bakker926af752012-11-23 13:38:07 +01002233 unsigned char *buf, *p;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002234 size_t n = 0, m = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002235 size_t cert_type_len = 0, dn_len = 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002236 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002237
2238 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2239
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002240 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2241 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2242 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2243 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2244 {
2245 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2246 ssl->state++;
2247 return( 0 );
2248 }
2249
Paul Bakker5121ce52009-01-03 21:22:43 +00002250 /*
2251 * 0 . 0 handshake type
2252 * 1 . 3 handshake length
Paul Bakker926af752012-11-23 13:38:07 +01002253 * 4 . 4 cert type count
2254 * 5 .. m-1 cert types
2255 * m .. m+1 sig alg length (TLS 1.2 only)
2256 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002257 * n .. n+1 length of all DNs
2258 * n+2 .. n+3 length of DN 1
2259 * n+4 .. ... Distinguished Name #1
2260 * ... .. ... length of DN 2, etc.
2261 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002262 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002263 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002264 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2265 {
2266 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2267 return( ret );
2268 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002269
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002270 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2271 {
2272 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2273 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2274 }
2275
2276 ssl->record_read = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002277 }
2278
2279 ssl->client_auth = 0;
2280 ssl->state++;
2281
2282 if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST )
2283 ssl->client_auth++;
2284
2285 SSL_DEBUG_MSG( 3, ( "got %s certificate request",
2286 ssl->client_auth ? "a" : "no" ) );
2287
Paul Bakker926af752012-11-23 13:38:07 +01002288 if( ssl->client_auth == 0 )
2289 goto exit;
2290
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002291 ssl->record_read = 0;
2292
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 &&
2377 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2378 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002379
2380static int ssl_parse_server_hello_done( ssl_context *ssl )
2381{
2382 int ret;
2383
2384 SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
2385
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002386 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002387 {
2388 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2389 {
2390 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2391 return( ret );
2392 }
2393
2394 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2395 {
2396 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002397 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002398 }
2399 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002400 ssl->record_read = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002401
2402 if( ssl->in_hslen != 4 ||
2403 ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
2404 {
2405 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002406 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002407 }
2408
2409 ssl->state++;
2410
2411 SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
2412
2413 return( 0 );
2414}
2415
2416static int ssl_write_client_key_exchange( ssl_context *ssl )
2417{
Paul Bakker23986e52011-04-24 08:57:21 +00002418 int ret;
2419 size_t i, n;
Paul Bakker41c83d32013-03-20 14:39:14 +01002420 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002421
2422 SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
2423
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002424#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002425 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002426 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002427 /*
2428 * DHM key exchange -- send G^X mod P
2429 */
Paul Bakker48916f92012-09-16 19:57:18 +00002430 n = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002431
2432 ssl->out_msg[4] = (unsigned char)( n >> 8 );
2433 ssl->out_msg[5] = (unsigned char)( n );
2434 i = 6;
2435
Paul Bakker29b64762012-09-25 09:36:44 +00002436 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002437 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker5121ce52009-01-03 21:22:43 +00002438 &ssl->out_msg[i], n,
2439 ssl->f_rng, ssl->p_rng );
2440 if( ret != 0 )
2441 {
2442 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2443 return( ret );
2444 }
2445
Paul Bakker48916f92012-09-16 19:57:18 +00002446 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2447 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002448
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002449 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002450
Paul Bakker48916f92012-09-16 19:57:18 +00002451 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2452 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002453 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002454 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002455 {
2456 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2457 return( ret );
2458 }
2459
Paul Bakker48916f92012-09-16 19:57:18 +00002460 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 }
2462 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002463#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002464#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002465 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2466 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2467 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002468 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002469 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2470 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2471 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002472 {
2473 /*
2474 * ECDH key exchange -- send client public value
2475 */
2476 i = 4;
2477
2478 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
2479 &n,
2480 &ssl->out_msg[i], 1000,
2481 ssl->f_rng, ssl->p_rng );
2482 if( ret != 0 )
2483 {
2484 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2485 return( ret );
2486 }
2487
2488 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2489
2490 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2491 &ssl->handshake->pmslen,
2492 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002493 POLARSSL_MPI_MAX_SIZE,
2494 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002495 {
2496 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2497 return( ret );
2498 }
2499
2500 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2501 }
2502 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002503#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002504 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2505 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2506 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002507#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002508 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002509 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002510 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2511 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002512 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002513 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002514 * opaque psk_identity<0..2^16-1>;
2515 */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002516 if( ssl->psk == NULL || ssl->psk_identity == NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002517 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2518
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002519 i = 4;
2520 n = ssl->psk_identity_len;
Manuel Pégourié-Gonnard65125542015-08-27 16:37:35 +02002521
2522 if( i + 2 + n > SSL_MAX_CONTENT_LEN )
2523 {
2524 SSL_DEBUG_MSG( 1, ( "psk identity too long or "
2525 "SSL buffer too short" ) );
2526 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2527 }
2528
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002529 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2530 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002531
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002532 memcpy( ssl->out_msg + i, ssl->psk_identity, ssl->psk_identity_len );
2533 i += ssl->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002534
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002535#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002536 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002537 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002538 n = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002539 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002540 else
2541#endif
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002542#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2543 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2544 {
2545 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2546 return( ret );
2547 }
2548 else
2549#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002550#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002551 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002552 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002553 /*
2554 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
2555 */
2556 n = ssl->handshake->dhm_ctx.len;
Manuel Pégourié-Gonnard65125542015-08-27 16:37:35 +02002557
2558 if( i + 2 + n > SSL_MAX_CONTENT_LEN )
2559 {
2560 SSL_DEBUG_MSG( 1, ( "psk identity or DHM size too long"
2561 " or SSL buffer too short" ) );
2562 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2563 }
2564
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002565 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2566 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002567
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002568 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakker68881672013-10-15 13:24:01 +02002569 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002570 &ssl->out_msg[i], n,
2571 ssl->f_rng, ssl->p_rng );
2572 if( ret != 0 )
2573 {
2574 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2575 return( ret );
2576 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002577 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002578 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002579#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002580#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002581 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002582 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002583 /*
2584 * ClientECDiffieHellmanPublic public;
2585 */
2586 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
2587 &ssl->out_msg[i], SSL_MAX_CONTENT_LEN - i,
2588 ssl->f_rng, ssl->p_rng );
2589 if( ret != 0 )
2590 {
2591 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2592 return( ret );
2593 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002594
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002595 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2596 }
2597 else
2598#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2599 {
2600 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002601 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002602 }
2603
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002604 if( ( ret = ssl_psk_derive_premaster( ssl,
2605 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002606 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002607 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002608 return( ret );
2609 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002610 }
2611 else
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002612#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002613#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +02002614 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002615 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002616 i = 4;
2617 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002618 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 }
Paul Bakkered27a042013-04-18 22:46:23 +02002620 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002621#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002622 {
2623 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002624 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002625 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02002626 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002627
Paul Bakker5121ce52009-01-03 21:22:43 +00002628 ssl->out_msglen = i + n;
2629 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2630 ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE;
2631
2632 ssl->state++;
2633
2634 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2635 {
2636 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2637 return( ret );
2638 }
2639
2640 SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
2641
2642 return( 0 );
2643}
2644
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002645#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2646 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002647 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2648 !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 &&
2825 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002826
Paul Bakkera503a632013-08-14 13:48:06 +02002827#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002828static int ssl_parse_new_session_ticket( ssl_context *ssl )
2829{
2830 int ret;
2831 uint32_t lifetime;
2832 size_t ticket_len;
2833 unsigned char *ticket;
2834
2835 SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2836
2837 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2838 {
2839 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2840 return( ret );
2841 }
2842
2843 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2844 {
2845 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2846 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2847 }
2848
2849 /*
2850 * struct {
2851 * uint32 ticket_lifetime_hint;
2852 * opaque ticket<0..2^16-1>;
2853 * } NewSessionTicket;
2854 *
2855 * 0 . 0 handshake message type
2856 * 1 . 3 handshake message length
2857 * 4 . 7 ticket_lifetime_hint
2858 * 8 . 9 ticket_len (n)
2859 * 10 . 9+n ticket content
2860 */
2861 if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
2862 ssl->in_hslen < 10 )
2863 {
2864 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2865 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2866 }
2867
2868 lifetime = ( ssl->in_msg[4] << 24 ) | ( ssl->in_msg[5] << 16 ) |
2869 ( ssl->in_msg[6] << 8 ) | ( ssl->in_msg[7] );
2870
2871 ticket_len = ( ssl->in_msg[8] << 8 ) | ( ssl->in_msg[9] );
2872
2873 if( ticket_len + 10 != ssl->in_hslen )
2874 {
2875 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2876 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2877 }
2878
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002879 SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
2880
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002881 /* We're not waiting for a NewSessionTicket message any more */
2882 ssl->handshake->new_session_ticket = 0;
2883
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002884 /*
2885 * Zero-length ticket means the server changed his mind and doesn't want
2886 * to send a ticket after all, so just forget it
2887 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002888 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002889 return( 0 );
2890
Paul Bakker34617722014-06-13 17:20:13 +02002891 polarssl_zeroize( ssl->session_negotiate->ticket,
2892 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002893 polarssl_free( ssl->session_negotiate->ticket );
2894 ssl->session_negotiate->ticket = NULL;
2895 ssl->session_negotiate->ticket_len = 0;
2896
2897 if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
2898 {
2899 SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
2900 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2901 }
2902
2903 memcpy( ticket, ssl->in_msg + 10, ticket_len );
2904
2905 ssl->session_negotiate->ticket = ticket;
2906 ssl->session_negotiate->ticket_len = ticket_len;
2907 ssl->session_negotiate->ticket_lifetime = lifetime;
2908
2909 /*
2910 * RFC 5077 section 3.4:
2911 * "If the client receives a session ticket from the server, then it
2912 * discards any Session ID that was sent in the ServerHello."
2913 */
2914 SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
2915 ssl->session_negotiate->length = 0;
2916
2917 SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2918
2919 return( 0 );
2920}
Paul Bakkera503a632013-08-14 13:48:06 +02002921#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002922
Paul Bakker5121ce52009-01-03 21:22:43 +00002923/*
Paul Bakker1961b702013-01-25 14:49:24 +01002924 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002925 */
Paul Bakker1961b702013-01-25 14:49:24 +01002926int ssl_handshake_client_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002927{
2928 int ret = 0;
2929
Paul Bakker1961b702013-01-25 14:49:24 +01002930 if( ssl->state == SSL_HANDSHAKE_OVER )
2931 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002932
Paul Bakker1961b702013-01-25 14:49:24 +01002933 SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
2934
2935 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2936 return( ret );
2937
2938 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002939 {
Paul Bakker1961b702013-01-25 14:49:24 +01002940 case SSL_HELLO_REQUEST:
2941 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002942 break;
2943
Paul Bakker1961b702013-01-25 14:49:24 +01002944 /*
2945 * ==> ClientHello
2946 */
2947 case SSL_CLIENT_HELLO:
2948 ret = ssl_write_client_hello( ssl );
2949 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002950
Paul Bakker1961b702013-01-25 14:49:24 +01002951 /*
2952 * <== ServerHello
2953 * Certificate
2954 * ( ServerKeyExchange )
2955 * ( CertificateRequest )
2956 * ServerHelloDone
2957 */
2958 case SSL_SERVER_HELLO:
2959 ret = ssl_parse_server_hello( ssl );
2960 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002961
Paul Bakker1961b702013-01-25 14:49:24 +01002962 case SSL_SERVER_CERTIFICATE:
2963 ret = ssl_parse_certificate( ssl );
2964 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002965
Paul Bakker1961b702013-01-25 14:49:24 +01002966 case SSL_SERVER_KEY_EXCHANGE:
2967 ret = ssl_parse_server_key_exchange( ssl );
2968 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002969
Paul Bakker1961b702013-01-25 14:49:24 +01002970 case SSL_CERTIFICATE_REQUEST:
2971 ret = ssl_parse_certificate_request( ssl );
2972 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002973
Paul Bakker1961b702013-01-25 14:49:24 +01002974 case SSL_SERVER_HELLO_DONE:
2975 ret = ssl_parse_server_hello_done( ssl );
2976 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002977
Paul Bakker1961b702013-01-25 14:49:24 +01002978 /*
2979 * ==> ( Certificate/Alert )
2980 * ClientKeyExchange
2981 * ( CertificateVerify )
2982 * ChangeCipherSpec
2983 * Finished
2984 */
2985 case SSL_CLIENT_CERTIFICATE:
2986 ret = ssl_write_certificate( ssl );
2987 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002988
Paul Bakker1961b702013-01-25 14:49:24 +01002989 case SSL_CLIENT_KEY_EXCHANGE:
2990 ret = ssl_write_client_key_exchange( ssl );
2991 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002992
Paul Bakker1961b702013-01-25 14:49:24 +01002993 case SSL_CERTIFICATE_VERIFY:
2994 ret = ssl_write_certificate_verify( ssl );
2995 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002996
Paul Bakker1961b702013-01-25 14:49:24 +01002997 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2998 ret = ssl_write_change_cipher_spec( ssl );
2999 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003000
Paul Bakker1961b702013-01-25 14:49:24 +01003001 case SSL_CLIENT_FINISHED:
3002 ret = ssl_write_finished( ssl );
3003 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003004
Paul Bakker1961b702013-01-25 14:49:24 +01003005 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003006 * <== ( NewSessionTicket )
3007 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003008 * Finished
3009 */
3010 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003011#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003012 if( ssl->handshake->new_session_ticket != 0 )
3013 ret = ssl_parse_new_session_ticket( ssl );
3014 else
Paul Bakkera503a632013-08-14 13:48:06 +02003015#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003016 ret = ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003017 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003018
Paul Bakker1961b702013-01-25 14:49:24 +01003019 case SSL_SERVER_FINISHED:
3020 ret = ssl_parse_finished( ssl );
3021 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003022
Paul Bakker1961b702013-01-25 14:49:24 +01003023 case SSL_FLUSH_BUFFERS:
3024 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3025 ssl->state = SSL_HANDSHAKE_WRAPUP;
3026 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003027
Paul Bakker1961b702013-01-25 14:49:24 +01003028 case SSL_HANDSHAKE_WRAPUP:
3029 ssl_handshake_wrapup( ssl );
3030 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003031
Paul Bakker1961b702013-01-25 14:49:24 +01003032 default:
3033 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3034 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3035 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003036
3037 return( ret );
3038}
Paul Bakker9af723c2014-05-01 13:03:14 +02003039#endif /* POLARSSL_SSL_CLI_C */